mp_objInp.html 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. // 功能说明:学校违纪录入使用其他移动端入口一致的默认底部按钮和文案
  206. const isSchoolViolationEntry = (serviceName = '', objName = '') => {
  207. const service = String(serviceName || state.service || getUrlParams().service || '').trim();
  208. const objectName = String(objName || state.ssObjName || '').trim();
  209. return objectName === 'xxwjjl' || service.startsWith('xxwjjl_');
  210. };
  211. const makeCloseBottomButton = (serviceName = '', objName = '') => ({
  212. text: isSchoolViolationEntry(serviceName, objName) ? '取消' : '关闭',
  213. action: 'close'
  214. });
  215. const makeSubmitBottomButton = (text, serviceName = '', objName = '') => {
  216. const button = {
  217. text: text || '保存并提交',
  218. action: 'saveSubmit'
  219. };
  220. if (!isSchoolViolationEntry(serviceName, objName)) {
  221. button.backgroundColor = '#575d6d';
  222. button.color = '#fff';
  223. }
  224. return button;
  225. };
  226. const updateBottomButtons = (submitTitle) => {
  227. if (!state.vm) return;
  228. state.vm.bottomButtons = [
  229. makeCloseBottomButton(state.service, state.ssObjName),
  230. makeSubmitBottomButton(submitTitle, state.service, state.ssObjName)
  231. ];
  232. setTimeout(updateContentHeight, 0);
  233. };
  234. // 功能说明:供 iframe 子页调用,控制父页底部按钮显隐(解决日期弹层被底部按钮遮挡) by xu 2026-02-28
  235. const setBottomVisible = (visible) => {
  236. if (!state.vm) return;
  237. state.vm.showBottom = visible !== false;
  238. setTimeout(updateContentHeight, 0);
  239. };
  240. const getFrameFormData = async () => {
  241. const frame = document.getElementById('objInpFrame');
  242. const frameWindow = frame ? frame.contentWindow : null;
  243. if (!frameWindow) return { valid: false, message: '表单页面未加载完成', data: {} };
  244. // 功能说明:优先走子页面暴露方法,尽量减少表单页工作量 by xu 2026-02-28
  245. if (typeof frameWindow.__mpObjInpGetFormData === 'function') {
  246. // 功能说明:支持子页异步取值(如提交前补查关联字段),确保提交参数完整 by xu 2026-03-01
  247. const result = await Promise.resolve(frameWindow.__mpObjInpGetFormData());
  248. if (result && typeof result === 'object') {
  249. return {
  250. valid: result.valid !== false,
  251. message: result.message || '',
  252. data: result.data && typeof result.data === 'object' ? result.data : {},
  253. };
  254. }
  255. }
  256. const doc = frameWindow.document;
  257. const data = {};
  258. const nodes = doc.querySelectorAll('input[name],select[name],textarea[name]');
  259. nodes.forEach((node) => {
  260. const name = node.getAttribute('name');
  261. if (!name || node.disabled) return;
  262. const type = (node.getAttribute('type') || '').toLowerCase();
  263. if ((type === 'checkbox' || type === 'radio') && !node.checked) return;
  264. data[name] = node.value;
  265. });
  266. return { valid: true, message: '', data };
  267. };
  268. const getSubmitConfig = () => {
  269. const p = state.initPayload || {};
  270. const saveServiceName = String(p.saveAndCommit || '').trim();
  271. const saveDest = String(p.saveAndCommitDest || '').trim();
  272. const routeServiceName = String(state.service || state.pageParams.service || state.pageParams.ssServ || '').trim();
  273. const routeDest = String(state.pageParams.dest || state.pageParams.ssDest || '').trim();
  274. const routeParamObj = parseJsonString(state.pageParams.param);
  275. const routeDataType = String(state.pageParams.dataType || routeParamObj.dataType || '').trim().toLowerCase();
  276. // 功能说明:变动页签若第一接口没回 saveAndCommitDest,则回退用当前 service+objInp+param 提交 by xu 2026-03-06
  277. const canUseRouteSubmit = routeDataType === 'change' && routeDest.toLowerCase() === 'objinp' && !!routeServiceName;
  278. const serviceName = saveServiceName || (canUseRouteSubmit ? routeServiceName : '');
  279. const dest = saveDest || (canUseRouteSubmit ? routeDest : '');
  280. const enabled = !!(serviceName && dest);
  281. return {
  282. enabled,
  283. serviceName,
  284. dest,
  285. title: String(p.saveAndCommitButtonValue || '保存并提交'),
  286. paramObj: pruneParams({
  287. ...(canUseRouteSubmit ? {
  288. ...routeParamObj,
  289. sqid: state.pageParams.sqid,
  290. shid: state.pageParams.shid,
  291. shyjm: state.pageParams.shyjm,
  292. bdlbm: state.pageParams.bdlbm,
  293. dataType: state.pageParams.dataType,
  294. } : {}),
  295. ...parseJsonString(p.saveAndCommitParam),
  296. }),
  297. };
  298. };
  299. const renderFormFrame = () => {
  300. const includeInput = (state.dataTagPayload && state.dataTagPayload.include_input) || '';
  301. const fallbackFormPath = resolveFallbackFormPath();
  302. const formPath = includeToMpFormPath(includeInput) || fallbackFormPath;
  303. console.log('[mp_objInp][renderFormFrame] include_input =', includeInput);
  304. console.log('[mp_objInp][renderFormFrame] fallbackFormPath =', fallbackFormPath);
  305. console.log('[mp_objInp][renderFormFrame] formPath =', formPath);
  306. if (!formPath) {
  307. if (state.vm) {
  308. state.vm.iframeSrc = '';
  309. state.vm.placeholderText = '未获取到 include_input,且未命中对象表单页';
  310. }
  311. state.submitEnabled = false;
  312. console.warn('[mp_objInp][renderFormFrame] formPath为空,终止渲染iframe');
  313. return;
  314. }
  315. const childParams = new URLSearchParams();
  316. Object.entries(state.pageParams || {}).forEach(([key, value]) => {
  317. if (value === undefined || value === null || value === '') return;
  318. childParams.set(key, value);
  319. });
  320. childParams.set('embed', '1');
  321. childParams.set('ssObjName', state.ssObjName || '');
  322. childParams.set('ssObjId', state.ssObjId || '');
  323. const iframeSrc = `${formPath}?${childParams.toString()}`;
  324. console.log('[mp_objInp][renderFormFrame] iframe src =', iframeSrc);
  325. if (state.vm) {
  326. state.vm.iframeSrc = iframeSrc;
  327. }
  328. const submitCfg = getSubmitConfig();
  329. state.submitEnabled = !!submitCfg.enabled;
  330. updateBottomButtons(submitCfg.title);
  331. setTimeout(updateContentHeight, 0);
  332. };
  333. const loadInitPayload = async () => {
  334. const req = ensureRequest();
  335. const postData = {
  336. ...state.pageParams,
  337. management: state.pageParams.management || '1',
  338. isReady: '1',
  339. };
  340. const res = await req.post(
  341. `/service?ssServ=${state.service}&management=1&isReady=1`,
  342. postData,
  343. { loading: false, formData: true }
  344. );
  345. state.initPayload = unwrapData(res ? res.data : null);
  346. state.ssObjName = String(state.initPayload.ssObjName || state.pageParams.ssObjName || '').trim();
  347. state.ssObjId = resolveObjIdFromInit(state.initPayload, state.ssObjName, state.pageParams);
  348. console.log('[mp_objInp] init响应', state.initPayload);
  349. };
  350. const loadDataTagPayload = async () => {
  351. const req = ensureRequest();
  352. const dataTagParams = {
  353. ...state.pageParams,
  354. ssObjName: state.ssObjName,
  355. ssObjId: state.ssObjId,
  356. };
  357. const res = await req.post(
  358. `/service?ssServ=dataTag&ssDest=data&name=inp&ssObjName=${encodeURIComponent(state.ssObjName)}&ssObjId=${encodeURIComponent(state.ssObjId)}`,
  359. dataTagParams,
  360. { loading: false, formData: true }
  361. );
  362. state.dataTagPayload = unwrapData(res ? res.data : null);
  363. console.log('[mp_objInp] dataTag响应', state.dataTagPayload);
  364. console.log('[mp_objInp] dataTag include_input =', (state.dataTagPayload && state.dataTagPayload.include_input) || '');
  365. };
  366. const submitSaveAndCommit = async () => {
  367. try {
  368. const cfg = getSubmitConfig();
  369. if (!state.submitEnabled || !cfg.enabled) {
  370. if (typeof showToastEffect === 'function') showToastEffect('当前页面不支持保存并提交', 1800, 'warning');
  371. return;
  372. }
  373. if (!cfg.serviceName || !cfg.dest) {
  374. if (typeof showToastEffect === 'function') showToastEffect('提交配置缺失', 1800, 'error');
  375. return;
  376. }
  377. const formResult = await getFrameFormData();
  378. if (!formResult.valid) {
  379. if (typeof showToastEffect === 'function') showToastEffect(formResult.message || '表单校验未通过', 2000, 'warning');
  380. return;
  381. }
  382. const req = ensureRequest();
  383. const payload = pruneParams({
  384. ...(cfg.paramObj || {}),
  385. ...(formResult.data || {}),
  386. ssObjName: state.ssObjName || undefined,
  387. ssObjId: state.ssObjId || undefined,
  388. [((state.initPayload && state.initPayload.ssObjIdName) || 'ssObjId')]: state.ssObjId || undefined,
  389. });
  390. const res = await req.post(
  391. `/service?ssServ=${cfg.serviceName}&ssDest=${encodeURIComponent(cfg.dest)}`,
  392. payload,
  393. { loading: true, formData: true }
  394. );
  395. console.log('[mp_objInp] saveAndCommit响应', res);
  396. const submitResp = unwrapData(res ? res.data : null);
  397. // 功能说明:保存并提交后若目标为 addSure,则在 inp 页二次确认并直接调最终提交接口 by xu 2026-03-01
  398. if (String(cfg.dest || '').trim().toLowerCase() === 'addsure') {
  399. const finalObjName = String((submitResp && submitResp.ssObjName) || state.ssObjName || '').trim();
  400. const finalObjIdName = String((submitResp && submitResp.ssObjIdName) || (state.initPayload && state.initPayload.ssObjIdName) || 'ssObjId').trim();
  401. const finalObjId = String((submitResp && submitResp.ssObjId) || state.ssObjId || '').trim();
  402. if (!finalObjName || !finalObjId) {
  403. if (typeof showToastEffect === 'function') showToastEffect('缺少提交参数,无法确认提交', 2200, 'error');
  404. return;
  405. }
  406. const finalServ = `${finalObjName}_lr_tj_qr`;
  407. const finalPayload = pruneParams({
  408. ssObjName: finalObjName,
  409. [finalObjIdName]: finalObjId,
  410. });
  411. // 功能说明:先缓存最终提交参数,再弹 ss-confirm 确认 by xu 2026-03-01
  412. state.pendingFinalSubmit = {
  413. serviceName: finalServ,
  414. payload: finalPayload,
  415. };
  416. if (state.vm) {
  417. state.vm.showSubmitConfirm = true;
  418. }
  419. return;
  420. }
  421. if (typeof showToastEffect === 'function') showToastEffect('提交成功', 1400, 'success');
  422. setTimeout(() => goBack(true), 600);
  423. } catch (err) {
  424. console.error('[mp_objInp] 提交失败', err);
  425. if (typeof showToastEffect === 'function') showToastEffect('提交失败,请稍后重试', 2200, 'error');
  426. }
  427. };
  428. const init = async () => {
  429. try {
  430. setStatus('正在初始化录入页...');
  431. state.pageParams = getUrlParams();
  432. state.service = String(state.pageParams.service || '').trim();
  433. if (!state.service) throw new Error('缺少 service 参数');
  434. setStatus('正在加载业务配置...');
  435. await loadInitPayload();
  436. if (!state.ssObjName) throw new Error('第一接口未返回 ssObjName');
  437. setStatus('正在加载 dataTag...');
  438. await loadDataTagPayload();
  439. setStatus('正在加载表单页面...');
  440. renderFormFrame();
  441. setTimeout(updateContentHeight, 0);
  442. } catch (err) {
  443. console.error('[mp_objInp] 初始化失败', err);
  444. setStatus(`初始化失败:${err && err.message ? err.message : err}`);
  445. state.submitEnabled = false;
  446. }
  447. };
  448. window.addEventListener('resize', updateContentHeight);
  449. // 功能说明:使用 ss-bottom 统一底部样式与交互 by xu 2026-02-28
  450. window.SS.dom.initializeFormApp({
  451. el: '#app',
  452. data() {
  453. return {
  454. iframeSrc: '',
  455. placeholderText: '正在加载录入页面...',
  456. showBottom: true,
  457. showSubmitConfirm: false,
  458. bottomButtons: [
  459. makeCloseBottomButton(getUrlParams().service),
  460. makeSubmitBottomButton('保存并提交', getUrlParams().service)
  461. ]
  462. }
  463. },
  464. methods: {
  465. handleFrameLoad() {
  466. console.log('[mp_objInp][renderFormFrame] iframe onload');
  467. setTimeout(updateContentHeight, 0);
  468. },
  469. handleBottomAction(payload) {
  470. if (payload && payload.action === 'close') {
  471. goBack(false);
  472. return;
  473. }
  474. if (payload && payload.action === 'saveSubmit') {
  475. submitSaveAndCommit();
  476. }
  477. },
  478. // 功能说明:二次确认弹窗点击“确认”后执行最终提交 by xu 2026-03-01
  479. handleConfirmSubmit() {
  480. submitFinalConfirm();
  481. }
  482. },
  483. mounted() {
  484. state.vm = this;
  485. window.__mpObjInpSetBottomVisible = setBottomVisible;
  486. init();
  487. },
  488. beforeUnmount() {
  489. window.removeEventListener('resize', updateContentHeight);
  490. try {
  491. if (window.__mpObjInpSetBottomVisible === setBottomVisible) {
  492. delete window.__mpObjInpSetBottomVisible;
  493. }
  494. } catch (_) {}
  495. }
  496. });
  497. };
  498. // 功能说明:SS.ready 某些场景可能不触发,增加超时兜底启动 by xu 2026-02-28
  499. let booted = false;
  500. let tries = 0;
  501. const maxTries = 20;
  502. const safeBoot = function () {
  503. if (booted) return;
  504. const ready = !!(window.SS && window.SS.dom && typeof window.SS.dom.initializeFormApp === 'function');
  505. if (!ready) {
  506. tries += 1;
  507. if (tries >= maxTries) {
  508. const ph = document.getElementById('placeholder');
  509. if (ph) ph.textContent = '初始化失败:ss 组件未就绪';
  510. return;
  511. }
  512. setTimeout(safeBoot, 200);
  513. return;
  514. }
  515. booted = true;
  516. boot();
  517. };
  518. if (window.SS && typeof window.SS.ready === 'function') {
  519. window.SS.ready(safeBoot);
  520. setTimeout(safeBoot, 1600);
  521. } else {
  522. setTimeout(safeBoot, 0);
  523. }
  524. })();
  525. </script>
  526. </body>
  527. </html>