PhoneLogin.vue
3.3 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
<template>
<view class="phone">
<view class="phone-topclose" @click="emit('editLoginStateHandler', 1)">
<u-icon name="close" size="22" color="#000"></u-icon>
</view>
<view class="phone-title">手机验证登录</view>
<view>
<up-input placeholder="请输入手机号" border="bottom" placeholderClass="placeholderClass" v-model="params.phoneNum">
<template #suffix>
<view class="phone-suffix" @click="sendCodeHandler">{{ countdown === 60 ? '获取验证码' : `${countdown}秒后可重新获取` }}</view>
</template>
</up-input>
<view style="height: 24rpx"></view>
<up-input placeholder="请输入验证码" border="bottom" placeholderClass="placeholderClass" v-model="params.code"></up-input>
</view>
<view class="bluetext" style="margin-top: 28rpx" @click="emit('editLoginStateHandler', 3)">账号密码登录</view>
<view style="color: red; margin-top: 20rpx">*未注册用户在登录后自动注册</view>
</view>
</template>
<script setup lang="ts">
import { reactive, getCurrentInstance, ComponentPublicInstance } from 'vue'
import useTimeHandler from '@/hooks/useTimeChange'
import { getSendMessage, getMessageLogin, getIndex } from '@/api'
import type { PhoneCodeLoginType, IndexType } from '../../../types'
import { DebounceBy } from '../../../utils/debounceBy'
const { proxy } = getCurrentInstance() as { proxy: ComponentPublicInstance }
const [countdown, startCountdown] = useTimeHandler()
const emit = defineEmits(['editLoginStateHandler'])
const params = reactive({
phoneNum: '',
code: ''
})
const loginHandler = async () => {
console.log(uni.getStorageSync('cid'), 'cid---------------------')
const { result }: { result: PhoneCodeLoginType } = await getMessageLogin({ ...params, cid: uni.getStorageSync('cid') })
uni.setStorageSync('token', result.token)
uni.setStorageSync('im_token', result.im_token)
uni.setStorageSync('userId', result.userId)
console.log(uni.getStorageSync('cid'), 'cid传值')
// const { result: IndexData }: { result: IndexType } = await getIndex()
// if (IndexData?.isHavePwd == 1) return proxy.$h.jumpUrl()
setTimeout(() => {
uni.$u.toast('登录成功')
}, 1000)
uni.reLaunch({ url: '/pages/index/index' })
}
const clickGetCodeHandler = async () => await getSendMessage({ phoneNum: params.phoneNum })
const sendCodeHandler = DebounceBy(() => {
if ([null, undefined, ''].includes(params.phoneNum)) return uni.$u.toast('请输入手机号')
if (!/^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/.test(params.phoneNum)) return uni.$u.toast('请输入正确的手机号')
startCountdown(clickGetCodeHandler)
}, 2000)
defineExpose({ loginHandler })
</script>
<style lang="scss" scoped>
.phone {
box-sizing: border-box;
padding: 0 64rpx;
margin: 236rpx 0 0;
&-title {
color: #323233;
font-size: 60rpx;
font-weight: 700;
margin-bottom: 98rpx;
}
&-topclose {
position: fixed;
top: 100rpx;
left: 32rpx;
}
&-suffix {
color: #c1c1c6;
font-size: 28rpx;
font-weight: 700;
}
}
:deep(.u-input) {
padding: 26rpx 9px !important;
font-size: 36rpx !important;
}
.placeholderClass {
color: #c1c1c6;
font-size: 36rpx;
font-weight: 700;
}
</style>