You need to sign in or sign up before continuing.
pwd-retrieve.vue 5.8 KB
Newer Older
芊里 已提交
1 2 3 4 5
<template>
	<view class="wrap">
		<view class="wrap-content">
			<view class="content">
				<!-- 顶部文字 -->
芊里 已提交
6
				<text class="content-top-title">重置密码</text>
芊里 已提交
7
				<login-ikonw class="login-iknow" :text="tipText"></login-ikonw>
芊里 已提交
8 9 10
				<!-- 登录框 (选择手机号所属国家和地区需要另行实现) -->
				<uni-forms ref="form" :value="formData" :rules="rules">
					<uni-forms-item name="phone">
芊里 已提交
11 12
						<uni-easyinput type="number" class="phone-input-box" :inputBorder="false"
							v-model="formData.phone" placeholder="请输入手机号"></uni-easyinput>
芊里 已提交
13 14 15
						<uni-easyinput type="number" class="phone-input-box" :inputBorder="false"
							v-model="formData.code" maxlength="6" placeholder="请输入验证码">
							<template slot="right">
芊里 已提交
16
								<login-short-code ref="shortCode" @getCode="getCode"></login-short-code>
芊里 已提交
17 18
							</template>
						</uni-easyinput>
芊里 已提交
19
						<uni-easyinput type="text" class="phone-input-box" :inputBorder="false"
芊里 已提交
20
							v-model="formData.pwd" placeholder="请输入新密码"></uni-easyinput>
芊里 已提交
21 22
					</uni-forms-item>
					<button class="send-btn-box" :disabled="!canSubmit" :type="canSubmit?'primary':'default'"
L
123  
linju 已提交
23
						@click="checkCode(submit)">完成</button>
芊里 已提交
24
				</uni-forms>
芊里 已提交
25 26 27 28 29
			</view>
		</view>
	</view>
</template>

芊里 已提交
30 31 32 33
<script>
import mixin from '../../common/loginPage.mixin.js';
	export default {
		mixins:[mixin],
芊里 已提交
34 35 36
		data() {
			return {
				password: '',
芊里 已提交
37 38 39
				currenPhoneArea: '',
				
				formData:{
芊里 已提交
40
					phone:'',
芊里 已提交
41 42 43 44
					code:'',
					pwd:''
				},
				rules: {
芊里 已提交
45 46 47 48 49 50 51 52 53 54 55
					phone:{
						rules:[{
								required: true,
								errorMessage: '请输入手机号',
							},
							{
								pattern: /^1\d{10}$/,
								errorMessage: '手机号格式不正确',
							}
						]
					},
芊里 已提交
56 57 58 59 60 61
					code: {
						rules: [{
								required: true,
								errorMessage: '请输入验证码',
							},
							{
芊里 已提交
62
								pattern: /^.{6}$/,
芊里 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
								errorMessage: '请输入6位验证码',
							}
						]
					},
					pwd:{
						rules: [{
								required: true,
								errorMessage: '请输入密码',
							},
							{
								pattern: /^.{6,20}$/,
								errorMessage: '密码应为6到20位',
							}
						]
					}
				}
芊里 已提交
79 80 81 82
			}
		},
		computed: {
			tipText() {
芊里 已提交
83
				return `验证码已通过短信发送至${this.currenPhoneArea} ${this.formData.phone}。密码为6 - 20位`
芊里 已提交
84 85 86 87 88
			},
			canSubmit() {
				let reg_phone = /^1\d{10}$/;
				let reg_pwd = /^.{6,20}$/;
				let reg_code = /^\d{6}$/;
芊里 已提交
89
				let isPhone = reg_phone.test(this.formData.phone);
芊里 已提交
90 91
				let isPwd = reg_pwd.test(this.formData.pwd);
				let isCode = reg_code.test(this.formData.code);
芊里 已提交
92 93 94 95
				return isPhone && isPwd && isCode;
			}
		},
		onLoad(event) {
芊里 已提交
96
			if (event && event.phoneNumber) {
芊里 已提交
97
				this.formData.phone = event.phoneNumber;
芊里 已提交
98 99
				this.currenPhoneArea = '+' + Number(event.phoneArea);
			}
芊里 已提交
100 101
		},
		onReady() {
芊里 已提交
102 103 104
			if(this.formData.phone){
				this.$refs.shortCode.start();
			}
芊里 已提交
105 106 107 108 109 110 111
		},
		methods: {
			/**
			 * 获取验证码倒计时
			 * 倒计时期间不会触发该方法
			 */
			getCode(done) {
芊里 已提交
112
				if (this.formData.phone == '') return uni.showToast({
芊里 已提交
113 114
					title: '请填写手机号',
					icon: 'none'
芊里 已提交
115 116 117 118 119 120
				});
				uniCloud.callFunction({
					"name": "user-center",
					"data": {
						"action": "sendSmsCode",
						"params": {
芊里 已提交
121
							"mobile": this.formData.phone,
芊里 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
							"type": "login"
						}
					},
					success: (e) => {
						console.log(e);
						// uni.showToast({
						// 	title: JSON.stringify(e.result),
						// 	icon: 'none'
						// });
						uni.showModal({
							content: JSON.stringify(e.result),
							showCancel: false,
							confirmText: '知道了'
						});
						// 发送成功后开启倒计时
						done();
					},
					fail: (err) => {
						console.log(err);
					},
					complete: () => {
						uni.hideLoading()
					}
				})
芊里 已提交
146
			},
L
123  
linju 已提交
147 148 149 150 151
			checkCode(callback){
				uniCloud.callFunction({//联网验证登陆
					"name": "user-center",
					"data": {
						"action": "loginBySms",
L
123  
linju 已提交
152
						"params":{
芊里 已提交
153
							"mobile":this.formData.phone,
L
123  
linju 已提交
154
							"code":this.formData.code
L
123  
linju 已提交
155 156 157 158 159 160 161 162 163
						}
					},
					success:async (e) => {
						uni.hideLoading()
						console.log(e.result);
						if(e.result.code === 0){
							uni.setStorageSync('uni_id_uid', e.result.uid)
							uni.setStorageSync('uni_id_token', e.result.token)
							uni.setStorageSync('uni_id_token_expired', e.result.tokenExpired)
L
123  
linju 已提交
164 165 166 167 168
							// uni.showToast({
							// 	title: '登陆成功',
							// 	icon: 'none'
							// });
							callback()
L
123  
linju 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
						}else{
							uni.showModal({
								title: '错误',
								content: e.result.msg,
								showCancel: false,
								confirmText: '知道了',
							});
						}
					},
					fail: (err) => {
						console.log(err);
						uni.showModal({
							title: '错误',
							content: JSON.stringify(err),
							showCancel: false,
							confirmText: '知道了',
						});
						if(err.errCode===30002){
							
						}
					},
					complete: () => {
						uni.hideLoading()
					}
				})
L
123  
linju 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
			},
			/**
			 * 完成并提交
			 */
			submit(){
				uniCloud.callFunction({
					name:"user-center",
					"data":{
						"action":"resetPwd",
						"params":{
							"password":this.formData.pwd
						}
					},
					success:async (e) => {
						uni.hideLoading()
						console.log(e.result);
						uni.showToast({
							title: e.result.msg,
							icon: 'none'
						});
						if(e.result.code === 0){
							uni.navigateBack()
						}
					},
					fail: (err) => {
						console.log(err);
						uni.showModal({
							title: '错误',
							content: JSON.stringify(err),
							showCancel: false,
							confirmText: '知道了',
						});
						if(err.errCode===30002){
							
						}
					},
					complete: () => {
						uni.hideLoading()
					}
				})
芊里 已提交
234 235 236 237 238 239 240 241 242 243 244
			}
		}
	}
</script>

<style>
	@import url("../../common/loginPage.css");
	.phone-input-box{
		margin-top: 20rpx;
	}
</style>