code.vue
4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<template>
<view class="code-page">
<!-- 公共组件-每个页面必须引入 -->
<public-module></public-module>
<view class="login-body">
<view class="login-phone">
<view class="phone-main">
<text class="text">{{ $t('login.phoneLogin') }}</text>
<view class="circleStyle"></view>
</view>
<view class="form-row"><u-input v-model="loginForm.phoneNumber" type="number" :placeholder="$t('login.PleaseEnterPhone')"
border="bottom"></u-input></view>
<view class="form-row">
<u-input type="number" v-model="loginForm.code" :placeholder="$t('login.PleaseEnterCode')" border="bottom">
<template slot="suffix">
<view @click="getVerifyCode" class="verify-code">{{ codeText }}</view>
</template>
</u-input>
</view>
<button class="submit" size="default" @click="onSubmit"><text class="text">{{ $t('login.login') }}</text></button>
<view class="u-flex account-style">
<view class="content" @click="openAccountFunc">{{ $t('login.accountPasswordLogin') }}</view>
</view>
<view class="circleStyleBottom"></view>
</view>
</view>
</view>
</template>
<script>
var clear;
import {mapState,mapMutations,mapActions} from 'vuex';
import {useShowToast,useNavigateTo,useReLaunch} from '@/plugins/utils.js'
import api from '@/api'
export default {
data() {
return {
loginForm: {
phoneNumber: '',
code: ''
},
readonly: false,
codeText:'',
};
},
onShow(){
this.$nextTick(()=>{
uni.setNavigationBarTitle({
title:this.$t('menu.phoneLogin')
})
})
},
onLoad(){
this.codeText = this.$t('login.obtainCode')
},
methods: {
...mapMutations(['setUserInfo']),
...mapActions(['updateBadgeTotal']),
//验证码按钮文字状态
codeCountdownText() {
const _this = this;
this.readonly = true;
this.codeText = this.$t('login.codeAfterSeconds',{number:60});
var s = 60;
clear = setInterval(() => {
s--;
_this.codeText = this.$t('login.codeAfterSeconds',{number:s});
if (s <= 0) {
clearInterval(clear);
_this.codeText = this.$t('login.obtainCode');
_this.readonly = false;
}
}, 1000);
},
//获取验证码
async getVerifyCode() {
const phoneRegular = /^1\d{10}$/;
if (this.readonly) {
// useShowToast('验证码已发送~')
return
}
if (!this.loginForm.phoneNumber) {
return useShowToast(this.$t('login.PleaseEnterPhone'))
}
if (!phoneRegular.test(this.loginForm.phoneNumber)) {
return useShowToast(this.$t('login.phoneFormatFail'))
}
// 获取验证码接口
await api.loginApi.postPhoneCodeApi(this.loginForm.phoneNumber)
this.codeCountdownText(); //开始倒计时
},
async onSubmit() {
const phoneRegular = /^1\d{10}$/;
const verifyCodeReg = /^\d{6}$/;
const validateValue = Object.values(this.loginForm)
if (!validateValue[0]) return uni.$u.toast(this.$t('login.PleaseEnterPhone'));
if (!validateValue[1]) return uni.$u.toast(this.$t('login.PleaseEnterCode'));
if (!phoneRegular.test(validateValue[0])) return uni.$u.toast(this.$t('login.phoneFormatFail'));
if (!verifyCodeReg.test(validateValue[1])) return uni.$u.toast(this.$t('login.codeFormatFail'));
const res = await api.loginApi.postPhoneLoginApi(this.loginForm)
if (res) {
// 储存登录信息
let tokenInfo = {
refreshToken: res.refreshToken,
isToken: res.token
};
let userInfo = {
...tokenInfo,
token: true, //token用于判断是否登录
isThirdLogin: false
};
if (userInfo.token) {
this.setUserInfo(userInfo);
}
useShowToast(this.$t('login.loginSuccess')).then(async (res) => {
this.saveUserInfo();
await this.getAlarmTotalData();
useReLaunch("/pages/index/index")
});
}
},
async getAlarmTotalData() {
const res = await await api.loginApi.getAlarmTotalApi()
if (!res) return
//异步实时更新告警徽标数
this.updateBadgeTotal(res.totalAlarm?.activedAlarm);
},
async saveUserInfo() {
//储存个人信息
const res = await api.loginApi.setUserInfoApi()
if (!res) return
this.setUserInfo(res);
},
openAccountFunc() {
useNavigateTo('../public/login')
}
}
};
</script>
<style lang="scss" scoped>
@import './static/code.scss';
</style>