utils.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. uploadArr.push(this.uploadCloud({
  52. filePath,
  53. cloudPath: this.uuid() + '.' + fileArr[fileArr.length - 1],
  54. fileType: (methods === 'chooseImage' ? 'image/' : 'video/') + fileArr[fileArr.length - 1]
  55. }).then(res => {
  56. if (res.success) {
  57. return {
  58. filePath: res.fileID
  59. }
  60. } else {
  61. uni.showToast({
  62. title: res.msg,
  63. icon: 'none'
  64. })
  65. }
  66. }))
  67. } else{
  68. uploadArr.push(this.upload({
  69. ...uploadParam,
  70. filePath
  71. }).then(res => {
  72. if (res.code === 200 || res.success) {
  73. return res.data
  74. } else {
  75. uni.showToast({
  76. title: res.msg,
  77. icon: 'none'
  78. })
  79. }
  80. }))
  81. }
  82. })
  83. uni.showLoading({
  84. title: '上传中...'
  85. })
  86. return Promise.all(uploadArr).then(res => {
  87. uni.hideLoading()
  88. return res.filter(item => item)
  89. })
  90. })
  91. },
  92. chooseImage(data) {
  93. return uni.chooseImage({
  94. count: 1,
  95. ...data
  96. }).then(res => {
  97. console.log(res);
  98. return {
  99. filePaths: res.tempFilePaths,
  100. tempFiles: res.tempFiles
  101. }
  102. })
  103. },
  104. chooseAndUploadImage(chooseParam, uploadParam, isCloud) {
  105. return this.chooseAndUpload('chooseImage',chooseParam, uploadParam, isCloud)
  106. },
  107. chooseVideo(data) {
  108. return uni.chooseVideo({
  109. ...data
  110. }).then(res => {
  111. return {
  112. filePaths: [res.tempFilePath],
  113. tempFiles: [res.tempFile]
  114. }
  115. })
  116. },
  117. chooseAndUploadVideo(chooseParam, uploadParam, isCloud) {
  118. return this.chooseAndUpload('chooseVideo',chooseParam, uploadParam, isCloud)
  119. },
  120. authLogin(cb) {
  121. if (!uni.getStorageSync('token')) {
  122. return uni.navigateTo({
  123. url: '/modules/common/login/login'
  124. })
  125. }
  126. cb && cb()
  127. }
  128. }