Browse Source

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

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

80
App.vue

@ -1,14 +1,11 @@
<script> <script>
import useGetToken from "@/hooks/user/useGetToken";
export default { export default {
async onLaunch(options) { async onLaunch(options) {
console.log('onLaunch options: ', options); console.log('onLaunch options: ', options);
this.checkNetwork(); // this.checkNetwork(); //
this.getSystemInfo(); // this.getSystemInfo(); //
this.syncLocalDataToStore(options.query.u); // localStoragestore await this.syncLocalDataToStore(options.query.u); // localStoragestore
const token = await this.getToken();
const token = await useGetToken();
if (!token) { if (!token) {
this.$ui.showToast('获取用户信息失败, 请登录'); this.$ui.showToast('获取用户信息失败, 请登录');
// TODO: // TODO:
@ -19,29 +16,65 @@ export default {
}, },
methods: { 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里 * 将localStorage里的数据同步到store里
* user, token, tokenExpiredTime * user, token, tokenExpiredTime
*/ */
syncLocalDataToStore(urlUserId) { syncLocalDataToStore(urlUserId) {
const localUser = uni.$storage.getStorageSync('user'); return new Promise((resolve, reject) => {
const localToken = uni.$storage.getStorageSync('anyringToken'); try {
const tokenExpiredTime = uni.$storage.getStorageSync('tokenExpiredTime'); const localUser = uni.$storage.getStorageSync('user');
if (!this.$store.state.user.user && localUser) { const localToken = uni.$storage.getStorageSync('anyringToken');
// user const tokenExpiredTime = uni.$storage.getStorageSync('tokenExpiredTime');
const user = JSON.parse(localUser); if (!this.$store.state.user.user) {
if (!urlUserId || user.id === urlUserId) { if (localUser) {
this.$store.commit('user/setUser', user); // user
} else { const user = JSON.parse(localUser);
this.$store.commit('user/setUser', { id: urlUserId }); if (!urlUserId || user.id === urlUserId) {
this.$store.commit('user/setUser', user);
} 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);
} }
}
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);
}
}, },
// store // store
@ -93,7 +126,8 @@ export default {
*/ */
async noPhone(phone) { async noPhone(phone) {
if (!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) - | 时间轴接口 | [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) - | 时间轴页面 | [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) - | 更新代码 | [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) - | 项目列表 | [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) - | 项目操作面板 | [3beb05e](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/3beb05e)
@ -20,6 +21,7 @@
范围|描述|commitId 范围|描述|commitId
--|--|-- --|--|--
- | calender格式及细节调整 | [db9602b](https://101.201.226.163:50022/TALL/TALL-MUI-4/commits/db9602b) - | 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" border-radius="25"
margin="0" margin="0"
> >
<view slot="body"> <template v-slot:body>
<scroll-view :scrollY="true" :style="{ 'max-height': globalsHeight - 30 + 'px' }"> <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> <skeleton :banner="false" :loading="!globals.length" :row="4" animate class="u-line-2 skeleton"></skeleton>
<view class="grid gap-2"> <view class="grid gap-2">
@ -32,7 +32,7 @@
</block> </block>
</view> </view>
</scroll-view> </scroll-view>
</view> </template>
</u-card> </u-card>
</view> </view>
</template> </template>
@ -41,11 +41,12 @@
import { computed } from 'vue'; import { computed } from 'vue';
import { useStore } from 'vuex'; import { useStore } from 'vuex';
const sysHeight = uni.getSystemInfoSync().screenHeight; //
const globalsHeight = Math.floor(((sysHeight - 44 - 30 - 10) / 5) * 4); //
const store = useStore(); const store = useStore();
const isShrink = computed(() => store.state.task.isShrink); const isShrink = computed(() => store.state.task.isShrink); //
const sysHeight = uni.getSystemInfoSync().screenHeight;
const globals = computed(() => store.getters['task/globals']); const globals = computed(() => store.getters['task/globals']);
const globalsHeight = computed(() => [((sysHeight.value - 44 - 30 - 10) / 5) * 4]); console.log('globals: ', globals.value);
// //
function openCard() { function openCard() {

1
hooks/user/useGetToken.js

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

82
pages/index/index.vue

@ -1,21 +1,19 @@
<template> <template>
<!-- <view class="flex flex-col h-full bg-gray-50" @click="openAuth"> --> <!-- <view class="flex flex-col h-full bg-gray-50" @click="openAuth"> -->
<view class="flex flex-col h-full bg-gray-50"> <view class="flex flex-col h-full bg-gray-50">
<view class="relative" @touchmove="onMove"> <view class="relative" @touchmove="onMove">
<!-- 日历 --> <!-- 日历 -->
<Calendar @selected-change="onDateChange" :show-back="true" ref="calendar" <Calendar @selected-change="onDateChange" :show-back="true" ref="calendar" @handleFindPoint="handleFindPoint" />
@handleFindPoint="handleFindPoint" <!-- 上传 导入wbs -->
/> <Upload @success="onUploadSuccess" @error="onUploadError" />
<!-- 上传 导入wbs --> </view>
<Upload @success="onUploadSuccess" @error="onUploadError" />
</view> <!-- 项目列表 -->
<Projects @getProjects="getProjects" class="flex-1 overflow-y-auto" />
<!-- 项目列表 -->
<Projects @getProjects="getProjects" class="flex-1 overflow-y-auto" /> <!-- 全局提示框 -->
<u-top-tips ref="uTips"></u-top-tips>
<!-- 全局提示框 --> </view>
<u-top-tips ref="uTips"></u-top-tips>
</view>
</template> </template>
<script setup> <script setup>
@ -109,29 +107,29 @@ function onMove(event) {
</script> </script>
<style> <style>
.content { .content {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
} }
.logo { .logo {
height: 200rpx; height: 200rpx;
width: 200rpx; width: 200rpx;
margin-top: 200rpx; margin-top: 200rpx;
margin-left: auto; margin-left: auto;
margin-right: auto; margin-right: auto;
margin-bottom: 50rpx; margin-bottom: 50rpx;
} }
.text-area { .text-area {
display: flex; display: flex;
justify-content: center; justify-content: center;
} }
.title { .title {
font-size: 36rpx; font-size: 36rpx;
color: #8f8f94; color: #8f8f94;
} }
</style> </style>

Loading…
Cancel
Save