diff --git a/api/document.js b/api/document.js new file mode 100644 index 0000000..e69de29 diff --git a/api/user.js b/api/user.js new file mode 100644 index 0000000..e69de29 diff --git a/app.js b/app.js index f9db25d..dc32ed3 100644 --- a/app.js +++ b/app.js @@ -1,4 +1,6 @@ // app.js +import request from './utils/request.js' + App({ onLaunch() { // 展示本地存储能力 @@ -6,12 +8,35 @@ App({ logs.unshift(Date.now()) wx.setStorageSync('logs', logs) - // 登录 wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId + request({ + url: '/wx/user/' + this.globalData.appID + '/login', + method: 'get', + data: { + code: res.code + } + }).then(res => { + console.log(res) + + this.globalData.login = true + // 设置登录时间 + this.globalData.loginTime = Date.now() + + // 设置登录信息 + const userInfo = this.globalData.userInfo + userInfo.openid = res.openid + userInfo.postmark = res.postmark + userInfo.token = res.token + }).then(res => { + + }).catch(err => { + console.log(err) + }) } }) + // 获取用户信息 wx.getSetting({ success: res => { @@ -34,6 +59,10 @@ App({ }) }, globalData: { - userInfo: null + userInfo: {}, + loginTime: null, + login: false, + appID: 'wxb915ee2a665fcb6c', + baseURL: 'http://localhost:8088' } }) diff --git a/app.json b/app.json index 3d7616f..f18ee85 100644 --- a/app.json +++ b/app.json @@ -1,14 +1,15 @@ { - "pages":[ + "pages": [ "pages/index/index", - "pages/logs/logs" + "pages/logs/logs", + "pages/bind-document/bind-document" ], - "window":{ - "backgroundTextStyle":"light", + "window": { + "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "Weixin", - "navigationBarTextStyle":"black" + "navigationBarTextStyle": "black" }, "style": "v2", "sitemapLocation": "sitemap.json" -} +} \ No newline at end of file diff --git a/pages/bind-document/bind-document.js b/pages/bind-document/bind-document.js new file mode 100644 index 0000000..9b57e55 --- /dev/null +++ b/pages/bind-document/bind-document.js @@ -0,0 +1,66 @@ +// pages/bind-document.js +Page({ + + /** + * 页面的初始数据 + */ + data: { + + }, + + /** + * 生命周期函数--监听页面加载 + */ + onLoad: function (options) { + + }, + + /** + * 生命周期函数--监听页面初次渲染完成 + */ + onReady: function () { + + }, + + /** + * 生命周期函数--监听页面显示 + */ + onShow: function () { + + }, + + /** + * 生命周期函数--监听页面隐藏 + */ + onHide: function () { + + }, + + /** + * 生命周期函数--监听页面卸载 + */ + onUnload: function () { + + }, + + /** + * 页面相关事件处理函数--监听用户下拉动作 + */ + onPullDownRefresh: function () { + + }, + + /** + * 页面上拉触底事件的处理函数 + */ + onReachBottom: function () { + + }, + + /** + * 用户点击右上角分享 + */ + onShareAppMessage: function () { + + } +}) \ No newline at end of file diff --git a/pages/bind-document/bind-document.json b/pages/bind-document/bind-document.json new file mode 100644 index 0000000..8835af0 --- /dev/null +++ b/pages/bind-document/bind-document.json @@ -0,0 +1,3 @@ +{ + "usingComponents": {} +} \ No newline at end of file diff --git a/pages/bind-document/bind-document.wxml b/pages/bind-document/bind-document.wxml new file mode 100644 index 0000000..d65088e --- /dev/null +++ b/pages/bind-document/bind-document.wxml @@ -0,0 +1,2 @@ + +pages/bind-document.wxml diff --git a/pages/bind-document/bind-document.wxss b/pages/bind-document/bind-document.wxss new file mode 100644 index 0000000..438029c --- /dev/null +++ b/pages/bind-document/bind-document.wxss @@ -0,0 +1 @@ +/* pages/bind-document.wxss */ \ No newline at end of file diff --git a/project.config.json b/project.config.json index 0f1f4ab..3f20f90 100644 --- a/project.config.json +++ b/project.config.json @@ -4,7 +4,7 @@ "ignore": [] }, "setting": { - "urlCheck": true, + "urlCheck": false, "es6": true, "enhance": false, "postcss": true, diff --git a/utils/request.js b/utils/request.js new file mode 100644 index 0000000..9574073 --- /dev/null +++ b/utils/request.js @@ -0,0 +1,102 @@ + +// 正在队列上的请求 +const pendings = [] + +let refreshing = false + +// 刷新token +const refresh_token = app => { + return new Promise((resolve, reject) => { + do_request(app, { + url: '/auth/token', + method: 'get' + }).then(res => { + // 设置登录信息 + const userInfo = app.globalData.userInfo + userInfo.token = res.token + resolve(true) + }).catch(err => { + console.log(err) + // 清空登录信息 + app.globalData.login = false + app.globalData.userInfo = {} + app.globalData.loginTime = null + refreshing = false + reject() + }).then(() => { + // 停止刷新状态 + refreshing = false; + // 执行所有的代发请求 + pendings.forEach(element => { + do_request(app, element) + }) + }) + }) +} + +// 实际操作函数 +const do_request = (app, options) => { + return new Promise((resolve, reject) => { + const { data, method, url } = options + + // 设置请求头 + let header_options = { + 'timestamp': Date.now(), + 'X-Requested-With': 'XMLHttpRequest' + } + + // 检查登录状态 + if(app.globalData.login) { + + } + + options.url = app.globalData.baseURL + url + + if(data && method === 'post') { + header_options['Content-Type'] = 'application/x-www-form-urlencoded' + } + wx.request({ + header: { + ...header_options + }, + ...options, + success: function(res) { + const data = res.data + if(data.status >= 200 && data.status < 300) { + resolve(data.data) + } else { + reject(data.data) + } + }, + fail: function(res) { + reject(res.data) + } + }) + }) +} + +// 对外接口 +const request = options => { + return new Promise((resolve, reject) => { + const app = getApp() + // 获得登录时间 + const loginTime = app.globalData.loginTime + // 检查是否达到刷新周期 + if(loginTime != null && loginTime + 1000 * 60 < Date.now()) { + app.globalData.loginTime = null + refreshing = true + refresh_token(app) + } + + if(refreshing) { + pendings.push(options) + } else { + do_request(app, options).then(res => { + resolve(res) + }).catch(err => { + reject(err) + }) + } + }) +} +export default request \ No newline at end of file