register.vue 4.8 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<!-- 账号注册页 -->
DCloud_JSON's avatar
DCloud_JSON 已提交
2 3
<template>
	<view class="uni-content">
JiaRongPing's avatar
JiaRongPing 已提交
4 5 6 7 8
		<match-media :min-width="690">
			<view class="login-logo">
				<image :src="logo"></image>
			</view>
			<!-- 顶部文字 -->
JiaRongPing's avatar
JiaRongPing 已提交
9
			<text class="title title-box">用户名密码注册</text>
JiaRongPing's avatar
JiaRongPing 已提交
10
		</match-media>
DCloud_JSON's avatar
DCloud_JSON 已提交
11 12 13 14 15 16 17 18 19 20 21
		<uni-forms ref="form" :value="formData" :rules="rules" validate-trigger="submit" err-show-type="toast">
			<uni-forms-item name="username" required>
				<uni-easyinput :inputBorder="false" :focus="focusUsername" @blur="focusUsername = false"
					class="input-box" placeholder="请输入用户名" v-model="formData.username" trim="both" />
			</uni-forms-item>
			<uni-forms-item name="nickname">
				<uni-easyinput :inputBorder="false" :focus="focusNickname" @blur="focusNickname = false" class="input-box" placeholder="请输入用户昵称" v-model="formData.nickname"
					trim="both" />
			</uni-forms-item>
			<uni-forms-item name="password" v-model="formData.password" required>
				<uni-easyinput :inputBorder="false" :focus="focusPassword" @blur="focusPassword = false"
DCloud_JSON's avatar
DCloud_JSON 已提交
22
					class="input-box" maxlength="20" :placeholder="'请输入' + (config.passwordStrength == 'weak'?'6':'8') + '-16位密码'" type="password"
DCloud_JSON's avatar
DCloud_JSON 已提交
23 24 25 26 27 28 29 30 31 32 33 34
					v-model="formData.password" trim="both" />
			</uni-forms-item>
			<uni-forms-item name="password2" v-model="formData.password2" required>
				<uni-easyinput :inputBorder="false" :focus="focusPassword2" @blur="focusPassword2 =false"
					class="input-box" placeholder="再次输入密码" maxlength="20" type="password" v-model="formData.password2"
					trim="both" />
			</uni-forms-item>
			<uni-forms-item>
				<uni-captcha ref="captcha" scene="register" v-model="formData.captcha" />
			</uni-forms-item>
			<uni-id-pages-agreements scope="register" ref="agreements" ></uni-id-pages-agreements>
			<button class="uni-btn" type="primary" @click="submit">注册</button>
JiaRongPing's avatar
JiaRongPing 已提交
35 36 37
			<button @click="navigateBack" class="register-back">返回</button>
			<match-media :min-width="690">
				<view class="link-box">
JiaRongPing's avatar
JiaRongPing 已提交
38
					<text class="link" @click="registerByEmail">邮箱验证码注册</text>
JiaRongPing's avatar
JiaRongPing 已提交
39 40 41
					<text class="link" @click="toLogin">已有账号?点此登录</text>
				</view>
			</match-media>
DCloud_JSON's avatar
DCloud_JSON 已提交
42 43 44 45 46 47 48
		</uni-forms>
	</view>
</template>

<script>
	import rules from './validator.js';
	import mixin from '@/uni_modules/uni-id-pages/common/login-page.mixin.js';
DCloud_JSON's avatar
DCloud_JSON 已提交
49
	import config from '@/uni_modules/uni-id-pages/config.js'
DCloud_JSON's avatar
DCloud_JSON 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62
	const uniIdCo = uniCloud.importObject("uni-id-co")
	export default {
		mixins: [mixin],
		data() {
			return {
				formData: {
					username: "",
					nickname: "",
					password: "",
					password2: "",
					captcha: ""
				},
				rules,
DCloud_JSON's avatar
DCloud_JSON 已提交
63
				focusUsername:false,
DCloud_JSON's avatar
DCloud_JSON 已提交
64 65
				focusNickname:false,
				focusPassword:false,
JiaRongPing's avatar
JiaRongPing 已提交
66 67
				focusPassword2:false,
				logo: "/static/logo.png"
DCloud_JSON's avatar
DCloud_JSON 已提交
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		onShow() {
			// #ifdef H5
			document.onkeydown = event => {
				var e = event || window.event;
				if (e && e.keyCode == 13) { //回车键的键值为13
					this.submit()
				}
			};
			// #endif
		},
		methods: {
			/**
			 * 触发表单提交
			 */
			submit() {
				this.$refs.form.validate().then((res) => {
					if(this.formData.captcha.length != 4){
						this.$refs.captcha.focusCaptchaInput = true
						return uni.showToast({
							title: '请输入验证码',
							icon: 'none'
						});
					}
					if (this.needAgreements && !this.agree) {
						return this.$refs.agreements.popup(()=>{
							this.submitForm(res)
						})
					}
					this.submitForm(res)
				}).catch((errors) => {
					let key = errors[0].key
					key = key.replace(key[0], key[0].toUpperCase())
					console.log(key);
					this['focus'+key] = true
				})
			},
			submitForm(params) {
				uniIdCo.registerUser(this.formData).then(e => {
					console.log(e);
					uni.navigateBack()
				})
				.catch(e => {
					console.log(e);
					console.log(e.message);
					//更好的体验:登录错误,直接刷新验证码
					this.$refs.captcha.getImageCaptcha()
				})
			},
			navigateBack() {
				uni.navigateBack()
JiaRongPing's avatar
JiaRongPing 已提交
123 124 125 126 127
			},
			toLogin() {
				uni.navigateTo({
					url: '/uni_modules/uni-id-pages/pages/login/login-withpwd'
				})
JiaRongPing's avatar
JiaRongPing 已提交
128 129 130 131 132
			},
			registerByEmail() {
				uni.navigateTo({
					url: '/uni_modules/uni-id-pages/pages/register/register-by-email'
				})
DCloud_JSON's avatar
DCloud_JSON 已提交
133 134 135 136 137 138 139
			}
		}
	}
</script>

<style lang="scss">
	@import "@/uni_modules/uni-id-pages/common/login-page.scss";
JiaRongPing's avatar
JiaRongPing 已提交
140 141 142 143
	
	@media screen and (max-width: 690px) {
		.uni-content{
			margin-top: 15px;
JiaRongPing's avatar
JiaRongPing 已提交
144 145
			height: 100%;
			background-color: #fff;
JiaRongPing's avatar
JiaRongPing 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
		}
	}
	@media screen and (min-width: 690px) {
		.uni-content{
			padding: 30px 40px 60px;
		}
		
		.link-box {
			/* #ifndef APP-NVUE */
			display: flex;
			/* #endif */
			flex-direction: row;
			justify-content: space-between;
			margin-top: 10px;
		}
		
		.link {
			font-size: 12px;
		}
165
	}
JiaRongPing's avatar
JiaRongPing 已提交
166
	
DCloud_JSON's avatar
DCloud_JSON 已提交
167 168 169 170 171 172 173 174 175
	.uni-content ::v-deep .uni-forms-item__label {
		position: absolute;
		left: -15px;
	}

	button {
		margin-top: 15px;
	}
</style>