setPassword.vue 4.9 KB
<template>
	<v-layout id="Password" column justify-center align-center>
    <v-flex>
			<Stepper :step="stepper" />
			<div class="PasswordInputBlock">
				<div class="passwordDisplayer" v-for="(len, index) in 6" :key="index"><span :class="inputValue.length >= len ? '' : 'white'">*</span></div>
			</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-bottom-sheet v-model="keyboardDisplay" hide-overlay full-width persistent>
      <PasswordBeyboard :showClose="false" @input="passwordInput" @delete="deleteInput"/>
    </v-bottom-sheet>
		<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'
	import PasswordBeyboard from '@/components/PasswordBeyboard'
	import {wechatShareSetting} from '@/helper'
	export default {
		layout: 'lightColor',
		head:() => ({
    	title: '设置交易密码'
  	}),
		data: () => ({
			stepper: 1,
			password: '',
			passwordConfirm: '',
			inputValue: '',
			dialogDisplay: false,
			isLoading: false,
			keyboardDisplay: true,
			dialogContent: '',
		}),
		components: {
			Stepper,
			PasswordBeyboard
		},
		computed: {
			disabled() {
				return (this.password.length !== 6 || this.passwordConfirm.length !== 6)
			}
		},
		mounted() {
			wechatShareSetting(this.$axios, this.$store)
		},
		methods: {
			passwordInput(e) {
				if (this.inputValue.length < 6) {
					this.inputValue += e
				}
				if (this.inputValue.length === 6 && this.stepper === 1) {
					this.password = this.inputValue.slice(0, 6)
					this.inputValue = ''
					this.stepper++
				}
				if (this.inputValue.length === 6 && this.stepper === 2) {
					this.passwordConfirm = this.inputValue.slice(0, 6)
				}
			},
			deleteInput(e) {
				this.inputValue = this.inputValue.slice(0, this.inputValue.length - 1)
			},
			reset() {
				this.dialogDisplay = false
				this.password = ''
				this.passwordConfirm = ''
				this.inputValue = ''
				this.stepper = 1
			},
			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
				if (data.success) {
        	this.$store.commit('updateUser',{
          	needSetPassword: false,
					})
					this.$store.dispatch('setMessageRedirect', this.$store.state.redirect || '/')
					this.$store.dispatch('displayMessage', '交易密码设置成功')       					
				} else {
					this.$store.dispatch('displayMessage', data.msg)
					this.reset()
				}
			},
			keydown(e) {
				if (e.key !== 'Backspace') return
				const id = e.target.id
				let index = parseInt(id.split('-')[1])
				if (index === 0) return
				if (index === 5 && this.inputValue[5] !== '') {
					this.inputValue[5] = ''
					return
				}
				const before = this.$refs[(index - 1).toString()]
				before.focus()
			}
		}
	}
</script>

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

			.passwordDisplayer {
				width: 3.5rem;
				height: 3.5rem;
				background-color: #fff;
				border: 1px solid $gold;
				border-left: none; 
				text-align: center;
				font-size: 2.5rem;
				display: inline-block;

				.white {
					color: #fff;
				}

				&:nth-child(1) {
					border-left: 1px solid $gold;
					border-radius: 5px 0 0 5px;
				}

				&:nth-child(6) {
					border-radius: 0 5px 5px 0;
				}

			}
		}

		.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>