Browse Source

feat: token过期策略

test2
xuesinan 4 years ago
parent
commit
8f16ae1c9d
  1. 12
      App.vue
  2. 1
      CHANGELOG.md
  3. 12
      apis/tall.js
  4. 14
      store/user/actions.js
  5. 20
      utils/request.js

12
App.vue

@ -18,10 +18,10 @@ export default {
this.checkNetwork(); // this.checkNetwork(); //
this.getSystemInfo(); // this.getSystemInfo(); //
// const token = this.$store.state.user.token || this.$storage.getStorageSync('anyringToken') || ''; const token = this.$store.state.user.token || this.$storage.getStorageSync('anyringToken') || '';
// if (token) { if (token) {
// this.$store.commit('user/setToken', token); this.$store.commit('user/setToken', token);
// } }
// if (!token) { // if (!token) {
// this.$ui.showToast(', '); // this.$ui.showToast(', ');
@ -29,8 +29,8 @@ export default {
// return; // return;
// } // }
await this.syncLocalDataToStore('1217647686598135808'); // localStoragestore // await this.syncLocalDataToStore('1217647686598135808'); // localStoragestore
const token = await this.getToken(); // const token = await this.getToken();
// this.noPhone(this.$store.state.user.phone); // this.noPhone(this.$store.state.user.phone);
this.$store.dispatch('socket/initSocket'); this.$store.dispatch('socket/initSocket');

1
CHANGELOG.md

@ -27,6 +27,7 @@
- | 项目列表 | [a52e6d5](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/a52e6d5) - | 项目列表 | [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) - | 项目列表新 | [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) - | 账户名密码登录 | [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) - | 注册、用户协议 | [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) - | 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) - | first commit | [8dc26de](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/8dc26de)

12
apis/tall.js

@ -1,9 +1,9 @@
import Config from '@/common/js/config.js' import Config from '@/common/js/config.js'
const apiUrl = Config.apiUrl; // const apiUrl = Config.apiUrl;
// const apiUrl = Config.apiUrlNew; const apiUrl = Config.apiUrlNew;
const tall = `${apiUrl}/tall3/v3.0`; // const tall = `${apiUrl}/tall3/v3.0`;
// const tall = `${apiUrl}/ptostall`; const tall = `${apiUrl}/ptostall`;
export function setupTall(app) { export function setupTall(app) {
uni.$u.api = { ...uni.$u.api } || {}; 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.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 // 根据userId获取token
uni.$u.api.getToken = userId => uni.$u.get(`${tall}/users/userId`, { userId }); uni.$u.api.getToken = userId => uni.$u.get(`${tall}/users/userId`, { userId });
// 绑定手机号 // 绑定手机号

14
store/user/actions.js

@ -33,6 +33,20 @@ const actions = {
throw error; 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; export default actions;

20
utils/request.js

@ -37,15 +37,29 @@ export function setupHttp(app) {
// res为服务端返回值,可能有code,result等字段 // res为服务端返回值,可能有code,result等字段
// 这里对res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到 // 这里对res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到
// 如果配置了originalData为true,请留意这里的返回值 // 如果配置了originalData为true,请留意这里的返回值
if (res.tokenObj.token) { 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); store.commit('user/setToken', res.tokenObj.token);
} }
return res.data; 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失效,这里跳转登录 // 假设201为token失效,这里跳转登录
ui.showToast('验证失败,请重新登录'); // ui.showToast('验证失败,请重新登录');
ui.showToast(res.msg);
// storage.setStorageSync('anyringToken', '');
// storage.setStorageSync('refreshToken', '');
// store.commit('user/setToken', ' ');
setTimeout(() => { setTimeout(() => {
// 此为uView的方法,详见路由相关文档 // 此为uView的方法,详见路由相关文档
uni.navigateTo({ uni.navigateTo({

Loading…
Cancel
Save