刘磊
2025-04-19 2f18780a16a68f7fc67dd3bca61b8d0aed7c8e1a
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<template>
    <view class="main-list oBorder">
        <!-- 文本框 -->
        <input 
            class="main-input" 
            :value="value" 
            :type="_type" 
            :maxlength="maxlength" 
            :placeholder="placeholder" 
            :password="type==='password'&&!showPassword" 
            
            @input="$emit('input', $event.target.value)"
            @blur="$emit('blur', $event)"
            @focus="$emit('focus', $event)"
            @longpress="$emit('longpress', $event)"
            @confirm="$emit('confirm', $event)"
            @click="$emit('click', $event)"
            @longtap="$emit('longtap', $event)"
            @touchcancel="$emit('touchcancel', $event)"
            @touchend="$emit('touchend', $event)"
            @touchmove="$emit('touchmove', $event)"
            @touchstart="$emit('touchstart', $event)"
        />
        <!-- 是否可见密码 -->
        <image 
            v-if="_isShowPass&&type==='password'&&!_isShowCode"
            class="img cuIcon" 
            :class="showPassword?'cuIcon-attention':'cuIcon-attentionforbid'" 
            @tap="showPass"
        ></image>
        <!-- 倒计时 -->
        <view 
            v-if="_isShowCode&&!_isShowPass"
            :class="['vercode',{'vercode-run': second>0}]" 
            @click="setCode"
        >{{ getVerCodeSecond }}</view>
        
    </view>
</template>
 
<script>
    var _this, countDown;
    export default{
        data(){
            return{
                showPassword: false, //是否显示明文
                second: 0, //倒计时
                isRunCode: false, //是否开始倒计时
            }
        },
        props:{
            type: String, //类型
            value: String, //值
            placeholder: String, //框内提示
            maxlength: {
                //最大长度
                type: [Number,String],
                default: 20,
            },
            isShowPass:{
                //是否显示密码图标(二选一)
                type: [Boolean,String],
                default: false,
            },
            isShowCode:{
                //是否显示获取验证码(二选一)
                type: [Boolean,String],
                default: false,
            },
            codeText:{
                type: String,
                default: "获取验证码",
            },
            setTime:{
                //倒计时时间设置
                type: [Number,String],
                default: 60,
            }
        },
        model: {
            prop: 'value',
            event: 'input'
        },
        mounted() {
            _this=this
            //准备触发
            this.$on('runCode',(val)=>{
                this.runCode(val);
            });
            clearInterval(countDown);//先清理一次循环,避免缓存
        },
        methods:{
            showPass(){
                //是否显示密码
                this.showPassword = !this.showPassword
            },
            setCode(){
                //设置获取验证码的事件
                if(this.isRunCode){
                    //判断是否开始倒计时,避免重复点击
                    return false;
                }
                this.$emit('setCode')
            },
            runCode(val){
                //开始倒计时
                if(String(val)=="0"){
                    
                    //判断是否需要终止循环
                    this.second = 0; //初始倒计时
                    clearInterval(countDown);//清理循环
                    this.isRunCode= false; //关闭循环状态
                    return false;
                }
                if(this.isRunCode){
                    //判断是否开始倒计时,避免重复点击
                    return false;
                }
                this.isRunCode= true
                this.second = this._setTime //倒数秒数
                
                let _this=this;
                countDown = setInterval(function(){
                    _this.second--
                    if(_this.second==0){
                        _this.isRunCode= false
                        clearInterval(countDown)
                    }
                },1000)
            }
        },
        computed:{
            _type(){
                //处理值
                const type = this.type
                return type == 'password' ? 'text' : type
            },
            _isShowPass() {
                //处理值
                return String(this.isShowPass) !== 'false'
            },
            _isShowCode() {
                //处理值
                return String(this.isShowCode) !== 'false'
            },
            _setTime() {
                //处理值
                const setTime = Number(this.setTime)
                return setTime>0 ? setTime : 60
            },
            getVerCodeSecond(){
                //验证码倒计时计算
                if(this.second<=0){
                    return this.codeText;
                }else{
                    if(this.second<10){
                        return '0'+this.second;
                    }else{
                        return this.second;
                    }
                }
                
            }
        }
    }
</script>
 
<style>
    @import url("./css/icon.css");
    
    .main-list{
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        /* height: 36rpx; */   /* Input 高度 */
        color: #333333;
        padding: 40rpx 32rpx;
        margin:32rpx 0;
    }
    .img{
        width: 32rpx;
        height: 32rpx;
        font-size: 32rpx;
    }
    .main-input{
        flex: 1;
        text-align: left;
        font-size: 28rpx;
        /* line-height: 100rpx; */
        padding-right: 10rpx;
        margin-left: 20rpx;
    }
    .vercode {
        color: rgba(0,0,0,0.7);
        font-size: 24rpx;
        /* line-height: 100rpx; */
    }
    .vercode-run {
        color: rgba(0,0,0,0.4) !important;
    }
    .oBorder{
        border: none;
        border-radius: 2.5rem ;
        -webkit-box-shadow: 0 0 60rpx 0 rgba(43,86,112,.1) ;
        box-shadow: 0 0 60rpx 0 rgba(43,86,112,.1) ;
    }
</style>