258 lines
7.4 KiB
JavaScript
258 lines
7.4 KiB
JavaScript
import Config from '../config';
|
|
import {
|
|
toEncrypt,
|
|
randomString
|
|
} from 'stringEncrypt';
|
|
let app = getApp();
|
|
|
|
function HttpNoUkeyRequest(loading, url, sessionChoose, params, method, callBack, reject) {
|
|
if (loading == true) {
|
|
wx.showLoading();
|
|
}
|
|
let paramSession = sessionChoose == 1 ? {
|
|
'content-type': 'application/x-www-form-urlencoded'
|
|
} : {
|
|
'content-type': 'application/json'
|
|
};
|
|
var params = Object.prototype.toString.call(params) == "[object Object]" ? params : {};
|
|
params["version"] = Config.version;
|
|
params["nonce_str"] = randomString();
|
|
params["app_id"] = Config.app_id;
|
|
let serectParams = {
|
|
...params,
|
|
sign: toEncrypt(params)
|
|
}
|
|
|
|
wx.request({
|
|
url: Config.api.baseUrl + url,
|
|
data: serectParams,
|
|
dataType: "json",
|
|
header: paramSession,
|
|
method: method,
|
|
success: function (res) {
|
|
if (loading == true) {
|
|
wx.hideLoading(); //隐藏提示框
|
|
}
|
|
//token 存在 还返回408,这个时候就要强制
|
|
if (res.data.code == 408) {
|
|
if (getCurrentPages().pop().route != 'pages/login/index') {
|
|
wx.clearStorage()
|
|
wx.reLaunch({
|
|
url: '/pages/login/index'
|
|
})
|
|
}
|
|
} else {
|
|
if (res.data.code == 200) {
|
|
callBack(res.data);
|
|
} else {
|
|
app.printErrorClient('failrequest', ['api地址:' + url, '传入数据:' + JSON.stringify(serectParams), '响应数据:' + JSON.stringify(res.data)])
|
|
reject(res.data);
|
|
if (Config.white401UrlList[url] === url) { //接口白名单
|
|
return
|
|
}
|
|
wx.showToast({
|
|
title: res.data.msg,
|
|
icon: "none",
|
|
duration: 2500
|
|
});
|
|
}
|
|
}
|
|
},
|
|
fail: function (res) {
|
|
console.warn("HttpRequst当中wx.request fail 执行了:调用失败")
|
|
app.printErrorClient('failrequest', ['HttpRequst当中wx.request fail 执行了:调用失败', 'api地址:' + url, '传入数据:' + JSON.stringify(serectParams), '响应数据:' + JSON.stringify(res.data)])
|
|
}
|
|
})
|
|
}
|
|
|
|
function HttpRequest(loading, url, sessionChoose, params, method, callBack, reject) {
|
|
let ukey = app.getStorageByKey("ukey");
|
|
let requestUrl = Config.api.baseUrl;
|
|
//判断ukey是否存在
|
|
// 存在就继续执行
|
|
if (!!ukey) {
|
|
if (loading == true) {
|
|
wx.showLoading();
|
|
}
|
|
let paramSession = sessionChoose == 1 ? {
|
|
'content-type': 'application/x-www-form-urlencoded'
|
|
} : {
|
|
'content-type': 'application/json'
|
|
};
|
|
var params = Object.prototype.toString.call(params) == "[object Object]" ? params : {};
|
|
params["ukey"] = ukey;
|
|
params["version"] = Config.version;
|
|
params["nonce_str"] = randomString();
|
|
params["app_id"] = Config.app_id;
|
|
let serectParams = {
|
|
...params,
|
|
sign: toEncrypt(params)
|
|
}
|
|
|
|
wx.request({
|
|
url: Config.api.baseUrl + url,
|
|
data: serectParams,
|
|
dataType: "json",
|
|
header: paramSession,
|
|
method: method,
|
|
success: function (res) {
|
|
if (loading == true) {
|
|
wx.hideLoading(); //隐藏提示框
|
|
}
|
|
//token 存在 还返回408,这个时候就要强制
|
|
if (res.data.code == 408) {
|
|
// asyncLogin(true).then((res) => {
|
|
// console.log("asyncLogin().then-success")
|
|
// HttpRequest(loading, url, sessionChoose, params, method, callBack, reject)
|
|
// })
|
|
//if (getCurrentPages().pop().route != 'pages/login/index'){
|
|
wx.clearStorage()
|
|
wx.reLaunch({
|
|
url: '/pages/login/index'
|
|
})
|
|
//}
|
|
} else {
|
|
if (res.data.code == 200) {
|
|
callBack(res.data);
|
|
} else {
|
|
app.printErrorClient('failrequest', ['api地址:' + url, '传入数据:' + JSON.stringify(serectParams), '响应数据:' + JSON.stringify(res.data)])
|
|
reject(res.data);
|
|
if (Config.white401UrlList[url] === url) { //接口白名单
|
|
return
|
|
}
|
|
if (res.data.msg) {
|
|
wx.showToast({
|
|
title: res.data.msg,
|
|
icon: "none"
|
|
});
|
|
}
|
|
}
|
|
}
|
|
},
|
|
fail: function (res) {
|
|
console.warn("HttpRequst当中wx.request fail 执行了:调用失败")
|
|
app.printErrorClient('failrequest', ['HttpRequst当中wx.request fail 执行了:调用失败', 'api地址:' + url, '传入数据:' + JSON.stringify(serectParams), '响应数据:' + JSON.stringify(res.data)])
|
|
}
|
|
})
|
|
} else {
|
|
//不存在(或者) 重走登录流程
|
|
//第一个执行的时候 后面的都需要挂起来
|
|
// asyncLogin().then((res) => {
|
|
// console.log("asyncLogin().then-没有token")
|
|
// HttpRequest(loading, url, sessionChoose, params, method, callBack, reject)
|
|
// })
|
|
//if (getCurrentPages().pop().route != 'pages/login/index') {
|
|
wx.clearStorage()
|
|
wx.reLaunch({
|
|
url: '/pages/login/index'
|
|
})
|
|
//}
|
|
}
|
|
}
|
|
|
|
function asyncLogin(flag) {
|
|
|
|
return new Promise((reslove, reject) => {
|
|
if (app.globalData.loginRuning) {
|
|
//已经在执行了
|
|
app.globalData.loginCollect.push(reslove);
|
|
} else {
|
|
app.globalData.loginRuning = true;
|
|
work(reslove, reject, flag)
|
|
}
|
|
|
|
})
|
|
}
|
|
|
|
|
|
function _resloveRun() {
|
|
app.globalData.loginCollect.forEach((reslove) => {
|
|
reslove();
|
|
// console.log("app.globalData.loginCollect.forEach")
|
|
});
|
|
// console.log("_resloveRun")
|
|
app.globalData.loginRuning = false;
|
|
app.globalData.loginCollect = [];
|
|
}
|
|
|
|
|
|
function work(reslove, reject, flag) {
|
|
wx.checkSession({
|
|
success: function () {
|
|
|
|
//同步获取token
|
|
let ukey = app.getStorageByKey("ukey");
|
|
console.log("asyncLogin(checkSession-success)-ukey:" + ukey)
|
|
//为了安全验证 判断ukey是否有本地存储
|
|
if (!ukey || (!!ukey && flag)) {
|
|
wxLogin(reslove);
|
|
} else {
|
|
//重新走登录流程
|
|
reslove(ukey);
|
|
_resloveRun();
|
|
}
|
|
},
|
|
fail: function () {
|
|
//重新走登录流程
|
|
wxLogin(reslove);
|
|
}
|
|
})
|
|
}
|
|
|
|
function wxLogin(reslove) {
|
|
wx.login({
|
|
success(res) {
|
|
console.log("执行了wx.login-code:" + res.code)
|
|
if (res.code) {
|
|
let params = {
|
|
code: res.code,
|
|
by_code: 1,
|
|
app_id: Config.app_id,
|
|
nonce_str: randomString()
|
|
}
|
|
let sendData = {
|
|
...params,
|
|
sign: toEncrypt(params)
|
|
}
|
|
wx.request({
|
|
url: Config.api.baseUrl + Config.api.appUserUkey,
|
|
data: sendData,
|
|
dataType: "json",
|
|
header: {
|
|
'content-type': 'application/json'
|
|
},
|
|
method: "GET",
|
|
success: function (res) {
|
|
console.log("wx.login-ukey:" + res.data.data.ukey)
|
|
if (res.data.code == 200) {
|
|
app.globalData.ukey = res.data.data.ukey;
|
|
app.setStorage("ukey", res.data.data.ukey, function () {
|
|
reslove();
|
|
_resloveRun();
|
|
})
|
|
}
|
|
},
|
|
fail: function (res) {
|
|
console.warn("wxLogin当中wx.request fail 执行了:调用失败")
|
|
}
|
|
})
|
|
} else {
|
|
console.warn('登录失败!' + res.errMsg)
|
|
}
|
|
},
|
|
fail: function () {
|
|
console.warn("调用wx.login接口失败!")
|
|
}
|
|
})
|
|
}
|
|
|
|
app.asyncLogin = asyncLogin;
|
|
|
|
|
|
|
|
export {
|
|
HttpNoUkeyRequest,
|
|
HttpRequest,
|
|
asyncLogin,
|
|
app
|
|
} |