utils.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import config from './config'
  2. import { ossPolicy } from '@/services/student-prosthetics'
  3. export default {
  4. isHttp(path) {
  5. return path.startsWith('http')
  6. },
  7. escapeHTML(a) {
  8. a = '' + a;
  9. return a.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(
  10. /'/g, "&apos;")
  11. },
  12. /**
  13. * @function unescapeHTML 还原html脚本 < > & " '
  14. * @param a - 字符串
  15. */
  16. unescapeHTML(a) {
  17. a = '' + a;
  18. return a.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&").replace(/&quot;/g, '"').replace(
  19. /&apos;/g, "'")
  20. },
  21. uuid(len = 16) {
  22. const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  23. const uuid = []
  24. if (len) {
  25. // 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
  26. for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * chars.length]
  27. } else {
  28. let r
  29. // rfc4122标准要求返回的uuid中,某些位为固定的字符
  30. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
  31. uuid[14] = '4'
  32. for (let i = 0; i < 36; i++) {
  33. if (!uuid[i]) {
  34. r = 0 | Math.random() * 16
  35. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r]
  36. }
  37. }
  38. }
  39. return uuid.join('')
  40. },
  41. upload(data) {
  42. if (data.editable) {
  43. return new Promise(resolve => {
  44. wx.editImage({
  45. src: data.filePath,
  46. success(res) {
  47. this.uploadFile({
  48. ...data,
  49. filePath: res.tempFilePath
  50. }).then(res => {
  51. resolve(res)
  52. })
  53. }
  54. })
  55. })
  56. } else {
  57. return this.uploadFile(data)
  58. }
  59. },
  60. uploadFile(data) {
  61. return uni.uploadFile({
  62. url: this.isHttp(data.url) ? data.url : config.apiBaseUrl + data.url,
  63. name: data.name || 'file',
  64. formData: {
  65. ...data.formData,
  66. token: uni.getStorageSync('token')
  67. },
  68. header: data.header || {},
  69. filePath: data.filePath
  70. }).then(res => {
  71. return JSON.parse(res)
  72. })
  73. },
  74. uploadCloud(data) {
  75. return uniCloud.uploadFile(data).then(res => {
  76. return res
  77. })
  78. },
  79. chooseAndUpload(methods, chooseParam, uploadParam, isCloud) {
  80. const uploadArr = []
  81. return this[methods](chooseParam).then(file => {
  82. file.filePaths.forEach((filePath, index) => {
  83. if (isCloud) {
  84. const fileArr = filePath.split('.')
  85. // 只在h5可用
  86. const fileType = file.tempFiles[index].type || 'image/png'
  87. const suffix = fileArr.length >= 2 ? fileArr[fileArr.length - 1] : fileType.split(
  88. '/')[1]
  89. uploadArr.push(this.uploadCloud({
  90. filePath,
  91. cloudPath: this.uuid() + '.' + suffix,
  92. fileType: (methods === 'chooseImage' ? 'image/' : 'video/') + suffix
  93. }).then(res => {
  94. if (res.success) {
  95. return {
  96. filePath: res.fileID
  97. }
  98. } else {
  99. uni.showToast({
  100. title: res.msg,
  101. icon: 'none'
  102. })
  103. }
  104. }))
  105. } else {
  106. uploadArr.push(this.upload({
  107. ...uploadParam,
  108. filePath
  109. }).then(res => {
  110. if (res.code === 200 || res.success) {
  111. return res.data
  112. } else {
  113. uni.showToast({
  114. title: res.msg,
  115. icon: 'none'
  116. })
  117. }
  118. }))
  119. }
  120. })
  121. uni.showLoading({
  122. title: '上传中...'
  123. })
  124. return Promise.all(uploadArr).then(res => {
  125. uni.hideLoading()
  126. return res.filter(item => item)
  127. })
  128. })
  129. },
  130. chooseImage(data) {
  131. return uni.chooseImage({
  132. count: 1,
  133. ...data
  134. }).then(res => {
  135. return res.tempFilePaths || []
  136. })
  137. },
  138. chooseAndUploadImage(chooseParam, uploadParam, isCloud) {
  139. return this.chooseAndUpload('chooseImage', chooseParam, uploadParam, isCloud)
  140. },
  141. chooseVideo(data) {
  142. return uni.chooseVideo({
  143. ...data
  144. }).then(res => {
  145. return {
  146. filePaths: [res.tempFilePath],
  147. tempFiles: [res.tempFile]
  148. }
  149. })
  150. },
  151. chooseAndUploadVideo(chooseParam, uploadParam, isCloud) {
  152. return this.chooseAndUpload('chooseVideo', chooseParam, uploadParam, isCloud)
  153. },
  154. transferOssUrl(files = []) {
  155. const uploadArr = []
  156. files.forEach(file => {
  157. uploadArr.push(ossPolicy(file).then(res => {
  158. if (res.success) {
  159. const ret = res || {}
  160. const filePath = file?.objectUrl || ''
  161. const fileName = file?.file?.name || ''
  162. const index = file.file.name.lastIndexOf(".");
  163. const tmpFileName = index < 0 ? '.jpg' : fileName.substr(index,
  164. fileName.length - index)
  165. const uuidNum = this.uuid(32, 16) + tmpFileName
  166. const ossKey = ret.dir + uuidNum
  167. uni.uploadFile({
  168. url: ret.host,
  169. filePath: filePath,
  170. fileType: 'image',
  171. name: 'file',
  172. formData: {
  173. 'OSSAccessKeyId': ret.accessKeyId,
  174. 'policy': ret.policy,
  175. 'key': ossKey,
  176. // 'x-oss-credential': ret['x-oss-credential'] || '',
  177. // 'x-oss-date': ret['x-oss-date'] || '',
  178. 'Signature': ret['signature'] || '',
  179. // 'x-oss-signature-version': ret['x-oss-signature-version'] || '',
  180. 'success_action_status': '200',
  181. },
  182. success: (res) => {
  183. uni.showToast({
  184. title: '上传成功',
  185. icon: 'none'
  186. })
  187. },
  188. fail: (err) => {
  189. throw new Error(err.errMsg);
  190. }
  191. })
  192. return {
  193. ...file,
  194. fileUrl: ossKey,
  195. fileName: fileName || uuidNum,
  196. fileType: tmpFileName.substr(1, tmpFileName.length - 1)
  197. }
  198. }
  199. }))
  200. })
  201. uni.showLoading({
  202. title: '上传中...',
  203. mask: true
  204. })
  205. return Promise.all(uploadArr).then(res => {
  206. uni.hideLoading()
  207. return res.filter(item => item)
  208. })
  209. }
  210. }