utils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import config from './config'
  2. export default {
  3. isHttp(path) {
  4. return path.startsWith('http') || path.startsWith('https')
  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. return uni.uploadFile({
  42. url: this.isHttp(data.url) ? data.url : config.apiBaseUrl + data.url,
  43. name: data.name || 'file',
  44. formData: {
  45. ...data.formData,
  46. token: uni.getStorageSync('token')
  47. },
  48. header: data.header || {},
  49. filePath: data.filePath
  50. }).then(res => {
  51. return JSON.parse(res)
  52. })
  53. },
  54. uploadCloud(data) {
  55. return uniCloud.uploadFile(data).then(res => {
  56. return res
  57. })
  58. },
  59. chooseAndUpload(methods, chooseParam, uploadParam, isCloud) {
  60. const uploadArr = []
  61. return this[methods](chooseParam).then(file => {
  62. file.filePaths.forEach((filePath, index) => {
  63. if (isCloud) {
  64. const fileArr = filePath.split('.')
  65. // 只在h5可用
  66. const fileType = file.tempFiles[index].type || 'image/png'
  67. const suffix = fileArr.length >= 2 ? fileArr[fileArr.length - 1] : fileType.split(
  68. '/')[1]
  69. uploadArr.push(this.uploadCloud({
  70. filePath,
  71. cloudPath: this.uuid() + '.' + suffix,
  72. fileType: (methods === 'chooseImage' ? 'image/' : 'video/') + suffix
  73. }).then(res => {
  74. if (res.success) {
  75. return {
  76. filePath: res.fileID
  77. }
  78. } else {
  79. uni.showToast({
  80. title: res.msg,
  81. icon: 'none'
  82. })
  83. }
  84. }))
  85. } else {
  86. uploadArr.push(this.upload({
  87. ...uploadParam,
  88. filePath
  89. }).then(res => {
  90. if (res.code === 200 || res.success) {
  91. return res.data
  92. } else {
  93. uni.showToast({
  94. title: res.msg,
  95. icon: 'none'
  96. })
  97. }
  98. }))
  99. }
  100. })
  101. uni.showLoading({
  102. title: '上传中...'
  103. })
  104. return Promise.all(uploadArr).then(res => {
  105. uni.hideLoading()
  106. return res.filter(item => item)
  107. })
  108. })
  109. },
  110. chooseImage(data) {
  111. return uni.chooseImage({
  112. count: 1,
  113. ...data
  114. }).then(res => {
  115. return {
  116. filePaths: res.tempFilePaths,
  117. tempFiles: res.tempFiles
  118. }
  119. })
  120. },
  121. chooseAndUploadImage(chooseParam, uploadParam, isCloud) {
  122. return this.chooseAndUpload('chooseImage', chooseParam, uploadParam, isCloud)
  123. },
  124. chooseVideo(data) {
  125. return uni.chooseVideo({
  126. ...data
  127. }).then(res => {
  128. return {
  129. filePaths: [res.tempFilePath],
  130. tempFiles: [res.tempFile]
  131. }
  132. })
  133. },
  134. chooseAndUploadVideo(chooseParam, uploadParam, isCloud) {
  135. return this.chooseAndUpload('chooseVideo', chooseParam, uploadParam, isCloud)
  136. },
  137. }