You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

269 lines
9.3KB

  1. // package_A/page/upload/index.js
  2. const crypto = require('crypto-js')
  3. import {Base64} from 'js-base64'
  4. import {getOssAuth} from '../../../api/upload.js'
  5. import {confirmOrder, getAllTask} from '../../../api/task.js'
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. type: 'upload',
  12. form: {},
  13. carNumberList: [],
  14. healthCodeList: [],
  15. formRules: {
  16. carNumberList: {
  17. validator: function (value) {
  18. return value.length!=0;
  19. },
  20. warning: false
  21. },
  22. healthCodeList: {
  23. validator: function (value) {
  24. return value.length!=0;
  25. },
  26. warning: false
  27. }
  28. }
  29. },
  30. onLoad(e) {
  31. if(e.id) {
  32. let id = parseInt(e.id)
  33. let params = {
  34. registerId: id,
  35. limit: 1,
  36. page: 1
  37. }
  38. getAllTask(params).then(res=> {
  39. if(res.code === 0) {
  40. let dataSource = res.data.records[0]
  41. let carNumberList = [dataSource.carUrl]
  42. let list = dataSource.registerFlowmanVoList || []
  43. let arr = []
  44. if(list.length > 0) {
  45. list.forEach(item=> {
  46. if(item.imageUrl) {
  47. arr.push(item.imageUrl)
  48. }
  49. })
  50. }
  51. let healthCodeList = arr
  52. this.setData({dataSource, type: 'detail', carNumberList, healthCodeList})
  53. }
  54. })
  55. }
  56. if(this.data.type === 'upload') {
  57. this.getOssAuthForm()
  58. }
  59. },
  60. // 图片预览
  61. previewImage(e) {
  62. let type = e.currentTarget.dataset.type
  63. let item = e.currentTarget.dataset.item
  64. let str = type + 'List'
  65. wx.previewImage({
  66. urls: this.data[str],
  67. current: item
  68. })
  69. },
  70. validate(name) {
  71. let formRules = this.data.formRules;
  72. let validator = formRules[name].validator
  73. let result
  74. result = validator ? !validator(this.data[name]) : false
  75. formRules[name].warning = result
  76. this.setData({formRules})
  77. return result
  78. },
  79. // 表单验证
  80. validateForm() {
  81. return new Promise((resolve, reject) => {
  82. try {
  83. let formRules = this.data.formRules;
  84. let result = false;
  85. for (let key in formRules) {
  86. let temp = this.validate(key)
  87. if (temp) {
  88. result = temp
  89. }
  90. }
  91. resolve(!result)
  92. } catch (e) {
  93. reject(e)
  94. }
  95. })
  96. },
  97. /* 获取图片上传鉴权 */
  98. getOssAuthForm() {
  99. let that = this
  100. getOssAuth().then((res)=> {
  101. let client = {
  102. region: 'oss-cn-shanghai',
  103. secure: true,
  104. accessKeyId: res.accessKeyId,
  105. accessKeySecret: res.accessKeySecret,
  106. securityToken: res.securityToken,
  107. bucket: 'ta-tech-image'
  108. }
  109. // 计算签名
  110. function computeSignature(accessKeySecret, canonicalString) {
  111. return crypto.enc.Base64.stringify(crypto.HmacSHA1(canonicalString, accessKeySecret));
  112. }
  113. const date = new Date();
  114. date.setHours(date.getHours() + 1);
  115. const policyText = {
  116. expiration: date.toISOString(), // 设置policy过期时间。
  117. conditions: [
  118. // 限制上传大小。
  119. ["content-length-range", 0, 1024 * 1024 * 1024],
  120. ],
  121. };
  122. // 获取签名
  123. const policy = Base64.encode(JSON.stringify(policyText)) // policy必须为base64的string。
  124. const signature = computeSignature(client.accessKeySecret, policy)
  125. let ossForm = that.data.ossForm
  126. ossForm = {
  127. OSSAccessKeyId: client.accessKeyId,
  128. signature,
  129. policy,
  130. SecurityToken: client.securityToken
  131. }
  132. that.setData({ossForm})
  133. }).catch(e=> {
  134. console.log(e);
  135. })
  136. },
  137. /* 上传图片 */
  138. uploadImages(e){
  139. let type = e.currentTarget.dataset.type
  140. let limit = e.currentTarget.dataset.limit
  141. // wx.chooseImage({
  142. wx.chooseMedia({
  143. count: limit - this.data[type + 'List'].length, // 最多可以选择的图片张数,默认9
  144. mediaType: ['image'], // 图片
  145. sizeType: ['compressed'], // original 原图,compressed 压缩图,默认二者都有
  146. sourceType: ['camera'], // album 从相册选图,camera 使用相机,默认二者都有
  147. success:(res) =>{
  148. // success
  149. let str = type+'List'
  150. // let imageList = this.data[str].concat(res.tempFilePaths);
  151. // console.log(imageList)
  152. let imageList = this.data[str].concat(res.tempFiles[0].tempFilePath);
  153. if(type === 'carNumber') {
  154. this.setData({carNumberList: imageList})
  155. } else {
  156. this.setData({healthCodeList: imageList})
  157. }
  158. this.validate(type+'List')
  159. },
  160. fail: (e) => {
  161. console.log('取消选择');
  162. }
  163. })
  164. },
  165. // 删除图片
  166. deleteImage(e) {
  167. let type = e.currentTarget.dataset.type
  168. let index = this.getCurrentData(e);
  169. let list = this.data[type+"List"]
  170. list.splice(index, 1)
  171. let str = type + 'List'
  172. if(type === 'carNumber') {
  173. this.setData({carNumberList: list})
  174. } else {
  175. this.setData({healthCodeList: list})
  176. }
  177. this.validate(str)
  178. },
  179. getCurrentData(e) {
  180. return e.currentTarget.dataset.current;
  181. },
  182. /* 取消 */
  183. returnLastPage() {
  184. this.setData({
  185. form: {}
  186. })
  187. wx.navigateBack({
  188. delta: 1,
  189. })
  190. },
  191. /* 表单上传 */
  192. submit(){
  193. this.validateForm().then(res => {
  194. if(res) {
  195. let carNumberList = this.data.carNumberList
  196. let healthCodeList = this.data.healthCodeList
  197. let imageList = carNumberList.concat(healthCodeList)
  198. let ossForm = this.data.ossForm
  199. let temp = []
  200. if (imageList.length > 0) {
  201. wx.showLoading({title:"上传中",mask:true})
  202. temp = imageList.map(item => {
  203. // 设置文件上传路径
  204. const randomString = Math.random().toString(36).slice(2)
  205. const timestamp = new Date().getTime()
  206. const key = `imagedir/${randomString}_${timestamp}.png`
  207. // 上传图片
  208. return new Promise((resolve, reject)=> {
  209. wx.uploadFile({
  210. url: 'https://ta-tech-image.oss-cn-shanghai.aliyuncs.com',
  211. filePath: item,
  212. name: 'file',
  213. formData: {
  214. key,
  215. OSSAccessKeyId: ossForm.OSSAccessKeyId,
  216. signature: ossForm.signature,
  217. policy: ossForm.policy,
  218. 'x-oss-security-token': ossForm.SecurityToken
  219. },
  220. success: (res)=> {
  221. if(res.statusCode === 204) {
  222. // 上传成功,将图片路径resolve出去
  223. resolve(key)
  224. }
  225. },
  226. fail: (e)=> {
  227. wx.showToast({
  228. title: '图片上传失败',
  229. })
  230. }
  231. })
  232. })
  233. })
  234. Promise.all(temp).then((res)=> {
  235. let form = this.data.form;
  236. form.carUrl = res[0]
  237. res.shift()
  238. form.flowManUrlList = res
  239. form.registerType = 0
  240. // 登记
  241. confirmOrder(form).then(res => {
  242. if (res?.code === 0) {
  243. wx.reLaunch({
  244. url: '/pages/task/task',
  245. })
  246. }
  247. }).catch(e=> {
  248. console.log(e);
  249. }).finally(()=>{
  250. wx.hideLoading();
  251. })
  252. }).catch((e)=> {
  253. wx.hideLoading()
  254. })
  255. }
  256. }
  257. })
  258. }
  259. })