|
|
@ -1,14 +1,26 @@ |
|
|
|
<script> |
|
|
|
import useGetToken from "@/hooks/user/useGetToken"; |
|
|
|
// import { mapState } from 'vuex'; |
|
|
|
|
|
|
|
export default { |
|
|
|
// computed: { |
|
|
|
// ...mapState(['theme']), |
|
|
|
// }, |
|
|
|
|
|
|
|
// watch: { |
|
|
|
// theme(newTheme) { |
|
|
|
// console.log('newTheme: ', newTheme); |
|
|
|
// if (!newTheme) return; |
|
|
|
// this.loadTheme(); |
|
|
|
// }, |
|
|
|
// }, |
|
|
|
|
|
|
|
async onLaunch(options) { |
|
|
|
// this.loadTheme(); |
|
|
|
console.log('onLaunch options: ', options); |
|
|
|
this.checkNetwork(); // 监听网络状态 |
|
|
|
this.getSystemInfo(); // 获取系统设备信息、 |
|
|
|
this.syncLocalDataToStore(options.query.u); // 将localStorage里的数据同步到store里 |
|
|
|
|
|
|
|
const token = await useGetToken(); |
|
|
|
await this.syncLocalDataToStore(options.query.u); // 将localStorage里的数据同步到store里 |
|
|
|
const token = await this.getToken(); |
|
|
|
if (!token) { |
|
|
|
this.$ui.showToast('获取用户信息失败, 请登录'); |
|
|
|
// TODO: 跳转登录界面 |
|
|
@ -19,15 +31,46 @@ export default { |
|
|
|
}, |
|
|
|
|
|
|
|
methods: { |
|
|
|
// loadTheme() { |
|
|
|
// const path = this.theme.replace('-', '/'); |
|
|
|
// import(`./common/styles/${path}.scss`); |
|
|
|
// }, |
|
|
|
|
|
|
|
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 store里有token 且没过期直接:使用store的token |
|
|
|
return token; |
|
|
|
} else { |
|
|
|
// 2. 根据userId获取token |
|
|
|
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 +78,24 @@ 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 |
|
|
|
} |
|
|
|
|
|
|
|
if (this.$store.state.user.token && localToken) { |
|
|
|
// 同步token |
|
|
|
this.$store.commit('user/setToken', localToken); |
|
|
|
} |
|
|
|
if (this.$store.state.user.tokenExpiredTime && tokenExpiredTime) { // 同步tokenExpiredTime |
|
|
|
if (this.$store.state.user.tokenExpiredTime && tokenExpiredTime) { |
|
|
|
// 同步tokenExpiredTime |
|
|
|
this.$store.commit('user/setTokenExpiredTime', +tokenExpiredTime); |
|
|
|
} |
|
|
|
resolve(); |
|
|
|
} catch (error) { |
|
|
|
reject(error); |
|
|
|
} |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 检查网络状态 设置store里的网络状态变量 |
|
|
@ -93,7 +147,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' }); |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
@ -102,10 +157,11 @@ export default { |
|
|
|
|
|
|
|
<style lang="scss"> |
|
|
|
/*每个页面公共css */ |
|
|
|
@import "@/uni_modules/vk-uview-ui/index.scss"; |
|
|
|
@import '@/uni_modules/vk-uview-ui/index.scss'; |
|
|
|
@import '@/common/styles/iconfont.scss'; |
|
|
|
@import '@/common/styles/app.scss'; |
|
|
|
@import '@/common/styles/tailwind.scss'; |
|
|
|
@import '@/common/styles/theme/index.scss'; |
|
|
|
|
|
|
|
page { |
|
|
|
height: 100%; |
|
|
|