Commit 6ee28efc by 梁建民

js

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