| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- 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 })
- }
- }
- })
- })
- }
- }
|