微信小程序发起HTTPS 网络请求封装

utils -> ajax.js

import { apiUrl, APPKEY } from './config.js';
import util from './util.js';
const app = getApp()
// 不需要特殊处理非200的情况
const ajax = function ({url,data,method='post'}) {
  let datas = setSign(JSON.parse(JSON.stringify(data || {})));
  let promise = new Promise(function(resolve,reject){
    wx.request({
      url: apiUrl + url,
      data: datas,
      method: method,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.code === 10011 || res.data.code === 10012 || res.data.code === 10013 || res.data.code === 10014 || res.data.code === 10021) {
          wx.clearStorage();
          wx.removeStorage({key: 'userInfo'})
          wx.hideLoading()
          wx.hideToast()
          wx.showToast({
            title: '亲, 你人品好到爆, 十万分之一的机会你都能够遇到。',
            icon: 'none'
          })
          wx.reLaunch({
            url: '/pages/guide/index',
          })
          return;
        } else if (res.data.code === 10022){
            wx.removeStorage({key: 'userInfo'})
            wx.showToast({
              title: '登录过期或其他设备登录,请重新登录。',
              icon: 'none'
            })
            wx.reLaunch({
              url: '/pages/guide/index',
            })
            return resolve(res.data)
        } else if (res.data.code !== 200) {
          wx.hideLoading()
          wx.hideToast()
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
          reject(res.data)
          return;
        }
        resolve(res.data)
      },
      fail: function (res) {
        wx.showToast({
          title: '网络请求失败,请检查您的网络配置',
          icon: 'none'
        })
      }
    });
  })
  return promise;
};
// 需要特殊处理非200的
const fetch = function ({url,data,method='post'}) {
  let datas = setSign(JSON.parse(JSON.stringify(data || {})));
  let promise = new Promise(function(resolve,reject){
    wx.request({
      url: apiUrl + url,
      data: datas,
      method: method,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.code === 10011 || res.data.code === 10012 || res.data.code === 10013 || res.data.code === 10014 || res.data.code === 10021) {
          wx.clearStorage();
          wx.removeStorage({key: 'userInfo'})
          wx.hideLoading()
          wx.hideToast()
          wx.showToast({
            title: '亲, 你人品好到爆, 十万分之一的机会你都能够遇到。',
            icon: 'none'
          })
          wx.reLaunch({
            url: '/pages/guide/index',
          })
          return;
        } else if (res.data.code === 10022){
            wx.removeStorage({key: 'userInfo'})
            wx.showToast({
              title: '登录过期或其他设备登录,请重新登录。',
              icon: 'none'
            })
            wx.reLaunch({
              url: '/pages/guide/index',
            })
            return;
        } else if (res.data.code !== 200) {
          wx.hideLoading()
          wx.hideToast()
          reject(res.data)
          return;
        }
        resolve(res.data)
      },
      fail: function (res) {
        wx.showToast({
          title: '网络请求失败,请检查您的网络配置',
          icon: 'none'
        })
      }
    });
  })
  return promise;
};
let setSign = function (data) {
  data.app_key = APPKEY;
  data.timestamp = new Date().getTime();
  data.app_version = app.globalData.app_version;
  data.token = (app.globalData.userInfo || wx.getStorageSync('userInfo') || {}).token || ''
  data.sign = util.__joinObj(data);
  return data;
};

module.exports = {
  ajax,
  fetch
};

api -> login -> index.js

import { ajax, fetch } from "../../utils/ajax.js";

// 登录接口
export const loginAjax = function(data){
  return ajax({ url: '/xxx',data });
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值