uni-id-pages-x-loginBySmsCode.uvue 3.1 KB
Newer Older
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
1 2 3
<template>
  <view>
    <text class="tip">未注册的账号验证通过后将自动注册</text>
A
Anne_LXM 已提交
4

DCloud_JSON's avatar
init  
DCloud_JSON 已提交
5 6 7 8 9 10 11
    <view class="form">
      <!-- 获取验证码组件(输入手机号码+发送短信验证码所需的图形验证码,获得短信验证码) -->
      <uni-id-pages-x-smsCode ref="smsCode" @input="smsCodeInput" ></uni-id-pages-x-smsCode>
      <!-- 同意隐私政策协议 -->
      <uni-id-pages-x-agreements class="agreements-box" ref="agreements" scope="login"/>
      <button class="uni-btn" type="primary" @click="sendSmsCode">获取手机验证码</button>
    </view>
A
Anne_LXM 已提交
12

DCloud_JSON's avatar
init  
DCloud_JSON 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25
    <!-- 多次登录错误时,获取登录时专用的图形验证码 (悬浮) -->
    <uni-popup-captcha ref="captcha" scene="login-by-sms" v-model="captcha" title="请输入验证码"></uni-popup-captcha>
  </view>
</template>

<script>
	import { loginSuccess } from '@/uni_modules/uni-id-pages-x/common/common.uts';
	const uniIdCo = uniCloud.importObject("uni-id-co", { "customUI": true })
	export default {
		name: "uni-id-pages-x-loginBySmsCode",
		data() {
			return {
				captcha: "",
A
Anne_LXM 已提交
26
				smsCodeRef: null as null | UniIdPagesXSmsCodeComponentPublicInstance
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
27 28 29
			}
		},
		mounted() {
A
Anne_LXM 已提交
30
			this.smsCodeRef = (this.$refs["smsCode"] as UniIdPagesXSmsCodeComponentPublicInstance)
A
Anne_LXM 已提交
31 32 33
      uni.$on('uni-id-pages-x-loginBySmsCode-showPopupCaptcha',(callback : () => void)=>{
        this.showPopupCaptcha(callback)
      })
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
34
		},
35 36 37
    unmounted() {
      uni.$off('uni-id-pages-x-loginBySmsCode-showPopupCaptcha')
    },
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
38 39
		methods: {
			sendSmsCode() {
40
				this.smsCodeRef!.$callMethod('sendSmsCode',true);
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
			},
			smsCodeInput(param : UTSJSONObject) {
				// console.log('smsCodeInput param', param);
				const mobile = param.getString("mobile") as string;
				const code = param.getString("code") as string;
				if (mobile.length == 11 && code.length == 6) {
					this.login(param)
				}
			},
			showPopupCaptcha(callback : () => void) {
				(this.$refs["agreements"] as UniIdPagesXAgreementsComponentPublicInstance).showPopupAgreements(callback)
			},
			login(param : UTSJSONObject) {
				uni.showLoading({ "title": "登录中" })
				uniIdCo.loginBySms(param)
					.finally(() : void => {
						uni.hideLoading()
					})
A
Anne_LXM 已提交
59
					.then((e : UTSJSONObject) : void => {
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
60
						// console.log(e);
A
Anne_LXM 已提交
61
						this.smsCodeRef!.$callMethod("hideCodeInput");
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
62 63 64 65 66
						uni.showToast({
							title: '登录成功',
							icon: 'none'
						});
						loginSuccess(e)
A
Anne_LXM 已提交
67
						this.smsCodeRef!.$callMethod('clearCodeInput')
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
68
					})
A
Anne_LXM 已提交
69
					.catch((err : any | null) : void => {
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82
						const error = err as UniCloudError
						console.error(error)
						console.error(error.code)
						if (["uni-id-captcha-required", "uni-captcha-verify-fail"].includes(error.code as string)) {
							(this.$refs["captcha"] as UniPopupCaptchaComponentPublicInstance).open(() => {
								this.login(param)
							});
						} else {
							uni.showToast({
								title: error.message,
								icon: 'none',
								mask: false
							});
A
Anne_LXM 已提交
83
							this.smsCodeRef!.$callMethod('clearCodeInput')
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96
							this.captcha = ""
						}
					})
			}
		}
	}
</script>

<style>
	.tip {
		font-size: 12px;
		padding: 0 0 5px 10px;
	}
A
Anne_LXM 已提交
97
</style>