/** * 上传公共方法 */ 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) => { 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(); }); }); };