| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- 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) {
- //后台接口异常
- }
- 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
- }
|