f-login.vue
15 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<template>
<view>
<u-popup :show="loginPopupShow" mode="bottom" :round="10" @close="closeLogin" zIndex="999998">
<view class="f__login">
<view class="loginLoading" v-if="isLoading">
<u-loadmore status="loading" loadingText="正在登录..."></u-loadmore>
</view>
<view class="logo">
<image class="img" src="/static/logo.png"></image>
</view>
<view class="title">欢迎登录~</view>
<view class="text">会员用户登录后消费可享受折扣,享受更好的服务体验</view>
<view class="loginButton" v-if="!isPhoneLogin">
<!-- #ifdef MP-WEIXIN -->
<button class="button" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber" :style="{background:PrimaryColor}">微信手机号登录</button><!-- 此功能需微信认证 -->
<button class="button marginT" @click="onAuthorization" :style="{background:PrimaryColor}">微信授权登录</button>
<!-- #endif -->
<!-- #ifdef MP-ALIPAY -->
<button class="button" open-type="getAuthorize" scope="phoneNumber" @getAuthorize="decryptPhoneNumber" @error="onAuthError" :style="{background:PrimaryColor}">支付宝手机号登录</button><!-- 此功能需申请 -->
<button class="button marginT" open-type="getAuthorize" scope="userInfo" @getAuthorize="onAuthorization" :style="{background:PrimaryColor}">支付宝授权登录</button>
<!-- #endif -->
<button class="button" @click="closeLogin" style="background:#fff;margin-top:24rpx;" :style="{border:'2rpx solid '+PrimaryColor,color:PrimaryColor}">
暂不登录
</button>
</view>
<!-- 验证码登录 -->
<view class="loginPhone" v-if="isPhoneLogin">
<view class="form-row">
<input class="input" type="number" v-model="phone" placeholder="请输入手机号码" placeholder-style="font-weight:normal;color:#bbbbbb;"></input>
</view>
<view class="form-row">
<input class="input" type="number" v-model="vCode" placeholder="请输入验证码" placeholder-style="font-weight:normal;color:#bbbbbb;"></input>
<view class="getvcode" :class="{forhidden:readonly}" @click="getVcode">{{ codeText }}</view>
</view>
<button class="submit" size="default" @click="onSubmit" :style="{background:PrimaryColor}">确定</button>
</view>
<!-- #ifdef MP -->
<!-- 快速登录和手机号登录切换 -->
<view class="tips">
<view @click="isPhoneLogin = !isPhoneLogin" class="goBuy" :style="{color:PrimaryColor}">{{isPhoneLogin?'快速登录':'手机号登录'}}</view>
</view>
<!-- #endif -->
</view>
</u-popup>
</view>
</template>
<script>
import { mapState, mapMutations } from 'vuex';
// #ifdef MP
import { getPhoneInfo,getUserInfo } from './f-login.js';
// #endif
var clear;
export default {
name: 'f-login',
computed: {
...mapState(['loginPopupShow']),
},
data() {
return {
// #ifndef MP-ALIPAY
PrimaryColor: '#1fba1a', //主题色
// #endif
// #ifdef MP-ALIPAY
PrimaryColor: '#007AFF',
// #endif
// #ifdef APP-PLUS || H5
isLoading:false,
isPhoneLogin:true,//是否显示验证码登录
// #endif
// #ifndef APP-PLUS || H5
isLoading:true,
isPhoneLogin:false,//是否显示验证码登录
// #endif
readonly: false,
codeText: '获取验证码',
phone: '', //号码
vCode: '', //验证码
code: '', //uni.login获取的code
};
},
watch:{
// #ifdef MP
loginPopupShow(val){
// 静默登录
val && this.getLoginByUnion()
}
// #endif
},
methods: {
...mapMutations(['setLoginPopupShow','setUserInfo']),
//个人信息授权登录
onAuthorization(e) {
getUserInfo(info=>{
console.log(info,'授权信息')
let httpData = {
code: this.code,
nickName: info.nickName || '', //昵称
avatarUrl: info.avatarUrl || '', //头像
gender: info.gender || '', //性别 0:未知 1:男 2:女
}
// uni.$u.http.post('您的接口', httpData).then(res => {
let userInfo = {
// ...res,
token:true,//token用于判断是否登录
}
// this.setUserInfo(userInfo)
// setTimeout(()=>{
// uni.showToast({
// title: '登录成功',
// icon: 'none'
// });
// this.closeLogin();
// },100)
// })
},err=>{
// this.closeLogin();
})
},
//授权手机号登录
decryptPhoneNumber(e) {
console.log(e,'授权手机号')
var that = this
// #ifdef MP-WEIXIN
if(e.detail.errMsg == 'getPhoneNumber:ok'){
if(e.detail.iv){
var userInfo = e.detail;
userInfo.code = that.code
this.closeLogin();
//请去getPhoneInfo方法中使用您的接口绑定信息
getPhoneInfo(userInfo, res=>{
let userInfo = {
// ...res,
token:true,//token用于判断是否登录
}
this.setUserInfo(userInfo)
setTimeout(()=>{
uni.showToast({
title: '登录成功',
icon: 'none'
});
this.closeLogin();
},100)
});
}else{
// 授权失败请使用验证码登录
this.isPhoneLogin = true
uni.showToast({
title: '请使用手机号登录',
icon: 'none'
});
}
}else{
// this.closeLogin()
}
// #endif
// #ifdef MP-ALIPAY
uni.getPhoneNumber({
success:(res)=>{
//请去getPhoneInfo方法中使用您的接口绑定信息
getPhoneInfo(res.response, res=>{
let userInfo = {
// ...res,
token:true,//token用于判断是否登录
}
this.setUserInfo(userInfo)
setTimeout(()=>{
uni.showToast({
title: '登录成功',
icon: 'none'
});
that.closeLogin();
},100)
}, err=>{
// 授权失败请使用验证码登录
that.isPhoneLogin = true
uni.showToast({
title: '授权登录失败,请使用手机号登录',
icon: 'none'
});
});
},
fail:(res)=>{
// 授权失败请使用验证码登录
that.isPhoneLogin = true
uni.showToast({
title: '授权登录失败,请使用手机号登录',
icon: 'none'
});
}
})
// #endif
},
// 支付宝拒绝
onAuthError(e){
uni.showToast({
title: '您已拒绝授权~',
icon: 'none'
});
},
closeLogin(){
console.log('closeLogin')
this.setLoginPopupShow(false);
// #ifdef APP-PLUS || H5
this.isLoading = false
this.isPhoneLogin = true//是否显示验证码登录
// #endif
// #ifndef APP-PLUS || H5
this.isLoading = true
this.isPhoneLogin = false//是否显示验证码登录
// #endif
},
// 自动静默登录----获取code
// 静默登录:就是已经登录过的用户,被迫下线。调用此接口快速登录
getLoginByUnion(){
console.log("自动静默登录--自定义接口--")
var that = this
uni.login({
success(res){
console.log(res, "获取code")
that.code = res.code
let httpData = {
code: res.code,
}
// uni.$u.http.post('您的接口', httpData).then(res => {
setTimeout(()=>{
// 自动静默登录失败--打开手动登录
that.isLoading = false
},500)
// }).catch(()=>{
// // 自动静默登录失败--打开手动登录
// that.isLoading = false
// })
}
})
},
//验证码按钮文字状态
getCodeState() {
const _this = this;
this.readonly = true;
this.codeText = '60S后重新获取';
var s = 60;
clear = setInterval(() => {
s--;
_this.codeText = s + 'S后重新获取';
if (s <= 0) {
clearInterval(clear);
_this.codeText = '获取验证码';
_this.readonly = false;
}
}, 1000);
},
//获取验证码
getVcode(){
console.log('getVcode')
if (this.readonly) {
uni.showToast({
title: '验证码已发送~',
icon: 'none'
});
return;
}
if (this.phone == '') {
uni.showToast({
title: '请输入手机号~',
icon: 'none'
});
return;
}
const phoneRegular = /^1\d{10}$/;
if (!phoneRegular.test(this.phone)) {
uni.showToast({
title: '手机号格式不正确~',
icon: 'none'
});
return;
}
let httpData = {}
// 获取验证码接口
// uni.$u.http.post('您的接口', httpData).then(res => {
this.getCodeState(); //开始倒计时
// })
},
// 手机号登录
onSubmit() {
if (this.phone == '') {
uni.showToast({
title: '请输入手机号~',
icon: 'none'
});
return;
}
const phoneRegular = /^1\d{10}$/;
if (!phoneRegular.test(this.phone)) {
uni.showToast({
title: '手机号格式不正确~',
icon: 'none'
});
return;
}
if (this.vCode == '') {
uni.showToast({
title: '请输入验证码~',
icon: 'none'
});
return;
}
let httpData = {};
// uni.$u.http.post('您的接口',httpData).then(res => {
uni.showToast({
title: '登录成功~',
icon: 'none'
});
// });
}
}
};
</script>
<style lang="scss" scoped>
.f__login {
padding: 48rpx 32rpx;
border-radius: 18rpx 18rpx 0 0;
z-index: 99;
position: relative;
.loginLoading {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(255, 255, 255, .95);
z-index: 999;
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex-direction: row;
justify-content: center;
align-items: center;
}
.logo {
width: 90rpx;
height: 90rpx;
border-radius: 18rpx;
overflow: hidden;
.img {
width: 90rpx;
height: 90rpx;
}
}
.title {
font-size: 40rpx;
font-weight: bold;
margin-top: 24rpx;
}
.text {
font-size: 24rpx;
color: #666;
margin-top: 16rpx;
}
.loginButton {
margin-top: 56rpx;
.button {
color: #fff;
width: 100%;
height: 92rpx;
}
.marginT{
margin-top: 24rpx;
}
}
.tips {
margin-top: 24rpx;
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex-direction: row;
justify-content: space-between;
align-items: center;
.left {
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex-direction: row;
}
.goBuy {
font-size: 24rpx;
/* margin-left: 16rpx; */
background: none;
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex-direction: row;
justify-content: flex-start;
padding: 0;
margin: 0;
color: #1fba1a;
}
}
}
.loginPhone{
.form-row {
position: relative;
border-bottom: 1rpx solid #e8e8e8;
line-height: 1;
margin-top: 24rpx;
.input{
font-size: 34rpx;
line-height: 102rpx;
height: 94rpx;
width: 100%;
box-sizing: border-box;
font-size: 30rpx;
padding: 0;
font-weight: bold;
}
.getvcode {
font-size: 26rpx;
height: 80rpx;
color: #333;
line-height: 80rpx;
background: #eee;
min-width: 188rpx;
text-align: center;
border-radius: 8rpx;
position: absolute;
top: 50%;
transform: translateY(-50%);
right: 0;
z-index: 11;
&.forhidden {
background: #eee;
color: #cccccc;
}
}
}
.submit{
margin-top: 60rpx;
color: #fff;
width: 100%;
border: none;
}
}
</style>