Browse Source

精简代码&优化逻辑

master
zmt 3 years ago
parent
commit
1bbbdf567b
  1. 35
      nxTemp/store/modules/user.js
  2. 24
      nxTemp/utils/tools.js
  3. 33
      pages/index/index.vue
  4. 8
      pages/login/login.vue

35
nxTemp/store/modules/user.js

@ -14,20 +14,17 @@ export const state = {
} }
export const actions = { export const actions = {
async setUserData({
state,
commit
}, data) {
async setUserData({state,commit}, data) {
commit('setStateAttr', { commit('setStateAttr', {
key: 'userInfo', key: 'userInfo',
val: data val: data
}) })
uni.setStorageSync('userInfo', data);
uni.setStorageSync('userInfo', data);
state.token = data.token
uni.setStorageSync('token', data.token);
}, },
// 登录过期 重新登录 // 登录过期 重新登录
reLogin({
commit
}, info) {
reLogin({commit}, info) {
commit('setStateAttr', { commit('setStateAttr', {
key: 'token', key: 'token',
val: '' val: ''
@ -37,7 +34,27 @@ export const actions = {
router.push({ router.push({
path: '/pages/login/login' path: '/pages/login/login'
}); });
}
},
//预留,已将token更新整合到setUserData,后面可能有单独更新token的必要
setUserToken({commit},token){
commit('setStateAttr', {
key: 'token',
val: token
})
uni.setStorageSync("token", token);
},
//清空登录信息
logoutUser({commit}){
commit('setStateAttr', {
key: 'token',
val: ''
})
uni.setStorageSync("token", '');
uni.setStorageSync("userInfo", '');
// router.push({
// path: '/pages/index/index'
// });
},
} }
export const mutations = { export const mutations = {

24
nxTemp/utils/tools.js

@ -31,6 +31,30 @@ export default {
}, },
/** /**
* @description: 封装弹窗交互
* @param {String} msg 提示信息
* @param {Function} succ 点确认执行函数
* @return {Promise}
*/
showModal(msg,succ){
let that = this
return new Promise((rs,rj)=>{
uni.showModal({
title: '温馨提示',
content: msg,
success: function (res) {
if (res.confirm) {
succ()
rs(true)
} else if (res.cancel) {
that.showNone("已取消")
rj(false)
}
}
});
})
},
/**
* @description: 弹窗封装 * @description: 弹窗封装
* @param {String} msg * @param {String} msg
* @return: {*} * @return: {*}

33
pages/index/index.vue

@ -4,7 +4,7 @@
<view class="login-box flex_col flex_start"> <view class="login-box flex_col flex_start">
<image class="bg" src="../../static/images/index/index_bg.png" mode="aspectFit"></image> <image class="bg" src="../../static/images/index/index_bg.png" mode="aspectFit"></image>
<view class="login-info flex_col" v-if="login">
<view class="login-info flex_col" v-if="hasLogin">
<text>{{userInfo.mobile||"-"}}</text> <text>{{userInfo.mobile||"-"}}</text>
<text>您好!</text> <text>您好!</text>
</view> </view>
@ -20,11 +20,11 @@
<view class="item flex_row flex_start bg-white padding solid-bottom" @click="clickEvent"> <view class="item flex_row flex_start bg-white padding solid-bottom" @click="clickEvent">
<image src="../../static/images/index/index_cup.png" mode=""></image> <image src="../../static/images/index/index_cup.png" mode=""></image>
<view class="text-m text-left">活动赛事{{hasLogin}}+{{userInfo.mobile}}</view>
<view class="text-m text-left">活动赛事</view>
<image src="../../static/images/index/arrow_right.png" mode=""></image> <image src="../../static/images/index/arrow_right.png" mode=""></image>
</view> </view>
<view class="item flex_row flex_start bg-white padding solid-bottom" v-if="login" @click="login=!login">
<view class="item flex_row flex_start bg-white padding solid-bottom" v-if="hasLogin" @click="logout">
<image src="../../static/images/index/index_logout.png" mode=""></image> <image src="../../static/images/index/index_logout.png" mode=""></image>
<view class="text-m text-left" >退出登录</view> <view class="text-m text-left" >退出登录</view>
<image src="../../static/images/index/arrow_right.png" mode=""></image> <image src="../../static/images/index/arrow_right.png" mode=""></image>
@ -43,9 +43,6 @@
computed:{ computed:{
...mapState(['userInfo']), ...mapState(['userInfo']),
...mapGetters(['hasLogin']), ...mapGetters(['hasLogin']),
// hasLogin(){
// return this.$store.getters.hasLogin
// }
}, },
onLoad(parms) { onLoad(parms) {
@ -54,26 +51,16 @@
}, },
methods: { methods: {
...mapActions(['getUserInfo']),
...mapActions(['getUserInfo','logoutUser']),
jumpPage(name){ jumpPage(name){
this.$Router.push({name:name}) this.$Router.push({name:name})
// this.$Router.pushTab('/pages/login/login')
}, },
clickEvent(){
let that = this
if(this.login)this.jumpPage('eventList');
else {
uni.showModal({
title: '提示',
content: '需要登录后才能查看,是否跳转去登录界面?',
success: function (res) {
if (res.confirm) {
that.jumpPage('login');
} else if (res.cancel) {
}
}
});
}
async clickEvent(){
if(this.hasLogin)this.jumpPage('eventList');
else await this.$tools.showModal("该功能需要登录后才能查看,是否跳转去登录界面?",()=>{this.jumpPage('login')})
},
async logout(){
await this.$tools.showModal("真的要退出登录?",this.logoutUser)
}, },
} }
} }

8
pages/login/login.vue

@ -50,7 +50,8 @@
//,page //,page
import { import {
postLogin,postSendSMS,wechatGetPhoneNumber postLogin,postSendSMS,wechatGetPhoneNumber
} from "@/nxTemp/apis/userAPI.js"
} from "@/nxTemp/apis/userAPI.js"
import {mapActions} from 'vuex'
export default { export default {
data() { data() {
return { return {
@ -71,7 +72,8 @@
}; };
}, },
methods: {
methods: {
...mapActions(["setUserData"]),
bindLogin() { bindLogin() {
if (this.login.mobile.length != 11) { if (this.login.mobile.length != 11) {
return this.$tools.showNone("请输入正确的手机号");} return this.$tools.showNone("请输入正确的手机号");}
@ -79,7 +81,7 @@
return this.$tools.showNone("请输入正确的验证码");} return this.$tools.showNone("请输入正确的验证码");}
postLogin(this.login).then(res => { postLogin(this.login).then(res => {
this.$tools.showNone("登录成功"); this.$tools.showNone("登录成功");
this.$store.dispatch('setUserData', res);
this.setUserData(res)
this.$Router.replace({name:"index"}) this.$Router.replace({name:"index"})
}); });
}, },

Loading…
Cancel
Save