zhangzengfei
2021-09-16 38430ddb8612fce15a2f1c940f9bd57d4da3e70b
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
<template>
    <view class="content">
        <view class="top">
            <text>请输入车牌号:</text>
            <view class="search-car-input" >
                <view class="car-input">
                    <view class="split-input" ref="splitInput" @click="carInputClick">
                        <view class="split-cell" :class="{'focus-cell':i===plateNum.length+1,'separator':i===2,'new-energe':i===8&& noVal}" v-for="i in 8" :key="i" >{{plateNum[i-1]}}</view>
                        <!-- <view class="energy" v-show="plateNum.length>7">
                            <image :src="energy" mode="widthFix" />
                        </view> -->
                    </view>
                </view>
            </view>
            <button class="save" type="default" :disabled="plateNum.length<=6" @click="save">保&nbsp;存</button>
        </view>
        
        <!-- 引用车牌组件 -->
        <plate-number ref="plate" v-model="plateNum" @showOrHide="showOrHide"></plate-number>
    </view>
</template>
 
<script>
    import cursor from '../../static/addCar/cursor.gif'
    import energy from '../../static/addCar/energy.png'
    import plateNumber from '@/components/plate-number/plateNumber.vue'
    
    export default {
        components:{plateNumber},
        data() {
            return {
                plateNum: '', //输入的车牌号
                cursor: '', //输入焦点gif地址
                isCursor: true, //是否显示焦点
                energy: '', //新能源图标的地址
                noVal: true
            };
        },
        watch:{
            plateNum(newV,oldV){
                if(newV.length==8){
                    this.noVal = false
                }else{
                    this.noVal = true
                }
            }
        },
        onLoad(option) {},
        onHide() {
            //恢复初始化
            this.plateNum = '';
        },
        mounted() {
            //初始化
            // this.$refs.plate.init();
            this.cursor = cursor;
            this.energy = energy;
     
            this.carInputClick();
        },
        methods: {
            /**
             * @desc 车牌选择关闭和打开
             */
            showOrHide(falg) {
                this.isCursor = falg;
            },
            /**
             * @desc 显示车牌选择器
             */
            carInputClick() {
                this.$refs.plate.show();
            },
            updateStorage(){
                let user = JSON.parse(uni.getStorageSync('user'));
                user.plateNos.push(this.plateNum);
                uni.setStorageSync('user',JSON.stringify(user));
            },
            save() {
                let _this = this;
                const {userId} = JSON.parse(uni.getStorageSync('user'));
                //输入了正确的车牌才执行请求
                if (this.plateNum.length > 6) {
                    this.$refs.plate.close();
                    this.$api.syncRequest({
                        url: '/basic/api/user/addPlateNo',
                        method: 'POST',
                        data: {userId,plateNo:_this.plateNum}
                    }).then(res=>{
                        if(res.data.success){
                            
                            uni.showToast({
                                icon:'none',
                                title: '车牌:'+_this.plateNum+'添加成功',
                                duration: 1500,                            
                                complete() {                                
                                    _this.updateStorage();                                
                                }
                            });
                            setTimeout(()=>{
                                uni.navigateTo({
                                    url:'/pages/myCarList/myCarList'
                                })
                            },2000)
                            
                        }
                        
                    })
                    
                } else {
                    uni.showToast({
                        title: '请输入正确的车牌号',
                        duration: 2000
                    });
                }
            }
        }
    }
</script>
 
<style lang="scss" scoped>
    uni-page-wrapper,uni-page-body{
        height: 100%;
    }
    .content{
        height: 100%;
        background-color: #fbfbfb;
        .top{
            padding: 40rpx 60rpx;
            text{
                line-height: 80rpx;
                font-size: 36rpx;
                color: #bfbfbf;
            }
            .search-car-input{
                .split-input{
                    display: flex;
                    .split-cell{
                        width: 64rpx;
                        height: 64rpx;
                        text-align: center;
                        line-height: 64rpx;
                        border: 1rpx solid #dadada;
                        margin-right:7rpx;
                        &.new-energe{
                            background: url(../../static/addCar/new.png) no-repeat center;
                            background-size: 100%;
                        }
                        &.focus-cell{
                            animation: focus-gif .5s infinite alternate-reverse;
                        }
                        &.separator{
                            position: relative;
                            margin-right:50rpx;
                            &:after{
                                content:'.';
                                position: absolute;
                                left: 65rpx;
                                top: -26rpx;
                                font-size: 36px;
                                width: 50rpx;
                                height: 64rpx;
                                color: #999;
                            }
                        }
                    }
                }
                .energy{
                    image{
                        width: 90rpx;
                    }
                }
            }
            uni-button[disabled]{
                opacity: .5;
            }
            .save{
                margin-top: 120rpx;
                background-color: #0bbf71;
                color: #fff;
                
            }
        }
    }
    @keyframes focus-gif{
        from{
            border-color: #c6f0ff;
        }
        to{
            border-color: #3ec5ff;
        }
    }
</style>