123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- /**
- * 上传文件公共方法
- */
- import uniAsync from '@/utils/uni-async.js'// uni api async 化
- import {baseUrl} from './config.js'
- import store from '@/store/index.js'
- /**
- * 上传图片
- * count 同时上传张数
- */
- export const uploadImg=async (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()
- })
- })
- }
- /**
- * 上传文件
- * count 同时上传文件数
- * type 能选择文件类型
- */
- export const uploadFiles=async ({count=1,type='all'})=>{
- const tempFilePaths=await asyncChooseFiles({count,type})
- uni.showLoading({
- title:'上传中...'
- })
- const uploadResArr=tempFilePaths.map(item=>{
- return uploadToServer(item.path)
- })
- 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()
- })
- })
- }
- /**
- * 选择文件
- */
- const asyncChooseFiles=({count,type})=>{
- return new Promise((resolve,reject)=>{
- wx.chooseMessageFile({
- count:count,
- type:type,
- success:(res)=>{
- resolve(res.tempFiles)
- },
- fail:(error)=>{
- console.log(error.errMsg);
- if(error.errMsg.search('not supported')!=-1){
- uni.showToast({
- title:'请通过手机上传附件',
- icon:'none'
- })
- }
- }
- })
- })
- }
- /**
- * 上传文件到服务器
- */
- const uploadToServer=async (path)=>{
- const temres=await uniAsync.uploadFile({
- url:baseUrl+'/resource/upload',
- filePath:path,
- name:'file',
- header:{
- Authorization:store.state.token
- }
- })
- const res=JSON.parse(temres.data)
- if(res.code===200){
- return res.data.ResourceUrl
- }
- }
|