u-car-keyboard.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <view class="u-keyboard" @touchmove.stop.prevent="() => {}">
  3. <view class="u-keyboard-grids">
  4. <view class="u-keyboard-grids-item" v-for="(group, i) in abc ? EngKeyBoardList : areaList" :key="i">
  5. <view :hover-stay-time="100" @touchstart="carInputClick(i, j)" hover-class="u-carinput-hover" class="u-keyboard-grids-btn"
  6. v-for="(item, j) in group" :key="j">
  7. {{ item }}
  8. </view>
  9. </view>
  10. <view @touchstart="backspaceClick" @touchend="clearTimer" :hover-stay-time="100" class="u-keyboard-back"
  11. hover-class="u-hover-class">
  12. <u-icon :size="38" name="backspace" :bold="true"></u-icon>
  13. </view>
  14. <view :hover-stay-time="100" class="u-keyboard-change" hover-class="u-carinput-hover" @click="changeCarInputMode">
  15. <text class="zh" :class="[!abc ? 'active' : 'inactive']">中</text>
  16. /
  17. <text class="en" :class="[abc ? 'active' : 'inactive']">英</text>
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. name: "u-keyboard",
  25. emits: ["change", "backspace"],
  26. props: {
  27. // 是否打乱键盘按键的顺序
  28. random: {
  29. type: Boolean,
  30. default: false
  31. }
  32. },
  33. data() {
  34. return {
  35. // 车牌输入时,abc=true为输入车牌号码,bac=false为输入省份中文简称
  36. abc: false
  37. };
  38. },
  39. computed: {
  40. areaList() {
  41. let data = [
  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. let tmp = [];
  80. // 打乱顺序
  81. if (this.random) data = this.$u.randomArray(data);
  82. // 切割成二维数组
  83. tmp[0] = data.slice(0, 10);
  84. tmp[1] = data.slice(10, 20);
  85. tmp[2] = data.slice(20, 30);
  86. tmp[3] = data.slice(30, 36);
  87. return tmp;
  88. },
  89. EngKeyBoardList() {
  90. let data = [
  91. 1,
  92. 2,
  93. 3,
  94. 4,
  95. 5,
  96. 6,
  97. 7,
  98. 8,
  99. 9,
  100. 0,
  101. 'Q',
  102. 'W',
  103. 'E',
  104. 'R',
  105. 'T',
  106. 'Y',
  107. 'U',
  108. 'I',
  109. 'O',
  110. 'P',
  111. 'A',
  112. 'S',
  113. 'D',
  114. 'F',
  115. 'G',
  116. 'H',
  117. 'J',
  118. 'K',
  119. 'L',
  120. 'Z',
  121. 'X',
  122. 'C',
  123. 'V',
  124. 'B',
  125. 'N',
  126. 'M'
  127. ];
  128. let tmp = [];
  129. if (this.random) data = this.$u.randomArray(data);
  130. tmp[0] = data.slice(0, 10);
  131. tmp[1] = data.slice(10, 20);
  132. tmp[2] = data.slice(20, 30);
  133. tmp[3] = data.slice(30, 36);
  134. return tmp;
  135. }
  136. },
  137. methods: {
  138. // 点击键盘按钮
  139. carInputClick(i, j) {
  140. let value = '';
  141. // 不同模式,获取不同数组的值
  142. if (this.abc) value = this.EngKeyBoardList[i][j];
  143. else value = this.areaList[i][j];
  144. if(!this.abc) this.abc = true;
  145. this.$emit('change', value);
  146. if(this.vibrate) uni.vibrateShort();
  147. },
  148. // 修改汽车牌键盘的输入模式,中文|英文
  149. changeCarInputMode() {
  150. this.abc = !this.abc;
  151. },
  152. // 修改汽车牌键盘的输入模式,中文|英文
  153. updateCarInputMode(abc) {
  154. this.abc = abc;
  155. },
  156. // 点击退格键
  157. backspaceClick() {
  158. let count = 1;
  159. this.backspaceFn(count);
  160. clearInterval(this.timer); //再次清空定时器,防止重复注册定时器
  161. this.timer = null;
  162. this.timer = setInterval(() => {
  163. count++;
  164. this.backspaceFn(count);
  165. }, 250);
  166. },
  167. backspaceFn(count){
  168. this.$emit('backspace',count);
  169. },
  170. clearTimer() {
  171. clearInterval(this.timer);
  172. this.timer = null;
  173. },
  174. }
  175. };
  176. </script>
  177. <style lang="scss" scoped>
  178. @import "../../libs/css/style.components.scss";
  179. .u-keyboard-grids {
  180. background: rgb(215, 215, 217);
  181. padding: 24rpx 0;
  182. position: relative;
  183. }
  184. .u-keyboard-grids-item {
  185. @include vue-flex;
  186. align-items: center;
  187. justify-content: center;
  188. }
  189. .u-keyboard-grids-btn {
  190. text-decoration: none;
  191. width: 62rpx;
  192. flex: 0 0 64rpx;
  193. height: 80rpx;
  194. /* #ifndef APP-NVUE */
  195. display: inline-flex;
  196. /* #endif */
  197. font-size: 36rpx;
  198. text-align: center;
  199. line-height: 80rpx;
  200. background-color: #fff;
  201. margin: 8rpx 5rpx;
  202. border-radius: 8rpx;
  203. box-shadow: 0 2rpx 0rpx #888992;
  204. font-weight: 500;
  205. justify-content: center;
  206. }
  207. .u-carinput-hover {
  208. background-color: rgb(185, 188, 195) !important;
  209. }
  210. .u-keyboard-back {
  211. position: absolute;
  212. width: 96rpx;
  213. right: 22rpx;
  214. bottom: 32rpx;
  215. height: 80rpx;
  216. background-color: rgb(185, 188, 195);
  217. @include vue-flex;
  218. align-items: center;
  219. border-radius: 8rpx;
  220. justify-content: center;
  221. box-shadow: 0 2rpx 0rpx #888992;
  222. }
  223. .u-keyboard-change {
  224. font-size: 24rpx;
  225. box-shadow: 0 2rpx 0rpx #888992;
  226. position: absolute;
  227. width: 96rpx;
  228. left: 22rpx;
  229. line-height: 1;
  230. bottom: 32rpx;
  231. height: 80rpx;
  232. background-color: #ffffff;
  233. @include vue-flex;
  234. align-items: center;
  235. border-radius: 8rpx;
  236. justify-content: center;
  237. }
  238. .u-keyboard-change .inactive.zh {
  239. transform: scale(0.85) translateY(-10rpx);
  240. }
  241. .u-keyboard-change .inactive.en {
  242. transform: scale(0.85) translateY(10rpx);
  243. }
  244. .u-keyboard-change .active {
  245. color: rgb(237, 112, 64);
  246. font-size: 30rpx;
  247. }
  248. .u-keyboard-change .zh {
  249. transform: translateY(-10rpx);
  250. }
  251. .u-keyboard-change .en {
  252. transform: translateY(10rpx);
  253. }
  254. </style>