my_form.vue 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. <template>
  2. <view>
  3. <view class="head" v-if="requiredFieldOnly">
  4. <view class="label">
  5. 基本信息
  6. </view>
  7. <view class="content">
  8. 仅显示必填信息<text style="padding: 0 2.5px;" /> <u-switch activeColor="#C40C24" v-model="unShowAll" />
  9. </view>
  10. </view>
  11. <slot name="head" />
  12. <block v-for="(item, index) in list " :key="item.key">
  13. <block v-if="item.isMust || !unShowAll">
  14. <!-- 文本输入 -->
  15. <view class="input-box" v-if="item.type == 'text'" :style="{ marginTop: tovw(item.marginTop || 0) }"
  16. @click="focusLabel = item.label">
  17. <view class="box" :class="item.unBorBot ? '' : 'borBot'">
  18. <view class="label">
  19. <text class="must" v-if="item.isMust">*</text>
  20. {{ item.label }}:
  21. </view>
  22. <view class="content-box">
  23. <view class="content">
  24. <input v-if="item.inputmode == 'number'" type="number"
  25. placeholder-style="color: #BBBBBB;font-family: Source Han Sans SC, Source Han Sans SC;"
  26. :focus="focusLabel == item.label" :placeholder="item.placeholder || '请填写' + item.label"
  27. :value="item.value" :style="{ width: item.value ? '220px' : '240px' }"
  28. @input="onInput($event, index)" :maxlength="item.maxlength || '499'"
  29. confirm-type="done" />
  30. <textarea v-else
  31. placeholder-style="color: #BBBBBB;font-family: Source Han Sans SC, Source Han Sans SC;"
  32. auto-height type="text" :focus="focusLabel == item.label"
  33. :placeholder="item.placeholder || '请填写' + item.label" :value="item.value"
  34. :style="{ width: item.value ? '220px' : '240px' }" @input="onInput($event, index)"
  35. :maxlength="item.maxlength || '499'" confirm-type="done" />
  36. <icon v-if="item.value" class="icon" type="clear" size="3.733vw"
  37. @click="onClearInput(index)" />
  38. </view>
  39. <view v-if="item.errText" class="err-text">
  40. <icon class="icon" color="#E3041F" type="clear" size="2.733vw" />
  41. {{ item.errText }}
  42. </view>
  43. </view>
  44. </view>
  45. </view>
  46. <!-- 文本域 -->
  47. <view class="textarea-box" v-else-if="item.type == 'textarea'">
  48. <textarea class="textarea" :class="item.unBorBot ? '' : 'borBot'"
  49. placeholder-style="color: #BBBBBB;font-family: Source Han Sans SC, Source Han Sans SC;" type="text"
  50. :placeholder="item.placeholder || '请填写' + item.label" :value="item.value"
  51. @input="onInput($event, index)" :maxlength="item.maxlength || '499'" confirm-type="done" />
  52. </view>
  53. <!-- 自定义选项分类 -->
  54. <view class="custom-class-box" v-else-if="item.type == 'customClass'"
  55. :style="{ marginTop: tovw(item.marginTop || 0) }">
  56. <view class="head">
  57. <view class="label">
  58. <text class="must" v-if="item.isMust">*</text>
  59. {{ item.label }}
  60. </view>
  61. <view class="state">
  62. {{ item.isMultipleChoice ? '可多选' : '仅单选' }}
  63. </view>
  64. </view>
  65. <view class="options">
  66. <view class="option"
  67. :class="item.isMultipleChoice ? (item.value.includes(option.value) ? 'active' : '') : (item.value == option.value ? 'active' : '')"
  68. v-for=" option in item.list " :key="option.value" hover-class="navigator-hover"
  69. @click="changOptions(option.value, index)">
  70. {{ option.remarks }}
  71. </view>
  72. </view>
  73. </view>
  74. <!-- 选择所在地区 -->
  75. <picker class="region" @change="selectRegion($event, index)" mode='region' :disabled="item.disabled"
  76. :value="item.value" v-else-if="item.type == 'region'"
  77. :style="{ marginTop: tovw(item.marginTop || 0), opacity: item.disabled ? 0.7 : 1 }">
  78. <view class="box" :class="item.unBorBot ? '' : 'borBot'">
  79. <view class="label">
  80. <text class="must" v-if="item.isMust">*</text>
  81. {{ item.label }}:
  82. </view>
  83. <view class="content-box">
  84. <view class="value" v-if="item.value.length">
  85. {{ item.value.join(",") }}
  86. </view>
  87. <view v-else class="placeholder" hover-class="none">
  88. {{ item.placeholder || '请选择' + item.label }}
  89. </view>
  90. <view v-if="!item.disabled" class="iconfont icon-a-wodetiaozhuan" />
  91. </view>
  92. </view>
  93. </picker>
  94. <!-- 日期 -->
  95. <picker class="region" @change="selectRegion($event, index)" mode='date' :disabled="item.disabled"
  96. :value="item.value" v-else-if="item.type == 'date'"
  97. :style="{ marginTop: tovw(item.marginTop || 0), opacity: item.disabled ? 0.7 : 1 }">
  98. <view class="box" :class="item.unBorBot ? '' : 'borBot'">
  99. <view class="label">
  100. <text class="must" v-if="item.isMust">*</text>
  101. {{ item.label }}:
  102. </view>
  103. <view class="content-box">
  104. <view class="value" v-if="item.value">
  105. {{ item.value }}
  106. </view>
  107. <view v-else class="placeholder" hover-class="none">
  108. {{ item.placeholder || '请选择' + item.label }}
  109. </view>
  110. <view v-if="!item.disabled" class="iconfont icon-a-wodetiaozhuan" />
  111. </view>
  112. </view>
  113. </picker>
  114. <!-- 上传附件 -->
  115. <view class="custom-class-box" v-else-if="item.type == 'upload'"
  116. :style="{ marginTop: tovw(item.marginTop || 0) }">
  117. <view class="head">
  118. <view class="label">
  119. <text class="must" v-if="item.isMust">*</text>
  120. {{ item.label }}
  121. </view>
  122. <view class="state">
  123. {{ item.placeholder }}
  124. </view>
  125. </view>
  126. <view class="content">
  127. <view class="file-box" v-for="file in item.value" :key="file.attachmentid">
  128. <image class="image" v-if="file.fileType == 'image'" :src="file.url" mode="aspectFill"
  129. lazy-load="true" @click="previewImg(file)" />
  130. <video v-else-if="file.fileType == 'video'" class="video" :poster="file.subfiles[0].url"
  131. :src="file.url" />
  132. <image class="delete" @click.stop="deleteFile(file, index)"
  133. src="https://yossys06593.obs.cn-east-3.myhuaweicloud.com:443/202404241713944430197B47af9b2f.png"
  134. mode="widthFix" />
  135. </view>
  136. <My_upload v-if="item.allowUpload" :showLoading="false" :accept="item.accept"
  137. @uploadCallback="uploadCallback($event, index)" @onLoading="onUploadLoading($event, index)">
  138. <view class="upload-box" hover-class="navigator-hover">
  139. <u-loading-icon v-if="item.loading" />
  140. <text v-else class="iconfont icon-xiazai" />
  141. <text style="margin-left: 5px;">上传</text>
  142. </view>
  143. </My_upload>
  144. </view>
  145. </view>
  146. <!-- 开关 -->
  147. <view class="region" v-else-if="item.type == 'switch'">
  148. <view class="box" :class="item.unBorBot ? '' : 'borBot'">
  149. <view class="label">
  150. <text class="must" v-if="item.isMust">*</text>
  151. {{ item.label }}:
  152. </view>
  153. <view class="content-box">
  154. <view />
  155. <u-switch activeColor="#70D95D" v-model="item.value" :disabled="item.disabled"
  156. @change="switchChange($event, index)" />
  157. </view>
  158. </view>
  159. </view>
  160. <!-- 性别 -->
  161. <view class="region" v-else-if="item.type == 'sex'">
  162. <view class="box" :class="item.unBorBot ? '' : 'borBot'">
  163. <view class="label">
  164. <text class="must" v-if="item.isMust">*</text>
  165. {{ item.label }}:
  166. </view>
  167. <u-radio-group v-model="item.value" placement="row" @change="groupChange($event, index)">
  168. <u-radio :customStyle="{ marginRight: tovw(60) }" label="男" name="男" />
  169. <u-radio label="女" name="女" />
  170. </u-radio-group>
  171. </view>
  172. </view>
  173. </block>
  174. </block>
  175. </view>
  176. </template>
  177. <script>
  178. import { formattedFiles, viewImage } from "../utils/settleFiles.js"
  179. export default {
  180. name: "my_form",
  181. props: {
  182. form: {
  183. type: Array,
  184. default: []
  185. },
  186. isUncomplete: {
  187. type: Function
  188. },
  189. onUploading: {
  190. type: Function
  191. },
  192. requiredFieldOnly: {
  193. type: Boolean
  194. },
  195. isShowAll: {
  196. type: Function
  197. }
  198. },
  199. data() {
  200. return {
  201. list: [],
  202. focusLabel: "",
  203. unShowAll: false,
  204. }
  205. },
  206. watch: {
  207. form: {
  208. handler: function (newVal) {
  209. if (newVal) {
  210. this.list = JSON.parse(JSON.stringify(newVal));
  211. setTimeout(() => {
  212. this.verify()
  213. }, 200);
  214. }
  215. },
  216. immediate: true,
  217. },
  218. unShowAll: function (newVal) {
  219. this.$emit("isShowAll", newVal)
  220. }
  221. },
  222. async created() {
  223. /* let list = [{
  224. key: "name",
  225. type: "text",
  226. label: "标题",
  227. isMust: true,//是否必填
  228. value: "",
  229. inputmode:"", //https://uniapp.dcloud.net.cn/component/input.html#type
  230. marginTop: 10,
  231. verify:[],
  232. }, {
  233. key: "Class",
  234. type: "customClass",
  235. label: "标题",
  236. isMust: false,//是否必填
  237. isMultipleChoice: true,//是否多选
  238. value: [],// 多选[] 单选 ""
  239. isMust: true,//是否必填
  240. list: await this.getCustomClass('picturespace'),
  241. marginTop: 10
  242. },{
  243. key: "attachmentids",
  244. type: "upload",
  245. label: "图片/视频",
  246. accept:"all",
  247. placeholder: "可上传多个视频或图片",
  248. ownertable: "temporary",
  249. ownerid: 999,
  250. usetype: 'default',
  251. allowUpload: true,
  252. allowDelete: true,
  253. value:[],
  254. marginTop: 10
  255. }, {
  256. key: "region",
  257. type: "region",
  258. label: "所在地区",
  259. isMust: true,//是否必填
  260. value: [],
  261. }, {
  262. key: "address",
  263. type: "textarea",
  264. label: "详细地址",
  265. isMust: true,//是否必填
  266. value: '',
  267. }, {
  268. key: "isdefault",
  269. type: "switch",
  270. label: "设为默认地址",
  271. isMust: false,//是否必填
  272. value: false,
  273. isNum: true,
  274. unBorBot: true
  275. }] */
  276. },
  277. methods: {
  278. onInput(e, index) {
  279. let item = this.list[index];
  280. item.errText = "";
  281. this.$set(this.list[index], 'value', e.detail.value)
  282. if (item.verify && item.verify.length && item.value != '') {
  283. let err = item.verify.find(r => !new RegExp(r.reg).test(item.value));
  284. if (err) this.$set(this.list[index], 'errText', err.errText)
  285. }
  286. this.verify()
  287. },
  288. onClearInput(index) {
  289. let item = this.list[index];
  290. item.errText = ''
  291. this.$set(this.list[index], 'value', '')
  292. this.verify()
  293. },
  294. switchChange(e, index) {
  295. this.$set(this.list[index], 'value', e)
  296. this.verify()
  297. },
  298. changOptions(value, index) {
  299. let item = this.list[index];
  300. if (item.isMultipleChoice) {
  301. let i = -1;
  302. try {
  303. i = item.value.findIndex(v => v == value)
  304. } catch (error) {
  305. }
  306. if (i == -1) {
  307. item.value.push(value)
  308. } else {
  309. item.value.splice(i, 1)
  310. }
  311. this.$set(this.list[index], 'value', item.value)
  312. } else {
  313. this.$set(this.list[index], 'value', value)
  314. }
  315. this.verify()
  316. },
  317. verify() {
  318. let list = this.list.filter(v => v.isMust);
  319. let Uncomplete = false;
  320. if (list.length) Uncomplete = list.some(v => {
  321. let res = false;
  322. if (v.type == 'customClass') {
  323. if (v.isMultipleChoice) {
  324. res = v.value.length == 0;
  325. } else {
  326. res = v.value == "";
  327. }
  328. } else if (v.type == 'upload') {
  329. res = v.value.length == 0;
  330. } else if (v.type == 'text') {
  331. res = v.value == "";
  332. if (v.errText) res = true;
  333. } else {
  334. res = v.value == "";
  335. }
  336. return res
  337. })
  338. if (!Uncomplete) Uncomplete = this.list.filter(v => !v.isMust).some(v => v.errText);
  339. this.$emit("isUncomplete", Uncomplete)
  340. },
  341. previewImg(item) {
  342. viewImage(item.url)
  343. },
  344. uploadCallback(attachmentids, index) {
  345. let item = this.list[index];
  346. this.$Http.basic({
  347. "classname": "system.attachment.Attachment",
  348. "method": "createFileLink",
  349. "content": {
  350. ownertable: item.ownertable,
  351. ownerid: item.ownerid,
  352. usetype: item.usetype,
  353. attachmentids
  354. }
  355. }).then(res => {
  356. console.log('绑定附件', res)
  357. if (this.cutoff(res.msg)) return;
  358. res.data = formattedFiles(res.data)
  359. item.value.push(res.data[0]);
  360. //临时文件
  361. if (res.data[0].ownertable == "temporary") try {
  362. item.temporarys.push(attachmentids[0])
  363. } catch (error) {
  364. item.temporarys = [attachmentids[0]]
  365. }
  366. this.$set(this.list[index], 'value', item.value)
  367. this.verify()
  368. })
  369. },
  370. deleteFiles() {
  371. this.list.forEach(v => {
  372. if (v.type == 'upload') {
  373. let linksids = v.value.filter(v => v.ownertable == "temporary").map(v => v.linksid)
  374. if (linksids.length) this.$Http.basic({
  375. "classname": "system.attachment.Attachment",
  376. "method": "deleteFileLink",
  377. "content": {
  378. linksids
  379. }
  380. }).then(res => {
  381. console.log("处理删除附件", res)
  382. if (this.cutoff(res.msg)) return;
  383. });
  384. }
  385. });
  386. },
  387. onUploadLoading(e, index) {
  388. this.$set(this.list[index], 'loading', e)
  389. this.$emit("onUploading", e)
  390. },
  391. groupChange(e, index) {
  392. this.$set(this.list[index], 'loading', e)
  393. this.verify()
  394. },
  395. selectRegion({ detail }, index) {
  396. this.$set(this.list[index], 'value', detail.value)
  397. this.verify()
  398. },
  399. deleteFile(flie, index) {
  400. let item = this.list[index];
  401. item.value = item.value.filter(v => v.attachmentid != flie.attachmentid)
  402. //临时文件
  403. if (flie.ownertable == "temporary") {
  404. item.temporarys = item.temporarys.filter(v => v != flie.attachmentid)
  405. this.$Http.basic({
  406. "classname": "system.attachment.Attachment",
  407. "method": "deleteFileLink",
  408. "content": {
  409. linksids: [flie.linksid]
  410. }
  411. }).then(res => {
  412. console.log("处理删除附件", res)
  413. if (this.cutoff(res.msg)) return;
  414. });
  415. } else {
  416. try {
  417. item.linksids.push(flie.linksid)
  418. } catch (error) {
  419. item.linksids = [flie.linksid]
  420. }
  421. }
  422. this.$set(this.list[index], 'value', item.value)
  423. this.verify()
  424. },
  425. submit() {
  426. return new Promise((resolve, reject) => {
  427. let res = {};
  428. this.list.forEach(v => {
  429. if (v.type == 'upload') {
  430. res.files = {
  431. temporarys: [],
  432. linksids: [],
  433. }
  434. try {
  435. res.files.temporarys = v.temporarys || []
  436. } catch (error) {
  437. }
  438. try {
  439. res.files.linksids = v.linksids || []
  440. } catch (error) {
  441. }
  442. } else if (v.type == 'region') {
  443. if (v.value.length) {
  444. res.province = v.value[0];
  445. res.city = v.value[1];
  446. res.county = v.value[2];
  447. } else {
  448. res.province = ''
  449. res.city = ''
  450. res.county = ''
  451. }
  452. } else if (v.type == 'switch') {
  453. if (v.isNum) {
  454. res[v.key] = v.value ? 1 : 0;
  455. } else {
  456. res[v.key] = v.value;
  457. }
  458. } else {
  459. try {
  460. res[v.key] = v.value.trim();
  461. } catch (error) {
  462. res[v.key] = v.value;
  463. }
  464. }
  465. })
  466. resolve(res)
  467. })
  468. }
  469. },
  470. }
  471. </script>
  472. <style lang="scss">
  473. .head {
  474. display: flex;
  475. align-items: center;
  476. justify-content: space-between;
  477. width: 100vw;
  478. height: 45px;
  479. background: #F7F7F7;
  480. padding: 0 10px;
  481. box-sizing: border-box;
  482. .label {
  483. font-family: PingFang SC, PingFang SC;
  484. font-weight: bold;
  485. font-size: 15px;
  486. color: #333333;
  487. line-height: 22px;
  488. }
  489. .content {
  490. display: flex;
  491. align-items: center;
  492. font-family: PingFang SC, PingFang SC;
  493. font-size: 14px;
  494. color: #999999;
  495. }
  496. }
  497. .borBot {
  498. border-bottom: 1px #DDDDDD solid;
  499. }
  500. .custom-class-box {
  501. width: 100%;
  502. background: #fff;
  503. padding: 15px 0 15px 10px;
  504. box-sizing: border-box;
  505. .head {
  506. width: 355px;
  507. height: 20px;
  508. display: flex;
  509. justify-content: space-between;
  510. align-items: flex-end;
  511. .label {
  512. font-size: 14px;
  513. color: #333333;
  514. line-height: 20px;
  515. }
  516. .state {
  517. font-family: Source Han Sans SC, Source Han Sans SC;
  518. font-size: 12px;
  519. color: #999999;
  520. }
  521. }
  522. .options {
  523. display: flex;
  524. flex-wrap: wrap;
  525. .option {
  526. padding: 6px 10px;
  527. text-align: center;
  528. min-width: 81px;
  529. font-family: PingFang SC, PingFang SC;
  530. font-size: 14px;
  531. color: #333333;
  532. border-radius: 5px;
  533. background: #F2F2F2;
  534. margin-top: 10px;
  535. margin-right: 10px;
  536. box-sizing: border-box;
  537. }
  538. .active {
  539. background: #C30D23;
  540. color: #fff;
  541. }
  542. }
  543. .content {
  544. .file-box {
  545. position: relative;
  546. width: 355px;
  547. height: 240px;
  548. margin-top: 10px;
  549. .video,
  550. .image {
  551. width: 355px;
  552. height: 240px;
  553. border-radius: 5px;
  554. }
  555. .delete {
  556. position: absolute;
  557. width: 30px;
  558. top: 0;
  559. right: 0;
  560. z-index: 1;
  561. }
  562. }
  563. .upload-box {
  564. display: flex;
  565. justify-content: center;
  566. align-items: center;
  567. width: 355px;
  568. height: 45px;
  569. background: #FFFFFF;
  570. border-radius: 5px;
  571. border: 1px dashed #C30D23;
  572. font-family: Source Han Sans SC, Source Han Sans SC;
  573. font-size: 14px;
  574. color: #C30D23;
  575. margin-top: 10px;
  576. }
  577. }
  578. }
  579. .textarea-box {
  580. width: 100%;
  581. .textarea {
  582. width: 355px;
  583. height: 160px;
  584. padding: 15px 10px;
  585. box-sizing: border-box;
  586. margin: 0 auto;
  587. }
  588. }
  589. .input-box {
  590. width: 100vw;
  591. background: #fff;
  592. box-sizing: border-box;
  593. padding-left: 10px;
  594. .box {
  595. display: flex;
  596. width: 100%;
  597. min-height: 54.4px;
  598. padding: 15px 0;
  599. box-sizing: border-box;
  600. align-items: center;
  601. }
  602. .label {
  603. width: 100px;
  604. margin-right: 10px;
  605. line-height: 20px;
  606. font-family: Source Han Sans SC, Source Han Sans SC;
  607. font-size: 14px;
  608. color: #666666;
  609. flex-shrink: 0;
  610. .must {
  611. color: #E3041F;
  612. margin-right: 5px;
  613. }
  614. }
  615. .content-box {
  616. padding-right: 10px;
  617. .content {
  618. flex: 1;
  619. display: flex;
  620. align-items: center;
  621. box-sizing: border-box;
  622. .icon {
  623. padding: 5px;
  624. }
  625. }
  626. .err-text {
  627. font-size: 12px;
  628. color: #E3041F;
  629. margin-bottom: -12px;
  630. .icon {
  631. margin-right: 2px;
  632. }
  633. }
  634. }
  635. }
  636. .region {
  637. width: 100vw;
  638. background: #fff;
  639. box-sizing: border-box;
  640. padding: 15px 10px 0;
  641. .box {
  642. display: flex;
  643. padding-bottom: 15px;
  644. .label {
  645. width: 100px;
  646. margin-right: 10px;
  647. line-height: 20px;
  648. font-family: Source Han Sans SC, Source Han Sans SC;
  649. font-size: 14px;
  650. color: #666666;
  651. flex-shrink: 0;
  652. .must {
  653. color: #E3041F;
  654. margin-right: 5px;
  655. }
  656. }
  657. .content-box {
  658. flex: 1;
  659. display: flex;
  660. align-items: center;
  661. justify-content: space-between;
  662. height: 20px;
  663. line-height: 20px;
  664. .placeholder {
  665. font-family: Source Han Sans SC, Source Han Sans SC;
  666. font-size: 14px;
  667. color: #BBBBBB;
  668. }
  669. }
  670. }
  671. }
  672. </style>