setPassword.vue 5.25 KB
<template>
	<v-layout id="Password" column justify-center align-center>
    <v-flex>
			<Stepper :step="stepper" />
			<div class="PasswordInputBlock">
				<input class="PasswordInput" id="PasswordInput-0" ref="0" maxlength="1" type="password" v-model="inputValue[0]" @input="passwordInput">
				<input class="PasswordInput" id="PasswordInput-1" ref="1" maxlength="1" type="password" v-model="inputValue[1]" @input="passwordInput">
				<input class="PasswordInput" id="PasswordInput-2" ref="2" maxlength="1" type="password" v-model="inputValue[2]" @input="passwordInput">
				<input class="PasswordInput" id="PasswordInput-3" ref="3" maxlength="1" type="password" v-model="inputValue[3]" @input="passwordInput">
				<input class="PasswordInput" id="PasswordInput-4" ref="4" maxlength="1" type="password" v-model="inputValue[4]" @input="passwordInput">
				<input class="PasswordInput" id="PasswordInput-5" ref="5" maxlength="1" type="password" v-model="inputValue[5]" @input="passwordInput">				
			</div>
			<div class="ConfirmButtonBlock" v-if="stepper === 2">
				<v-btn depressed id="ConfirmButton" :disabled="disabled" :loading="isLoading" @click.native="passwordSubmit">确  认</v-btn>				
				<v-btn depressed id="resetButton" :loading="isLoading" @click.native="reset">重  置</v-btn>
			</div>
    </v-flex>
		<v-dialog  v-model="dialogDisplay" persistent max-width="290">
      <v-card class="hintDialog">
        <v-card-title class="headline">提示</v-card-title>
        <v-card-text class='dialogContent'>
          <p>{{dialogContent}}</p>
          </v-card-text>          
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="black" flat @click.native="reset">确  定</v-btn>            
        </v-card-actions>
      </v-card>
    </v-dialog>
	</v-layout>
</template>

<script>
	import Stepper from '@/components/Stepper'
	export default {
		layout: 'lightColor',
		head:() => ({
    	title: '设置交易密码'
  	}),
		data: () => ({
			stepper: 1,
			password: '',
			passwordConfirm: '',
			inputValue: {
				0: '',
				1: '',
				2: '',
				3: '',
				4: '',
				5: '',
			},
			dialogDisplay: false,
			isLoading: false,
			dialogContent: '',
		}),
		components: {
			Stepper
		},
		computed: {
			disabled() {
				return (this.password.length !== 6 || this.passwordConfirm.length !== 6)
			}
		},
		methods: {
			reset() {
				this.dialogDisplay = false
				this.password = ''
				this.passwordConfirm = ''
				this.inputValue = {
					0: '',
					1: '',
					2: '',
					3: '',
					4: '',
					5: ''
				}
				this.stepper = 1
			},
			passwordInput(e) {
				let id = e.target.id
				let index = parseInt(id.split('-')[1])
				if (e.inputType === 'deleteContentBackward') return
				if (index < 5) {
					index++
					let next = this.$refs[index.toString()]
					next.focus()
					if (this.inputValue[index] !== '') {
						next.setSelectionRange(0, 1)
						return
					}
				} else {
					if (this.stepper === 1) {
						this.password = this.passwordConnect()
						if (this.password !== false) {
							this.stepper++
							this.inputValue = {
								0: '',
								1: '',
								2: '',
								3: '',
								4: '',
								5: ''
							}
							this.$refs['0'].focus()
						}
					} else {
						this.passwordConfirm = this.passwordConnect()
					}
				}
			},
			passwordConnect() {
				let result = ''
				for (let i = 0; i < 6; i++) {
					let code = this.inputValue[i]
					if (code) {
						result += code
					} else {
						return false
					}
				}
				return result
			},
			async passwordSubmit() {
				this.loading = true
				if (this.password !== this.passwordConfirm ) {
					this.dialogContent = '两次输入的密码不一致'
					this.dialogDisplay = true
					return
				}
				let {data} = await this.$axios.post('/user/password', {
					password: this.password,
					userId: this.$store.state.user.id
				})
				this.isLoading = false
				console.log(data)
				if (data.success) {
					this.$store.dispatch('displayMessage', '交易密码设置成功')        
        	this.$store.commit('updateUser',{
          	needSetPassword: false,
        	})
        	this.$router.push(this.$store.state.redirect || '/')
				} else {
					this.$store.dispatch('displayMessage', data.msg)
					this.reset()
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	#Password {
		
		.PasswordInputBlock {
			margin-top: 2.9rem;
			text-align: center;

			.PasswordInput {
				width: 2.5rem;
				height: 2.5rem;
				line-height: 2.5rem;
				background-color: #fff;
				border: 1px solid $gold;
				outline: none;
				text-align: center;
				font-size: 1.5rem;
				margin: 0 0.75rem;
			}
		}

		.ConfirmButtonBlock {
      width: 100%;
			margin-top: 3.5rem;
			text-align: center;

      #ConfirmButton {
        color: #484646;
        background-image: linear-gradient(-213deg, #DAB269 0%, #E7C78C 92%);
        border-radius: 2px;
        width: 18.5rem;
        height: 2.8rem;
        margin: auto;
			}
			#resetButton {
        color: #484646;
				border-radius: 2px;
				border: 1px solid #484646;
        width: 18.5rem;
        height: 2.8rem;
        margin:  1rem auto;
			}
    }
	}
</style>