register.vue 3.2 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7
<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" :rules="rules" validate-trigger="submit" err-show-type="undertext">
			<uni-forms-item name="username" required>
				<uni-easyinput :inputBorder="false" class="easyinput" :placeholder="$t('register.usernamePlaceholder')" v-model="formData.username" trim="both" />
			</uni-forms-item>
			<uni-forms-item name="nickname">
8
				<uni-easyinput :inputBorder="false" class="easyinput" :placeholder="$t('register.nicknamePlaceholder')" v-model="formData.nickname" trim="both"  />
DCloud_JSON's avatar
DCloud_JSON 已提交
9 10 11 12 13 14
			</uni-forms-item>
			<uni-forms-item name="password" v-model="formData.password" required>
				<uni-easyinput :inputBorder="false" class="easyinput" :placeholder="$t('register.passwordDigitsPlaceholder')" type="password" v-model="formData.password" trim="both" />
			</uni-forms-item>
			<uni-forms-item name="pwd2" v-model="formData.pwd2" required>
				<uni-easyinput :inputBorder="false" class="easyinput" :placeholder="$t('register.passwordAgain')" type="password" v-model="formData.pwd2" trim="both" />
DCloud_JSON's avatar
DCloud_JSON 已提交
15 16 17 18
			</uni-forms-item>
			<uni-forms-item name="captcha" required>
				<uni-captcha scene="register" v-model="formData.captcha"></uni-captcha>
			</uni-forms-item>
DCloud_JSON's avatar
DCloud_JSON 已提交
19
			<uni-agreements @setAgree="agree = $event"></uni-agreements>
20
			<button class="send-btn" :disabled="!canSubmit" :type="canSubmit?'primary':'default'" @click="submit">{{$t('register.registerAndLogin')}}</button>
DCloud_JSON's avatar
DCloud_JSON 已提交
21 22 23 24 25 26 27 28 29 30 31 32
		</uni-forms>
	</view>
</template>

<script>
import rules from './validator.js';
import mixin from '../common/login-page.mixin.js';
	export default {
		mixins:[mixin],
		data() {
			return {
				formData: {
DCloud_JSON's avatar
DCloud_JSON 已提交
33 34 35
					"username": "",
					"nickname": "",
					"password":"",
DCloud_JSON's avatar
DCloud_JSON 已提交
36 37
					"pwd2":"",
					"captcha":false
DCloud_JSON's avatar
DCloud_JSON 已提交
38 39
				},
				rules,
DCloud_JSON's avatar
DCloud_JSON 已提交
40 41 42 43 44 45
				agree:false,
			}
		},
		computed:{
			canSubmit(){
				return this.formData.username.length && this.formData.password.length && this.formData.captcha.length == 4 && this.agree
DCloud_JSON's avatar
DCloud_JSON 已提交
46
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
47
		},
DCloud_JSON's avatar
DCloud_JSON 已提交
48 49 50 51 52 53 54
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		onLoad() {
			uni.setNavigationBarTitle({
				title: this.$t('register.navigationBarTitle')
			})
DCloud_JSON's avatar
DCloud_JSON 已提交
55
		},
DCloud_JSON's avatar
DCloud_JSON 已提交
56 57 58 59 60 61 62
		methods: {
			/**
			 * 触发表单提交
			 */
			submit() {
				if(!this.agree){
					return uni.showToast({
63
						title: this.$t('common.noAgree'),
DCloud_JSON's avatar
DCloud_JSON 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
						icon: 'none'
					});
				}
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
						this.submitForm(res)
					}).catch((errors) => {
						console.log(errors);
					})
					.finally(() => {
						uni.hideLoading()
					})
			},
			submitForm(params) {
DCloud_JSON's avatar
DCloud_JSON 已提交
80 81 82 83 84 85 86 87 88 89
				uniCloud.callFunction({
					name:'uni-id-cf',
					data:{
						action:'register',
						params,
					},
					success: ({result}) => {
						console.log(result);
						if(result.code === 0){
							this.loginSuccess(result)
DCloud_JSON's avatar
DCloud_JSON 已提交
90 91 92 93 94
						}else{
							uni.showModal({
								content: result.msg,
								showCancel: false
							});
DCloud_JSON's avatar
DCloud_JSON 已提交
95 96
						}
					}
DCloud_JSON's avatar
DCloud_JSON 已提交
97 98 99 100 101 102
				})
			}
		}
	}
</script>

103
<style lang="scss" scoped>
DCloud_JSON's avatar
DCloud_JSON 已提交
104 105 106
	@import url("../common/login-page.css");
	.uni-container {
		padding: 15px;
DCloud_JSON's avatar
DCloud_JSON 已提交
107
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
108
	.send-btn{
109
		margin-top: 15px;
DCloud_JSON's avatar
DCloud_JSON 已提交
110 111 112
	}
	.uni-container ::v-deep .uni-forms-item__label{
		width: 15px !important;
DCloud_JSON's avatar
DCloud_JSON 已提交
113 114
	}
</style>