index_form.vue 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. <template>
  2. <main class="index_main newsDetail">
  3. <HomePageHead></HomePageHead>
  4. <HomePageNavigation1></HomePageNavigation1>
  5. <!-- 面包屑导航 -->
  6. <div class="breadcrumb">
  7. <div class="inner">
  8. <span class="location">当前位置:</span>
  9. <el-breadcrumb :separator-icon="ArrowRight">
  10. <el-breadcrumb-item>
  11. <NuxtLink to="/">首页</NuxtLink>
  12. </el-breadcrumb-item>
  13. <el-breadcrumb-item> {{ title }}</el-breadcrumb-item>
  14. </el-breadcrumb>
  15. </div>
  16. </div>
  17. <div class="breadcrumb_title">
  18. <div>{{ remark }}</div>
  19. </div>
  20. <el-skeleton v-if="pageLoading" :rows="24" :animated="true" style="margin: 20px;">
  21. <template #template>
  22. <div class="custom-skeleton-multi">
  23. <span
  24. v-for="(item, idx) in skeletonChars"
  25. :key="idx"
  26. class="custom-skeleton-char"
  27. :style="{
  28. top: item.top + '%',
  29. left: item.left + '%',
  30. fontSize: item.fontSize + 'px',
  31. opacity: item.opacity,
  32. transform: `rotate(${item.rotate}deg)`
  33. }"
  34. >政讯通</span>
  35. </div>
  36. </template>
  37. </el-skeleton>
  38. <el-form v-else :model="formData" :rules="rules_js" ref="formRef" label-width="120px" class="form_box">
  39. <el-form-item
  40. v-for="(per_obj,per_index) in table_head.value"
  41. :key="per_index"
  42. :prop="getFieldName(per_obj)"
  43. :label="per_obj.title + ':'"
  44. :required="per_obj.is_check === 1"
  45. >
  46. <!-- 文本文字1 -->
  47. <el-input
  48. v-if="checkFieldType(per_obj, 1)"
  49. v-model="formData[getFieldName(per_obj)]"
  50. :placeholder="`请输入${per_obj.title}`"
  51. />
  52. <!-- 多行textarea 2-->
  53. <el-input
  54. v-if="checkFieldType(per_obj, 2)"
  55. v-model="formData[getFieldName(per_obj)]"
  56. type="textarea"
  57. :autosize="{ minRows: 2, maxRows: 4 }"
  58. :placeholder="`请输入${per_obj.title}`"
  59. />
  60. <!-- 单选按钮 3 -->
  61. <el-radio-group
  62. v-if="checkFieldType(per_obj, 3)"
  63. v-model="formData[getFieldName(per_obj)]">
  64. <el-radio
  65. v-for="per_3_obj in radio_arr_fun(per_obj.option_value)"
  66. :key="`${getFieldName(per_obj)}_${per_3_obj[1]}`"
  67. :label="per_3_obj[1]">
  68. {{ per_3_obj[0] }}
  69. </el-radio>
  70. </el-radio-group>
  71. <!-- 下拉选择 4 -->
  72. <el-select
  73. v-if="checkFieldType(per_obj, 4)"
  74. placeholder="请选择"
  75. v-model="formData[getFieldName(per_obj)]"
  76. style="width: 100%"
  77. >
  78. <el-option
  79. v-for="per_4_obj in select_arr_fun(per_obj.option_value)"
  80. :key="`${getFieldName(per_obj)}_${per_4_obj.value}`"
  81. :label="per_4_obj.label"
  82. :value="per_4_obj.value">
  83. </el-option>
  84. </el-select>
  85. <!-- 复选框5 -->
  86. <el-checkbox-group
  87. v-if="checkFieldType(per_obj, 5)"
  88. v-model="formData[getFieldName(per_obj)]"
  89. @change="(value) => handleCheckboxChange(getFieldName(per_obj), value)"
  90. >
  91. <el-checkbox
  92. v-for="per_5_obj in checkbox_arr_fun(per_obj.option_value)"
  93. :key="`${getFieldName(per_obj)}_${per_5_obj.value}`"
  94. :label="per_5_obj.value">
  95. {{ per_5_obj.label }}
  96. </el-checkbox>
  97. </el-checkbox-group>
  98. <!-- 日期选择器 6 -->
  99. <el-date-picker
  100. v-if="checkFieldType(per_obj, 6)"
  101. v-model="formData[getFieldName(per_obj)]"
  102. type="date"
  103. value-format="YYYY-MM-DD"
  104. format="YYYY-MM-DD"
  105. placeholder="选择日期"
  106. style="width: 100%"
  107. :locale="zhCn">
  108. </el-date-picker>
  109. <!-- 单文件上传 7 (新) -->
  110. <el-upload
  111. v-if="checkFieldType(per_obj, 7)"
  112. :ref="(el) => uploadRefs[getFieldName(per_obj)] = el"
  113. :action="`${$webUrl}/public/uploadFile`"
  114. :limit="1"
  115. :on-exceed="(files) => handleExceed(files, getFieldName(per_obj))"
  116. :on-success="(res, file) => handleFileSuccess(res, file, getFieldName(per_obj))"
  117. :on-remove="() => handleFileRemove(getFieldName(per_obj))"
  118. :file-list="fileLists[getFieldName(per_obj)] || []"
  119. >
  120. <el-button type="primary">点击上传文件</el-button>
  121. <template #tip>
  122. <div class="el-upload__tip">
  123. 只能上传一个文件,新文件将覆盖旧文件。
  124. </div>
  125. </template>
  126. </el-upload>
  127. <!-- img上传 8 -->
  128. <el-upload
  129. v-if="checkFieldType(per_obj, 8)"
  130. class="avatar-uploader"
  131. :action="`${$webUrl}/public/uploadFile`"
  132. :show-file-list="false"
  133. :on-success="(res) => handleAvatarSuccess(res, getFieldName(per_obj))"
  134. :before-upload="beforeAvatarUpload"
  135. >
  136. <img v-if="formData[getFieldName(per_obj)]" :src="formData[getFieldName(per_obj)]" class="avatar" >
  137. <div v-else class="avatar-uploader-placeholder">
  138. <img src="http://img.bjzxtw.org.cn/master/image/noImage.png" class="avatar-placeholder-img" alt="上传头像">
  139. <span class="upload-tip">点击上传</span>
  140. </div>
  141. </el-upload>
  142. </el-form-item>
  143. <!-- 验证码 -->
  144. <el-form-item v-if="showCaptcha" prop="code" label="验证码:">
  145. <div class="captcha-wrapper">
  146. <el-input v-model="formData.code" placeholder="请输入验证码" @keyup.enter="submitForm"></el-input>
  147. <img v-if="captchaImage" :src="captchaImage" @click="refreshCaptcha" alt="验证码" class="captcha-img" title="点击刷新">
  148. </div>
  149. </el-form-item>
  150. <el-form-item class="form-actions">
  151. <el-button type="primary" @click="submitForm" :loading="submitLoading">提交</el-button>
  152. <el-button @click="resetForm">重置</el-button>
  153. </el-form-item>
  154. </el-form>
  155. <HomeFoot></HomeFoot>
  156. </main>
  157. <el-dialog v-model="dialogVisible" title="提示" width="300px" :close-on-click-modal="false" :show-close="false" align-center>
  158. <div style="text-align:center;">表单提交成功!</div>
  159. <template #footer>
  160. <div class="dialog-footer-center">
  161. <el-button type="primary" @click="onDialogConfirm" class="dialog-confirm-btn">确认</el-button>
  162. </div>
  163. </template>
  164. </el-dialog>
  165. </template>
  166. <style scoped>
  167. @charset "utf-8";
  168. * {
  169. margin: 0;
  170. padding: 0;
  171. font-family: "微软雅黑", "microsoft yahei";
  172. }
  173. ul,
  174. ol {
  175. list-style: none;
  176. }
  177. a:active {
  178. text-decoration: none;
  179. }
  180. a:hover {
  181. text-decoration: none;
  182. }
  183. a:visited {
  184. text-decoration: none;
  185. }
  186. a:link {
  187. text-decoration: none;
  188. }
  189. a:focus {
  190. text-decoration: none;
  191. }
  192. body {
  193. position: relative;
  194. }
  195. .clearfix {
  196. overflow: hidden;
  197. }
  198. .clearfix_2::after {
  199. content: '';
  200. display: block;
  201. height: 0;
  202. visibility: hidden;
  203. clear: both;
  204. }
  205. .hiddenColor {
  206. visibility: hidden;
  207. }
  208. .hand {
  209. cursor: pointer;
  210. }
  211. .aTag_parent {
  212. position: relative;
  213. }
  214. .aTag_parent>a,
  215. .aTag {
  216. display: block;
  217. width: 100%;
  218. height: 100%;
  219. position: absolute;
  220. z-index: 99;
  221. border: 0px;
  222. top: 0px;
  223. left: 0px;
  224. background: rgba(0, 0, 0, 0);
  225. }
  226. .dot1 {
  227. display: block;
  228. word-break: keep-all;
  229. white-space: nowrap;
  230. overflow: hidden;
  231. text-overflow: ellipsis;
  232. }
  233. .dot2 {
  234. overflow: hidden;
  235. display: -webkit-box;
  236. -webkit-box-orient: vertical;
  237. -webkit-line-clamp: 2;
  238. }
  239. .dot3{
  240. overflow: hidden;
  241. display: -webkit-box;
  242. -webkit-box-orient: vertical;
  243. -webkit-line-clamp: 3;
  244. }
  245. input,
  246. img {
  247. border: none;
  248. }
  249. .cover100 img {
  250. display: block;
  251. width: 100%;
  252. height: 100%;
  253. object-fit: cover;
  254. }
  255. .back100 {
  256. background-size: 100% 100%;
  257. background-repeat: no-repeat;
  258. }
  259. article,
  260. aside,
  261. footer,
  262. header,
  263. time,
  264. video,
  265. main,
  266. nav,
  267. h4,
  268. h3,
  269. section {
  270. display: block;
  271. }
  272. .index_main {
  273. margin: 0 auto;
  274. }
  275. .slow_6 {
  276. -webkit-transition: all .6s;
  277. -moz-transition: all .6s;
  278. -ms-transition: all .6s;
  279. -o-transition: all .6s;
  280. transition: all .6s;
  281. }
  282. .form_box {
  283. width: 1200px;
  284. margin: 0px auto 20px auto;
  285. /* border: dashed 1px #000; */
  286. padding: 20px;
  287. }
  288. .form_box :deep(.avatar-uploader) {
  289. border: 1px dashed var(--el-border-color);
  290. border-radius: 6px;
  291. cursor: pointer;
  292. position: relative;
  293. overflow: hidden;
  294. width: 178px;
  295. height: 178px;
  296. }
  297. .form_box :deep(.avatar-uploader:hover) {
  298. border-color: var(--el-color-primary);
  299. }
  300. .form_box :deep(.avatar-uploader .avatar) {
  301. width: 100%;
  302. height: 100%;
  303. display: block;
  304. }
  305. .form_box :deep(.avatar-uploader .el-upload) {
  306. width: 100%;
  307. height: 100%;
  308. }
  309. .form_box :deep(.el-button--primary) {
  310. padding: 0 11px;
  311. }
  312. @media screen and (min-width:1200px) {
  313. /*pc_1440*/
  314. @media screen and (max-width:1440px) {
  315. /*1200*/
  316. }
  317. .pc_none {
  318. display: none;
  319. }
  320. }
  321. @media screen and (max-width:599px) {}
  322. @media screen and (max-width:320px) {}
  323. .form_box .el-form-item {
  324. margin-bottom: 20px!important;
  325. }
  326. .avatar-uploader-placeholder {
  327. display: flex;
  328. flex-direction: column;
  329. justify-content: center;
  330. align-items: center;
  331. width: 100%;
  332. height: 100%;
  333. }
  334. .avatar-placeholder-img {
  335. width: 50px;
  336. height: 50px;
  337. opacity: 0.5;
  338. }
  339. .upload-tip {
  340. margin-top: 8px;
  341. color: #8c939d;
  342. font-size: 12px;
  343. }
  344. .form-actions :deep(.el-form-item__content) {
  345. justify-content: center;
  346. }
  347. .form-actions .el-button + .el-button {
  348. margin-left: 20px;
  349. padding: 0 11px;
  350. }
  351. .captcha-wrapper {
  352. display: flex;
  353. align-items: center;
  354. width: 100%;
  355. }
  356. .captcha-img {
  357. margin-left: 10px;
  358. cursor: pointer;
  359. height: 32px; /* 与el-input默认高度对齐 */
  360. border-radius: 4px;
  361. }
  362. .form_box :deep(.el-checkbox) {
  363. margin-right: 20px;
  364. }
  365. .form_box :deep(.el-radio) {
  366. margin-right: 20px;
  367. }
  368. /* 弹窗footer按钮居中 */
  369. .dialog-footer-center {
  370. display: flex;
  371. justify-content: center;
  372. align-items: center;
  373. }
  374. .dialog-confirm-btn {
  375. padding: 0 32px;
  376. }
  377. .custom-skeleton-multi {
  378. position: relative;
  379. width: 100%;
  380. height: 700px;
  381. background: #f5f6fa;
  382. border-radius: 8px;
  383. overflow: hidden;
  384. }
  385. .custom-skeleton-char {
  386. position: absolute;
  387. color: #e5e6eb;
  388. font-weight: bold;
  389. letter-spacing: 10px;
  390. user-select: none;
  391. pointer-events: none;
  392. transition: all 0.3s;
  393. }
  394. .breadcrumb{
  395. width: 1200px !important;
  396. margin: 20px auto 0px auto !important;
  397. }
  398. .breadcrumb_title{
  399. width: 1200px;
  400. margin: 20px auto 0px auto;
  401. background-color:#0E6EBE ;
  402. color:white;
  403. line-height: 28px;
  404. }
  405. .breadcrumb_title div{
  406. font-size: 16px;
  407. /* font-weight: bold; */
  408. padding: 5px 20px;
  409. display: inline-block;
  410. vertical-align: middle;
  411. letter-spacing: 3px; /* 添加字间距,可以根据需要调整数值 */
  412. }
  413. </style>
  414. <style lang="less" scoped>
  415. .breadcrumb {
  416. width: 100%;
  417. height: 22px;
  418. margin-top: 40px;
  419. font-family: Microsoft YaHei, Microsoft YaHei;
  420. font-weight: 400;
  421. font-size: 20px;
  422. color: #666666;
  423. line-height: 23px;
  424. text-align: left;
  425. font-style: normal;
  426. text-transform: none;
  427. .el-breadcrumb::v-deep {
  428. display: inline-block;
  429. vertical-align: -4px;
  430. }
  431. :deep(.el-breadcrumb__inner a),
  432. :deep(.el-breadcrumb__inner.is-link) {
  433. color: #666666;
  434. font-weight: 400;
  435. text-decoration: none;
  436. transition: var(--el-transition-color);
  437. }
  438. span {
  439. font-family: Microsoft YaHei, Microsoft YaHei;
  440. font-weight: 400;
  441. font-size: 20px;
  442. color: #666666;
  443. line-height: 23px;
  444. text-align: left;
  445. font-style: normal;
  446. text-transform: none;
  447. }
  448. span:hover {
  449. color: #666666;
  450. }
  451. .location {
  452. margin-right: 20px;
  453. width: 100px;
  454. height: 22px;
  455. font-family: Microsoft YaHei, Microsoft YaHei;
  456. font-weight: 400;
  457. font-size: 20px;
  458. color: #666666;
  459. line-height: 23px;
  460. text-align: left;
  461. font-style: normal;
  462. text-transform: none;
  463. }
  464. }
  465. @media screen and (max-width:800px){/*ipad_phone*/
  466. .inner{width:92%;margin:0px auto;overflow:hidden;}
  467. /* body{padding-bottom:44px;} */
  468. .index_main .breadcrumb{width:100%!important;line-height:33px;height:33px;}
  469. .index_main .breadcrumb .inner *{ font-size:14px;line-height:33px;height:33px;float:left;
  470. font-weight:normal; }
  471. .breadcrumb .location{width:auto;margin-right:5px;}
  472. .breadcrumb_title{width:100%;}
  473. .form_box{box-sizing:border-box;width:100%;}
  474. }
  475. </style>
  476. <script setup>
  477. import { ArrowRight } from '@element-plus/icons-vue'
  478. //页面是否已经加载完毕
  479. const pageLoading = ref(true)
  480. const submitLoading = ref(false)
  481. const showCaptcha = ref(false)
  482. const captchaImage = ref('')
  483. const captchaId = ref('')
  484. const dialogVisible = ref(false)
  485. const title = ref('ceshi')
  486. const remark = ref('nice')
  487. const seoTitle = ref('')
  488. const seoKeywords = ref('')
  489. const seoDescription = ref('')
  490. //1.加载页面必备组件 start---------------------------------------->
  491. import { ref, onMounted, reactive, nextTick, onBeforeUpdate } from 'vue';
  492. import { useSeoMeta } from '#imports';
  493. import {ElBreadcrumb, ElBreadcrumbItem, ElForm, ElFormItem, ElInput, ElRadioGroup, ElRadio,
  494. ElCheckboxGroup, ElCheckbox, ElUpload, ElMessage,
  495. ElSelect, ElOption, ElDatePicker, ElButton, genFileId,ElDialog } from 'element-plus'
  496. const { $webUrl, $CwebUrl } = useNuxtApp();
  497. // 表单引用
  498. const formRef = ref(null)
  499. // 上传组件引用
  500. const uploadRefs = ref({});
  501. const fileLists = ref({});
  502. onBeforeUpdate(() => {
  503. uploadRefs.value = {};
  504. });
  505. // 动态验证规则
  506. const rules_js = ref({})
  507. const get_from_data_1 = reactive({
  508. value:{}
  509. });
  510. const get_from_data= reactive({
  511. value:{}
  512. });
  513. const chinese_calendar = reactive({});
  514. //列表头
  515. const table_head = reactive({
  516. value:[]
  517. });
  518. //列表内容
  519. const table_body = reactive({
  520. value:[]
  521. });
  522. // 表单数据 - 使用ref而不是reactive来避免响应式问题
  523. const formData = ref({})
  524. const route = useRoute()
  525. // 从路由获取 table_id,如果不存在则使用默认值
  526. const table_id = ref(route.query.table_id || 59)
  527. // 得到id
  528. const recive_id = reactive({
  529. value:route.query.id || route.params.id
  530. })
  531. // 工具函数定义
  532. function getFieldName(field) {
  533. if (field.field) return field.field;
  534. const possibleFieldNames = ['name', 'field_name', 'key', 'id'];
  535. for (const name of possibleFieldNames) {
  536. if (field[name]) return field[name];
  537. }
  538. return undefined;
  539. }
  540. // 检查字段类型的函数,支持数组格式
  541. function checkFieldType(field, type) {
  542. if (!field.field_type) return false;
  543. if (Array.isArray(field.field_type)) {
  544. return field.field_type.map(Number).includes(Number(type));
  545. }
  546. return Number(field.field_type) === Number(type);
  547. }
  548. // 检查是否为数组类型字段(field_type为5或包含5的数组)
  549. function isArrayTypeField(field) {
  550. return checkFieldType(field, 5);
  551. }
  552. // 生成动态验证规则
  553. function generateValidationRules() {
  554. const rules = {}
  555. table_head.value.forEach((field, index) => {
  556. const fieldRules = []
  557. const fieldName = getFieldName(field)
  558. // 必填验证
  559. if (field.is_check === 1) {
  560. fieldRules.push({
  561. required: true,
  562. message: `${field.title}不能为空`,
  563. trigger: ['blur', 'change']
  564. })
  565. }
  566. // 正则验证
  567. if (field.regular && field.regular.trim()) {
  568. try {
  569. const regex = parseRegExp(field.regular)
  570. fieldRules.push({
  571. validator: (rule, value, callback) => {
  572. if (value && !regex.test(value)) {
  573. callback(new Error(`${field.title}格式不正确`))
  574. } else {
  575. callback()
  576. }
  577. },
  578. trigger: ['blur', 'change']
  579. })
  580. } catch (error) {
  581. console.error(`正则表达式错误: ${field.regular}`, error)
  582. }
  583. }
  584. if (fieldRules.length > 0 && fieldName) {
  585. rules[fieldName] = fieldRules
  586. }
  587. })
  588. // 添加验证码规则
  589. if (showCaptcha.value) {
  590. rules['code'] = [
  591. { required: true, message: '验证码不能为空', trigger: 'blur' }
  592. ]
  593. }
  594. rules_js.value = rules
  595. }
  596. // 刷新验证码
  597. async function refreshCaptcha() {
  598. try {
  599. // 拼接 GET 参数
  600. const params = new URLSearchParams({ table_id: table_id.value });
  601. const url = `${$webUrl}/form/getWebGlobalTableFieldList?${params.toString()}`;
  602. const response = await fetch(url, {
  603. method: 'GET',
  604. headers: {
  605. 'Content-Type': 'application/json',
  606. 'Userurl': $CwebUrl,
  607. 'Origin': $CwebUrl
  608. }
  609. });
  610. const get_from_data = await response.json();
  611. // 只处理验证码相关的数据
  612. if (get_from_data.data?.table?.is_code === 1 && get_from_data.data?.code?.img) {
  613. showCaptcha.value = true
  614. captchaId.value = get_from_data.data.code.code_uniqid
  615. // 确保base64字符串包含正确的前缀
  616. if (get_from_data.data.code.img.startsWith('data:image')) {
  617. captchaImage.value = get_from_data.data.code.img
  618. } else {
  619. captchaImage.value = 'data:image/png;base64,' + get_from_data.data.code.img
  620. }
  621. // 清空验证码输入框
  622. formData.value.code = ''
  623. } else {
  624. showCaptcha.value = false
  625. captchaImage.value = ''
  626. captchaId.value = ''
  627. }
  628. } catch (error) {
  629. console.error('刷新验证码失败:', error);
  630. ElMessage.error('刷新验证码失败,请重试');
  631. }
  632. }
  633. useSeoMeta({
  634. title: seoTitle,
  635. meta: [
  636. { name: 'keywords', content: seoKeywords, tagPriority: 10 },
  637. { name: 'description', content: seoDescription, tagPriority: 10 },
  638. { name: 'viewport', content: 'width=device-width,initial-scale=1,user-scalable=no',tagPriority: 10 }
  639. ]
  640. });
  641. async function get_from_data_fun() {
  642. pageLoading.value = true
  643. try {
  644. // 拼接 GET 参数
  645. const params = new URLSearchParams({ table_id: table_id.value });
  646. const url = `/form/getWebGlobalTableFieldList?${params.toString()}`;
  647. console.log("url:",url)
  648. const response = await requestDataPromise(url, {
  649. method: 'GET',
  650. query: {}
  651. });
  652. const get_from_data = response
  653. console.log("返回数据:",get_from_data,response)
  654. title.value = get_from_data.data.table.name
  655. remark.value = get_from_data.data.table.remark
  656. seoTitle.value = response.data.table.name + "_" + response.data.table.website_name + "_" + response.data.table.suffix
  657. seoKeywords.value = response.data.table.keywords + "_" + response.data.table.website_name + "_" + response.data.table.suffix
  658. seoDescription.value = response.data.table.description + "_" + response.data.table.website_name + "_" + response.data.table.suffix
  659. // 检查是否需要显示验证码(仅在初始化时)
  660. if (get_from_data.data?.table?.is_code === 1 && get_from_data.data?.code?.img) {
  661. showCaptcha.value = true
  662. captchaId.value = get_from_data.data.code.code_uniqid
  663. // 确保base64字符串包含正确的前缀
  664. if (get_from_data.data.code.img.startsWith('data:image')) {
  665. captchaImage.value = get_from_data.data.code.img
  666. } else {
  667. captchaImage.value = 'data:image/png;base64,' + get_from_data.data.code.img
  668. }
  669. } else {
  670. showCaptcha.value = false
  671. captchaImage.value = ''
  672. captchaId.value = ''
  673. }
  674. // 赋值表头和表体
  675. table_head.value = get_from_data.data?.fields || [];
  676. table_body.value = get_from_data.data?.table || [];
  677. get_from_data_1.value = get_from_data;
  678. // 统一 field_type 为数字或数字数组
  679. table_head.value.forEach(field => {
  680. if (typeof field.field_type === 'string') {
  681. if (field.field_type.includes(',')) {
  682. field.field_type = field.field_type.split(',').map(Number);
  683. } else {
  684. field.field_type = Number(field.field_type);
  685. }
  686. }
  687. });
  688. // 如果API返回的数据为空,使用默认数据
  689. if (table_head.value.length === 0) {
  690. table_head.value = []
  691. }
  692. // 详细检查每个字段的结构
  693. table_head.value.forEach((field, index) => {
  694. if (field.option_value) {
  695. }
  696. })
  697. // 初始化表单数据
  698. table_head.value.forEach(field => {
  699. const fieldName = getFieldName(field)
  700. if (!fieldName) {
  701. console.warn(`字段 ${field.title} 无法确定字段名,跳过初始化`)
  702. return
  703. }
  704. // 根据字段类型初始化不同的默认值
  705. if (isArrayTypeField(field)) {
  706. // 数组类型字段(field_type为5或包含5的数组)初始化为空数组
  707. formData.value[fieldName] = []
  708. } else if (checkFieldType(field, 7) || checkFieldType(field, 8)) {
  709. formData.value[fieldName] = ''
  710. fileLists.value[fieldName] = []
  711. }
  712. else {
  713. // 其他类型初始化为空字符串
  714. formData.value[fieldName] = ''
  715. }
  716. })
  717. // 单独初始化验证码字段
  718. if (showCaptcha.value) {
  719. formData.value.code = ''
  720. }
  721. // 生成验证规则
  722. generateValidationRules()
  723. pageLoading.value = false
  724. } catch (error) {
  725. console.error('请求失败:', error);
  726. // 使用默认数据
  727. table_head.value = []
  728. // 初始化默认数据
  729. table_head.value.forEach(field => {
  730. const fieldName = getFieldName(field)
  731. if (isArrayTypeField(field)) {
  732. formData.value[fieldName] = []
  733. } else if (checkFieldType(field, 7) || checkFieldType(field, 8)) {
  734. formData.value[fieldName] = ''
  735. fileLists.value[fieldName] = []
  736. }
  737. else {
  738. formData.value[fieldName] = ''
  739. }
  740. })
  741. // 单独初始化验证码字段
  742. if (showCaptcha.value) {
  743. formData.value.code = ''
  744. }
  745. generateValidationRules()
  746. pageLoading.value = false
  747. ElMessage.warning('API请求失败,使用默认数据')
  748. }
  749. }
  750. get_from_data_fun();
  751. // 提交表单
  752. async function submitForm() {
  753. if (!formRef.value) {
  754. ElMessage.error('表单引用不存在')
  755. return
  756. }
  757. try {
  758. submitLoading.value = true
  759. // 手动检查必填字段
  760. const requiredFields = table_head.value.filter(field => field.is_check === 1)
  761. const missingFields = requiredFields.filter(field => {
  762. const fieldName = getFieldName(field)
  763. if (!fieldName) {
  764. console.warn(`字段 ${field.title} 无法确定字段名`)
  765. return true
  766. }
  767. const value = formData.value[fieldName]
  768. const isEmpty = value === '' || value === null || value === undefined || (Array.isArray(value) && value.length === 0)
  769. return isEmpty
  770. })
  771. if (missingFields.length > 0) {
  772. ElMessage.error(`请填写必填字段: ${missingFields.map(f => f.title).join(', ')}`)
  773. return
  774. }
  775. // 表单验证 - 使用正确的验证方式
  776. await formRef.value.validate(async (valid, fields) => {
  777. if (valid) {
  778. // 构建提交数据
  779. const otherDataPayload = { 'table_id': table_id.value };
  780. const dataToSubmit = { ...formData.value };
  781. if (showCaptcha.value) {
  782. otherDataPayload.code = dataToSubmit.code;
  783. otherDataPayload.code_uniqid = captchaId.value;
  784. delete dataToSubmit.code;
  785. }
  786. const submitData = {
  787. otherData: otherDataPayload,
  788. data: dataToSubmit
  789. }
  790. // 这里可以调用实际的提交接口
  791. const response = await fetch(`${$webUrl}/form/addWebGlobalTableData`, {
  792. method: 'POST',
  793. headers: {
  794. 'Content-Type': 'application/json',
  795. 'Userurl': $CwebUrl,
  796. 'Origin': $CwebUrl
  797. },
  798. body: JSON.stringify(submitData)
  799. })
  800. const result = await response.json();
  801. if(result.code==200){
  802. dialogVisible.value = true
  803. return
  804. }else{
  805. ElMessage.error(result.message)
  806. return
  807. }
  808. } else {
  809. ElMessage.error('请检查表单填写是否正确')
  810. }
  811. })
  812. } catch (error) {
  813. console.error('提交失败:', error)
  814. ElMessage.error('提交失败,请重试')
  815. } finally {
  816. submitLoading.value = false
  817. }
  818. }
  819. // 重置表单
  820. async function resetForm() {
  821. await get_from_data_fun();
  822. ElMessage.success('表单已重置');
  823. }
  824. // 下拉选择对象转数组
  825. function select_arr_fun(option_obj){
  826. if (!option_obj || typeof option_obj !== 'object') return []
  827. let new_arr = []
  828. // 遍历对象,key为value,value为label
  829. Object.keys(option_obj).forEach(key => {
  830. let the_in_obj = {}
  831. the_in_obj.label = option_obj[key]
  832. the_in_obj.value = +key // 转换为数字
  833. new_arr.push(the_in_obj)
  834. })
  835. return new_arr
  836. }
  837. // 单选按钮对象转数组
  838. function radio_arr_fun(option_obj) {
  839. if (!option_obj || typeof option_obj !== 'object') return []
  840. let new_arr = []
  841. // 遍历对象,key为value,value为label
  842. Object.keys(option_obj).forEach(key => {
  843. new_arr.push([option_obj[key], key]) // [label, value] 格式
  844. })
  845. return new_arr
  846. }
  847. // 复选框对象转数组
  848. function checkbox_arr_fun(option_obj) {
  849. if (!option_obj || typeof option_obj !== 'object') return []
  850. let new_arr = []
  851. // 遍历对象,key为value,value为label
  852. Object.keys(option_obj).forEach(key => {
  853. let the_in_obj = {}
  854. the_in_obj.label = option_obj[key]
  855. the_in_obj.value = String(key) // 确保值为字符串类型
  856. new_arr.push(the_in_obj)
  857. })
  858. return new_arr
  859. }
  860. // 文件上传相关
  861. // 文件超出限制时的处理
  862. const handleExceed = (files, fieldName) => {
  863. const upload = uploadRefs.value[fieldName];
  864. if (upload) {
  865. upload.clearFiles();
  866. const file = files[0];
  867. file.uid = genFileId();
  868. upload.handleStart(file);
  869. upload.submit();
  870. }
  871. };
  872. // 普通文件上传成功
  873. const handleFileSuccess = (res, file, fieldName) => {
  874. if (res.data && res.data.imgUrl) {
  875. formData.value[fieldName] = res.data.imgUrl;
  876. if (fileLists.value[fieldName]) {
  877. fileLists.value[fieldName] = [{
  878. name: file.name,
  879. url: res.data.imgUrl
  880. }];
  881. }
  882. }
  883. };
  884. // 文件移除
  885. const handleFileRemove = (fieldName) => {
  886. formData.value[fieldName] = '';
  887. if(fileLists.value[fieldName]) {
  888. fileLists.value[fieldName] = [];
  889. }
  890. };
  891. // img上传 8
  892. const handleAvatarSuccess = (res, fieldName) => {
  893. if (res.data && res.data.imgUrl) {
  894. formData.value[fieldName] = res.data.imgUrl
  895. }
  896. }
  897. const beforeAvatarUpload = (file) => {
  898. const isJPG = file.type === 'image/jpeg'
  899. const isPNG = file.type === 'image/png'
  900. const isLt2M = file.size / 1024 / 1024 < 2
  901. if (!isJPG && !isPNG) {
  902. ElMessage.error('只能上传 JPG/PNG 格式!')
  903. return false
  904. }
  905. if (!isLt2M) {
  906. ElMessage.error('图片大小不能超过 2MB!')
  907. return false
  908. }
  909. return (isJPG || isPNG) && isLt2M
  910. }
  911. // 页面加载时获取数据
  912. onMounted(async () => {
  913. await get_from_data_fun()
  914. // 确保表单引用存在
  915. await nextTick()
  916. if (formRef.value) {
  917. } else {
  918. }
  919. })
  920. // 复选框变化处理
  921. function handleCheckboxChange(field, value) {
  922. // 这里可以根据需要添加更多的逻辑处理
  923. }
  924. function parseRegExp(str) {
  925. if (typeof str === 'string' && str.startsWith('/') && str.lastIndexOf('/') > 0) {
  926. const lastSlash = str.lastIndexOf('/');
  927. const pattern = str.slice(1, lastSlash);
  928. const flags = str.slice(lastSlash + 1);
  929. return new RegExp(pattern, flags);
  930. }
  931. return new RegExp(str);
  932. }
  933. function onDialogConfirm() {
  934. dialogVisible.value = false;
  935. resetForm();
  936. }
  937. const skeletonChars = Array.from({ length: 16 }).map(() => ({
  938. top: Math.random() * 80 + 5, // 5%~85%
  939. left: Math.random() * 80 + 5, // 5%~85%
  940. fontSize: Math.random() * 60 + 60, // 60~120px
  941. opacity: Math.random() * 0.4 + 0.3, // 0.3~0.7
  942. rotate: Math.random() * 60 - 30 // -30~30deg
  943. }));
  944. </script>