commonRequire.js笑程序封装请求(网关)

// export const WXURL = "http://10.19.150.150:38861/gateway/interface"; //dev
export const WXURL = "https://uatpxapi.yundasys.com/gateway/interface"; //uat
export const APPID = "f4vuqola4l5w2nhb"  //uat
// export const WXURL = "https://pxapi.yundasys.com/gateway/interface"; //real
// export const APPID = "wwkphhob6jq2fjsb"  //real
function httpsRequest(postData, action,method) {
  console.log("now requesting  "+action)
  var token = wx.getStorageSync("token");
  var userId = wx.getStorageSync("accountId");
  var data2 = {
    "appid": APPID,
    "token": token,
    "userId": userId,
    "action": action,
    "version": "V1.0",
    "req_time": new Date().getTime(),
    "data": postData
  };
  return new Promise(function (resolve, reject) {
    wx.request({
      url: WXURL,
      data: data2,
      method: method || 'POST',
      dataType: "json",
      header: {
        'content-type': 'application/json',
        'account-type': 'ydgj_wechat'
      },
      success: (res) => {
        const app = getApp();
        console.log("base request callback")
        console.log(res)
        if (res.statusCode == 200) {
          let result = res.data;
          if (res.data.success) {
            if (res.data.errorcode === "TOKEN_EXPIRED") {
              wx.hideLoading();
              wx.removeStorageSync('token')
              app.getToken().then(res => {
                if (getCurrentPages().length != 0) {
                  let currentPage = getCurrentPages()[getCurrentPages().length - 1].options;
                  //刷新当前页面的数据
                  getCurrentPages()[getCurrentPages().length - 1].onLoad(currentPage);
                  getCurrentPages()[getCurrentPages().length - 1].onShow(currentPage);
                }
              })
              return;
            } else if (res.data.body.result == "加密数据提交错误") {
              wx.hideLoading();
              wx.removeStorageSync('token')
              app.getToken().then(res => {
                if (getCurrentPages().length != 0) {
                  let currentPage = getCurrentPages()[getCurrentPages().length - 1].options;
                  //刷新当前页面的数据
                  getCurrentPages()[getCurrentPages().length - 1].onLoad(currentPage);
                  getCurrentPages()[getCurrentPages().length - 1].onShow(currentPage);
                }
              })
              return;
            } else {
              resolve(result);
              // wx.removeStorageSync('enterPath')
            }
          } else {
            if (res.data.errorcode === "TOKEN_EXPIRED") {
              wx.hideLoading();
              wx.removeStorageSync('token')
              app.getToken().then(res => {
                if (getCurrentPages().length != 0) {
                  let currentPage = getCurrentPages()[getCurrentPages().length - 1].options;
                  //刷新当前页面的数据
                  getCurrentPages()[getCurrentPages().length - 1].onLoad(currentPage);
                  getCurrentPages()[getCurrentPages().length - 1].onShow(currentPage);
                }
              })
              return;
            } else {
              wx.showToast({
                icon: 'none',
                title: res.data.msg,
                duration: 2000
              })
              resolve({
                isError: true,
                remark: res.data.msg
              });
            }
          }
        } else {
          wx.showToast({
            title: "服务器异常,请稍后再试",
            icon: 'none',
            duration: 1500,
            mask: false,
          });
          resolve({
            isError: true,
            remark: "服务器异常,请稍后再试"
          });
        }
      },
      fail: function () {
        reject("网络异常")
        wx.showToast({
          icon: 'none',
          title: '网络异常',
          duration: 2000
        })
      }
    });
  })
}
function simpleRequest(postData,action,method){
  console.log("now requesting  " + action)
  var data2 = {
    "appid": APPID,
    "action": action,
    "version": "V1.0",
    "req_time": new Date().getTime(),
    "data": JSON.stringify(postData)

  };
  return new Promise(function (resolve, reject) {
    wx.request({
      url: WXURL,
      data: data2,
      method: method || 'POST',
      dataType: "json",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'account-type': 'ydgj_wechat'
      },
      success: (res) => {
        const app = getApp();
        console.log("base request callback")
        console.log(res)
        if (res.statusCode == 200) {
          let result = res.data;
          if (res.data.success) {
              resolve(result);
          } else {
              wx.showToast({
                icon: 'none',
                title: res.data.msg,
                duration: 2000
              })
              resolve({
                isError: true,
                remark: res.data.msg
              });
          }
        } else {
          wx.showToast({
            title: "服务器异常,请稍后再试",
            icon: 'none',
            duration: 1500,
            mask: false,
          });
          resolve({
            isError: true,
            remark: "服务器异常,请稍后再试"
          });
        }
      },
      fail: function () {
        reject("网络异常")
        wx.showToast({
          icon: 'none',
          title: '网络异常',
          duration: 2000
        })
      }
    });
  })
}




module.exports = {
  'simpleRequest': simpleRequest,
  'httpsRequest': httpsRequest,
  'WXURL': WXURL,
  'APPID': APPID
}

你可能感兴趣的:(commonRequire.js笑程序封装请求(网关))