完善与服务端的对接;添加token刷新机制;调整部分代码;

This commit is contained in:
Saturneric 2021-03-17 00:59:21 +08:00
parent 6ecb37eea2
commit 4a3d2f03fe
7 changed files with 127 additions and 31 deletions

View File

@ -0,0 +1,11 @@
import request from '../utils/request.js'
export const getDocument = openid => {
return request({
url: "/document",
method: "get",
data: {
openid
}
})
}

View File

@ -0,0 +1 @@
import request from '../utils/request.js'

28
app.js
View File

@ -3,11 +3,13 @@ import request from './utils/request.js'
App({
onLaunch() {
// 展示本地存储能力
const logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
@ -25,18 +27,14 @@ App({
this.globalData.loginTime = Date.now()
// 设置登录信息
const userInfo = this.globalData.userInfo
userInfo.openid = res.openid
userInfo.postmark = res.postmark
userInfo.token = res.token
const userBaseInfo = {}
userBaseInfo.openid = res.openid
userBaseInfo.postmark = res.postmark
userBaseInfo.token = res.token
this.globalData.userBaseInfo = userBaseInfo
}).then(res => {
}).catch(err => {
console.log(err)
})
}
})
// 获取用户信息
wx.getSetting({
success: res => {
@ -57,9 +55,15 @@ App({
}
}
})
}).catch(err => {
console.log(err)
})
}
})
},
globalData: {
userInfo: {},
userInfo: null,
userBaseInfo: null,
loginTime: null,
login: false,
appID: 'wxb915ee2a665fcb6c',

View File

@ -7,7 +7,7 @@
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "Weixin",
"navigationBarTitleText": "全员育人小程序",
"navigationBarTextStyle": "black"
},
"style": "v2",

View File

@ -1,4 +1,7 @@
// index.js
import {getDocument} from '../../api/document'
// 获取应用实例
const app = getApp()
@ -7,7 +10,7 @@ Page({
motto: 'Hello World',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo')
canIUse: true
},
// 事件处理函数
bindViewTap() {
@ -16,12 +19,14 @@ Page({
})
},
onLoad() {
if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
})
} else if (this.data.canIUse) {
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.userInfoReadyCallback = res => {
@ -29,6 +34,13 @@ Page({
userInfo: res.userInfo,
hasUserInfo: true
})
console.log(app.globalData)
getDocument(app.globalData.userBaseInfo.openid).then(res => {
console.log(res)
}).catch(err => {
console.log(err )
})
}
} else {
// 在没有 open-type=getUserInfo 版本的兼容处理
@ -42,6 +54,7 @@ Page({
}
})
}
},
getUserInfo(e) {
console.log(e)

View File

@ -1,3 +1,5 @@
import sha1 from './sha1'
// 正在队列上的请求
const pendings = []
@ -47,7 +49,20 @@ const do_request = (app, options) => {
// 检查登录状态
if(app.globalData.login) {
const userBaseInfo = app.globalData.userBaseInfo
let random_code = `RandomCode [${userBaseInfo.openid}][${header_options.timestamp}][${app.globalData.appID}]`
console.log(random_code)
random_code = sha1(random_code)
console.log(random_code)
let signed = `SIGN [${userBaseInfo.postmark}][${random_code}][${userBaseInfo.token}]`
signed = sha1(signed)
console.log(signed)
header_options['signed'] = signed;
header_options['postmark'] = userBaseInfo.postmark;
}
options.url = app.globalData.baseURL + url
@ -65,7 +80,7 @@ const do_request = (app, options) => {
if(data.status >= 200 && data.status < 300) {
resolve(data.data)
} else {
reject(data.data)
reject(data.msg)
}
},
fail: function(res) {

52
utils/sha1.js Normal file
View File

@ -0,0 +1,52 @@
function encodeUTF8(s) {
var i, r = [], c, x;
for (i = 0; i < s.length; i++)
if ((c = s.charCodeAt(i)) < 0x80) r.push(c);
else if (c < 0x800) r.push(0xC0 + (c >> 6 & 0x1F), 0x80 + (c & 0x3F));
else {
if ((x = c ^ 0xD800) >> 10 == 0) //对四字节UTF-16转换为Unicode
c = (x << 10) + (s.charCodeAt(++i) ^ 0xDC00) + 0x10000,
r.push(0xF0 + (c >> 18 & 0x7), 0x80 + (c >> 12 & 0x3F));
else r.push(0xE0 + (c >> 12 & 0xF));
r.push(0x80 + (c >> 6 & 0x3F), 0x80 + (c & 0x3F));
};
return r;
};
// 字符串加密成 hex 字符串
const sha1 = s => {
var data = new Uint8Array(encodeUTF8(s))
var i, j, t;
var l = ((data.length + 8) >>> 6 << 4) + 16, s = new Uint8Array(l << 2);
s.set(new Uint8Array(data.buffer)), s = new Uint32Array(s.buffer);
for (t = new DataView(s.buffer), i = 0; i < l; i++)s[i] = t.getUint32(i << 2);
s[data.length >> 2] |= 0x80 << (24 - (data.length & 3) * 8);
s[l - 1] = data.length << 3;
var w = [], f = [
function () { return m[1] & m[2] | ~m[1] & m[3]; },
function () { return m[1] ^ m[2] ^ m[3]; },
function () { return m[1] & m[2] | m[1] & m[3] | m[2] & m[3]; },
function () { return m[1] ^ m[2] ^ m[3]; }
], rol = function (n, c) { return n << c | n >>> (32 - c); },
k = [1518500249, 1859775393, -1894007588, -899497514],
m = [1732584193, -271733879, null, null, -1009589776];
m[2] = ~m[0], m[3] = ~m[1];
for (i = 0; i < s.length; i += 16) {
var o = m.slice(0);
for (j = 0; j < 80; j++)
w[j] = j < 16 ? s[i + j] : rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1),
t = rol(m[0], 5) + f[j / 20 | 0]() + m[4] + w[j] + k[j / 20 | 0] | 0,
m[1] = rol(m[1], 30), m.pop(), m.unshift(t);
for (j = 0; j < 5; j++)m[j] = m[j] + o[j] | 0;
};
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) {
return (e < 16 ? "0" : "") + e.toString(16);
}).join("");
return hex;
};
export default sha1