123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- // 配置项
- import Notify from '../miniprogram_npm/@vant/weapp/notify/notify';
- const token = wx.getStorageSync('token')
- const config = {
- // baseURL: 'http://192.168.1.6:8802', // 基础URL
- baseURL: 'https://www.xsip.cn', // 基础URL
- timeout: 30000, // 超时时间(单位ms)
- header: {
- 'content-type': 'application/json', // 默认请求头
- // 其他全局请求头...
-
- },
- // 其他全局配置...
- tabbar:["pages/index/index","pages/hotProduct/hotProduct","pages/mine/mine"]
- };
- const apiUrl = {
- develop: 'https://www.xsip.cn', // 开发环境地址
- trial: 'https://www.xsip.cn', // 测试环境地址
- release: 'https://www.xsip.cn' // 生产环境地址
- };
- const accountInfo = wx.getAccountInfoSync();
- const envVersion = accountInfo.miniProgram.envVersion;
- config.baseURL = apiUrl[envVersion];
- function getPages(){
- var obj = {
- type: 'danger'
- }
- var pages = getCurrentPages()
- var route = pages[pages.length - 1]?.route
- console.log(config.tabbar,route)
- if(route && config.tabbar.indexOf(route)==-1){
- obj.top = wx.getStorageSync('height')
- }
- return obj
- }
- // 封装请求方法
- function upload(url="/fileManager/uploadNormalFile", path = '', header = {},formData={}) {
- return new Promise((resolve, reject) => {
- wx.uploadFile({
- url: config.baseURL + url, //仅为示例,非真实的接口地址
- filePath: path,
- name: 'files',
- formData: {
- sourceId:5,
- ...formData
- },
- header: {
- ...config.header,
- ...header,
- "Authorization": wx.getStorageSync('token')
- },
- timeout: config.timeout,
- success: function(res){
- wx.hideLoading()
- var obj = getPages()
- if(res.data){
- res.data = JSON.parse(res.data)
- }
- // 根据业务逻辑处理成功响应
- if (res.data && res.data.code === 200) { // 假设服务器返回code为200表示成功
- resolve(res.data);
- } else {
- switch(res.data.code){
- case 401:
- if(isLogin){
- // Notify({ type: 'danger', message: '未登录',top:'90' });
- obj.message = '未登录'
- wx.navigateTo({
- url: '/pages/login/login',
- })
- }
- wx.setStorageSync('token', '')
- break;
- default:
- // obj.message = '请求失败'
- obj.message = res.data.message
- // Notify({ type: 'danger', message: '请求失败',top:'90' });
-
- }
- // wx.showToast({
- // title: '请求失败',
- // icon: 'error',
- // duration: 2000
- // })
- console.log(obj)
- if(obj.message){
- Notify(obj)
- }
-
- reject(res.data.message || '未知错误');
- }
- },
- fail: function(error) {
- console.log(error)
- wx.hideLoading()
- var obj = getPages()
- // 处理请求失败
- // wx.showToast({
- // title: '请求失败',
- // icon: 'error',
- // duration: 2000
- // })
- obj.message = '请求超时'
- // Notify({ type: 'danger', message: '请求失败',top:'90' });
- Notify(obj)
- reject(error);
- },
- });
- });
- }
-
- // 导出请求方法
- module.exports = {
- upload: ( path,formData,url, header) => upload(url, path, header,formData),
- };
|