utils.js 3.5 KB

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