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 _data = Object.assign(data, { user_token: wx.getStorageSync('user_token'), user_id: wx.getStorageSync('user_id'), member_id: wx.getStorageSync('member_id'), }) for (let key in _data) { if (!_data[key] && _data[key] !== 0) { delete _data[key] } } console.log('data=>', _data) return _data; } // const getUrl = function(url) { // if (url.indexOf('https://') || url.indexOf('https://')) { // } // } /** * 封装http 请求方法 */ const http = (params) => { // console.log(params) //返回promise 对象 return new Promise((resolve, reject) => { // wx.showToast({ // icon: 'loading', // title: '加载中...', // mask: true, // duration: 10000, // success: res => { // console.log(new Date()) // loading = false // } // }) if (loading) { } wx.showLoading({ title: 'loading', mask: true, success: res => { loading = false } }); console.log(params.url) 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) { console.log("res=>", res.data) wx.hideLoading({ success: res => { loading = true } }); if (res.statusCode == 200) { var errorCode = res.data.errcode if (errorCode == 0) { // wx.hideToast() // wx.showToast({ // icon: 'loading', // title: '加载中...', // duration: 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 == 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) { wx.reLaunch({ 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) { //用户未登录 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) { console.log('error=>', e) wx.showToast({ icon: "none", title: "网络异常", success: () => { loading = true reject() } }) }, complete: () => { // wx.hideLoading({ // success: res => { // loading = true // } // }) // wx.showToast({ // icon: 'loading', // title: 'loading...', // duration: 0 // }) // wx.hideToast({ // success: res => { // console.log(new Date()) // console.log('complete close') // } // }) wx.stopPullDownRefresh(); } }) }) }; // function _request(options) { // wx.request({ // url: options.url, // data: options.data, // header: { // "Content-Type": "application/json", // ...options.header // }, // method: options.method, // dataType: options.dataType || 'json', // responseType: options.responseType || 'text', // success: function(res) { // options.success && options.success(res) // }, // fail: function(res) { // options.fail && options.fail(res) // }, // complete: function(res) { // options.complete && options.complete(res) // }, // }) // } module.exports = { http }