My_form.vue 18 KB

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