mp_objInp.html 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  6. <title>公共录入</title>
  7. <script src="/js/mp_base/base.js"></script>
  8. <style>
  9. body {
  10. margin: 0;
  11. background: #f5f5f5;
  12. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  13. }
  14. #app {
  15. min-height: 100vh;
  16. background: #f5f5f5;
  17. overflow: hidden;
  18. }
  19. .content {
  20. width: 100%;
  21. height: 100vh;
  22. min-height: 0;
  23. }
  24. .form-frame {
  25. width: 100%;
  26. height: 100%;
  27. border: 0;
  28. display: block;
  29. background: #fff;
  30. }
  31. .placeholder {
  32. padding: 28px 16px;
  33. color: #666;
  34. font-size: 14px;
  35. text-align: center;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <div id="app">
  41. <div class="content" id="contentWrap">
  42. <iframe
  43. v-if="iframeSrc"
  44. id="objInpFrame"
  45. class="form-frame"
  46. :src="iframeSrc"
  47. @load="handleFrameLoad"
  48. ></iframe>
  49. <div v-else class="placeholder" id="placeholder">{{ placeholderText }}</div>
  50. </div>
  51. <ss-bottom
  52. v-if="showBottom"
  53. :buttons="bottomButtons"
  54. @button-click="handleBottomAction"
  55. >
  56. </ss-bottom>
  57. <!-- 功能说明:inp 保存并提交后使用统一样式二次确认弹窗,不再使用浏览器原生 confirm by xu 2026-03-01 -->
  58. <ss-confirm
  59. v-model="showSubmitConfirm"
  60. title="提示"
  61. @confirm="handleConfirmSubmit"
  62. >
  63. <div style="text-align:center;padding:16px 0;font-size: 16px;">是否提交?</div>
  64. </ss-confirm>
  65. </div>
  66. <script>
  67. // 功能说明:公共录入壳页(先调业务接口+dataTag,再按 include_input 装载 mp_ 表单页) by xu 2026-02-28
  68. (function () {
  69. const boot = function () {
  70. const setStatus = (text) => {
  71. if (state.vm) {
  72. state.vm.placeholderText = text;
  73. return;
  74. }
  75. const placeholder = document.getElementById('placeholder');
  76. if (placeholder) placeholder.textContent = text;
  77. };
  78. const state = {
  79. pageParams: {},
  80. service: '',
  81. initPayload: {},
  82. dataTagPayload: {},
  83. ssObjName: '',
  84. ssObjId: '',
  85. iframe: null,
  86. submitEnabled: false,
  87. vm: null,
  88. pendingFinalSubmit: null,
  89. finalSubmitting: false,
  90. };
  91. // 功能说明:执行 inp 二次确认后的最终提交(${ssObjName}_lr_tj_qr) by xu 2026-03-01
  92. const submitFinalConfirm = async () => {
  93. if (state.finalSubmitting) return;
  94. const pending = state.pendingFinalSubmit;
  95. if (!pending || typeof pending !== 'object') {
  96. if (typeof showToastEffect === 'function') showToastEffect('提交参数缺失', 1800, 'error');
  97. return;
  98. }
  99. const req = ensureRequest();
  100. state.finalSubmitting = true;
  101. try {
  102. await req.post(
  103. `/service?ssServ=${encodeURIComponent(pending.serviceName)}`,
  104. pending.payload,
  105. { loading: true, formData: true }
  106. );
  107. state.pendingFinalSubmit = null;
  108. if (typeof showToastEffect === 'function') showToastEffect('提交成功', 1400, 'success');
  109. setTimeout(() => goBack(true), 600);
  110. } catch (error) {
  111. console.error('[mp_objInp] 二次确认提交失败', error);
  112. if (typeof showToastEffect === 'function') showToastEffect('提交失败,请稍后重试', 2200, 'error');
  113. } finally {
  114. state.finalSubmitting = false;
  115. }
  116. };
  117. const getUrlParams = () => {
  118. const params = {};
  119. const search = new URLSearchParams(window.location.search || '');
  120. for (const [key, value] of search.entries()) {
  121. params[key] = decodeURIComponent(value || '');
  122. }
  123. return params;
  124. };
  125. const unwrapData = (data) => {
  126. if (!data || typeof data !== 'object') return {};
  127. if (data.ssData && typeof data.ssData === 'object') return data.ssData;
  128. return data;
  129. };
  130. const parseJsonString = (text) => {
  131. if (!text || typeof text !== 'string') return {};
  132. try {
  133. const obj = JSON.parse(text);
  134. return obj && typeof obj === 'object' ? obj : {};
  135. } catch (_) {
  136. return {};
  137. }
  138. };
  139. const pruneParams = (obj) => {
  140. const out = {};
  141. Object.entries(obj || {}).forEach(([key, value]) => {
  142. if (value === undefined || value === null || value === '') return;
  143. out[key] = value;
  144. });
  145. return out;
  146. };
  147. // 功能说明:新增场景 id=0 统一按空处理,和你要求保持一致 by xu 2026-02-28
  148. const resolveObjIdFromInit = (payload, ssObjName, urlParams) => {
  149. const fromUrl = String(urlParams.ssObjId || '').trim();
  150. if (fromUrl) return fromUrl;
  151. if (!payload || typeof payload !== 'object') return '';
  152. const objName = String(ssObjName || '').trim();
  153. if (!objName) return '';
  154. const objData = payload[objName];
  155. if (!objData || typeof objData !== 'object') return '';
  156. const idField = String(payload.ssObjIdName || objData.idName || '').trim();
  157. if (!idField) return '';
  158. const rawId = objData[idField];
  159. if (rawId === undefined || rawId === null || rawId === '' || Number(rawId) === 0) return '';
  160. return String(rawId);
  161. };
  162. // 功能说明:/page/xyqj_inp.jsp -> /page/mp_xyqj_inp.html by xu 2026-02-28
  163. const includeToMpFormPath = (includeInput) => {
  164. const raw = String(includeInput || '').trim();
  165. if (!raw) return '';
  166. const noPrefix = raw.replace(/^\/page\//i, '');
  167. const base = noPrefix.replace(/\.ss\.jsp$/i, '').replace(/\.jsp$/i, '');
  168. if (!base) return '';
  169. return `/page/mp_${base}.html`;
  170. };
  171. // 功能说明:变动页签里的 objInp 可能拿不到 include_input,此时按对象名兜底到 mp_xxx_inp.html by xu 2026-03-06
  172. const resolveFallbackFormPath = () => {
  173. const objName = String(state.ssObjName || state.pageParams.ssObjName || '').trim();
  174. if (objName) {
  175. return `/page/mp_${objName}_inp.html`;
  176. }
  177. const serviceName = String(state.service || state.pageParams.service || state.pageParams.ssServ || '').trim();
  178. const match = serviceName.match(/^([a-zA-Z0-9_]+?)_/);
  179. if (match && match[1]) {
  180. return `/page/mp_${match[1]}_inp.html`;
  181. }
  182. return '';
  183. };
  184. const goBack = (refreshParent) => {
  185. if (window.NavigationManager && typeof window.NavigationManager.goBack === 'function') {
  186. window.NavigationManager.goBack({ refreshParent: !!refreshParent });
  187. } else {
  188. window.history.back();
  189. }
  190. };
  191. const ensureRequest = () => {
  192. if (!window.request || typeof window.request.post !== 'function') {
  193. throw new Error('request 未就绪');
  194. }
  195. return window.request;
  196. };
  197. // 功能说明:内容区高度按 100vh 减去 ss-bottom 实际高度计算 by xu 2026-02-28
  198. const updateContentHeight = () => {
  199. const contentWrap = document.getElementById('contentWrap');
  200. if (!contentWrap) return;
  201. const bottom = document.querySelector('#app .ss-bottom');
  202. const bottomHeight = bottom ? bottom.offsetHeight : 0;
  203. contentWrap.style.height = `calc(100vh - ${bottomHeight}px)`;
  204. };
  205. const updateBottomButtons = (submitTitle) => {
  206. if (!state.vm) return;
  207. state.vm.bottomButtons = [
  208. { text: '关闭', action: 'close' },
  209. {
  210. text: submitTitle || '保存并提交',
  211. action: 'saveSubmit',
  212. backgroundColor: '#575d6d',
  213. color: '#fff'
  214. }
  215. ];
  216. setTimeout(updateContentHeight, 0);
  217. };
  218. // 功能说明:供 iframe 子页调用,控制父页底部按钮显隐(解决日期弹层被底部按钮遮挡) by xu 2026-02-28
  219. const setBottomVisible = (visible) => {
  220. if (!state.vm) return;
  221. state.vm.showBottom = visible !== false;
  222. setTimeout(updateContentHeight, 0);
  223. };
  224. const getFrameFormData = async () => {
  225. const frame = document.getElementById('objInpFrame');
  226. const frameWindow = frame ? frame.contentWindow : null;
  227. if (!frameWindow) return { valid: false, message: '表单页面未加载完成', data: {} };
  228. // 功能说明:优先走子页面暴露方法,尽量减少表单页工作量 by xu 2026-02-28
  229. if (typeof frameWindow.__mpObjInpGetFormData === 'function') {
  230. // 功能说明:支持子页异步取值(如提交前补查关联字段),确保提交参数完整 by xu 2026-03-01
  231. const result = await Promise.resolve(frameWindow.__mpObjInpGetFormData());
  232. if (result && typeof result === 'object') {
  233. return {
  234. valid: result.valid !== false,
  235. message: result.message || '',
  236. data: result.data && typeof result.data === 'object' ? result.data : {},
  237. };
  238. }
  239. }
  240. const doc = frameWindow.document;
  241. const data = {};
  242. const nodes = doc.querySelectorAll('input[name],select[name],textarea[name]');
  243. nodes.forEach((node) => {
  244. const name = node.getAttribute('name');
  245. if (!name || node.disabled) return;
  246. const type = (node.getAttribute('type') || '').toLowerCase();
  247. if ((type === 'checkbox' || type === 'radio') && !node.checked) return;
  248. data[name] = node.value;
  249. });
  250. return { valid: true, message: '', data };
  251. };
  252. const getSubmitConfig = () => {
  253. const p = state.initPayload || {};
  254. const saveServiceName = String(p.saveAndCommit || '').trim();
  255. const saveDest = String(p.saveAndCommitDest || '').trim();
  256. const routeServiceName = String(state.service || state.pageParams.service || state.pageParams.ssServ || '').trim();
  257. const routeDest = String(state.pageParams.dest || state.pageParams.ssDest || '').trim();
  258. const routeParamObj = parseJsonString(state.pageParams.param);
  259. const routeDataType = String(state.pageParams.dataType || routeParamObj.dataType || '').trim().toLowerCase();
  260. // 功能说明:变动页签若第一接口没回 saveAndCommitDest,则回退用当前 service+objInp+param 提交 by xu 2026-03-06
  261. const canUseRouteSubmit = routeDataType === 'change' && routeDest.toLowerCase() === 'objinp' && !!routeServiceName;
  262. const serviceName = saveServiceName || (canUseRouteSubmit ? routeServiceName : '');
  263. const dest = saveDest || (canUseRouteSubmit ? routeDest : '');
  264. const enabled = !!(serviceName && dest);
  265. return {
  266. enabled,
  267. serviceName,
  268. dest,
  269. title: String(p.saveAndCommitButtonValue || '保存并提交'),
  270. paramObj: pruneParams({
  271. ...(canUseRouteSubmit ? {
  272. ...routeParamObj,
  273. sqid: state.pageParams.sqid,
  274. shid: state.pageParams.shid,
  275. shyjm: state.pageParams.shyjm,
  276. bdlbm: state.pageParams.bdlbm,
  277. dataType: state.pageParams.dataType,
  278. } : {}),
  279. ...parseJsonString(p.saveAndCommitParam),
  280. }),
  281. };
  282. };
  283. const renderFormFrame = () => {
  284. const includeInput = (state.dataTagPayload && state.dataTagPayload.include_input) || '';
  285. const fallbackFormPath = resolveFallbackFormPath();
  286. const formPath = includeToMpFormPath(includeInput) || fallbackFormPath;
  287. console.log('[mp_objInp][renderFormFrame] include_input =', includeInput);
  288. console.log('[mp_objInp][renderFormFrame] fallbackFormPath =', fallbackFormPath);
  289. console.log('[mp_objInp][renderFormFrame] formPath =', formPath);
  290. if (!formPath) {
  291. if (state.vm) {
  292. state.vm.iframeSrc = '';
  293. state.vm.placeholderText = '未获取到 include_input,且未命中对象表单页';
  294. }
  295. state.submitEnabled = false;
  296. console.warn('[mp_objInp][renderFormFrame] formPath为空,终止渲染iframe');
  297. return;
  298. }
  299. const childParams = new URLSearchParams();
  300. Object.entries(state.pageParams || {}).forEach(([key, value]) => {
  301. if (value === undefined || value === null || value === '') return;
  302. childParams.set(key, value);
  303. });
  304. childParams.set('embed', '1');
  305. childParams.set('ssObjName', state.ssObjName || '');
  306. childParams.set('ssObjId', state.ssObjId || '');
  307. const iframeSrc = `${formPath}?${childParams.toString()}`;
  308. console.log('[mp_objInp][renderFormFrame] iframe src =', iframeSrc);
  309. if (state.vm) {
  310. state.vm.iframeSrc = iframeSrc;
  311. }
  312. const submitCfg = getSubmitConfig();
  313. state.submitEnabled = !!submitCfg.enabled;
  314. updateBottomButtons(submitCfg.title);
  315. setTimeout(updateContentHeight, 0);
  316. };
  317. const loadInitPayload = async () => {
  318. const req = ensureRequest();
  319. const postData = {
  320. ...state.pageParams,
  321. management: state.pageParams.management || '1',
  322. isReady: '1',
  323. };
  324. const res = await req.post(
  325. `/service?ssServ=${state.service}&management=1&isReady=1`,
  326. postData,
  327. { loading: false, formData: true }
  328. );
  329. state.initPayload = unwrapData(res ? res.data : null);
  330. state.ssObjName = String(state.initPayload.ssObjName || state.pageParams.ssObjName || '').trim();
  331. state.ssObjId = resolveObjIdFromInit(state.initPayload, state.ssObjName, state.pageParams);
  332. console.log('[mp_objInp] init响应', state.initPayload);
  333. };
  334. const loadDataTagPayload = async () => {
  335. const req = ensureRequest();
  336. const dataTagParams = {
  337. ...state.pageParams,
  338. ssObjName: state.ssObjName,
  339. ssObjId: state.ssObjId,
  340. };
  341. const res = await req.post(
  342. `/service?ssServ=dataTag&ssDest=data&name=inp&ssObjName=${encodeURIComponent(state.ssObjName)}&ssObjId=${encodeURIComponent(state.ssObjId)}`,
  343. dataTagParams,
  344. { loading: false, formData: true }
  345. );
  346. state.dataTagPayload = unwrapData(res ? res.data : null);
  347. console.log('[mp_objInp] dataTag响应', state.dataTagPayload);
  348. console.log('[mp_objInp] dataTag include_input =', (state.dataTagPayload && state.dataTagPayload.include_input) || '');
  349. };
  350. const submitSaveAndCommit = async () => {
  351. try {
  352. const cfg = getSubmitConfig();
  353. if (!state.submitEnabled || !cfg.enabled) {
  354. if (typeof showToastEffect === 'function') showToastEffect('当前页面不支持保存并提交', 1800, 'warning');
  355. return;
  356. }
  357. if (!cfg.serviceName || !cfg.dest) {
  358. if (typeof showToastEffect === 'function') showToastEffect('提交配置缺失', 1800, 'error');
  359. return;
  360. }
  361. const formResult = await getFrameFormData();
  362. if (!formResult.valid) {
  363. if (typeof showToastEffect === 'function') showToastEffect(formResult.message || '表单校验未通过', 2000, 'warning');
  364. return;
  365. }
  366. const req = ensureRequest();
  367. const payload = pruneParams({
  368. ...(cfg.paramObj || {}),
  369. ...(formResult.data || {}),
  370. ssObjName: state.ssObjName || undefined,
  371. ssObjId: state.ssObjId || undefined,
  372. [((state.initPayload && state.initPayload.ssObjIdName) || 'ssObjId')]: state.ssObjId || undefined,
  373. });
  374. const res = await req.post(
  375. `/service?ssServ=${cfg.serviceName}&ssDest=${encodeURIComponent(cfg.dest)}`,
  376. payload,
  377. { loading: true, formData: true }
  378. );
  379. console.log('[mp_objInp] saveAndCommit响应', res);
  380. const submitResp = unwrapData(res ? res.data : null);
  381. // 功能说明:保存并提交后若目标为 addSure,则在 inp 页二次确认并直接调最终提交接口 by xu 2026-03-01
  382. if (String(cfg.dest || '').trim().toLowerCase() === 'addsure') {
  383. const finalObjName = String((submitResp && submitResp.ssObjName) || state.ssObjName || '').trim();
  384. const finalObjIdName = String((submitResp && submitResp.ssObjIdName) || (state.initPayload && state.initPayload.ssObjIdName) || 'ssObjId').trim();
  385. const finalObjId = String((submitResp && submitResp.ssObjId) || state.ssObjId || '').trim();
  386. if (!finalObjName || !finalObjId) {
  387. if (typeof showToastEffect === 'function') showToastEffect('缺少提交参数,无法确认提交', 2200, 'error');
  388. return;
  389. }
  390. const finalServ = `${finalObjName}_lr_tj_qr`;
  391. const finalPayload = pruneParams({
  392. ssObjName: finalObjName,
  393. [finalObjIdName]: finalObjId,
  394. });
  395. // 功能说明:先缓存最终提交参数,再弹 ss-confirm 确认 by xu 2026-03-01
  396. state.pendingFinalSubmit = {
  397. serviceName: finalServ,
  398. payload: finalPayload,
  399. };
  400. if (state.vm) {
  401. state.vm.showSubmitConfirm = true;
  402. }
  403. return;
  404. }
  405. if (typeof showToastEffect === 'function') showToastEffect('提交成功', 1400, 'success');
  406. setTimeout(() => goBack(true), 600);
  407. } catch (err) {
  408. console.error('[mp_objInp] 提交失败', err);
  409. if (typeof showToastEffect === 'function') showToastEffect('提交失败,请稍后重试', 2200, 'error');
  410. }
  411. };
  412. const init = async () => {
  413. try {
  414. setStatus('正在初始化录入页...');
  415. state.pageParams = getUrlParams();
  416. state.service = String(state.pageParams.service || '').trim();
  417. if (!state.service) throw new Error('缺少 service 参数');
  418. setStatus('正在加载业务配置...');
  419. await loadInitPayload();
  420. if (!state.ssObjName) throw new Error('第一接口未返回 ssObjName');
  421. setStatus('正在加载 dataTag...');
  422. await loadDataTagPayload();
  423. setStatus('正在加载表单页面...');
  424. renderFormFrame();
  425. setTimeout(updateContentHeight, 0);
  426. } catch (err) {
  427. console.error('[mp_objInp] 初始化失败', err);
  428. setStatus(`初始化失败:${err && err.message ? err.message : err}`);
  429. state.submitEnabled = false;
  430. }
  431. };
  432. window.addEventListener('resize', updateContentHeight);
  433. // 功能说明:使用 ss-bottom 统一底部样式与交互 by xu 2026-02-28
  434. window.SS.dom.initializeFormApp({
  435. el: '#app',
  436. data() {
  437. return {
  438. iframeSrc: '',
  439. placeholderText: '正在加载录入页面...',
  440. showBottom: true,
  441. showSubmitConfirm: false,
  442. bottomButtons: [
  443. { text: '关闭', action: 'close' },
  444. {
  445. text: '保存并提交',
  446. action: 'saveSubmit',
  447. backgroundColor: '#575d6d',
  448. color: '#fff'
  449. }
  450. ]
  451. }
  452. },
  453. methods: {
  454. handleFrameLoad() {
  455. console.log('[mp_objInp][renderFormFrame] iframe onload');
  456. setTimeout(updateContentHeight, 0);
  457. },
  458. handleBottomAction(payload) {
  459. if (payload && payload.action === 'close') {
  460. goBack(false);
  461. return;
  462. }
  463. if (payload && payload.action === 'saveSubmit') {
  464. submitSaveAndCommit();
  465. }
  466. },
  467. // 功能说明:二次确认弹窗点击“确认”后执行最终提交 by xu 2026-03-01
  468. handleConfirmSubmit() {
  469. submitFinalConfirm();
  470. }
  471. },
  472. mounted() {
  473. state.vm = this;
  474. window.__mpObjInpSetBottomVisible = setBottomVisible;
  475. init();
  476. },
  477. beforeUnmount() {
  478. window.removeEventListener('resize', updateContentHeight);
  479. try {
  480. if (window.__mpObjInpSetBottomVisible === setBottomVisible) {
  481. delete window.__mpObjInpSetBottomVisible;
  482. }
  483. } catch (_) {}
  484. }
  485. });
  486. };
  487. // 功能说明:SS.ready 某些场景可能不触发,增加超时兜底启动 by xu 2026-02-28
  488. let booted = false;
  489. let tries = 0;
  490. const maxTries = 20;
  491. const safeBoot = function () {
  492. if (booted) return;
  493. const ready = !!(window.SS && window.SS.dom && typeof window.SS.dom.initializeFormApp === 'function');
  494. if (!ready) {
  495. tries += 1;
  496. if (tries >= maxTries) {
  497. const ph = document.getElementById('placeholder');
  498. if (ph) ph.textContent = '初始化失败:ss 组件未就绪';
  499. return;
  500. }
  501. setTimeout(safeBoot, 200);
  502. return;
  503. }
  504. booted = true;
  505. boot();
  506. };
  507. if (window.SS && typeof window.SS.ready === 'function') {
  508. window.SS.ready(safeBoot);
  509. setTimeout(safeBoot, 1600);
  510. } else {
  511. setTimeout(safeBoot, 0);
  512. }
  513. })();
  514. </script>
  515. </body>
  516. </html>