index.vue
4.01 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!-- 手机短信登录页 -->
<template>
<view class="loginBox">
<view class="tit">
<text>手机号登录</text>
<text class="text" @click="goLogin">
账号登录
<icon></icon>
</text>
</view>
<!-- 登录表单 手机号、验证码 -->
<view class="loginMain">
<uni-forms ref="customForm" :rules="customRules" :modelValue="fromInfo">
<uni-forms-item name="phone"
><uni-easyinput
class="item"
v-model="fromInfo.phone"
placeholder="请输入手机号"
/></uni-forms-item>
<uni-forms-item name="verifyCode">
<uni-easyinput
class="item inputW"
v-model="fromInfo.verifyCode"
placeholder="请输入验证码"
/>
<view class="codeBox">
<text class="code" v-show="codeInfo.show" @click="getCode"
>获取验证码</text
>
<text class="code fontCol" v-show="!codeInfo.show"
>{{ codeInfo.times }}s后重新获取</text
>
</view>
</uni-forms-item>
</uni-forms>
<!-- 按钮 -->
<view class="btnBox">
<button
class="btn-default"
:disabled="
fromInfo.phone.length === 0 || fromInfo.verifyCode.length === 0
"
:class="
fromInfo.phone.length === 0 || fromInfo.verifyCode.length === 0
? 'disabled'
: ''
"
type="primary"
@click="handleSubmit"
>
登录
</button>
</view>
<!-- end -->
</view>
<!-- end -->
</view>
</template>
<script setup>
import {onMounted, reactive, ref} from "vue";
import {useStore} from "vuex";
// 接口
import {phoneLogins} from "@/api/user.js";
// 验证
import {isPhone, timeCountdown, validatePhone} from "@/utils/validate";
// ------定义变量------
const store = useStore(); //vuex获取储存数据
const customForm = ref();
let isVerifySuccess = ref(false);
// 表单数据
let fromInfo = reactive({
phone: "", //手机号
verifyCode: "", // 验证码
});
// 验证码倒计时数据
let codeInfo = reactive({
show: true,
timer: null,
times: 60,
});
// 表单校验
const customRules = reactive({
phone: {
rules: [
{
required: true,
validateFunction: validatePhone,
errorMessage: "请输入手机号",
},
],
},
verifyCode: {
rules: [
{
required: true,
errorMessage: "请输入验证码",
},
],
},
});
// ------生命周期------
onMounted(() => {});
// ------定义方法------
// 获取验证码
const getCode = async () => {
let p = fromInfo.phone;
isVerifySuccess.value = isPhone(p);
if (isVerifySuccess.value) {
timeCountdown(codeInfo);
const parent = {
phone: phone.value,
};
// TODO暂时保留
// 获取手机验证码
// const {data} = await getsmsCode(parent)
// if(data.code===0){
// }else{
// return uni.showToast({
// title: data.msg,
// duration: 1000,
// icon: 'none'
// });
// }
} else {
return uni.showToast({
title: "手机号输入错误!请重新输入",
duration: 1000,
icon: "none",
});
}
};
// 登录
const handleSubmit = async () => {
// 表单校验
const valid = await customForm.value.validate();
if (valid) {
// 登录接口
await phoneLogins(fromInfo).then((res) => {
if (res.code === 0) {
store.commit("user/setToken", res.token);
// 通过vuex获取用户信息
store.dispatch("user/GetUsersInfo");
// 跳转到首页
uni.redirectTo({
url: "/pages/index/index",
});
} else {
return uni.showToast({
title: res.msg,
duration: 1000,
icon: "none",
});
}
});
}
};
// 去手机登录页面
const goLogin = () => {
uni.redirectTo({
url: "/pages/login/user",
});
};
</script>
<style src="./index.scss" lang="scss" scoped></style>