wk
2024-10-18 036831ad52733ca354e65b0274b7be9b75973651
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<template>
    <view class="xm-keyboard-box">
        <view class="xm-keyboard-box-line" v-for="(line, li) in lines[mode]" :key="li" :style="{
            marginLeft: diffSize(line.diff) + 'px',
            marginRight: diffSize(line.diff) / -1 + 'px',
        }">
            <view v-for="(item, index) in line.list" :key="index" class="xm-keyboard-box-item" :class="{
                'xm-keyboard-box-item-empty': item == '',
            }" :style="{
                width: btnWidth + 'px',
                height: btnHeight + 'px'
            }" @click="toClick(item)">
                {{ item }}
            </view>
        </view>
        <view class="xm-keyboard-box-line xm-keyboard-box-toolbar">
            <view v-if="showCancelBtn" class="xm-keyboard-box-item xm-keyboard-box-btn xm-keyboard-box-btn-cancel" :style="{
                marginRight: btnWidth / ratio + 'px',
                height: btnHeight + 'px'
            }" @click="toCancel()">取消</view>
            <view class="xm-keyboard-box-item xm-keyboard-box-btn xm-keyboard-box-btn-clear" :style="{
                marginRight: btnWidth / ratio + 'px',
                height: btnHeight + 'px'
            }" @click="toClear()">清空</view>
            <view class="xm-keyboard-box-item xm-keyboard-box-btn-over" :style="{
                marginRight: btnWidth / ratio + 'px',
                height: btnHeight + 'px'
            }" @click="toConfirm()">完成</view>
        </view>
 
        <view v-if="showChangeBtn" class="xm-keyboard-box-item xm-keyboard-box-btn xm-keyboard-box-btn-change" :style="{
            width: handlerWidth + 'px',
            height: btnHeight + 'px',
            bottom: 'calc(20px + '+btnHeight+'px)'
        }" @click="changeMode()">
            <i class="iconxmk2 icon-xm-k2-jianpan" style="font-size: 24px;"></i>
        </view>
 
        <view class="xm-keyboard-box-item xm-keyboard-box-btn xm-keyboard-box-btn-del" :style="{
            width: handlerWidth + 'px',
            height: btnHeight + 'px',
            bottom: 'calc(20px + '+btnHeight+'px)'
        }" @click="toDel()">
            <i class="iconxmk2 icon-xm-k2-backspace" style="font-size: 24px;"></i>
        </view>
 
    </view>
</template>
 
<script>
    export default {
        name: 'xm-keyboard-box',
        emits: ['add', 'del', 'confirm', 'cancel', 'clear'],
        props: {
            // 是否开启震动效果
            vibration: {
                type: Boolean,
                default: false
            },
            // 是否显示切换按钮
            showChangeBtn: {
                type: Boolean,
                default: true
            },
            // 是否显示取消按钮
            showCancelBtn: {
                type: Boolean,
                default: true
            }
        },
        data() {
            return {
                mode: 0,
                ratio: 7,
                max: 10,
                gutter: 10,
                btnWidth: 10,
                btnHeight: 10,
                handlerWidth: 10,
                lines: [
                    [{
                            list: ["京", "沪", "浙", "苏", "粤", "鲁", "晋", "冀", "豫", "川"],
                            diff: 0,
                        },
                        {
                            list: ["渝", "辽", "吉", "黑", "皖", "鄂", "津", "贵", "云", "桂"],
                            diff: 0,
                        },
                        {
                            list: ["琼", "青", "新", "藏", "蒙", "宁", "甘", "陕", "闽", "赣"],
                            diff: 0,
                        },
                        {
                            list: ["湘", "使", "领", "警", "学", "港", "澳", "", "", ""],
                            diff: 3,
                        },
                    ],
                    [{
                            list: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
                            diff: 0,
                        },
                        {
                            list: ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'],
                            diff: 0,
                        },
                        {
                            list: ['A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ''],
                            diff: 1,
                        },
                        {
                            list: ['Z', 'X', 'C', 'V', 'B', 'N', 'M', '', '', ''],
                            diff: 3,
                        },
                    ],
                ],
            }
        },
        methods: {
            diffSize(pos) {
                if (pos == 0) {
                    return 0
                }
                return (pos * this.btnWidth + pos * this.btnWidth / this.ratio) / 2
            },
            changeMode(v) {
                this.mode = v == void 0 ? (this.mode == 0 ? 1 : 0) : v
            },
            toClick(item) {
                if (item === '') {
                    return;
                }
                this.toEmit('add', item);
            },
            toDel() {
                this.toEmit('del');
            },
            toCancel(){
                this.toEmit('cancel');
            },
            toConfirm() {
                this.toEmit('confirm')
            },
            toClear() {
                this.toEmit('clear')
            },
            toEmit(type, params){
                this.toVibration();
                this.$emit(type, params);
            },
            toVibration() {
                if (this.vibration && uni.vibrateShort) {
                    uni.vibrateShort();
                }
            }
        },
        mounted() {
            const {
                windowWidth
            } = uni.getSystemInfoSync();
            let _width = (windowWidth - this.gutter * 2) * this.ratio / (this.max * this.ratio + this.max - 1)
            this.btnWidth = _width.toFixed(2)
            this.btnHeight = (_width / 3 * 4).toFixed(2)
            this.handlerWidth = (_width * 1.5 + _width / (this.ratio * 2)).toFixed(2)
        }
    }
</script>
 
<style lang="scss" scoped>
    @import url(../../styles/iconfont/iconfont.css);
 
    .xm-keyboard-box {
        $gutter: 10px;
 
        background-color: #d4d5d9;
        padding: $gutter;
        position: relative;
 
        .xm-flex {
            display: flex;
            align-items: center;
        }
 
        &-line {
            @extend .xm-flex;
            justify-content: space-between;
            margin-bottom: $gutter;
 
            &:last-child {
                margin-bottom: 0;
            }
        }
 
        &-item {
            background-color: #FCFFFF;
            @extend .xm-flex;
            justify-content: center;
            border-radius: 4px;
            box-shadow: 0px 2px 2px #999;
            position: relative;
 
            &:active {
                background-color: rgba(0, 0, 0, 0.1);
            }
 
            &-empty {
                background-color: unset;
                box-shadow: unset;
 
                &:active {
                    background-color: unset;
                }
            }
        }
 
        &-btn {
            background-color: #b6bcc4;
 
            &:active {
                background-color: rgba(182, 188, 196, 0.8);
            }
        }
 
        &-btn-del {
            position: absolute;
            right: $gutter;
        }
 
        &-btn-change {
            position: absolute;
            left: $gutter;
        }
 
        &-btn-over {
            // position: absolute;
            background-color: #f37b1d;
            color: #fff;
 
            &:active {
                background-color: rgba(243, 123, 29, 0.8);
            }
        }
 
        &-toolbar {
            margin-bottom: 0;
            .xm-keyboard-box-item {
                width: 100%;
 
                &:last-child {
                    margin-right: 0 !important;
                }
            }
        }
    }
</style>