upload.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * 上传公共方法
  3. */
  4. import uniAsync from "@/utils/uni-async.js"; // uni api async 化
  5. import {baseApiUrl} from './config.js'
  6. import store from '@/store/index.js'
  7. import CryptoJS from './crypto.js'
  8. /**
  9. * 上传文件到服务器
  10. */
  11. export const uploadToServer = async (tempFilePath) => {
  12. const { envVersion } = uni.getAccountInfoSync().miniProgram
  13. const temres = await uniAsync.uploadFile({
  14. url: baseApiUrl + "/public/upload",
  15. filePath: tempFilePath,
  16. name: "file",
  17. header: {
  18. Authorization: store.state.user.token,
  19. },
  20. });
  21. const res = envVersion === 'release' ? JSON.parse(CryptoJS.Des3Decrypt(temres.data)) : JSON.parse(temres.data);
  22. if (res.code === 200) {
  23. return res.data;
  24. }
  25. };
  26. /**
  27. *
  28. * 上传回复音频到服务器
  29. */
  30. export const uploadAudioToServer = async(tempFilePath)=>{
  31. const { envVersion } = uni.getAccountInfoSync().miniProgram
  32. const temres = await uniAsync.uploadFile({
  33. url: baseApiUrl + "/community/question/reply/upload_audio",
  34. filePath: tempFilePath,
  35. name: "file",
  36. header: {
  37. Authorization: store.state.user.token,
  38. },
  39. });
  40. const res = envVersion === 'release' ? JSON.parse(CryptoJS.Des3Decrypt(temres.data)) : JSON.parse(temres.data);
  41. return res;
  42. }
  43. /**
  44. * 上传图片
  45. * count 同时上传张数 默认:1
  46. */
  47. export const uploadImg = async ({count = 1}) => {
  48. console.log(count);
  49. const { tempFilePaths } = await uniAsync.chooseImage({ count });
  50. uni.showLoading({
  51. title: "上传中...",
  52. });
  53. const uploadResArr = tempFilePaths.map((item) => {
  54. return uploadToServer(item);
  55. });
  56. return new Promise((resolve, reject) => {
  57. Promise.all(uploadResArr)
  58. .then((res) => {
  59. uni.hideLoading();
  60. const arr = res.map((item) => {
  61. return item;
  62. });
  63. resolve(arr);
  64. })
  65. .catch((res) => {
  66. uni.hideLoading();
  67. reject(res)
  68. });
  69. });
  70. };
  71. /**
  72. * 公共上传音频方法
  73. */
  74. export const commonUploadAudio=(tempFilePath)=>{
  75. const { envVersion } = uni.getAccountInfoSync().miniProgram
  76. wx.showLoading({
  77. title: '上传音频中...',
  78. mask: true,
  79. success: (result) => {},
  80. fail: () => {},
  81. complete: () => {}
  82. });
  83. return new Promise((resolve,reject)=>{
  84. uni.uploadFile({
  85. url: baseApiUrl + "/public/upload_audio",
  86. filePath: tempFilePath,
  87. name: 'file',
  88. header: {
  89. Authorization: store.state.user.token,
  90. },
  91. success: (result) =>{
  92. const res = envVersion === 'release' ? JSON.parse(CryptoJS.Des3Decrypt(result.data)) : JSON.parse(result.data);
  93. resolve(res)
  94. },
  95. fail: () => {
  96. console.log('上传音频失败');
  97. reject('上传音频失败')
  98. uni.showToast({
  99. title:'上传音频失败,请稍后重试!',
  100. icon:'none'
  101. })
  102. },
  103. complete: () => {
  104. wx.hideLoading();
  105. }
  106. })
  107. })
  108. }