utils.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import config from './config'
  2. export default {
  3. isHttp(path) {
  4. return path.startsWith('http')
  5. },
  6. escapeHTML(a) {
  7. a = '' + a;
  8. return a.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(
  9. /'/g, "&apos;")
  10. },
  11. /**
  12. * @function unescapeHTML 还原html脚本 < > & " '
  13. * @param a - 字符串
  14. */
  15. unescapeHTML(a) {
  16. a = '' + a;
  17. return a.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&").replace(/&quot;/g, '"').replace(
  18. /&apos;/g, "'")
  19. },
  20. uuid(len = 16) {
  21. const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  22. const uuid = []
  23. if (len) {
  24. // 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
  25. for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * chars.length]
  26. } else {
  27. let r
  28. // rfc4122标准要求返回的uuid中,某些位为固定的字符
  29. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
  30. uuid[14] = '4'
  31. for (let i = 0; i < 36; i++) {
  32. if (!uuid[i]) {
  33. r = 0 | Math.random() * 16
  34. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r]
  35. }
  36. }
  37. }
  38. return uuid.join('')
  39. },
  40. upload(data) {
  41. if (data.editable) {
  42. return new Promise(resolve => {
  43. wx.editImage({
  44. src: data.filePath,
  45. success(res) {
  46. this.uploadFile({
  47. ...data,
  48. filePath: res.tempFilePath
  49. }).then(res => {
  50. resolve(res)
  51. })
  52. }
  53. })
  54. })
  55. } else {
  56. return this.uploadFile(data)
  57. }
  58. },
  59. uploadFile(data) {
  60. return uni.uploadFile({
  61. url: this.isHttp(data.url) ? data.url : config.apiBaseUrl + data.url,
  62. name: data.name || 'file',
  63. formData: {
  64. ...data.formData,
  65. token: uni.getStorageSync('token')
  66. },
  67. header: data.header || {},
  68. filePath: data.filePath
  69. }).then(res => {
  70. return JSON.parse(res)
  71. })
  72. },
  73. uploadCloud(data) {
  74. return uniCloud.uploadFile(data).then(res => {
  75. return res
  76. })
  77. },
  78. chooseAndUpload(methods, chooseParam, uploadParam, isCloud) {
  79. const uploadArr = []
  80. return this[methods](chooseParam).then(file => {
  81. file.filePaths.forEach((filePath, index) => {
  82. if (isCloud) {
  83. const fileArr = filePath.split('.')
  84. // 只在h5可用
  85. const fileType = file.tempFiles[index].type || 'image/png'
  86. const suffix = fileArr.length >= 2 ? fileArr[fileArr.length - 1] : fileType.split(
  87. '/')[1]
  88. uploadArr.push(this.uploadCloud({
  89. filePath,
  90. cloudPath: this.uuid() + '.' + suffix,
  91. fileType: (methods === 'chooseImage' ? 'image/' : 'video/') + suffix
  92. }).then(res => {
  93. if (res.success) {
  94. return {
  95. filePath: res.fileID
  96. }
  97. } else {
  98. uni.showToast({
  99. title: res.msg,
  100. icon: 'none'
  101. })
  102. }
  103. }))
  104. } else {
  105. uploadArr.push(this.upload({
  106. ...uploadParam,
  107. filePath
  108. }).then(res => {
  109. if (res.code === 200 || res.success) {
  110. return res.data
  111. } else {
  112. uni.showToast({
  113. title: res.msg,
  114. icon: 'none'
  115. })
  116. }
  117. }))
  118. }
  119. })
  120. uni.showLoading({
  121. title: '上传中...'
  122. })
  123. return Promise.all(uploadArr).then(res => {
  124. uni.hideLoading()
  125. return res.filter(item => item)
  126. })
  127. })
  128. },
  129. chooseImage(data) {
  130. return uni.chooseImage({
  131. count: 1,
  132. ...data
  133. }).then(res => {
  134. return {
  135. filePaths: res.tempFilePaths,
  136. tempFiles: res.tempFiles
  137. }
  138. })
  139. },
  140. chooseAndUploadImage(chooseParam, uploadParam, isCloud) {
  141. return this.chooseAndUpload('chooseImage', chooseParam, uploadParam, isCloud)
  142. },
  143. chooseVideo(data) {
  144. return uni.chooseVideo({
  145. ...data
  146. }).then(res => {
  147. return {
  148. filePaths: [res.tempFilePath],
  149. tempFiles: [res.tempFile]
  150. }
  151. })
  152. },
  153. chooseAndUploadVideo(chooseParam, uploadParam, isCloud) {
  154. return this.chooseAndUpload('chooseVideo', chooseParam, uploadParam, isCloud)
  155. },
  156. }