index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog";
  2. Page({
  3. data: {
  4. uploadImg: "../../assets/accessory/upload.png",
  5. value: [
  6. {
  7. name: "身份证",
  8. detail: '需要身份证清晰的正反面',
  9. require: true,
  10. maxlen: 2,
  11. key: 1,
  12. value: [
  13. // { value: "https://ossweb-img.qq.com/images/lol/web201310/skin/big10006.jpg" }
  14. ],
  15. },
  16. {
  17. name: "驾驶证",
  18. detail: '需要驾驶证清晰的正反面',
  19. require: true,
  20. maxlen: 2,
  21. key: 2,
  22. value: [],
  23. },
  24. {
  25. name: "收入类",
  26. key: 3,
  27. value: [],
  28. },
  29. {
  30. name: "半年流水",
  31. key: 3,
  32. value: [],
  33. },
  34. {
  35. name: "营业执照",
  36. key: 4,
  37. value: [],
  38. },
  39. {
  40. name: "挂靠协议",
  41. key: 5,
  42. value: [],
  43. },
  44. ]
  45. },
  46. // 查看
  47. seeFunc (e) { wx.previewImage({ current: e.currentTarget.dataset.url, urls: [e.currentTarget.dataset.url] }) },
  48. // 刪除
  49. deleteFunc (e) {
  50. Dialog.confirm({ message: '确认删除吗?' }).then(() => this._delete(e)).catch(() => { });
  51. },
  52. async _delete (e) {
  53. try {
  54. await wx.$request({ url: `/order/${this.data.id}/deleteImage/`, method: 'post', data: { id: e.currentTarget.dataset.id } });
  55. let { value } = this.data;
  56. value[e.currentTarget.dataset.index]['value'].splice(e.currentTarget.dataset.ind, 1);
  57. this.setData({ value })
  58. } catch (err) { wx.$err(err) };
  59. },
  60. // 添加
  61. addFunc (e) {
  62. this.photoAblum(e.currentTarget.dataset.index);
  63. },
  64. /**
  65. * 证件照来源
  66. * @param {number}
  67. */
  68. photoAblum (data) {
  69. let { value } = this.data;
  70. let count = value[data].hasOwnProperty('maxlen') ? value[data]['maxlen'] - value[data]['value'].length : 9;
  71. wx.chooseImage({
  72. count,
  73. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  74. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  75. success: res => this.request(res.tempFiles, value[data]['key'], data)
  76. })
  77. },
  78. /**
  79. * 上传图片
  80. * @param {array} data 图片本地路径 res.tempFiles
  81. * @param {string} key 订单id
  82. * @param {string} _index value[_index]
  83. */
  84. request (data, key, _index) {
  85. let { value } = this.data, brr = [], { id } = this.data;
  86. data.forEach((item, index) => {
  87. wx.$uploadFile({ url: `/order/${id}/upload_image/`, filePath: item.path, name: 'file', formData: { type: key } }).then(res => {
  88. value[_index]['value'].push({ value: JSON.parse(res).data.path, key: JSON.parse(res).data.id });
  89. index == data.length - 1 && end.bind(this)();
  90. }).catch(err => {
  91. brr.push(`第${index + 1}张图片上传失败!`);
  92. index == data.length - 1 && end.bind(this)();
  93. });
  94. })
  95. function end () {
  96. let obj = { value };
  97. if (brr.length > 0) {
  98. obj['errorShow'] = true;
  99. obj['megs'] = brr;
  100. };
  101. this.setData({ ...obj });
  102. }
  103. },
  104. onLoad: function (options) {
  105. let { value } = this.data;
  106. if (options.imgs || options.images) {
  107. options.imgs = JSON.parse(options.imgs);
  108. options.images = JSON.parse(options.images);
  109. value = value.map(itemA => {
  110. if (itemA.hasOwnProperty('maxlen') && options.imgs.length > 0) itemA['disabled'] = true;
  111. options.imgs.forEach(itemB => {
  112. if (itemA.key == itemB.name && itemA.hasOwnProperty('maxlen')) {
  113. itemA["disabled"] = false;
  114. itemA['errMsg'] = itemB.reason;
  115. };
  116. })
  117. options.images.forEach(itemC => {
  118. itemC.type == itemA.key && itemA.value.push({ ...itemA, key: itemC.upload_id, value: itemC.url })
  119. })
  120. return itemA
  121. })
  122. }
  123. this.setData({ id: options.data, value })
  124. },
  125. onReady: function () {
  126. },
  127. onShow: function () {
  128. },
  129. onHide: function () {
  130. },
  131. onUnload: function () {
  132. },
  133. onPullDownRefresh: function () {
  134. },
  135. onReachBottom: function () {
  136. }
  137. })