12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- /**
- * 上传文件公共方法
- */
- 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)
- }
- })
- })
- }
- /**
- * 上传文件到服务器
- */
- 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
- }
- }
|