var config = require('../config/config.js') import { showToast } from '../utils/tips.js' //model状态 let model = true; let loading = true; //服务器地址 const apiUrl = config.apiUrl; /** * 处理请求数据 */ const getData = (data) => { // 预先取值 const { user_token, user_id, member_id } = { user_token: wx.getStorageSync('user_token'), user_id: wx.getStorageSync('user_id'), member_id: wx.getStorageSync('member_id'), } // 删除空值 let tmp_data = { user_token, user_id, member_id } for (let key in tmp_data) { if (!tmp_data[key] && tmp_data[key] !== 0) { delete tmp_data[key] } } // 再次过滤 const _data = Object.assign(data, tmp_data) for (let key in _data) { if (!_data[key] && _data[key] !== 0) { delete _data[key] } } return _data; } /** * 封装http 请求方法 */ const http = (params) => { //返回promise 对象 return new Promise((resolve, reject) => { if (loading) { loading = false; wx.showLoading({ title: 'loading', mask: true }); } const pages = getCurrentPages(); wx.request({ url: apiUrl + params.url, //服务器url+参数中携带的接口具体地址 data: getData(params.data), //请求参数 header: Object.assign({ "Content-Type": "application/json" //设置后端需要的常用的格式就好,特殊情况调用的时候单独设置 }, params.header || {}), method: params.method || 'GET', //默认为GET,可以不写,如常用请求格式为POST,可以设置POST为默认请求方式 dataType: params.dataType, //返回的数据格式,默认为JSON,特殊格式可以在调用的时候传入参数 responseType: params.responseType, //响应的数据类型 success: function (res) { wx.hideLoading({ success: () => { loading = true } }); if (res.statusCode == 200) { var errorCode = res.data.errcode if (errorCode == 0) { return resolve(res.data) } else if (errorCode == 1014) { wx.setStorageSync('openId', res.data.data.openId); wx.setStorageSync('sessionKey', res.data.data.sessionKey); wx.setStorageSync('unionId', res.data.data.unionId); //前往绑定手机号 wx.redirectTo({ url: '/pages/bind-phone/index', }) } else if (errorCode == 4000) { wx.showModal({ title: '提示', confirmText: '好的', content: res.data.errmsg, showCancel: false, success: () => { // wx.setClipboardData({ // data: '4009960099', // success: (res) => { // // wx.showToast({ // // icon: 'none', // // title: '客服电话已复制', // // }) // }, // }) } }) return } else if (errorCode == 1005) { //未获取到微信登录信息 wx.navigateTo({ url: '/pages/login/login', }) } else if (errorCode == 2009) { //用户信息重复,请选择用户信息 wx.setStorageSync('userList', res.data.data.userList); wx.setStorageSync('currentUser', res.data.data.currentUser); wx.redirectTo({ url: '/pages/select-info/index', }) } else if (errorCode == 2008) { //账号不存在,或token无效 if (model && params.data.task_id !== 10) { const _type = pages.length === 1 ? 'reLaunch' : 'navigateTo'; wx[_type]({ url: '/pages/login/login', success: res => { wx.showModal({ title: '提示', content: '登录失效请重新登录', showCancel: false, success: res => { if (res.confirm) { model = true; } } }) } }) // if (pages.length === 1) { // wx.reLaunch({ // url: '/pages/login/login', // success: res => { // wx.showModal({ // title: '提示', // content: '登录失效请重新登录', // showCancel: false, // success: res => { // if (res.confirm) { // model = true; // } // } // }) // } // }) // } else { // wx.navigateTo({ // url: '/pages/login/login', // success: res => { // wx.showModal({ // title: '提示', // content: '登录失效请重新登录', // showCancel: false, // success: res => { // if (res.confirm) { // model = true; // } // } // }) // } // }) // } model = false } return } else if (errorCode == 1007) { //手机号已被绑定 } else if (errorCode == 40005) { //用户信息和微信信息不匹配 } else if (errorCode == 1015) { //用户尚未创建简历 return reject(res.data); } else if (errorCode == 40007) { //账号类型不符 } else if (errorCode == 2006) { //后台接口异常 } else if (errorCode == 2010) { //用户未登录 const _type = pages.length === 1 ? 'reLaunch' : 'navigateTo'; wx[_type]({ url: '/pages/login/login', success: data => { wx.showToast({ icon: 'none', title: '请先登录', }) } }) // if (pages.length === 1) { // wx.reLaunch({ // url: '/pages/login/login', // success: data => { // wx.showToast({ // icon: 'none', // title: '请先登录', // }) // } // }) // } else { // wx.navigateTo({ // url: '/pages/login/login', // success: data => { // wx.showToast({ // icon: 'none', // title: '请先登录', // }) // } // }) // } return } params.data.task_id !== 10 && showToast(res.data.errmsg); reject(res.data); } else { //2. 操作不成功返回数据,以toast方式弹出响应信息,如后端未格式化非操作成功异常信息,则可以统一定义异常提示 wx.showToast({ icon: "none", title: "网络异常", success: () => { reject() } }) } }, fail: function (e) { wx.hideLoading({ success: () => { loading = true } }); wx.showToast({ icon: "none", title: "网络异常", success: () => { loading = true reject() } }) }, complete: (res) => { wx.stopPullDownRefresh({ success: () => { console.log(`${pages[pages.length - 1] ? pages[pages.length - 1].route : pages} => stopPullDownRefresh`); } }); try { console.log('当前页面=>', pages[pages.length - 1].route) console.log("请求参数=>", params); console.log(`请求状态码=>${res.statusCode}`, `状态信息=>${res.errMsg}`); console.log("返回结果=>", res.data); console.log(' '); } catch (error) { console.log(error); } } }) }) }; module.exports = { http }