My_form.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 My_Files from "./My_Files.vue";
  126. import { formatTime } from "../utils/getTime.js"
  127. export default {
  128. name: "My_form",
  129. components: { upload, My_Files },
  130. computed: {
  131. uploadIsShow() {
  132. return function (name, list) {
  133. return list.some(v => v == name)
  134. };
  135. }
  136. },
  137. props: {
  138. isReadOnly: Boolean,
  139. onConfirm: Function
  140. },
  141. data() {
  142. return {
  143. form: [],
  144. attachmentids: [],//待绑定附件ID列表
  145. linksids: [],//待删除附件列表
  146. }
  147. },
  148. methods: {
  149. render(form, init = false) {
  150. try {
  151. if (init) {
  152. this.form = form.map(v => {
  153. if (v.type == 'location') {
  154. v.value = init[v.key] || {
  155. address: ""
  156. };
  157. } else if (v.type == 'route') {
  158. v.value = init[v.key] || {
  159. showList: [],
  160. value: []
  161. }
  162. } else {
  163. v.value = init[v.key] || v.value;
  164. }
  165. return v
  166. });
  167. this.filesIndex = this.form.findIndex(v => v.type == 'file');
  168. if (this.form[this.filesIndex].value.length) setTimeout(() => {
  169. this.$refs['My_Files' + this.filesIndex][0].handleFiles(this.form[this.filesIndex].value, true)
  170. }, 500)
  171. }
  172. } catch (error) {
  173. console.log("init报错", error)
  174. }
  175. this.form = form;
  176. this.onComplete()
  177. },
  178. onInput(e, index) {
  179. this.$set(this.form[index], 'value', e.detail.value)
  180. this.onComplete()
  181. },
  182. //清空行内容
  183. clearRowValue(index) {
  184. let data = this.form[index],
  185. that = this;
  186. uni.showModal({
  187. title: '提示',
  188. content: `是否确定清空“${data.label}”内容`,
  189. success: ({ confirm }) => {
  190. if (confirm) {
  191. if (data.type == 'location') {
  192. that.form[index].value = {
  193. address: ""
  194. };
  195. } else {
  196. that.form[index].value = "";
  197. }
  198. that.onComplete()
  199. }
  200. },
  201. })
  202. },
  203. onRadio(value, index) {
  204. this.$set(this.form[index], 'value', value)
  205. console.log(this.form[index])
  206. this.onComplete()
  207. },
  208. //是否完成表单必填
  209. onComplete() {
  210. this.$emit("onConfirm", this.form.filter(v => v.required).map(v => {
  211. if (v.type == "file" && v.required) {
  212. return v.value.length !== 0
  213. } else if (v.type == "location" && v.required) {
  214. return v.value.address.length
  215. } else if (v.type == "route" && v.required) {
  216. return v.value.value.length
  217. } else {
  218. return v.required && !(v.value == "")
  219. }
  220. }).some(v => !v));
  221. },
  222. uploadCallback(attachmentids) {
  223. this.handleFileLink(attachmentids)
  224. },
  225. handleFileLink(attachmentids, ownertable = "temporary", ownerid = 1, data) {
  226. return new Promise((resolve, reject) => {
  227. if (attachmentids.length == 0) return resolve(true);
  228. this.$Http.basic({
  229. "classname": "system.attachment.Attachment",
  230. "method": "createFileLink",
  231. "content": {
  232. ownertable,
  233. ownerid,
  234. usetype: 'default',
  235. attachmentids
  236. }
  237. }).then(res => {
  238. console.log('跟进记录绑定附件', res)
  239. if (this.cutoff(res.msg)) return;
  240. resolve(res.data)
  241. if (ownertable == "temporary") {
  242. //临时文件
  243. if (this.filesIndex != -1) this.form[this.filesIndex].value = this.form[this.filesIndex].value.concat(res.data)
  244. this.attachmentids = this.attachmentids.concat(res.data.map(v => v.attachmentid))
  245. setTimeout(() => {
  246. this.$refs['My_Files' + this.filesIndex][0].handleFiles(this.form[this.filesIndex].value, true)
  247. }, 50)
  248. } else {
  249. //绑定数据
  250. this.attachmentids = [];
  251. }
  252. this.onComplete()
  253. })
  254. })
  255. },
  256. //删除附件
  257. onDeteleFiles({ deleteid, attachmentid }) {
  258. this.attachmentids = this.attachmentids.filter(v => v != attachmentid);
  259. this.form[this.filesIndex].value = this.form[this.filesIndex].value.filter(v => v.attachmentid != attachmentid);
  260. this.$refs['My_Files' + this.filesIndex][0].handleFiles(this.form[this.filesIndex].value, true);
  261. if (this.form[this.filesIndex].aDeletion || false) this.linksids.push(deleteid);
  262. this.onComplete()
  263. },
  264. //去添加
  265. routeToAdd(index) {
  266. let data = this.form[index];
  267. this.$Http.route = { data, index };
  268. this.$Http.handleAdd = this.routeHandleAdd.bind(this);
  269. uni.navigateTo({ url: data.path })
  270. },
  271. routeHandleAdd(result) {
  272. const { data, index } = this.$Http.route;
  273. data.value = result
  274. this.form[index] = data;
  275. uni.navigateBack();
  276. delete this.$Http.route;
  277. delete this.$Http.handleAdd;
  278. this.onComplete()
  279. },
  280. //移除route选项
  281. routeHandleDetele(name, i, index) {
  282. let that = this;
  283. uni.showModal({
  284. title: '提示',
  285. content: `是否确定移除"${name}"`,
  286. success: ({ confirm }) => {
  287. if (confirm) {
  288. that.form[index].value.showList.splice(i, 1)
  289. that.form[index].value.value.splice(i, 1)
  290. that.onComplete()
  291. }
  292. },
  293. })
  294. },
  295. //异步删除文件
  296. handleDeteleFiles(linksids) {
  297. return new Promise((resolve, reject) => {
  298. if (linksids.length == 0) return resolve(true);
  299. this.$Http.basic({
  300. "classname": "system.attachment.Attachment",
  301. "method": "deleteFileLink",
  302. "content": {
  303. linksids
  304. }
  305. }).then(res => {
  306. console.log("处理删除附件", res)
  307. if (this.cutoff(res.msg)) return;
  308. resolve(res)
  309. });
  310. })
  311. },
  312. geoLocation(item, index) {
  313. let that = this;
  314. if (item.value.longitude) {
  315. uni.showModal({
  316. title: '提示',
  317. content: '是否确定重新获取定位',
  318. success: ({ confirm }) => {
  319. if (confirm) handle();
  320. },
  321. })
  322. } else {
  323. handle();
  324. }
  325. function handle() {
  326. uni.showLoading({
  327. title: "定位中...",
  328. mask: true,
  329. })
  330. that.getLocation(true).then(res => {
  331. uni.hideLoading()
  332. if (res.errMsg != "getLocation:ok") return uni.showToast({
  333. title: '定位失败',
  334. icon: "none"
  335. });
  336. that.form[index].value = {
  337. longitude: res.longitude,
  338. latitude: res.latitude,
  339. address: res.result.address,
  340. time: formatTime()
  341. };
  342. that.onComplete()
  343. })
  344. }
  345. },
  346. onSubmit() {
  347. let res = {};
  348. //是否存在附件
  349. if (this.filesIndex != -1) {
  350. res.attachmentids = this.attachmentids;
  351. res.linksids = this.linksids;
  352. }
  353. this.form.forEach(v => {
  354. if (v.type == "radio") {
  355. v.value = v.options.find(s => s[v.showKey] == v.value);
  356. if (v.selectKey) v.value = v.value[v.selectKey]
  357. console.log(v)
  358. } else {
  359. if (v.key) res[v.key] = v.value;
  360. }
  361. })
  362. return res
  363. }
  364. },
  365. }
  366. </script>
  367. <style lang="scss" scoped>
  368. .form {
  369. width: 355px;
  370. margin: 0 auto;
  371. .form-row {
  372. margin-top: 10px;
  373. .label {
  374. display: flex;
  375. color: #ddd;
  376. font-size: 12px;
  377. .required {
  378. color: #FA3534;
  379. margin-right: 3px;
  380. }
  381. .file {
  382. display: flex;
  383. flex: 1;
  384. justify-content: flex-end;
  385. /deep/ .u-upload {
  386. flex: 0 !important;
  387. }
  388. .image {
  389. height: 18px;
  390. margin-right: 10px;
  391. }
  392. }
  393. }
  394. .value-box {
  395. display: flex;
  396. justify-content: center;
  397. align-items: center;
  398. width: 355px;
  399. background: #fff;
  400. margin-top: 10px;
  401. box-sizing: border-box;
  402. padding: 10px;
  403. border-radius: 4px;
  404. overflow: hidden;
  405. min-height: 40px;
  406. .input {
  407. flex: 1;
  408. font-size: 14px;
  409. }
  410. .textarea {
  411. padding: 0 !important;
  412. margin: 0 !important;
  413. }
  414. .placeholder-class {
  415. font-size: 14px;
  416. }
  417. .clear {
  418. padding-left: 4px;
  419. font-size: 14px;
  420. opacity: .9;
  421. }
  422. .tag {
  423. display: flex;
  424. align-items: center;
  425. padding: 4px 6px;
  426. background: #0054E1;
  427. color: #fff;
  428. border-radius: 4px;
  429. font-size: 13px;
  430. margin-right: 6px;
  431. margin-bottom: 4px;
  432. .iconfont {
  433. margin-left: 5px;
  434. font-size: 12px;
  435. }
  436. }
  437. }
  438. .tag-box {
  439. flex-wrap: wrap;
  440. .tag {
  441. flex-shrink: 0;
  442. }
  443. }
  444. .options {
  445. flex-wrap: wrap;
  446. justify-content: flex-start;
  447. .option {
  448. font-size: 12px;
  449. padding: 4px 6px;
  450. border-radius: 4px;
  451. border: 1px solid #ddd;
  452. margin-right: 6px;
  453. }
  454. .active {
  455. border: 0;
  456. background: #0054E1;
  457. color: #fff;
  458. }
  459. }
  460. }
  461. }
  462. </style>