login.vue
11.1 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<template>
<view class="container">
<u-navbar placeholder safeAreaInsetTop>
<view class="" slot="left" v-show="hasForgot" @click="closeForgot">
<u-icon name="arrow-left"></u-icon>
</view>
<view class="text-lg" slot="right" @click="handleModeChange" v-if="!hasForgot">
{{loginText}}
</view>
</u-navbar>
<view class="auth-header">
<view class="auth-logo">
<u-image width="80px" height="80px" src="@/static/lpg-logo.png"></u-image>
</view>
</view>
<view class="loginTitleCss flex flex-direction">
<view class="text-xxl text-bold">
<!-- 账号密码登录 -->
{{accountTile}}
</view>
<view class="margin-top-sm text-gray">
{{accountTip}}
<!-- 未注册手机号登录后自动生成账号 -->
</view>
</view>
<view class="auth-box">
<u-gap height="10"></u-gap>
<!-- 登录表单 -->
<u--form labelPosition="left" :model="formData" :rules="rules" ref="form">
<u-form-item prop="mobile" borderBottom ref="item-mobile">
<u-input type="text" maxlength="11" v-model="formData.mobile" clearable
:placeholder="accountPlaceholder" border="none"></u-input>
</u-form-item>
<u-gap height="20"></u-gap>
<u-form-item v-if="currentModeIndex === 1" prop="password" labelWidth="60" borderBottom
ref="item-password">
<u-input :type="inputType" maxlength="16" v-model="formData.password" placeholder="请输入密码"
border="none">
<template slot="suffix">
<u-icon v-if="inputType === 'password'" size="20" color="#666666" name="eye-fill"
@click="inputType = 'text';"></u-icon>
<u-icon v-if="inputType === 'text'" size="20" color="#666666" name="eye-off"
@click="inputType = 'password';"></u-icon>
</template>
</u-input>
</u-form-item>
<!-- <u-form-item v-else label="验证码" prop="code" labelWidth="60" borderBottom>
<u--input type="number" maxlength="4" v-model="formData.code" border="none"
placeholder="请填写验证码"></u--input>
<u-button slot="right" @tap="getCode" :text="codeTips" type="success" size="mini"
:disabled="codeDisabled"></u-button>
</u-form-item> -->
<view class="flex align-start margin-top" v-if="!hasForgot">
<view class="radioCss">
<u-checkbox-group v-model="checkValue" @change="groupChange" activeColor="#EA5504">
<u-checkbox name="agree" shape="circle"></u-checkbox>
</u-checkbox-group>
</view>
<view class="margin-left-xs">
<text class="text-gray">阅读并同意</text>
<text class="text-bold">《同城服务条款》、</text>
<text class="text-bold">《同城隐私政策》和</text>
<text class="text-bold">个人信息保护规则</text>
</view>
</view>
<view class="flex justify-between align-center">
<view class="text-gray" @click="goToCodePage">
忘记密码
</view>
<view class="btn-group">
<view class="w100">
<u-button class="auth-btn" shape="circle" :disabled="formData.mobile === ''"
:style="{background:formData.mobile?'#EA5504':'#D5D8DF'}"
@click="handleSubmit">{{btnText}}</u-button>
</view>
</view>
</view>
<u-code ref="uCode" @change="codeChange" seconds="60" @start="codeDisabled = true" style="height: 50px;"
@end="codeDisabled = false"></u-code>
</u--form>
</view>
</view>
</template>
<script>
import {
sendSmsCode
} from '@/api/auth.js'
import {
decrypt
} from '@/utils/encrypt.js'
export default {
data() {
return {
currentModeIndex: 2, //1账号密码 2验证码 3忘记密码
inputType: 'password',
codeDisabled: false,
codeTips: '',
formData: {
mobile: '',
password: '',
},
flag:true,
rules: {
mobile: [{
type: 'string',
required: true,
message: '请输入手机号',
trigger: ['blur', 'change']
},
// {
// // 自定义验证函数,见上说明
// validator: (rule, value, callback) => {
// // 上面有说,返回true表示校验通过,返回false表示不通过
// // uni.$u.test.mobile()就是返回true或者false的
// return uni.$u.test.mobile(value)
// },
// message: '手机号码不正确',
// // 触发器可以同时用blur和change
// trigger: ['change', 'blur']
// }
],
password: {
type: 'string',
min: 4,
max: 16,
required: true,
message: '密码长度4-16位密码',
trigger: ['blur', 'change']
},
code: {
type: 'integer',
len: 4,
required: true,
message: '请填写4位验证码',
trigger: ['blur', 'change']
}
},
hasForgot: false, //忘记密码
loginText: "短信登录",
btnText: "发送验证码",
checkValue: ['agree'],
accountTile: "短信验证码登录",
accountTip: "未注册手机号登录后自动生成账号",
accountPlaceholder: "请输入手机号"
}
},
onLoad() {
this.defaultInit()
},
async onShow() {
// #ifdef APP
let AppToken = uni.getStorageSync('App-Token');
let autoInformation = uni.getStorageSync('autoInformation');
console.log(AppToken);
if (AppToken && autoInformation) {
this.$modal.showLoading('自动登录中...');
console.log("存在账号信息", autoInformation);
let {
username,
password
} = autoInformation
let decryptUser = await decrypt(username);
let decryptPwd = await decrypt(password);
let data = {
mobile: decryptUser,
password: decryptPwd
}
console.log("解密账号", data);
setTimeout(() => {
this.mobileLogin(data)
}, 1000)
}
// #endif
},
onReady() {
// 如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则
this.$refs.form.setRules(this.rules)
},
methods: {
defaultInit() {
if (this.currentModeIndex === 0) {
this.btnText = '发送验证码';
this.accountTile = "短信验证码登录";
this.loginText = '账号密码登录';
this.accountTip = "未注册手机号登录后自动生成账号"
} else if (this.currentModeIndex === 1) {
this.btnText = '登录';
this.accountTile = "账号密码登录";
this.loginText = '短信登录';
this.accountTip = "请输入您的注册账号和登录密码"
} else if (this.currentModeIndex === 2) {
this.btnText = '发送验证码';
this.accountTile = "短信验证码登录";
this.loginText = '账号密码登录';
this.accountTip = "未注册手机号登录后自动生成账号"
this.btnText = "发送验证码"
} else if (this.currentModeIndex === 3) {
this.btnText = '发送验证码';
this.accountTile = "找回密码";
this.accountTip = "请输入需要找回密码的手机号"
this.btnText = "发送验证码"
}
},
handleModeChange() {
if (this.currentModeIndex === 0) {
this.currentModeIndex = 1;
this.btnText = '登录';
this.loginText = '短信登录';
this.accountTile = "账号密码登录";
this.accountTip = "请输入您的注册账号和登录密码"
} else if (this.currentModeIndex === 1) {
this.currentModeIndex = 0;
this.btnText = '发送验证码';
this.loginText = '账号密码登录';
this.accountTile = "短信验证码登录";
this.accountTip = "未注册手机号登录后自动生成账号"
} else if (this.currentModeIndex === 2) {
this.currentModeIndex = 1;
this.btnText = '登录';
this.loginText = '短信登录';
this.accountTile = "账号密码登录";
this.accountTip = "请输入您的注册账号和登录密码"
} else if (this.currentModeIndex === 3) {
this.currentModeIndex = 3;
this.btnText = '发送验证码';
this.loginText = '账号密码登录';
this.accountTile = "短信验证码登录";
this.accountTip = "未注册手机号登录后自动生成账号"
}
},
codeChange(text) {
if (this.currentModeIndex === 0) {
this.btnText = text
}
},
getCode() {
const mobile = this.formData.mobile
// return
let that = this;
if (!mobile) {
uni.$u.toast('请填写手机号')
} else if (!uni.$u.test.mobile(mobile)) {
uni.$u.toast('手机号格式不正确')
} else if (this.$refs.uCode.canGetCode) {
// 模拟向后端请求验证码
uni.showLoading({
title: '正在获取验证码'
})
//scene:1登陆获取验证码场景
// uni.navigateTo({
// url: `/pages/code?mobile=${mobile}&mode=${that.currentModeIndex}`
// })
this.$api.loginApi.getCode({
phone: mobile
}).then(res => {
if (res.code === 200) {
uni.hideLoading()
uni.$u.toast('验证码已发送')
this.$refs.uCode.start();
setTimeout(() => {
uni.navigateTo({
url: `/pages/code?mobile=${mobile}&mode=${that.currentModeIndex}`
})
}, 500)
}
})
// sendSmsCode({
// mobile: mobile,
// scene: 1
// }).then(res => {
// //console.log(res)
// uni.hideLoading()
// uni.$u.toast('验证码已发送')
// // 通知验证码组件内部开始倒计时
// })
} else {
uni.$u.toast('倒计时结束后再发送')
}
},
groupChange(e) {
console.log(e);
},
handleSubmit() {
if (!this.checkValue.length && !this.hasForgot) {
uni.$u.toast('请先勾选同意');
return;
} else {
this.$refs.form.validate().then(res => {
if (this.currentModeIndex === 1) {
this.mobileLogin(this.formData)
} else {
console.log("发送验证码");
this.getCode();
}
})
}
},
goToCodePage() {
this.hasForgot = true;
this.currentModeIndex = 3;
this.defaultInit();
},
closeForgot() {
console.log(1111);
this.currentModeIndex = 1;
this.defaultInit();
this.hasForgot = false;
},
mobileLogin(data) {
// console.log("登录data", data);
// uni.setStorageSync('App-Token','eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsInRlbmFudElkIjoiLTEiLCJleHAiOjE3MTA1ODI1MzYsImlhdCI6MTcxMDQ5NjEzNn0.9wxRh7YJLzZsjPAnsblZ775_tUyfbh4P8hmzi28Rx6kRA5zylA6XAkkN4kozE8aa3Iy8QcjyaLciSc4N34Qjsg')
// uni.switchTab({
// url:'/pages/home/home'
// })
this.$store.dispatch('Login', data).then(res => {
uni.$u.toast('登录成功');
this.$store.dispatch('GetInfo');
setTimeout(() => {
uni.navigateTo({
url: '/pages/home/home'
})
}, 500)
}).catch(err => {
// uni.$u.toast(err)
})
}
}
}
</script>
<style lang="scss" scoped>
.container {
background: #fff;
height: 100vh;
}
.auth-header {
width: 100%;
height: 200rpx;
.auth-logo {
margin: 40px 0 0 40px;
}
}
.auth-box {
margin: 0 40px;
.btn-group {
height: 100px;
width: 140px;
display: flex;
justify-content: flex-end;
align-items: center;
.auth-btn {
color: #fff;
height: 90rpx;
font-size: 32rpx;
}
}
}
.radioCss {
margin-top: 2px;
}
.loginTitleCss {
margin: 0 40px;
}
/deep/.u-form-item__body__right__message {
margin-left: 0px !important;
}
</style>