change_pwd.vue 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
<!-- 修改密码 -->
<template>
	<view class="uni-content">
		<uni-forms ref="form" :value="formData" err-show-type="toast">
			<uni-forms-item name="oldPassword">
				<uni-easyinput :focus="focusOldPassword" @blur="focusOldPassword = false" class="input-box"
					type="password" :inputBorder="false" v-model="formData.oldPassword" placeholder="请输入旧密码">
				</uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="newPassword">
				<uni-easyinput :focus="focusNewPassword" @blur="focusNewPassword = false" class="input-box"
					type="password" :inputBorder="false" v-model="formData.newPassword" placeholder="请输入新密码">
				</uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="newPassword2">
				<uni-easyinput :focus="focusNewPassword2" @blur="focusNewPassword2 = false" class="input-box"
					type="password" :inputBorder="false" v-model="formData.newPassword2" placeholder="请再次输入新密码">
				</uni-easyinput>
			</uni-forms-item>
			<button class="uni-btn send-btn-box" type="primary" @click="submit">提交</button>
		</uni-forms>
	</view>
</template>

<script>
	import mixin from '@/uni_modules/uni-id-pages/common/login-page.mixin.js';
	const uniIdCo = uniCloud.importObject("uni-id-co", {
		customUI:true
	})
	export default {
		mixins: [mixin],
		data() {
			return {
				focusOldPassword: false,
				focusNewPassword: false,
				focusNewPassword2: false,
				formData: {
					'oldPassword': '',
					'newPassword': '',
					'newPassword2': '',
				},
				rules: {
					oldPassword: {
						rules: [{
								required: true,
								errorMessage: '请输入新密码',
							},
							{
								pattern: /^.{6,20}$/,
								errorMessage: '密码为6 - 20位',
							}
						]
					},
					newPassword: {
						rules: [{
								required: true,
								errorMessage: '请输入新密码',
							},
							{
								pattern: /^.{6,20}$/,
								errorMessage: '密码为6 - 20位',
							}
						]
					},
					newPassword2: {
						rules: [{
								required: true,
								errorMessage: '请确认密码',
							},
							{
								pattern: /^.{6,20}$/,
								errorMessage: '密码为6 - 20位',
							},
							{
								validateFunction: function(rule, value, data, callback) {
									if (value != data.newPassword) {
										callback('两次输入密码不一致')
									};
									return true
								}
							}
						]
					}
				}
			}
		},
		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() {
				console.log("formData", this.formData);
				console.log('rules', this.rules);
				this.$refs.form.validate()
					.then(res => {
						let {
							oldPassword,
							newPassword
						} = this.formData
						uniIdCo.updatePwd({
								oldPassword,
								newPassword
							}).then(e => {
								console.log(e);
								uni.removeStorageSync('uni_id_token');
								uni.setStorageSync('uni_id_token_expired', 0)
								uni.redirectTo({
									url:'/uni_modules/uni-id-pages/pages/login/login-withpwd'
								})
							}).catch(e => {
								uni.showModal({
									content: e.message,
									showCancel: false
								});
							})
					}).catch(errors => {
						let key = errors[0].key
						key = key.replace(key[0], key[0].toUpperCase())
						console.log(key, 'focus' + key);
						this['focus' + key] = true
					})
			}
		}
	}
</script>

<style lang="scss">
	@import "@/uni_modules/uni-id-pages/common/login-page.scss";

	.uni-content {
		margin-top: 15px;
	}
</style>