Browse Source

fix(app.vue): 修复获取token报错的问题

develop
wally 4 years ago
parent
commit
9120d541a0
  1. 48
      App.vue
  2. 2
      CHANGELOG.md
  3. 11
      components/Globals/Globals.vue
  4. 1
      hooks/user/useGetToken.js
  5. 20
      pages/index/index.vue

48
App.vue

@ -1,14 +1,11 @@
<script>
import useGetToken from "@/hooks/user/useGetToken";
export default {
async onLaunch(options) {
console.log('onLaunch options: ', options);
this.checkNetwork(); //
this.getSystemInfo(); //
this.syncLocalDataToStore(options.query.u); // localStoragestore
const token = await useGetToken();
await this.syncLocalDataToStore(options.query.u); // localStoragestore
const token = await this.getToken();
if (!token) {
this.$ui.showToast('获取用户信息失败, 请登录');
// TODO:
@ -19,15 +16,41 @@ export default {
},
methods: {
async getToken() {
const { token } = this.$store.state.user;
const tokenIsAvailable = this.$store.getters['user/tokenIsAvailable'];
const userId = this.$store.getters['user/userId'];
if (token && tokenIsAvailable) {
// 1.1 storetoken 使storetoken
return token;
} else {
// 2. userIdtoken
if (userId) {
try {
const { token } = await this.$store.dispatch('user/getTokenByUserId', userId);
return token;
} catch (error) {
console.error('error: ', error);
return null;
}
} else {
return null;
}
}
},
/**
* 将localStorage里的数据同步到store里
* user, token, tokenExpiredTime
*/
syncLocalDataToStore(urlUserId) {
return new Promise((resolve, reject) => {
try {
const localUser = uni.$storage.getStorageSync('user');
const localToken = uni.$storage.getStorageSync('anyringToken');
const tokenExpiredTime = uni.$storage.getStorageSync('tokenExpiredTime');
if (!this.$store.state.user.user && localUser) {
if (!this.$store.state.user.user) {
if (localUser) {
// user
const user = JSON.parse(localUser);
if (!urlUserId || user.id === urlUserId) {
@ -35,13 +58,23 @@ export default {
} else {
this.$store.commit('user/setUser', { id: urlUserId });
}
} else {
this.$store.commit('user/setUser', { id: urlUserId });
}
}
if (this.$store.state.user.token && localToken) { // token
this.$store.commit('user/setToken', localToken);
}
if (this.$store.state.user.tokenExpiredTime && tokenExpiredTime) { // tokenExpiredTime
this.$store.commit('user/setTokenExpiredTime', +tokenExpiredTime);
}
resolve();
} catch (error) {
reject(error);
}
})
},
// store
@ -93,7 +126,8 @@ export default {
*/
async noPhone(phone) {
if (!phone) {
uni.navigateTo({ title: '/pages/phone-bind/phone-bind' });
// TODO:
// uni.navigateTo({ url: '/pages/phone-bind/phone-bind' });
}
},
},

2
CHANGELOG.md

@ -12,6 +12,7 @@
- | 时间轴接口 | [a95d005](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/a95d005)
- | 时间轴页面 | [e926b75](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/e926b75)
- | 更新代码 | [392c8cc](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/392c8cc)
- | 添加 timeline | [72dad2b](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/72dad2b)
- | 项目列表 | [a52e6d5](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/a52e6d5)
- | 项目操作面板 | [3beb05e](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/3beb05e)
@ -20,6 +21,7 @@
范围|描述|commitId
--|--|--
- | calender格式及细节调整 | [db9602b](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/db9602b)
- | 细节调整 | [bdd5f87](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/bdd5f87)
### 🔨 代码重构

11
components/Globals/Globals.vue

@ -8,7 +8,7 @@
border-radius="25"
margin="0"
>
<view slot="body">
<template v-slot:body>
<scroll-view :scrollY="true" :style="{ 'max-height': globalsHeight - 30 + 'px' }">
<skeleton :banner="false" :loading="!globals.length" :row="4" animate class="u-line-2 skeleton"></skeleton>
<view class="grid gap-2">
@ -32,7 +32,7 @@
</block>
</view>
</scroll-view>
</view>
</template>
</u-card>
</view>
</template>
@ -41,11 +41,12 @@
import { computed } from 'vue';
import { useStore } from 'vuex';
const sysHeight = uni.getSystemInfoSync().screenHeight; //
const globalsHeight = Math.floor(((sysHeight - 44 - 30 - 10) / 5) * 4); //
const store = useStore();
const isShrink = computed(() => store.state.task.isShrink);
const sysHeight = uni.getSystemInfoSync().screenHeight;
const isShrink = computed(() => store.state.task.isShrink); //
const globals = computed(() => store.getters['task/globals']);
const globalsHeight = computed(() => [((sysHeight.value - 44 - 30 - 10) / 5) * 4]);
console.log('globals: ', globals.value);
//
function openCard() {

1
hooks/user/useGetToken.js

@ -16,6 +16,7 @@ export default async function useGetToken() {
const token = computed(() => store.state.user.token);
const tokenIsAvailable = computed(() => store.getters['user/tokenIsAvailable']); // token是否可用
const userId = computed(() => store.getters['user/userId']);
debugger;
if (token.value && tokenIsAvailable.value) {
// 1.1 store里有token 且没过期直接:使用store的token
return token.value;

20
pages/index/index.vue

@ -3,9 +3,7 @@
<view class="flex flex-col h-full bg-gray-50">
<view class="relative" @touchmove="onMove">
<!-- 日历 -->
<Calendar @selected-change="onDateChange" :show-back="true" ref="calendar"
@handleFindPoint="handleFindPoint"
/>
<Calendar @selected-change="onDateChange" :show-back="true" ref="calendar" @handleFindPoint="handleFindPoint" />
<!-- 上传 导入wbs -->
<Upload @success="onUploadSuccess" @error="onUploadError" />
</view>
@ -109,29 +107,29 @@ function onMove(event) {
</script>
<style>
.content {
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
}
.logo {
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
}
.text-area {
.text-area {
display: flex;
justify-content: center;
}
}
.title {
.title {
font-size: 36rpx;
color: #8f8f94;
}
}
</style>

Loading…
Cancel
Save