bindMobile.uvue 3.3 KB
Newer Older
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<template>
	<view class="page">
		<view class="form">
			<!-- 获取验证码组件(输入手机号码+发送短信验证码所需的图形验证码,获得短信验证码) -->
			<uni-id-pages-x-smsCode scene="bind-mobile-by-sms" ref="smsCode"
				@input="smsCodeInput"></uni-id-pages-x-smsCode>
			<button class="uni-btn" type="primary" @click="sendSmsCode">获取验证码</button>
		</view>

		<!-- 多次登录错误时,获取登录时专用的图形验证码 (悬浮) -->
		<uni-popup-captcha @cancel="popupCaptchaCancel" ref="popup-captcha" scene="bind-mobile-by-sms" v-model="captcha"
			title="请输入验证码"></uni-popup-captcha>
	</view>
</template>

<script>
A
Anne_LXM 已提交
17
	import { mutations } from '@/uni_modules/uni-id-pages-x/store.uts';
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
18 19 20
	export default {
		data() {
			return {
A
Anne_LXM 已提交
21
				smsCodeEl: null as null | UniIdPagesXSmsCodeComponentPublicInstance,
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
22 23 24 25 26
				needCaptcha: false,
				captcha: ""
			}
		},
		mounted() {
A
Anne_LXM 已提交
27
			this.smsCodeEl = (this.$refs["smsCode"] as UniIdPagesXSmsCodeComponentPublicInstance)
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
28 29 30
		},
		methods: {
			sendSmsCode() {
31
				this.smsCodeEl!.sendSmsCode(false);
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
32 33 34 35 36 37 38 39 40 41 42
			},
			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.bindMobileBySms(param)
				}
			},
			bindMobileBySms(param : UTSJSONObject) {
				// 设置(添加)验证码并重新发起
A
Anne_LXM 已提交
43
				const setCaptchaRetry = ()=> {
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
					uni.showToast({
						title: '请输入验证码',
						icon: 'none',
						duration: 3000
					});
					(this.$refs['popup-captcha'] as UniPopupCaptchaComponentPublicInstance).open(() => {
						this.bindMobileBySms(param)
					});
				}

				if (this.needCaptcha) {
					if (this.captcha.length != 4) {
						return setCaptchaRetry()
					} else {
						param.set("captcha", this.captcha)
					}
				}

				uni.showLoading({ "title": "请求中" })
				const uniIdCo = uniCloud.importObject("uni-id-co", { "customUI": true })
				uniIdCo.bindMobileBySms(param)
					.finally(() : void => {
						uni.hideLoading()
					})
A
Anne_LXM 已提交
68
					.then((_: UTSJSONObject) : void => {
69
						// console.log(e);
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
70 71 72 73 74 75 76 77 78 79 80 81
						uni.showToast({
							title: '绑定成功',
							icon: 'none',
							duration: 3000,
							complete() {
								uni.navigateBack()
							}
						});
						mutations.updateUserInfo({
							"mobile": param.getString("mobile")
						} as UTSJSONObject)
					})
A
Anne_LXM 已提交
82
					.catch((err : any | null) : void => {
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
83 84 85 86 87 88 89 90
						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.needCaptcha = true;
							//登录失败,自动重新获取验证码
							return setCaptchaRetry()
						} else if (error.code == "uni-id-mobile-verify-code-error") {
A
Anne_LXM 已提交
91
							this.smsCodeEl!.clearCodeInput();
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
92
						} else {
A
Anne_LXM 已提交
93
							this.smsCodeEl!.reset();
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
94 95 96 97 98 99 100 101 102 103 104
							this.captcha = ""
						}
						uni.showToast({
							title: error.message,
							icon: 'none',
							duration: 3000,
							mask: false
						});
					})
			},
			popupCaptchaCancel() {
A
Anne_LXM 已提交
105
				this.smsCodeEl!.reset();
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
106 107 108 109 110 111 112 113 114 115 116 117
				this.captcha = ""
			}
		}
	}
</script>

<style>
@import url("/uni_modules/uni-id-pages-x/common/common.scss");
.uni-btn {
  margin-top: 10px;
}
</style>