index_form.vue 29 KB

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