utils.js 3.1 KB

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