diff --git a/api/document.js b/api/document.js index e69de29..22e2f9b 100644 --- a/api/document.js +++ b/api/document.js @@ -0,0 +1,11 @@ +import request from '../utils/request.js' + +export const getDocument = openid => { + return request({ + url: "/document", + method: "get", + data: { + openid + } + }) +} \ No newline at end of file diff --git a/api/user.js b/api/user.js index e69de29..e8d223c 100644 --- a/api/user.js +++ b/api/user.js @@ -0,0 +1 @@ +import request from '../utils/request.js' \ No newline at end of file diff --git a/app.js b/app.js index dc32ed3..5630495 100644 --- a/app.js +++ b/app.js @@ -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,41 +27,43 @@ 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 => { - + // 获取用户信息 + wx.getSetting({ + success: res => { + if (res.authSetting['scope.userInfo']) { + // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 + wx.getUserInfo({ + success: res => { + // 可以将 res 发送给后台解码出 unionId + this.globalData.userInfo = res.userInfo + + // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 + // 所以此处加入 callback 以防止这种情况 + if (this.userInfoReadyCallback) { + this.userInfoReadyCallback(res) + } + } + }) + } + } + }) }).catch(err => { console.log(err) }) } }) - - // 获取用户信息 - wx.getSetting({ - success: res => { - if (res.authSetting['scope.userInfo']) { - // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 - wx.getUserInfo({ - success: res => { - // 可以将 res 发送给后台解码出 unionId - this.globalData.userInfo = res.userInfo - - // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 - // 所以此处加入 callback 以防止这种情况 - if (this.userInfoReadyCallback) { - this.userInfoReadyCallback(res) - } - } - }) - } - } - }) }, globalData: { - userInfo: {}, + userInfo: null, + userBaseInfo: null, loginTime: null, login: false, appID: 'wxb915ee2a665fcb6c', diff --git a/app.json b/app.json index f18ee85..dd7a73c 100644 --- a/app.json +++ b/app.json @@ -7,7 +7,7 @@ "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", - "navigationBarTitleText": "Weixin", + "navigationBarTitleText": "全员育人小程序", "navigationBarTextStyle": "black" }, "style": "v2", diff --git a/pages/index/index.js b/pages/index/index.js index aa6487c..e64bc94 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -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) diff --git a/utils/request.js b/utils/request.js index 9574073..8ecc432 100644 --- a/utils/request.js +++ b/utils/request.js @@ -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) { diff --git a/utils/sha1.js b/utils/sha1.js new file mode 100644 index 0000000..8bbaa2a --- /dev/null +++ b/utils/sha1.js @@ -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 \ No newline at end of file