123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- /**
- * 上传公共方法
- */
- import uniAsync from "@/utils/uni-async.js"; // uni api async 化
- import {baseApiUrl} from './config.js'
- import store from '@/store/index.js'
- import CryptoJS from './crypto.js'
- /**
- * 上传文件到服务器
- */
- export const uploadToServer = async (tempFilePath) => {
- const { envVersion } = uni.getAccountInfoSync().miniProgram
- const temres = await uniAsync.uploadFile({
- url: baseApiUrl + "/public/upload",
- filePath: tempFilePath,
- name: "file",
- header: {
- Authorization: store.state.user.token,
- },
- });
- const res = envVersion === 'release' ? JSON.parse(CryptoJS.Des3Decrypt(temres.data)) : JSON.parse(temres.data);
- if (res.code === 200) {
- return res.data;
- }
- };
- /**
- *
- * 上传回复音频到服务器
- */
- export const uploadAudioToServer = async(tempFilePath)=>{
- const { envVersion } = uni.getAccountInfoSync().miniProgram
- const temres = await uniAsync.uploadFile({
- url: baseApiUrl + "/community/question/reply/upload_audio",
- filePath: tempFilePath,
- name: "file",
- header: {
- Authorization: store.state.user.token,
- },
- });
- const res = envVersion === 'release' ? JSON.parse(CryptoJS.Des3Decrypt(temres.data)) : JSON.parse(temres.data);
- return res;
-
- }
- /**
- * 上传图片
- * count 同时上传张数 默认:1
- */
- export const uploadImg = async ({count = 1}) => {
- console.log(count);
- const { tempFilePaths } = await uniAsync.chooseImage({ count });
- uni.showLoading({
- title: "上传中...",
- });
- const uploadResArr = tempFilePaths.map((item) => {
- return uploadToServer(item);
- });
- return new Promise((resolve, reject) => {
- Promise.all(uploadResArr)
- .then((res) => {
- uni.hideLoading();
- const arr = res.map((item) => {
- return item;
- });
- resolve(arr);
- })
- .catch((res) => {
- uni.hideLoading();
- reject(res)
- });
- });
- };
- /**
- * 公共上传音频方法
- */
- export const commonUploadAudio=(tempFilePath)=>{
- const { envVersion } = uni.getAccountInfoSync().miniProgram
- wx.showLoading({
- title: '上传音频中...',
- mask: true,
- success: (result) => {},
- fail: () => {},
- complete: () => {}
- });
- return new Promise((resolve,reject)=>{
- uni.uploadFile({
- url: baseApiUrl + "/public/upload_audio",
- filePath: tempFilePath,
- name: 'file',
- header: {
- Authorization: store.state.user.token,
- },
- success: (result) =>{
- const res = envVersion === 'release' ? JSON.parse(CryptoJS.Des3Decrypt(result.data)) : JSON.parse(result.data);
- resolve(res)
- },
- fail: () => {
- console.log('上传音频失败');
- reject('上传音频失败')
- uni.showToast({
- title:'上传音频失败,请稍后重试!',
- icon:'none'
- })
- },
- complete: () => {
- wx.hideLoading();
- }
- })
- })
- }
|