From a23544b82a31abdfede7654528fe77166e2bc0b8 Mon Sep 17 00:00:00 2001 From: wuxw <928255095@qq.com> Date: Wed, 1 Jan 2020 14:14:02 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=20=E4=B8=80=E6=AE=B5?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E5=90=8Etoken=E5=A4=B1=E6=95=88=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- context/Java110Context.js | 18 ++++++-- factory/HttpFactory.js | 45 +++++++++++++++++++ factory/LoginFactory.js | 17 ++++++- factory/index.js | 4 +- .../applicationKeyLocation.js | 2 +- pages/bindOwner/bindOwner.js | 2 +- pages/complaint/complaint.js | 2 +- pages/viewBindOwner/viewBindOwner.js | 2 +- 8 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 factory/HttpFactory.js diff --git a/context/Java110Context.js b/context/Java110Context.js index 2dbb639..6758a3c 100644 --- a/context/Java110Context.js +++ b/context/Java110Context.js @@ -23,6 +23,15 @@ const getHeaders = function () { cookie: '_java110_token_=' + wx.getStorageSync('token') } } +/** + * http 请求 加入是否登录判断 + */ +const request = function (_reqObj) { + //检查是否登录成功 + factory.login.checkLoginStatus(function () { + wx.request(_reqObj); + }); +} /** * 获取位置 @@ -61,7 +70,7 @@ const _loadArea = function (_level, _parentAreaCode, callBack = (_areaList)=>{}) callBack(areaList); return ; } - wx.request({ + request({ url: constant.url.areaUrl, header: getHeaders(), data: { @@ -121,7 +130,7 @@ const getOwner = function(callBack = (_ownerInfo)=>{}){ if (_ownerInfo){ callBack(_ownerInfo); }else{ - wx.request({ + request({ url: constant.url.queryAppUserBindingOwner, header: getHeaders(), data: { @@ -157,6 +166,8 @@ const getOwner = function(callBack = (_ownerInfo)=>{}){ } + + /** * 获取当前小区信息 */ @@ -176,5 +187,6 @@ module.exports = { _loadArea: _loadArea, getCurrentLocation: getCurrentLocation, getOwner: getOwner, - getCurrentCommunity: getCurrentCommunity + getCurrentCommunity: getCurrentCommunity, + request: request }; \ No newline at end of file diff --git a/factory/HttpFactory.js b/factory/HttpFactory.js new file mode 100644 index 0000000..9af94d9 --- /dev/null +++ b/factory/HttpFactory.js @@ -0,0 +1,45 @@ +/** + * 重新封装 http 请求 + * add by wuxw 2020-01-01 + * + * java110团队 + */ + +class HttpFactory{ + constructor(){} + + getSync(_httpHead,_url,_data){ + return new Promise((resolve, reject) => { + wx.request({ + url: _url, + header: _httpHead, + method: 'GET', + data: _data, + success(res) { + resolve(res) + }, + fail(err) { + reject(err) + } + }); + }); + } + postSync(_httpHead, _url, _data){ + return new Promise((resolve, reject) => { + wx.request({ + url: _url, + header: _httpHead, + method: 'POST', + data: _data, + success(res) { + resolve(res) + }, + fail(err) { + reject(err) + } + }); + }); + } +} + +module.exports = new HttpFactory(); \ No newline at end of file diff --git a/factory/LoginFactory.js b/factory/LoginFactory.js index 400fc05..6559b3e 100644 --- a/factory/LoginFactory.js +++ b/factory/LoginFactory.js @@ -21,7 +21,9 @@ class LoginFactory { checkLoginStatus(callback = () => { }) { let _that = this; let loginFlag = wx.getStorageSync(constant.mapping.LOGIN_FLAG); - if (loginFlag) { + console.log("afterOneHourDate", loginFlag); + let nowDate = new Date(); + if (loginFlag && loginFlag.expireTime > nowDate.getTime()) { // 检查 session_key 是否过期 wx.checkSession({ // session_key 有效(为过期) @@ -92,7 +94,18 @@ class LoginFactory { //that.globalData.userInfo = res.userInfo; console.log(res.userInfo); wx.setStorageSync(constant.mapping.USER_INFO, JSON.stringify(res.userInfo)); - wx.setStorageSync(constant.mapping.LOGIN_FLAG, res.sessionKey); + let date = new Date(); + let year = date.getFullYear(); //获取当前年份 + let mon = date.getMonth(); //获取当前月份 + let da = date.getDate(); //获取当前日 + let h = date.getHours()+1; //获取小时 + let m = date.getMinutes(); //获取分钟 + let s = date.getSeconds(); //获取秒 + console.log("获取过去时间",year, mon, da, h, m, s) + //将时间格式转化为时间戳 + let afterOneHourDate = new Date(year, mon, da, h, m, s); //30s之后的时间 + console.log("afterOneHourDate", afterOneHourDate) + wx.setStorageSync(constant.mapping.LOGIN_FLAG, { sessionKey: res.sessionKey, expireTime: afterOneHourDate.getTime()}); wx.setStorageSync(constant.mapping.TOKEN, res.token); callback(); } else { diff --git a/factory/index.js b/factory/index.js index 5864c44..4eec15c 100644 --- a/factory/index.js +++ b/factory/index.js @@ -10,12 +10,14 @@ const loginFactory = require("LoginFactory.js"); const userFactory = require("UserFactory.js"); const fileFactory = require("FileFactory.js"); const coreFactory = require("CoreFactory.js"); +const httpFactory = require("HttpFactory.js"); module.exports = { login: loginFactory, user: userFactory, file: fileFactory, - core: coreFactory + core: coreFactory, + http: httpFactory } \ No newline at end of file diff --git a/pages/applicationKeyLocation/applicationKeyLocation.js b/pages/applicationKeyLocation/applicationKeyLocation.js index 6857afb..6d65fc8 100644 --- a/pages/applicationKeyLocation/applicationKeyLocation.js +++ b/pages/applicationKeyLocation/applicationKeyLocation.js @@ -83,7 +83,7 @@ Page({ _that.setData({ communityName: _owner.communityName }); - wx.request({ + context.request({ url: constant.url.listOwnerMachines, header: context.getHeaders(), method: "GET", diff --git a/pages/bindOwner/bindOwner.js b/pages/bindOwner/bindOwner.js index 392bd4f..fae38ae 100644 --- a/pages/bindOwner/bindOwner.js +++ b/pages/bindOwner/bindOwner.js @@ -153,7 +153,7 @@ Page({ }) } else { console.log("提交数据", obj); - wx.request({ + context.request({ url: constant.url.appUserBindingOwner, header: context.getHeaders(), method: "POST", diff --git a/pages/complaint/complaint.js b/pages/complaint/complaint.js index 0cf6ef8..ca6c0ef 100644 --- a/pages/complaint/complaint.js +++ b/pages/complaint/complaint.js @@ -157,7 +157,7 @@ Page({ }) } else { console.log("提交数据", obj); - wx.request({ + context.request({ url: constant.url.appUserBindingOwner, header: context.getHeaders(), method: "POST", diff --git a/pages/viewBindOwner/viewBindOwner.js b/pages/viewBindOwner/viewBindOwner.js index e68f334..8631307 100644 --- a/pages/viewBindOwner/viewBindOwner.js +++ b/pages/viewBindOwner/viewBindOwner.js @@ -126,7 +126,7 @@ Page({ }); return ; } - wx.request({ + context.request({ url: constant.url.appUserUnBindingOwner, header: context.getHeaders(), method: "POST",