123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- // request.js
- import Notify from '../miniprogram_npm/@vant/weapp/notify/notify';
- // 配置项
- const config = {
- // baseURL: 'http://192.168.1.18:8901', // 基础URL
- baseURL: 'https://www.xsip.cn', // 基础URL
- timeout: 300000, // 超时时间(单位ms)
- header: {
- 'content-type': 'application/json', // 默认请求头
- // 其他全局请求头...
- },
- };
- const apiUrl = {
- develop: 'http://192.168.2.105:8901', // 开发环境地址
- trial: 'https://www.xsip.cn/test', // 测试环境地址
- release: 'https://www.xsip.cn' // 生产环境地址
- };
- const accountInfo = wx.getAccountInfoSync();
- const envVersion = accountInfo.miniProgram.envVersion;
- config.baseURL = apiUrl[envVersion];
- function getPages(){
- var obj = {
- type: 'danger'
- }
- if(wx.getStorageSync('isComponent')){
- obj.top = wx.getStorageSync('height')
- }
- return obj
- }
- // 封装请求方法
- function request(url, method = 'GET', data = {}, header = {},isLogin=true) {
- return new Promise((resolve, reject) => {
- var str = ''
- if(method === 'GET'){
- str ='?' + Object.keys(data).map(key => `${key}=${encodeURIComponent(data[key])}`).join('&')
- }
- wx.request({
- url: config.baseURL + url + str,
- method: method.toUpperCase(),
- data: method === 'GET' ? {} : data, // GET请求时将data置为空对象
- header: {
- ...config.header,
- ...header,
- "Authorization": wx.getStorageSync('token')
- // "Cookie": wx.getStorageSync('token')
- },
- timeout: config.timeout,
- success: function(res){
- var obj = getPages()
- wx.hideLoading()
- // 根据业务逻辑处理成功响应
- if (res.data && res.data.code === 200) { // 假设服务器返回code为200表示成功
- resolve(res.data);
- } else if(res.data && !res.data.code){
- resolve(res.data);
- }
- else {
- switch(res.data.code){
- case 901:
- resolve(res.data);
- break;
- case 401:
- if(isLogin){
- // Notify({ type: 'danger', message: '未登录',top:'90' });
- obj.message = '未登录'
- wx.navigateTo({
- url: '/pages/login/login',
- })
- }
- wx.setStorageSync('token', '')
- break;
- case 606://未开通会员或会员过期
- wx.showModal({
- title: '',
- content: '前往会员中心开通会员',
- complete: (res) => {
- if (res.cancel) {}
- if (res.confirm) {
- wx.navigateTo({
- url: '/pages/memberCenter/menberCenter2',
- })
- }
- }
- })
- break;
- // case 607://已开通会员且超过额度
-
- // 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 || '未知错误');
- }
- // if($stopWuxRefresher()){
- // $stopWuxRefresher()
- // }
- },
- 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);
- // if($stopWuxRefresher()){
- // $stopWuxRefresher()
- // }
- },
- });
- });
- }
-
- function upload(url="/fileManager/uploadNormalFile", path = '', header = {},formData={}) {
- return new Promise((resolve, reject) => {
- wx.uploadFile({
- url: config.baseURL + url, //仅为示例,非真实的接口地址
- filePath: path,
- name: 'multipartFile',
- formData: 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 || '未知错误');
- }
- // if($stopWuxRefresher()){
- // $stopWuxRefresher()
- // }
- },
- 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 = {
- get: (url, data,isLogin, header) => request(url, 'GET', data, header,isLogin),
- post: (url, data,isLogin, header) => request(url, 'POST', data, header,isLogin),
- // 可以继续添加其他方法,如put, delete等
- // ...
- upload: ( path,formData,url, header) => upload(url, path, header,formData),
- };
|