123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import {BASE_URL} from './request';
- /**
- * 上传图片
- * @param {string} filePath 本地文件路径
- * @param {string} url 上传接口路径(默认 /api/upload/image)
- * @param {string} name 后端接收文件字段名(默认 file)
- */
- export function uploadImage(filePath, url = '/api/upload/image', name = 'file') {
- return new Promise((resolve, reject) => {
- // wx.showLoading({
- // title: '上传中...',
- // mask: true
- // });
- wx.uploadFile({
- url: BASE_URL + url,
- filePath,
- name,
- header: {
- 'Authorization': wx.getStorageSync('token') || ''
- },
- success(res) {
- // wx.hideLoading();
- try {
- const data = JSON.parse(res.data);
- console.log(data)
- if (data.code === 200 && data.ret) {
- wx.showToast({
- title: '上传成功',
- icon: 'none'
- });
- resolve(data.data);
- } else {
- wx.showToast({
- title: data.message || '上传失败',
- icon: 'none'
- });
- reject(data);
- }
- } catch (err) {
- reject(err);
- }
- },
- fail(err) {
- // wx.hideLoading();
- wx.showToast({
- title: '网络异常,请重试',
- icon: 'none'
- });
- reject(err);
- }
- });
- });
- }
|