Commit cdf251ba authored by Damon's avatar Damon

feat: 登录流程

parent 9497f9d6
...@@ -3,6 +3,8 @@ export default { ...@@ -3,6 +3,8 @@ export default {
onLaunch: function () { onLaunch: function () {
// 版本更新 // 版本更新
this.updateApp() this.updateApp()
// 检查登录是否已过期
this.checkLogin()
}, },
onShow: function () { onShow: function () {
console.log('App Show') console.log('App Show')
...@@ -11,6 +13,27 @@ export default { ...@@ -11,6 +13,27 @@ export default {
console.log('App Hide') console.log('App Hide')
}, },
methods: { methods: {
checkLogin() {
if (this.vuex_token) {
uni.checkSession({
success: (result) => {
//session_key 未过期,并且在本生命周期一直有效
console.log("session未过期", result)
},
fail: (error) => {
// session_key 已经失效,需要重新执行登录流程
console.log("error", error)
this.$u.route({
url: 'pages/login/index'
})
}
})
} else {
this.$u.route({
url: 'pages/login/index'
})
}
},
/* 版本更新 */ /* 版本更新 */
updateApp() { updateApp() {
if (uni.canIUse('getUpdateManager')) { if (uni.canIUse('getUpdateManager')) {
......
<template> <template>
<view> <view>
<view class="u-flex user-box u-p-l-30 u-p-t-30 u-p-r-20 u-p-b-30"> <view class="u-flex user-box u-p-l-30 u-p-t-30 u-p-r-20 u-p-b-30" @tap="login">
<view class="u-m-r-10"> <view class="u-m-r-10">
<view class="avatar"> <u-avatar :src="avatar" size="140"></u-avatar>
<open-data type="userAvatarUrl"></open-data>
</view>
</view> </view>
<view class="u-flex-1"> <view class="u-flex-1">
<view class="u-font-18 u-p-b-20"> <view class="u-font-18 u-p-b-20">{{ nikeName }}</view>
<open-data type="userNickName"></open-data>
</view>
<view class="u-font-14 u-tips-color">累计刷题俩999次</view> <view class="u-font-14 u-tips-color">累计刷题俩999次</view>
</view> </view>
</view> </view>
<view class="u-m-t-20"> <view class="u-m-t-20">
<u-cell-group> <u-cell-group>
<u-cell-item icon="star" title="个人信息" value="完善度40%" @tap="login"></u-cell-item> <u-cell-item icon="star" title="个人信息" value="完善度40%"></u-cell-item>
<u-cell-item icon="photo" title="我的题库"></u-cell-item> <u-cell-item icon="photo" title="我的题库"></u-cell-item>
<u-cell-item icon="coupon" title="联系客服"></u-cell-item> <u-cell-item icon="coupon" title="联系客服"></u-cell-item>
<u-cell-item icon="coupon" title="退出登录" @tap="logout"></u-cell-item>
</u-cell-group> </u-cell-group>
</view> </view>
</view> </view>
</template> </template>
<script> <script>
export default { export default {
data() { data() {
return { return {
pic: 'https://uviewui.com/common/logo.png', pic:'https://uviewui.com/common/logo.png',
show: true, show:true
} }
}, },
computed: {}, computed: {
onLoad() {}, avatar() {
return this.userInfo.avatarUrl || this.pic
},
nikeName() {
return this.userInfo.nickName || '未登录'
}
},
onLoad() {
},
methods: { methods: {
login() { login() {
this.$u.route({ this.$u.route({
url: 'pages/login/index', url: 'pages/login/index'
}) })
}, },
}, logout() {
} this.$u.vuex('vuex_token', '')
this.$u.vuex('userInfo', {})
}
}
}
</script> </script>
<style lang="scss"> <style lang="scss">
page { page{
background-color: #ededed; background-color: #ededed;
} }
.camera { .camera{
width: 54px; width: 54px;
height: 44px; height: 44px;
&:active { &:active{
background-color: #ededed; background-color: #ededed;
} }
} }
.user-box { .user-box{
background-color: #fff; background-color: #fff;
.avatar {
width: 120rpx;
height: 120rpx;
}
} }
</style> </style>
\ No newline at end of file
...@@ -18,22 +18,33 @@ export default { ...@@ -18,22 +18,33 @@ export default {
} }
}, },
onShow() { onShow() {
console.log("this", this) this.checkLogin()
}, },
methods: { methods: {
checkLogin() {
if (this.vuex_token) {
uni.checkSession({
success: (result) => {
this.$u.route({ type: 'back' })
}
})
}
},
wxLogin() { wxLogin() {
// 获取code 小程序专有,用户登录凭证。 // 获取code 小程序专有,用户登录凭证。
uni.getUserProfile({ uni.getUserProfile({
desc: '获取用户基本资料', desc: '获取用户基本资料',
lang: 'zh_CN', lang: 'zh_CN',
success: (user) => { success: (user) => {
//获取成功基本资料后开启登录,因为基本资料首先要授权 // 获取成功基本资料后开启登录,因为基本资料首先要授权
uni.login({ uni.login({
provider: 'weixin', provider: 'weixin',
success: (code_res) => { success: (code_res) => {
if (code_res.errMsg == 'login:ok') { if (code_res.errMsg == 'login:ok') {
let code = code_res.code let code = code_res.code
console.log('user, code', user, code) console.log('user, code', user, code)
this.$u.vuex('vuex_token', code)
this.$u.vuex('userInfo', user.userInfo)
this.$u.route({ this.$u.route({
type: 'back' type: 'back'
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment