123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- /**
- * 上传公共方法
- */
- import uniAsync from "@/utils/uni-async.js"; // uni api async 化
- import {baseApiUrl} from './config.js'
- import store from '@/store/index.js'
- /**
- * 上传文件到服务器
- */
- const uploadToServer = async (tempFilePath) => {
- const temres = await uniAsync.uploadFile({
- url: baseApiUrl + "/public/upload",
- filePath: tempFilePath,
- name: "file",
- header: {
- Authorization: store.state.user.token,
- },
- });
- const res = JSON.parse(temres.data);
- if (res.code === 200) {
- return res.data;
- }
- };
- /**
- * 上传图片
- * 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();
- });
- });
- };
|