my_form.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <template>
  2. <view>
  3. <block v-for="(item, index) in list " :key="item.key">
  4. <!-- 文本输入 -->
  5. <view class="input-box" v-if="item.type == 'text'" :style="{ marginTop: tovw(item.marginTop || 0) }"
  6. @click="focusLabel = item.label">
  7. <view class="box">
  8. <view class="label">
  9. <text class="must" v-if="item.isMust">*</text>
  10. {{ item.label }}:
  11. </view>
  12. <view class="content-box">
  13. <textarea auto-height type="text" :focus="focusLabel == item.label"
  14. :placeholder="item.placeholder || '请填写' + item.label" :value="item.value"
  15. :style="{ width: item.value ? '220px' : '240px' }" @input="onInput($event, index)"
  16. :maxlength="item.maxlength || '499'" confirm-type="done" />
  17. <icon v-if="item.value" class="icon" type="clear" size="3.733vw" @click="onClearInput(index)" />
  18. </view>
  19. </view>
  20. </view>
  21. <!-- 自定义选项分类 -->
  22. <view class="custom-class-box" v-else-if="item.type == 'customClass'"
  23. :style="{ marginTop: tovw(item.marginTop || 0) }">
  24. <view class="head">
  25. <view class="label">
  26. <text class="must" v-if="item.isMust">*</text>
  27. {{ item.label }}
  28. </view>
  29. <view class="state">
  30. {{ item.isMultipleChoice ? '可多选' : '仅单选' }}
  31. </view>
  32. </view>
  33. <view class="options">
  34. <view class="option"
  35. :class="item.isMultipleChoice ? (item.value.includes(option.value) ? 'active' : '') : (item.value == option.value ? 'active' : '')"
  36. v-for=" option in item.list " :key="option.value" hover-class="navigator-hover"
  37. @click="changOptions(option.value, index)">
  38. {{ option.remarks }}
  39. </view>
  40. </view>
  41. </view>
  42. <!-- 上传附件 -->
  43. <view class="custom-class-box" v-else-if="item.type == 'upload'"
  44. :style="{ marginTop: tovw(item.marginTop || 0) }">
  45. <view class="head">
  46. <view class="label">
  47. <text class="must" v-if="item.isMust">*</text>
  48. {{ item.label }}
  49. </view>
  50. <view class="state">
  51. {{ item.placeholder }}
  52. </view>
  53. </view>
  54. <view class="content">
  55. <view class="file-box" v-for="file in item.value" :key="file.attachmentid">
  56. <image class="image" v-if="file.fileType == 'image'" :src="file.url" mode="aspectFill"
  57. lazy-load="true" @click="previewImg(file)" />
  58. <video v-else-if="file.fileType == 'video'" class="video" :poster="file.subfiles[0].url"
  59. :src="file.url" />
  60. <image class="delete" @click.stop="deleteFile(file, index)"
  61. src="https://yossys06593.obs.cn-east-3.myhuaweicloud.com:443/202404241713944430197B47af9b2f.png"
  62. mode="widthFix" />
  63. </view>
  64. <my-upload v-if="item.allowUpload" :showLoading="false" :accept="item.accept"
  65. @uploadCallback="uploadCallback($event, index)" @onLoading="onUploadLoading($event, index)">
  66. <view class="upload-box" hover-class="navigator-hover">
  67. <u-loading-icon v-if="item.loading" />
  68. <text v-else class="iconfont icon-xiazai" />
  69. <text style="margin-left: 5px;">上传</text>
  70. </view>
  71. </my-upload>
  72. </view>
  73. </view>
  74. </block>
  75. </view>
  76. </template>
  77. <script>
  78. import myUpload from "./my-upload.vue";
  79. import { formattedFiles, viewImage } from "../utils/settleFiles.js"
  80. export default {
  81. name: "my_form",
  82. components: { myUpload },
  83. props: {
  84. form: {
  85. type: Array,
  86. default: []
  87. },
  88. isUncomplete: {
  89. type: Function
  90. },
  91. onUploading: {
  92. type: Function
  93. }
  94. },
  95. data() {
  96. return {
  97. list: [],
  98. focusLabel: ""
  99. }
  100. },
  101. watch: {
  102. form: function (newVal) {
  103. if (newVal) {
  104. this.list = JSON.parse(JSON.stringify(newVal));
  105. this.verify()
  106. } else {
  107. }
  108. }
  109. },
  110. async created() {
  111. /* let list = [{
  112. key: "name",
  113. type: "text",
  114. label: "标题",
  115. isMust: true,//是否必填
  116. value: "",
  117. marginTop: 10
  118. }, {
  119. key: "Class",
  120. type: "customClass",
  121. label: "标题",
  122. isMust: false,//是否必填
  123. isMultipleChoice: true,//是否多选
  124. value: [],// 多选[] 单选 ""
  125. isMust: true,//是否必填
  126. list: await this.getCustomClass('picturespace'),
  127. marginTop: 10
  128. },{
  129. key: "attachmentids",
  130. type: "upload",
  131. label: "图片/视频",
  132. accept:"all",
  133. placeholder: "可上传多个视频或图片",
  134. ownertable: "temporary",
  135. ownerid: 999,
  136. usetype: 'default',
  137. allowUpload: true,
  138. allowDelete: true,
  139. value:[],
  140. marginTop: 10
  141. }] */
  142. },
  143. methods: {
  144. onInput(e, index) {
  145. this.$set(this.list[index], 'value', e.detail.value)
  146. if (this.list[index].isMust) this.verify()
  147. },
  148. onClearInput(index) {
  149. this.$set(this.list[index], 'value', '')
  150. if (this.list[index].isMust) this.verify()
  151. },
  152. changOptions(value, index) {
  153. let item = this.list[index];
  154. if (item.isMultipleChoice) {
  155. let i = -1;
  156. try {
  157. i = item.value.findIndex(v => v == value)
  158. } catch (error) {
  159. }
  160. if (i == -1) {
  161. item.value.push(value)
  162. } else {
  163. item.value.splice(i, 1)
  164. }
  165. this.$set(this.list[index], 'value', item.value)
  166. } else {
  167. this.$set(this.list[index], 'value', value)
  168. }
  169. if (this.list[index].isMust) this.verify()
  170. },
  171. verify() {
  172. let list = this.list.filter(v => v.isMust);
  173. let Uncomplete = false;
  174. if (list.length) Uncomplete = list.some(v => {
  175. let res = false;
  176. if (v.type == 'customClass') {
  177. if (v.isMultipleChoice) {
  178. res = v.value.length == 0;
  179. } else {
  180. res = v.value == "";
  181. }
  182. } else if (v.type == 'upload') {
  183. res = v.value.length == 0;
  184. } else {
  185. res = v.value == "";
  186. }
  187. return res
  188. })
  189. this.$emit("isUncomplete", Uncomplete)
  190. },
  191. previewImg(item) {
  192. viewImage(item.url)
  193. },
  194. uploadCallback(attachmentids, index) {
  195. let item = this.list[index];
  196. this.$Http.basic({
  197. "classname": "system.attachment.Attachment",
  198. "method": "createFileLink",
  199. "content": {
  200. ownertable: item.ownertable,
  201. ownerid: item.ownerid,
  202. usetype: item.usetype,
  203. attachmentids
  204. }
  205. }).then(res => {
  206. console.log('绑定附件', res)
  207. if (this.cutoff(res.msg)) return;
  208. res.data = formattedFiles(res.data)
  209. item.value.push(res.data[0]);
  210. //临时文件
  211. if (res.data[0].ownertable == "temporary") try {
  212. item.temporarys.push(attachmentids[0])
  213. } catch (error) {
  214. item.temporarys = [attachmentids[0]]
  215. }
  216. this.$set(this.list[index], 'value', item.value)
  217. if (this.list[index].isMust) this.verify()
  218. })
  219. },
  220. deleteFiles() {
  221. this.list.forEach(v => {
  222. if (v.type == 'upload') {
  223. let linksids = v.value.filter(v => v.ownertable == "temporary").map(v => v.linksid)
  224. if (linksids.length) this.$Http.basic({
  225. "classname": "system.attachment.Attachment",
  226. "method": "deleteFileLink",
  227. "content": {
  228. linksids
  229. }
  230. }).then(res => {
  231. console.log("处理删除附件", res)
  232. if (this.cutoff(res.msg)) return;
  233. });
  234. }
  235. });
  236. },
  237. onUploadLoading(e, index) {
  238. this.$set(this.list[index], 'loading', e)
  239. this.$emit("onUploading", e)
  240. },
  241. deleteFile(flie, index) {
  242. let item = this.list[index];
  243. item.value = item.value.filter(v => v.attachmentid != flie.attachmentid)
  244. //临时文件
  245. if (flie.ownertable == "temporary") {
  246. item.temporarys = item.temporarys.filter(v => v != flie.attachmentid)
  247. this.$Http.basic({
  248. "classname": "system.attachment.Attachment",
  249. "method": "deleteFileLink",
  250. "content": {
  251. linksids: [flie.linksid]
  252. }
  253. }).then(res => {
  254. console.log("处理删除附件", res)
  255. if (this.cutoff(res.msg)) return;
  256. });
  257. } else {
  258. try {
  259. item.linksids.push(flie.linksid)
  260. } catch (error) {
  261. item.linksids = [flie.linksid]
  262. }
  263. }
  264. this.$set(this.list[index], 'value', item.value)
  265. if (this.list[index].isMust) this.verify()
  266. },
  267. submit() {
  268. return new Promise((resolve, reject) => {
  269. let res = {};
  270. this.list.forEach(v => {
  271. if (v.type == 'upload') {
  272. res.files = {
  273. temporarys: [],
  274. linksids: [],
  275. }
  276. try {
  277. res.files.temporarys = v.temporarys || []
  278. } catch (error) {
  279. }
  280. try {
  281. res.files.linksids = v.linksids || []
  282. } catch (error) {
  283. }
  284. } else {
  285. res[v.key] = v.value;
  286. }
  287. })
  288. resolve(res)
  289. })
  290. }
  291. },
  292. }
  293. </script>
  294. <style lang="scss">
  295. .custom-class-box {
  296. width: 100%;
  297. background: #fff;
  298. padding: 15px 0 15px 10px;
  299. box-sizing: border-box;
  300. .head {
  301. width: 355px;
  302. height: 20px;
  303. display: flex;
  304. justify-content: space-between;
  305. align-items: flex-end;
  306. .label {
  307. font-size: 14px;
  308. color: #333333;
  309. line-height: 20px;
  310. }
  311. .state {
  312. font-family: Source Han Sans SC, Source Han Sans SC;
  313. font-size: 12px;
  314. color: #999999;
  315. }
  316. }
  317. .options {
  318. display: flex;
  319. flex-wrap: wrap;
  320. .option {
  321. padding: 6px 10px;
  322. text-align: center;
  323. min-width: 81px;
  324. font-family: PingFang SC, PingFang SC;
  325. font-size: 14px;
  326. color: #333333;
  327. border-radius: 5px;
  328. background: #F2F2F2;
  329. margin-top: 10px;
  330. margin-right: 10px;
  331. box-sizing: border-box;
  332. }
  333. .active {
  334. background: #C30D23;
  335. color: #fff;
  336. }
  337. }
  338. .content {
  339. .file-box {
  340. position: relative;
  341. width: 355px;
  342. height: 240px;
  343. margin-top: 10px;
  344. .video,
  345. .image {
  346. width: 355px;
  347. height: 240px;
  348. border-radius: 5px;
  349. }
  350. .delete {
  351. position: absolute;
  352. width: 30px;
  353. top: 0;
  354. right: 0;
  355. z-index: 1;
  356. }
  357. }
  358. .upload-box {
  359. display: flex;
  360. justify-content: center;
  361. align-items: center;
  362. width: 355px;
  363. height: 45px;
  364. background: #FFFFFF;
  365. border-radius: 5px;
  366. border: 1px dashed #C30D23;
  367. font-family: Source Han Sans SC, Source Han Sans SC;
  368. font-size: 14px;
  369. color: #C30D23;
  370. margin-top: 10px;
  371. }
  372. }
  373. }
  374. .input-box {
  375. width: 100vw;
  376. background: #fff;
  377. box-sizing: border-box;
  378. padding-left: 10px;
  379. .box {
  380. display: flex;
  381. width: 100%;
  382. min-height: 54.4px;
  383. padding: 15px 0;
  384. box-sizing: border-box;
  385. border-bottom: 1px #DDDDDD solid;
  386. align-items: center;
  387. }
  388. .label {
  389. width: 100px;
  390. margin-right: 10px;
  391. line-height: 20px;
  392. font-family: Source Han Sans SC, Source Han Sans SC;
  393. font-size: 14px;
  394. color: #666666;
  395. flex-shrink: 0;
  396. .must {
  397. color: #E3041F;
  398. margin-right: 5px;
  399. }
  400. }
  401. .content-box {
  402. flex: 1;
  403. display: flex;
  404. align-items: center;
  405. padding-right: 10px;
  406. box-sizing: border-box;
  407. .icon {
  408. padding: 5px;
  409. }
  410. }
  411. }
  412. </style>