From 8f16ae1c9d6c28788cb96d96dd052b153b3526dd Mon Sep 17 00:00:00 2001 From: xuesinan <1404152492@qq.com> Date: Mon, 17 Jan 2022 18:14:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20token=E8=BF=87=E6=9C=9F=E7=AD=96?= =?UTF-8?q?=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.vue | 12 ++++++------ CHANGELOG.md | 1 + apis/tall.js | 12 +++++++----- store/user/actions.js | 14 ++++++++++++++ utils/request.js | 20 +++++++++++++++++--- 5 files changed, 45 insertions(+), 14 deletions(-) diff --git a/App.vue b/App.vue index b6bf46d..525fde7 100644 --- a/App.vue +++ b/App.vue @@ -18,10 +18,10 @@ export default { this.checkNetwork(); // 监听网络状态 this.getSystemInfo(); // 获取系统设备信息、 - // const token = this.$store.state.user.token || this.$storage.getStorageSync('anyringToken') || ''; - // if (token) { - // this.$store.commit('user/setToken', token); - // } + const token = this.$store.state.user.token || this.$storage.getStorageSync('anyringToken') || ''; + if (token) { + this.$store.commit('user/setToken', token); + } // if (!token) { // this.$ui.showToast('获取用户信息失败, 请登录'); @@ -29,8 +29,8 @@ export default { // return; // } - await this.syncLocalDataToStore('1217647686598135808'); // 将localStorage里的数据同步到store里 - const token = await this.getToken(); + // await this.syncLocalDataToStore('1217647686598135808'); // 将localStorage里的数据同步到store里 + // const token = await this.getToken(); // this.noPhone(this.$store.state.user.phone); this.$store.dispatch('socket/initSocket'); diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c9de93..8eb512f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ - | 项目列表 | [a52e6d5](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/a52e6d5) - | 项目列表新 | [88cf48d](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/88cf48d) - | 账户名密码登录 | [ebf456e](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/ebf456e) + - | 主体颜色 | [bb5c0e3](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/bb5c0e3) - | 注册、用户协议 | [68e9189](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/68e9189) - | app.vue | [970cf9a](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/970cf9a) - | first commit | [8dc26de](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/8dc26de) diff --git a/apis/tall.js b/apis/tall.js index 718e84b..1a12595 100644 --- a/apis/tall.js +++ b/apis/tall.js @@ -1,9 +1,9 @@ import Config from '@/common/js/config.js' -const apiUrl = Config.apiUrl; -// const apiUrl = Config.apiUrlNew; -const tall = `${apiUrl}/tall3/v3.0`; -// const tall = `${apiUrl}/ptostall`; +// const apiUrl = Config.apiUrl; +const apiUrl = Config.apiUrlNew; +// const tall = `${apiUrl}/tall3/v3.0`; +const tall = `${apiUrl}/ptostall`; export function setupTall(app) { uni.$u.api = { ...uni.$u.api } || {}; @@ -14,7 +14,9 @@ export function setupTall(app) { // 获取图片验证码 uni.$u.api.getImageCode = () => uni.$u.get(`${tall}/users/code`); // 获取短信验证码 - uni.$u.api.getSmsCode = params => uni.$u.get(`${tall}/users/smscode`, params); + uni.$u.api.getSmsCode = params => uni.$u.get(`${tall}/users/smscode`, params); + // 根据refreshToken重新获取token + uni.$u.api.getNewToken = refreshToken => uni.$u.get(`${tall}/users/refreshToken`, { refreshToken }); // 根据userId获取token uni.$u.api.getToken = userId => uni.$u.get(`${tall}/users/userId`, { userId }); // 绑定手机号 diff --git a/store/user/actions.js b/store/user/actions.js index 52a1c24..e238ae7 100644 --- a/store/user/actions.js +++ b/store/user/actions.js @@ -33,6 +33,20 @@ const actions = { throw error; } }, + + /** + * 根据refreshToken重新获取token + * @param {string} refreshToken + */ + async getTokenByRefreshToken({ commit }, refreshToken) { + try { + const res = await uni.$u.api.getNewToken(refreshToken); + return res; + } catch (error) { + uni.$ui.showToast(error.msg); + throw error; + } + }, }; export default actions; diff --git a/utils/request.js b/utils/request.js index 4a75fd8..7606bcc 100644 --- a/utils/request.js +++ b/utils/request.js @@ -37,15 +37,29 @@ export function setupHttp(app) { // res为服务端返回值,可能有code,result等字段 // 这里对res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到 // 如果配置了originalData为true,请留意这里的返回值 + if (res.tokenObj.token) { - storage.setStorageSync('anyringToken', res.tokenObj.token || ''); + storage.setStorageSync('anyringToken', res.tokenObj.token || ''); + storage.setStorageSync('refreshToken', res.tokenObj.refreshToken || ''); store.commit('user/setToken', res.tokenObj.token); } return res.data; - } else if (res.code === 401) { + } else if (res.code === 400) { // deviceId为空 + ui.showToast(res.msg); + return false; + } else if (res.code === 49) { // token过期 + let refreshToken = storage.getStorageSync('refreshToken'); + store.dispatch('user/getTokenByRefreshToken', refreshToken); + return false; + } else if (res.code === 401) { // refreshToken过期 // 假设201为token失效,这里跳转登录 - ui.showToast('验证失败,请重新登录'); + // ui.showToast('验证失败,请重新登录'); + ui.showToast(res.msg); + // storage.setStorageSync('anyringToken', ''); + // storage.setStorageSync('refreshToken', ''); + // store.commit('user/setToken', ' '); + setTimeout(() => { // 此为uView的方法,详见路由相关文档 uni.navigateTo({