bind-mobile.vue 3.4 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
2
	<view class="uni-content">
DCloud_JSON's avatar
DCloud_JSON 已提交
3
		<!-- 登录框 (选择手机号所属国家和地区需要另行实现) -->
4 5 6 7
		<uni-easyinput clearable focus type="number" class="input-box" :inputBorder="false" v-model="formData.mobile"
			maxlength="11" placeholder="请输入手机号"></uni-easyinput>
		<uni-easyinput clearable type="number" class="input-box" :inputBorder="false" v-model="formData.code"
			maxlength="6" :placeholder="$t('common.verifyCodePlaceholder')">
DCloud_JSON's avatar
DCloud_JSON 已提交
8
			<template v-slot:right>
9
				<uni-send-sms-code ref="shortCode" code-type="bind" :phone="formData.mobile"></uni-send-sms-code>
DCloud_JSON's avatar
DCloud_JSON 已提交
10 11
			</template>
		</uni-easyinput>
12 13 14
		<button class="uni-btn send-btn-box" :disabled="!canSubmit" :type="canSubmit?'primary':'default'"
			@click="submit">提交</button>
		<uni-popup-captcha ref="popup-captcha" @confirm="submit" v-model="formData.captcha" scene="bindMobileBySms"></uni-popup-captcha>
DCloud_JSON's avatar
DCloud_JSON 已提交
15 16 17 18 19 20 21 22 23 24 25 26
	</view>
</template>
<script>
	import {
		mapMutations,
		mapGetters
	} from 'vuex';
	export default {
		data() {
			return {
				currenPhoneArea: '',
				formData: {
27 28 29
					phone: "",
					code: "",
					captcha: false
DCloud_JSON's avatar
DCloud_JSON 已提交
30 31 32
				}
			}
		},
33
		computed: {
DCloud_JSON's avatar
DCloud_JSON 已提交
34
			tipText() {
35
				return "验证码已通过短信发送至" + `${this.currenPhoneArea} ${this.formData.mobile}。` + "密码为6 - 20位"
DCloud_JSON's avatar
DCloud_JSON 已提交
36 37
			},
			canSubmit() {
38
				return this.isPhone() && this.isCode();
DCloud_JSON's avatar
DCloud_JSON 已提交
39 40
			}
		},
41 42
		onLoad(event) {},
		onReady() {},
DCloud_JSON's avatar
DCloud_JSON 已提交
43 44 45 46 47 48 49 50 51 52
		methods: {
			...mapMutations({
				setUserInfo: 'user/login'
			}),
			/**
			 * 完成并提交
			 */
			submit() {
				console.log(this.formData);
				uniCloud.callFunction({
53 54 55 56
					name: 'uni-id-cf',
					data: {
						action: 'bindMobileBySms',
						params: this.formData
DCloud_JSON's avatar
DCloud_JSON 已提交
57
					},
58 59 60 61 62 63 64 65 66 67 68
					success: ({
						result
					}) => {
						console.log(result);
						uni.showToast({
							title: result.msg || result.errMsg,
							icon: 'none'
						});
						if(result.errCode == "CAPTCHA_REQUIRED"){
							return this.$refs['popup-captcha'].open()
						}
DCloud_JSON's avatar
DCloud_JSON 已提交
69
						if (result.code === 0) {
70
							this.setUserInfo({"mobile":result.mobile})
DCloud_JSON's avatar
DCloud_JSON 已提交
71 72
							uni.navigateBack()
						}
73 74 75
					},
					complete: () => {
						this.formData.captcha = false
DCloud_JSON's avatar
DCloud_JSON 已提交
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
				/*
        const uniIdCo = uniCloud.importObject("uni-id-co")
        uniIdCo.bindMobileBySms(this.formData).then(e => {
          console.log(e);
          uni.showToast({
            title: e.errMsg,
            icon: 'none'
          });
          uni.navigateBack()
        }).catch(e => {
          if( e.errCode == 'CAPTCHA_REQUIRED'){
            this.$refs.popup.open()
          }
				}).finally(e=>{
          this.formData.captcha = false
        })
		*/
			},
			isPhone() {
				let reg_phone = /^1\d{10}$/;
				let isPhone = reg_phone.test(this.formData.mobile);
				return isPhone;
			},
			isCode() {
				let reg_code = /^\d{6}$/;
				let isCode = reg_code.test(this.formData.code);
				return isCode;
DCloud_JSON's avatar
DCloud_JSON 已提交
105 106 107 108 109 110
			}
		}
	}
</script>

<style>
111 112
	.uni-content {
		padding: 0;
DCloud_JSON's avatar
DCloud_JSON 已提交
113 114 115 116 117
		align-items: center;
		justify-content: center;
		padding: 50rpx;
		padding-top: 10px;
	}
118 119 120 121 122

	/* #ifndef APP-NVUE  || VUE3 */
	.uni-content /deep/ .uni-easyinput__content {}

	/* #endif */
DCloud_JSON's avatar
DCloud_JSON 已提交
123 124 125 126 127 128 129
	.input-box {
		width: 100%;
		margin-top: 16px;
		background-color: #f9f9f9;
		border-radius: 6rpx;
		flex-direction: row;
		flex-wrap: nowrap;
130
		margin-bottom: 10px;
DCloud_JSON's avatar
DCloud_JSON 已提交
131 132 133 134 135
	}

	.send-btn-box {
		margin-top: 15px;
	}
136
</style>