My_form.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <template>
  2. <view class="form">
  3. <view v-for="item, index in form" :key="item.key" :style="{ opacity: isReadOnly || item.disabled ? .9 : 1 }">
  4. <view class="form-row">
  5. <view class="label">
  6. <text class="required" v-if="item.required">*</text>
  7. {{ item.label }}
  8. <view class="file" v-if="item.type == 'file' && !item.disabled && !isReadOnly">
  9. <upload accept="image" v-if="uploadIsShow('image', item.fileType)" @uploadCallback="uploadCallback">
  10. <view hover-class="navigator-hover">
  11. <image class="image" src="../static/file/image.png" mode="heightFix" />
  12. </view>
  13. </upload>
  14. <upload accept="video" v-if="uploadIsShow('video', item.fileType)" @uploadCallback="uploadCallback">
  15. <view hover-class="navigator-hover">
  16. <image class="image" src="../static/file/video.png" mode="heightFix" />
  17. </view>
  18. </upload>
  19. <upload accept="file" v-if="uploadIsShow('file', item.fileType)" @uploadCallback="uploadCallback">
  20. <view hover-class="navigator-hover">
  21. <image class="image" src="../static/file/folder.png" mode="heightFix" />
  22. </view>
  23. </upload>
  24. </view>
  25. </view>
  26. <view v-if="item.type == 'radio'" class="value-box options">
  27. <view class="option" :class="option[item.showKey] == item.value ? 'active' : ''"
  28. v-for="option in item.options" :key="option[item.showKey]"
  29. :hover-class="isReadOnly || item.disabled ? '' : 'navigator-hover'"
  30. @click=" isReadOnly || item.disabled ? '' : onRadio(option[item.showKey], index)">
  31. {{ option[item.showKey] }}
  32. </view>
  33. </view>
  34. <view v-else-if="item.type == 'route'" class="value-box tag-box"
  35. style="justify-content: flex-start;padding-bottom: 6px;">
  36. <view class="tag" :hover-class="isReadOnly || item.disabled ? '' : 'navigator-hover'"
  37. v-for="(name, i) in item.value.showList" :key="name" @click="routeHandleDetele(name, i, index)">
  38. {{ name }}
  39. <text v-if="!(isReadOnly || item.disabled)" class="iconfont icon-dibu-diudan" />
  40. </view>
  41. <view class="tag" v-if="!(isReadOnly || item.disabled)" hover-class="navigator-hover"
  42. @click="routeToAdd(index)">
  43. 去选择
  44. <text class="iconfont" style="font-weight: bold;font-size: 14px;">+</text>
  45. </view>
  46. <view v-if="(isReadOnly || item.disabled) && item.value.showList.length == 0" style="font-size: 14px;">
  47. 未添加{{ item.label }}
  48. </view>
  49. </view>
  50. <view v-else-if="item.type == 'file'" class="value-box" style="justify-content: flex-start;">
  51. <view v-if="!item.value.length" style="font-size: 14px;">
  52. 暂未上传附件
  53. </view>
  54. <My_Files v-else :ref="'My_Files' + index" :aDeletion="item.aDeletion"
  55. :isDelete="item.isDelete && !item.disabled && !isReadOnly" @onDeteleFiles="onDeteleFiles" />
  56. </view>
  57. <view v-else-if="item.type == 'location'" class="value-box"
  58. :hover-class="isReadOnly || item.disabled ? '' : 'navigator-hover'"
  59. style="justify-content: space-between;" @click="geoLocation(item, index)">
  60. <view v-if="item.value.address">
  61. {{ item.value.address }}
  62. <view style="color: #666; font-size: 12px;margin-top: 6px;">
  63. {{ item.value.time }}
  64. </view>
  65. </view>
  66. <view v-else>
  67. <text class="iconfont icon-a-wodemendianxinxidizhi" />
  68. 获取当前位置
  69. </view>
  70. <text v-if="item.value.address && !item.disabled && !isReadOnly" class="iconfont icon-dibu-diudan clear"
  71. @click.stop="clearRowValue(index)" />
  72. </view>
  73. <label :for="item.label" v-else class="value-box">
  74. <textarea :id="item.label" v-if="item.type == 'textarea'" class="input textarea" :value="item.value"
  75. auto-height @input="onInput($event, index)" placeholder-class="placeholder-class" :type="item.type"
  76. :disabled="isReadOnly || item.disabled" :password="item.password"
  77. :placeholder="item.placeholder || '请填写' + item.label" :maxlength="item.maxlength || '-1'" />
  78. <input :id="item.label" v-else class="input" :value="item.value" @input="onInput($event, index)"
  79. placeholder-class="placeholder-class" :type="item.type" :disabled="isReadOnly || item.disabled"
  80. :password="item.password" :placeholder="item.placeholder || '请填写' + item.label"
  81. :maxlength="item.maxlength || '-1'" />
  82. <text v-if="item.value && !item.disabled && !isReadOnly" class="iconfont icon-dibu-diudan clear"
  83. @click.stop="clearRowValue(index)" />
  84. </label>
  85. </view>
  86. </view>
  87. </view>
  88. </template>
  89. <script>
  90. let form示例 = [{
  91. label: "工序说明",//标题
  92. disabled: false,//禁用
  93. type: 'text',//类型
  94. password: false,//是否密码类型
  95. placeholder: "",
  96. value: "",
  97. maxlength: -1,//最大长度
  98. required: false,//是否必填
  99. }, {
  100. label: "备注",
  101. disabled: false,//禁用
  102. type: 'textarea',//类型
  103. placeholder: "",
  104. value: "",
  105. maxlength: -1,//最大长度
  106. required: false,//是否必填
  107. }, {
  108. label: "是否确认",
  109. disabled: false,//禁用
  110. type: 'radio',//类型
  111. value: "",
  112. options: [{
  113. name: "是",
  114. value: 1
  115. },
  116. {
  117. name: "否",
  118. value: 0
  119. }],
  120. showKey: 'name',//必传 唯一值
  121. selectKey: "",//传值key返回该key的值,不传返回整个选择数据
  122. required: false,//是否必填
  123. }];
  124. import upload from "./my-upload.vue";
  125. import { formatTime } from "../utils/getTime.js"
  126. export default {
  127. name: "My_form",
  128. components: { upload },
  129. computed: {
  130. uploadIsShow() {
  131. return function (name, list) {
  132. return list.some(v => v == name)
  133. };
  134. }
  135. },
  136. props: {
  137. isReadOnly: Boolean,
  138. onConfirm: Function
  139. },
  140. data() {
  141. return {
  142. form: [],
  143. attachmentids: [],//待绑定附件ID列表
  144. linksids: [],//待删除附件列表
  145. }
  146. },
  147. methods: {
  148. render(form, init = false) {
  149. try {
  150. if (init) {
  151. this.form = form.map(v => {
  152. if (v.type == 'location') {
  153. v.value = init[v.key] || {
  154. address: ""
  155. };
  156. } else if (v.type == 'route') {
  157. v.value = init[v.key] || {
  158. showList: [],
  159. value: []
  160. }
  161. } else {
  162. v.value = init[v.key] || v.value;
  163. }
  164. return v
  165. });
  166. this.filesIndex = this.form.findIndex(v => v.type == 'file');
  167. if (this.form[this.filesIndex].value.length) setTimeout(() => {
  168. this.$refs['My_Files' + this.filesIndex][0].handleFiles(this.form[this.filesIndex].value, true)
  169. }, 500)
  170. } else {
  171. this.filesIndex = form.findIndex(v => v.type == 'file');
  172. this.form = form;
  173. }
  174. } catch (error) {
  175. console.log("init报错", error)
  176. this.form = form;
  177. }
  178. this.onComplete()
  179. },
  180. onInput(e, index) {
  181. this.$set(this.form[index], 'value', e.detail.value)
  182. this.onComplete()
  183. },
  184. //清空行内容
  185. clearRowValue(index) {
  186. let data = this.form[index],
  187. that = this;
  188. uni.showModal({
  189. title: '提示',
  190. content: `是否确定清空“${data.label}”内容`,
  191. success: ({ confirm }) => {
  192. if (confirm) {
  193. if (data.type == 'location') {
  194. that.form[index].value = {
  195. address: ""
  196. };
  197. } else {
  198. that.form[index].value = "";
  199. }
  200. that.onComplete()
  201. }
  202. },
  203. })
  204. },
  205. onRadio(value, index) {
  206. this.$set(this.form[index], 'value', value)
  207. console.log(this.form[index])
  208. this.onComplete()
  209. },
  210. //是否完成表单必填
  211. onComplete() {
  212. this.$emit("onConfirm", this.form.filter(v => v.required).map(v => {
  213. if (v.type == "file" && v.required) {
  214. return v.value.length !== 0
  215. } else if (v.type == "location" && v.required) {
  216. return v.value.address.length
  217. } else if (v.type == "route" && v.required) {
  218. return v.value.value.length
  219. } else {
  220. return v.required && !(v.value == "")
  221. }
  222. }).some(v => !v));
  223. },
  224. uploadCallback(attachmentids) {
  225. this.handleFileLink(attachmentids)
  226. },
  227. handleFileLink(attachmentids, ownertable = "temporary", ownerid = 1, data) {
  228. return new Promise((resolve, reject) => {
  229. if (attachmentids.length == 0) return resolve(true);
  230. this.$Http.basic({
  231. "classname": "system.attachment.Attachment",
  232. "method": "createFileLink",
  233. "content": {
  234. ownertable,
  235. ownerid,
  236. usetype: 'default',
  237. attachmentids
  238. }
  239. }).then(res => {
  240. console.log('跟进记录绑定附件', res)
  241. if (this.cutoff(res.msg)) return;
  242. resolve(res.data)
  243. if (ownertable == "temporary") {
  244. //临时文件
  245. console.log(this.form)
  246. console.log(this.filesIndex)
  247. if (this.filesIndex != -1) this.form[this.filesIndex].value = this.form[this.filesIndex].value.concat(res.data)
  248. this.attachmentids = this.attachmentids.concat(res.data.map(v => v.attachmentid))
  249. setTimeout(() => {
  250. this.$refs['My_Files' + this.filesIndex][0].handleFiles(this.form[this.filesIndex].value, true)
  251. }, 50)
  252. } else {
  253. //绑定数据
  254. this.attachmentids = [];
  255. }
  256. this.onComplete()
  257. })
  258. })
  259. },
  260. //删除附件
  261. onDeteleFiles({ deleteid, attachmentid }) {
  262. this.attachmentids = this.attachmentids.filter(v => v != attachmentid);
  263. this.form[this.filesIndex].value = this.form[this.filesIndex].value.filter(v => v.attachmentid != attachmentid);
  264. this.$refs['My_Files' + this.filesIndex][0].handleFiles(this.form[this.filesIndex].value, true);
  265. if (this.form[this.filesIndex].aDeletion || false) this.linksids.push(deleteid);
  266. this.onComplete()
  267. },
  268. //去添加
  269. routeToAdd(index) {
  270. let data = this.form[index];
  271. this.$Http.route = { data, index };
  272. this.$Http.handleAdd = this.routeHandleAdd.bind(this);
  273. uni.navigateTo({ url: data.path })
  274. },
  275. routeHandleAdd(result) {
  276. const { data, index } = this.$Http.route;
  277. data.value = result
  278. this.form[index] = data;
  279. uni.navigateBack();
  280. delete this.$Http.route;
  281. delete this.$Http.handleAdd;
  282. this.onComplete()
  283. },
  284. //移除route选项
  285. routeHandleDetele(name, i, index) {
  286. let that = this;
  287. uni.showModal({
  288. title: '提示',
  289. content: `是否确定移除"${name}"`,
  290. success: ({ confirm }) => {
  291. if (confirm) {
  292. that.form[index].value.showList.splice(i, 1)
  293. that.form[index].value.value.splice(i, 1)
  294. that.onComplete()
  295. }
  296. },
  297. })
  298. },
  299. //异步删除文件
  300. handleDeteleFiles(linksids) {
  301. return new Promise((resolve, reject) => {
  302. if (linksids.length == 0) return resolve(true);
  303. this.$Http.basic({
  304. "classname": "system.attachment.Attachment",
  305. "method": "deleteFileLink",
  306. "content": {
  307. linksids
  308. }
  309. }).then(res => {
  310. console.log("处理删除附件", res)
  311. if (this.cutoff(res.msg)) return;
  312. resolve(res)
  313. });
  314. })
  315. },
  316. geoLocation(item, index) {
  317. let that = this;
  318. if (item.value.longitude) {
  319. uni.showModal({
  320. title: '提示',
  321. content: '是否确定重新获取定位',
  322. success: ({ confirm }) => {
  323. if (confirm) handle();
  324. },
  325. })
  326. } else {
  327. handle();
  328. }
  329. function handle() {
  330. uni.showLoading({
  331. title: "定位中...",
  332. mask: true,
  333. })
  334. that.getLocation(true).then(res => {
  335. console.log("定位", res)
  336. uni.hideLoading()
  337. if (res.errMsg != "getLocation:ok") return uni.showToast({
  338. title: '定位失败',
  339. icon: "none"
  340. });
  341. that.form[index].value = {
  342. longitude: res.longitude,
  343. latitude: res.latitude,
  344. address: res.result.address,
  345. time: formatTime(),
  346. result: res.result
  347. };
  348. that.onComplete()
  349. })
  350. }
  351. },
  352. onSubmit() {
  353. let res = {};
  354. //是否存在附件
  355. if (this.filesIndex != -1) {
  356. res.attachmentids = this.attachmentids;
  357. res.linksids = this.linksids;
  358. }
  359. this.form.forEach(v => {
  360. if (v.type == "radio") {
  361. v.value = v.options.find(s => s[v.showKey] == v.value);
  362. if (v.selectKey) v.value = v.value[v.selectKey]
  363. }
  364. if (v.key) res[v.key] = v.value;
  365. })
  366. return res
  367. }
  368. },
  369. }
  370. </script>
  371. <style lang="scss" scoped>
  372. .form {
  373. width: 355px;
  374. margin: 0 auto;
  375. .form-row {
  376. margin-top: 10px;
  377. .label {
  378. display: flex;
  379. color: #ddd;
  380. font-size: 12px;
  381. .required {
  382. color: #FA3534;
  383. margin-right: 3px;
  384. }
  385. .file {
  386. display: flex;
  387. flex: 1;
  388. justify-content: flex-end;
  389. /deep/ .u-upload {
  390. flex: 0 !important;
  391. }
  392. .image {
  393. height: 18px;
  394. margin-right: 10px;
  395. }
  396. }
  397. }
  398. .value-box {
  399. display: flex;
  400. justify-content: center;
  401. align-items: center;
  402. width: 355px;
  403. background: #fff;
  404. margin-top: 10px;
  405. box-sizing: border-box;
  406. padding: 10px;
  407. border-radius: 4px;
  408. overflow: hidden;
  409. min-height: 40px;
  410. .input {
  411. flex: 1;
  412. font-size: 14px;
  413. }
  414. .textarea {
  415. padding: 0 !important;
  416. margin: 0 !important;
  417. }
  418. .placeholder-class {
  419. font-size: 14px;
  420. }
  421. .clear {
  422. padding-left: 4px;
  423. font-size: 14px;
  424. opacity: .9;
  425. }
  426. .tag {
  427. display: flex;
  428. align-items: center;
  429. padding: 4px 6px;
  430. background: #0054E1;
  431. color: #fff;
  432. border-radius: 4px;
  433. font-size: 13px;
  434. margin-right: 6px;
  435. margin-bottom: 4px;
  436. .iconfont {
  437. margin-left: 5px;
  438. font-size: 12px;
  439. }
  440. }
  441. }
  442. .tag-box {
  443. flex-wrap: wrap;
  444. .tag {
  445. flex-shrink: 0;
  446. }
  447. }
  448. .options {
  449. flex-wrap: wrap;
  450. justify-content: flex-start;
  451. .option {
  452. font-size: 12px;
  453. padding: 4px 6px;
  454. border-radius: 4px;
  455. border: 1px solid #ddd;
  456. margin-right: 6px;
  457. }
  458. .active {
  459. border: 0;
  460. background: #0054E1;
  461. color: #fff;
  462. }
  463. }
  464. }
  465. }
  466. </style>