123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- const toolHelper = require("../utils/toolHelper.js");
- import {getApi} from '../utils/dataHelper'
- const Base64 = require("../utils/base64");
-
- let header = {
- 'Accept': 'application/json',
- 'Accept-charset': 'utf-8'
- }
-
- const CLIENTID = 'miniapp'
- const CLIENTSECRET = 'miniapp_secret'
-
-
-
- function queryData(data) {
- var str = '';
- for (var i in data) {
- str += i + "=" + data[i] + '&';
- }
- if (str) {
- str = '?' + str;
- str = str.substr(0, str.length - 1);
- }
- return str;
- }
-
- const http = {};
- http.get = function get(url, data, cb) {
- console.log(getApi(), url, data)
- if (url != '/blade-auth/token' && url != '/grants/wechat/consumerinfopublic/list' && url != '/grants/wechat/consumerinfopublic/detail' && url != '/grants/wechat/guide') {
- if (!wx.getStorageSync('token')) {
- wx.showToast({
- title: '请先登录',
- icon: 'none'
- })
- return false
- }
- }
-
- header.Authorization = `Basic ${Base64.Base64.encode(`${CLIENTID}:${CLIENTSECRET}`)}`;
- header['content-type'] = 'application/x-www-form-urlencoded';
- if (wx.getStorageSync('token')) {
- header['Blade-Auth'] = `bearer ${wx.getStorageSync('token')}`
- }
- wx.showLoading({
- title: '加载中',
- })
- wx.request({
- url: getApi() + url + queryData(data),
- method: 'get',
- header: header,
- success: function (res) {
- console.log(res.data)
- wx.hideToast();
- let resData = res.data;
- switch (res.data.code) {
- case 200:
- toolHelper.isFunction(cb) && cb(resData);
- break;
- case 401:
- wx.showToast({
- icon: "none",
- title: '请重新登录'
- });
- setTimeout(() => {
- wx.clearStorage()
- wx.reLaunch({
- url: '/pages/my/index/index',
- })
- })
- break;
- default:
- wx.showToast({
- icon: "none",
- title: resData.msg
- });
- }
- },
- fail(res) {
- wx.showToast({
- title: '请求出错,请联系相关人员',
- icon: 'none'
- })
- wx.hideLoading();
- },
- complete(res) {
- wx.hideLoading();
- }
- })
- }
-
- http.post = function post(url, data, cb) {
- console.log(getApi(), url, data)
-
- if (url != '/blade-auth/token') {
- if (!wx.getStorageSync('token')) {
- wx.showToast({
- title: '请先登录',
- icon: 'none'
- })
- return false
- }
- header['content-type'] = 'application/json';
- } else {
- header['content-type'] = 'application/x-www-form-urlencoded';
- }
-
- header.Authorization = `Basic ${Base64.Base64.encode(`${CLIENTID}:${CLIENTSECRET}`)}`;
- if (wx.getStorageSync('token')) {
- header['Blade-Auth'] = `bearer ${wx.getStorageSync('token')}`
- }
- wx.request({
- url: getApi() + url,
- method: 'post',
- data: data,
- header: header,
- formData: data,
- success: function (res) {
- let resData = res.data;
- switch (res.data.code) {
- case 200:
- toolHelper.isFunction(cb) && cb(resData);
- break;
- case 401:
- wx.showModal({
- title: '错误信息',
- content: resData.msg,
- confirmText: '知道了',
- showCancel: false,
- success(res1) {
- if (res1.confirm) {
- wx.clearStorage()
- wx.reLaunch({
- url: '/pages/my/index/index',
- })
- }
- }
- })
- break;
- default:
- wx.showToast({
- icon: "none",
- title: resData.msg
- });
- }
- },
- fail(err) {
- wx.showToast({
- title: '请求出错,请联系相关人员',
- icon: 'none'
- })
- },
- complete(res) {
- }
- });
- }
-
-
- module.exports = http;
|