import BaseTool from '@/utils/tool' import Msg from '@/utils/message' export function uploadImageList(tempFilePaths) { const promiseList = []; tempFilePaths.forEach((item, index, arr) => { const promise = new Promise((resolve, reject) => { uni.uploadFile({ url: BaseTool.UserUtil.getUploadUrl(), // 仅为示例,非真实的接口地址 filePath: item, name: 'file', header: { Authorization: BaseTool.UserUtil.getToken(BaseTool.UserUtil.getUploadUrl()) }, success: uploadFileRes => { if (uploadFileRes.statusCode === 200) { resolve(JSON.parse(uploadFileRes.data).data); } else if (uploadFileRes.statusCode === 401) { uni.showModal({ content: '登录已过期,请重新登录', showCancel: false, success: result => { uni.reLaunch({ url: '/pages/auth/login/index' }); } }); return reject(null); } else { resolve(null); } }, fail: () => { resolve(null); } }); }); promiseList.push(promise); }); return Promise.all(promiseList); } export function uploadWxFileList(tempFileList) { const promiseList = []; tempFileList.forEach((item, index, arr) => { const promise = new Promise((resolve, reject) => { uni.uploadFile({ url: BaseTool.UserUtil.getUploadUrl() + '/name?fileName=' + item.name, // 仅为示例,非真实的接口地址 filePath: item.path, name: 'file', header: { Authorization: BaseTool.UserUtil.getToken(BaseTool.UserUtil.getUploadUrl()) }, success: uploadFileRes => { if (uploadFileRes.statusCode === 200) { resolve(JSON.parse(uploadFileRes.data).data); } else if (uploadFileRes.statusCode === 401) { uni.showModal({ content: '登录已过期,请重新登录', showCancel: false, success: result => { uni.reLaunch({ url: '/pages/login/login' }); } }); return reject(null); } else { resolve(null); } }, fail: () => { resolve(null); } }); }); promiseList.push(promise); }); return Promise.all(promiseList); } export function uploadImageOpen (params = { type: 1, count: 1 }) { let isWx = false // #ifdef MP-WEIXIN isWx = true // #endif if (params.type === 1 || !isWx) { return new Promise((resolve, reject) => { uni.chooseImage({ count: params.count, success: function(chooseImageRes) { // console.log(JSON.stringify(res.tempFilePaths)); // uni.showToast({ icon: "none", title: '密码与确认密码不一致111' }) const tempFilePaths = chooseImageRes.tempFilePaths const choseFileList = chooseImageRes.tempFiles // 校验文件 for (const choseFile of choseFileList) { const item = choseFile.name if (BaseTool.Object.isNotBlank(item) && item.length > 3) { const itemSub = item.substr(item.length - 3) if (itemSub !== 'PNG' && itemSub !== 'png' && itemSub !== 'peg' && itemSub !== 'PEG' && itemSub !== 'jpg' && itemSub !== 'JPG' && itemSub !== 'GIF' && itemSub !== 'gif' && itemSub !== 'bmp' && itemSub !== 'BMP') { reject({ code: 501, message: '请上传图片' }) return } } } Msg.loadingMustClose('正在上传') try { uploadImageList(tempFilePaths).then(fileResult => { const imageListResult = [] if (fileResult && fileResult.length > 0) { fileResult.forEach(item => { if (item != null) { imageListResult.push(item) } }) } resolve(imageListResult) }, (err) => { reject(err) }).catch(err => { reject(err) }) .finally(function() { Msg.hideLoading() }) } catch (e) { Msg.hideLoading() reject({ code: 5502, message: '发生异常', data: e }) } } }); }) } else if (params.type === 2 && isWx) { return new Promise((resolve, reject) => { wx.chooseMessageFile({ count: params.count, type: 'image', success (res) { // tempFilePath可以作为img标签的src属性显示图片 const tempFilePaths = res.tempFiles for (const tempFilePath of tempFilePaths) { const fileName = tempFilePath.name if (BaseTool.Object.isNotBlank(fileName) && fileName.length > 0) { const itemSub = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase() if (itemSub !== 'PNG' && itemSub !== 'png' && itemSub !== 'peg' && itemSub !== 'PEG' && itemSub !== 'jpg' && itemSub !== 'JPG' && itemSub !== 'GIF' && itemSub !== 'gif' && itemSub !== 'bmp' && itemSub !== 'BMP') { // eslint-disable-next-line prefer-promise-reject-errors reject({ code: 501, message: '请上传图片' }) return } } } try { uploadWxFileList(tempFilePaths).then(fileResult => { const imageListResult = [] if (fileResult && fileResult.length > 0) { fileResult.forEach(item => { if (item != null) { imageListResult.push(item) } }) } resolve(imageListResult) }, (err) => { reject(err) }).catch(err => { reject(err) }) .finally(function() { Msg.hideLoading() }) } catch (e) { Msg.hideLoading() // eslint-disable-next-line prefer-promise-reject-errors reject({ code: 5502, message: '发生异常', data: e }) } } }) }) } }