12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- // 配置项
- const token = wx.getStorageSync('token')
- const config = {
- baseURL: 'http://192.168.1.6:8801', // 基础URL
- timeout: 30000, // 超时时间(单位ms)
- header: {
- 'content-type': 'application/json', // 默认请求头
- "token":token
- // 其他全局请求头...
- },
- // 其他全局配置...
- };
- // 封装请求方法
- function upload(url="/fileManager/uploadNormalFile", path = '', header = {},formData={}) {
- return new Promise((resolve, reject) => {
- wx.uploadFile({
- url: config.baseURL + url, //仅为示例,非真实的接口地址
- filePath: path,
- name: 'file',
- formData: formData,
- header: {
- ...config.header,
- ...header,
- },
- timeout: config.timeout,
- success: function(res){
- // 根据业务逻辑处理成功响应
- if (res.data && res.data.code === 200) { // 假设服务器返回code为200表示成功
- resolve(res.data);
- } else {
- // wx.showToast({
- // title: '请求失败',
- // icon: 'error',
- // duration: 2000
- // })
- Notify({ type: 'danger', message: '上传失败' });
- reject(res.data.message || '未知错误');
- }
- },
- fail: function(error) {
- console.log(error)
-
- // 处理请求失败
- // wx.showToast({
- // title: '请求失败',
- // icon: 'error',
- // duration: 2000
- // })
- Notify({ type: 'danger', message: '上传失败' });
- reject(error);
- },
- });
- });
- }
-
- // 导出请求方法
- module.exports = {
- upload: ( path,formData,url, header) => upload(url, path, header,formData),
- };
|