Commit 6ee28efc by 梁建民

js

parent e1cec561
Showing with 118 additions and 101 deletions
//数据请求(get,post)
const getData = (url, type, param, callBack, loading, isheader, istoken) => {
wx.showNavigationBarLoading();
var token, header;
//获取token
token = wx.getStorageSync('access_token');
//参数字段追加来源字段,token字段
if (istoken) {
var params = Object.assign({}, param, {
source: 1,
token: token
});
} else {
var params = Object.assign({}, param, {
source: 1
});
}
//是否启用loading加载效果
if (loading) {
wx.showLoading();
}
//是否启用请求头token
if (isheader) {
header = {
"Content-Type": "applciation/json",
"Authorization": 'Bearer ' + token
}
} else {
header = {
"Content-Type": "applciation/json"
}
}
wx.request({
url: url,
data: params,
header: header,
method: type,
success: (res) => {
//处理token失效的情况
if (res.data.hasOwnProperty('data')) {
if (res.data.err_code === 501 || res.data.errcode === 501) {
wx.navigateTo({
url: '/pages/person/login/index'
});
} else {
typeof callBack == "function" && callBack(res.data, "");
}
} else {
if (res.data.err_code === 501 || res.data.errcode === 501) {
wx.navigateTo({
url: '/pages/person/login/index'
});
} else {
typeof callBack == "function" && callBack(res.data, "");
}
}
wx.hideNavigationBarLoading();
if (loading) {
wx.hideLoading();
}
},
fail: (err) => {
typeof callBack == "function" && callBack(null, err.errMsg);
console.log(err);
wx.hideNavigationBarLoading();
if (loading) {
wx.hideLoading();
}
}
})
wx.showNavigationBarLoading();
var token, header;
//获取token
token = wx.getStorageSync('access_token');
//参数字段追加来源字段,token字段
if (istoken) {
var params = Object.assign({}, param, {
source: 1,
token: token
});
} else {
var params = Object.assign({}, param, {
source: 1
});
}
//是否启用loading加载效果
if (loading) {
wx.showLoading();
}
//是否启用请求头token
if (isheader) {
header = {
"Content-Type": "applciation/json",
"Authorization": 'Bearer ' + token
}
} else {
header = {
"Content-Type": "applciation/json"
}
}
wx.request({
url: url,
data: params,
header: header,
method: type,
success: (res) => {
//处理token失效的情况
if (res.data.hasOwnProperty('data')) {
if (res.data.err_code === 501 || res.data.errcode === 501) {
wx.redirectTo({
url: '/pages/person/login/index'
});
} else {
typeof callBack == "function" && callBack(res.data, "");
}
} else {
if (res.data.err_code === 501 || res.data.errcode === 501) {
wx.redirectTo({
url: '/pages/person/login/index'
});
} else {
typeof callBack == "function" && callBack(res.data, "");
}
}
wx.hideNavigationBarLoading();
if (loading) {
wx.hideLoading();
}
},
fail: (err) => {
typeof callBack == "function" && callBack(null, err.errMsg);
console.log(err);
wx.hideNavigationBarLoading();
if (loading) {
wx.hideLoading();
}
}
})
};
//上传文件
......@@ -144,13 +144,17 @@ const chooseImg = (url, num, callback) => {
}
})
};
const tips = (text) => {
wx.showToast({
title: text,
icon: 'none',
duration: 2000
});
}
};
const changeTime = (str) => {
let date = new Date(str);
let Hours = date.getHours();
......@@ -161,7 +165,9 @@ const changeTime = (str) => {
let time = date.getFullYear() + "-" + (Month < 10 ? "0" + Month : Month) + "-" + (Day < 10 ? "0" + Day : Day) + " " +
(Hours < 10 ? "0" + Hours : Hours) + ":" + (Minutes < 10 ? "0" + Minutes : Minutes) + ":" + (Seconds < 10 ? "0" + Seconds : Seconds);
return time;
}
};
const randomFun = () => {
let str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let newStr = "";
......@@ -170,7 +176,9 @@ const randomFun = () => {
newStr += str.substr(randomNum, 1)
}
return newStr
}
};
const sort = (arr) => {
for (let j = 0; j < arr.length; j++) {
for (let i = 0; i < arr.length - 1 - j; i++) {
......@@ -180,7 +188,9 @@ const sort = (arr) => {
}
}
return arr;
}
};
const encodeUTF8 = (s) => {
var i, r = [],
c, x;
......@@ -196,6 +206,8 @@ const encodeUTF8 = (s) => {
};
return r;
};
const sha1 = (s) => {
var data = new Uint8Array(encodeUTF8(s))
var i, j, t;
......@@ -207,20 +219,20 @@ const sha1 = (s) => {
s[l - 1] = data.length << 3;
var w = [],
f = [
function() {
function () {
return m[1] & m[2] | ~m[1] & m[3];
},
function() {
function () {
return m[1] ^ m[2] ^ m[3];
},
function() {
function () {
return m[1] & m[2] | m[1] & m[3] | m[2] & m[3];
},
function() {
function () {
return m[1] ^ m[2] ^ m[3];
}
],
rol = function(n, c) {
rol = function (n, c) {
return n << c | n >>> (32 - c);
},
k = [1518500249, 1859775393, -1894007588, -899497514],
......@@ -237,11 +249,13 @@ const sha1 = (s) => {
t = new DataView(new Uint32Array(m).buffer);
for (var i = 0; i < 5; i++) m[i] = t.getUint32(i << 2);
var hex = Array.prototype.map.call(new Uint8Array(new Uint32Array(m).buffer), function(e) {
var hex = Array.prototype.map.call(new Uint8Array(new Uint32Array(m).buffer), function (e) {
return (e < 16 ? "0" : "") + e.toString(16);
}).join("");
return hex;
};
const dataEncryption = () => {
let timestamp = Date.parse(new Date()) / 1000;
let randomStr = randomFun();
......@@ -253,25 +267,27 @@ const dataEncryption = () => {
singnatrueStr: singnatrueStr,
timestampStr: timestamp
}
}
};
const judgeToken = (boolen) => {
let token = wx.getStorageSync('access_token');
if(token){
if (token) {
return true
}else{
if(boolen){
} else {
if (boolen) {
wx.redirectTo({
url: '/pages/person/login/index',
url: '/pages/person/login/index'
})
}
return false;
}
}
};
module.exports = {
getData: getData,
chooseImg: chooseImg,
tips: tips,
changeTime: changeTime,
dataEncryption: dataEncryption,
judgeToken:judgeToken
judgeToken: judgeToken
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment