uni-plate-input.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. * @author minisola
  3. * @version 20190814
  4. */
  5. <template>
  6. <view class="so-mask">
  7. <view class="so-plate animation-scale-up">
  8. <view class="so-plate-head">
  9. <view class="so-plate-type">
  10. <radio-group @change="typeChange">
  11. <label>
  12. <radio value="1" :checked="type===1" />
  13. 普通车牌
  14. </label>
  15. <label>
  16. <radio value="2" :checked="type===2" />
  17. 新能源车牌
  18. </label>
  19. </radio-group>
  20. </view>
  21. </view>
  22. <view class="so-plate-body">
  23. <view class="so-plate-word" :class="{ active: currentInputIndex == 0 }" @tap="inputSwitch" data-index="0">
  24. <text>{{ currentInputValue[0] }}</text>
  25. </view>
  26. <view class="so-plate-word" :class="{ active: currentInputIndex == 1 }" @tap="inputSwitch" data-index="1">
  27. <text>{{ currentInputValue[1] }}</text>
  28. </view>
  29. <view class="so-plate-dot"></view>
  30. <view class="so-plate-word" :class="{ active: currentInputIndex == 2 }" @tap="inputSwitch" data-index="2">
  31. <text>{{ currentInputValue[2] }}</text>
  32. </view>
  33. <view class="so-plate-word" :class="{ active: currentInputIndex == 3 }" @tap="inputSwitch" data-index="3">
  34. <text>{{ currentInputValue[3] }}</text>
  35. </view>
  36. <view class="so-plate-word" :class="{ active: currentInputIndex == 4 }" @tap="inputSwitch" data-index="4">
  37. <text>{{ currentInputValue[4] }}</text>
  38. </view>
  39. <view class="so-plate-word" :class="{ active: currentInputIndex == 5 }" @tap="inputSwitch" data-index="5">
  40. <text>{{ currentInputValue[5] }}</text>
  41. </view>
  42. <view class="so-plate-word" :class="{ active: currentInputIndex == 6 }" @tap="inputSwitch" data-index="6">
  43. <text>{{ currentInputValue[6] }}</text>
  44. </view>
  45. <view class="so-plate-word" :class="{ active: currentInputIndex == 7 }" @tap="inputSwitch" v-if="type == 2" data-index="7">
  46. <text>{{ currentInputValue[7] }}</text>
  47. </view>
  48. </view>
  49. <view class="so-plate-foot">
  50. <view class="so-plate-keyboard" :style="{height:keyboardHeight}">
  51. <view id="keyboard">
  52. <block v-if="inputType == 1">
  53. <view hover-class="hover" class="so-plate-key" v-for="el of provinceText" :key="el" :data-value="el" @tap="chooseKey">{{ el }}</view>
  54. </block>
  55. <block v-if="inputType == 1">
  56. <text class="so-plate-key fill-block"></text>
  57. <text class="so-plate-key fill-block"></text>
  58. </block>
  59. <block v-if="inputType >= 3">
  60. <view hover-class="hover" class="so-plate-key" v-for="el of numberText" :key="el" :data-value="el" @tap="chooseKey">{{ el }}</view>
  61. </block>
  62. <block v-if="inputType >= 2">
  63. <view hover-class="hover" class="so-plate-key" v-for="el of wordText" :key="el" :data-value="el" @tap="chooseKey">{{ el }}</view>
  64. </block>
  65. <block v-if="inputType == 3">
  66. <text v-for="el of fillBlock" :key="el.num" class="so-plate-key fill-block"></text>
  67. </block>
  68. <block v-if="inputType == 4">
  69. <view hover-class="hover" class="so-plate-key" v-for="el of lastWordText" :key="el" :data-value="el" @tap="chooseKey">{{ el }}</view>
  70. </block>
  71. <text v-if="inputType == 4" class="so-plate-key fill-block"></text>
  72. </view>
  73. </view>
  74. <view class="so-plate-btn-group">
  75. <view>
  76. <button class="so-plate-btn so-plate-btn--cancel" @tap="$emit('close')">取消</button>
  77. </view>
  78. <view>
  79. <button class="so-plate-btn so-plate-btn--delete" @tap="deleteKey">删除</button>
  80. <button class="so-plate-btn so-plate-btn--submit" @tap="exportPlate">完成</button>
  81. </view>
  82. </view>
  83. </view>
  84. </view>
  85. </view>
  86. </template>
  87. <script>
  88. export default {
  89. name: 'uni-plate-input',
  90. data() {
  91. return {
  92. type: 1, //车牌类型
  93. currentInputIndex: 0, //当前编辑的输入框
  94. currentInputValue: ['', '', '', '', '', '', ''],
  95. fillBlock:[{num:11},{num:12},{num:13},{num:14},{num:15},{num:16}], //避免:key报错
  96. keyboardHeightInit:false,
  97. keyboardHeight:'auto',
  98. provinceText: [
  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. numberText: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
  132. wordText: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
  133. lastWordText: ['港', '澳', '学', '领', '警']
  134. };
  135. },
  136. props: {
  137. plate: {
  138. type: String
  139. }
  140. },
  141. computed: {
  142. //输入框类型
  143. inputType() {
  144. switch (this.currentInputIndex) {
  145. case 0:
  146. return 1;
  147. break;
  148. case 1:
  149. return 2;
  150. break;
  151. case 2:
  152. return 3;
  153. break;
  154. case 3:
  155. return 3;
  156. break;
  157. case 4:
  158. return 3;
  159. break;
  160. case 5:
  161. return 3;
  162. break;
  163. case 6:
  164. return this.type==2 ? 3 :4 ;
  165. break;
  166. case 7:
  167. return 4;
  168. break;
  169. default:
  170. return 1;
  171. break;
  172. }
  173. }
  174. },
  175. watch:{
  176. currentInputIndex:function(n,o){
  177. if(!this.keyboardHeightInit) return
  178. this.$nextTick(()=>{
  179. this.changeKeyboardHeight()
  180. })
  181. }
  182. },
  183. methods: {
  184. //车牌类型切换
  185. typeChange(e) {
  186. const {value} = e.detail;
  187. this.type = parseInt(value)
  188. this.currentInputIndex = 0
  189. if(value==1){
  190. this.currentInputValue = ['','','','','','','']
  191. }else{
  192. this.currentInputValue = ['','','','','','','','']
  193. }
  194. },
  195. inputSwitch(e) {
  196. const { index } = e.currentTarget.dataset;
  197. this.currentInputIndex = parseInt(index);
  198. },
  199. chooseKey(e) {
  200. const { value } = e.currentTarget.dataset;
  201. this.$set(this.currentInputValue, this.currentInputIndex, value);
  202. if(this.type==1 && this.currentInputIndex<6){
  203. this.currentInputIndex++
  204. }
  205. if(this.type==2 && this.currentInputIndex<7){
  206. this.currentInputIndex++
  207. }
  208. },
  209. deleteKey(){
  210. this.$set(this.currentInputValue,this.currentInputIndex,'')
  211. if(this.currentInputIndex!=0) this.currentInputIndex--
  212. },
  213. exportPlate(){
  214. const plate = this.currentInputValue.join('')
  215. let err = false
  216. if(this.type===1&&plate.length!=7){
  217. err = true
  218. }else if(this.type===2&&plate.length!=8){
  219. err = true
  220. }
  221. if(err) return uni.showToast({
  222. title:'请输入完整的车牌号码',
  223. icon:'none'
  224. })
  225. this.$emit('export',plate)
  226. },
  227. changeKeyboardHeight(){
  228. const that = this
  229. const query = uni.createSelectorQuery().in(this);
  230. query.select('#keyboard').boundingClientRect();
  231. query.exec(function(res) {
  232. if(res&&res[0]){
  233. that.keyboardHeight = res[0].height + uni.upx2px(30) + 'px'
  234. that.keyboardHeightInit = true
  235. }
  236. });
  237. }
  238. },
  239. mounted() {
  240. console.log(this.plate);
  241. const plateKey = this.plate.split('')
  242. if(plateKey.length===7){
  243. this.type=1
  244. }else if(plateKey.length===8){
  245. this.type=2
  246. }
  247. if(plateKey.length===7 || plateKey.length===8){
  248. this.currentInputValue = plateKey
  249. this.currentInputIndex = plateKey.length-1
  250. }
  251. setTimeout(() => { //在动画结束之后才开始获取
  252. this.$nextTick(()=>{
  253. this.changeKeyboardHeight()
  254. })
  255. }, 500);
  256. }
  257. };
  258. </script>
  259. <style scoped lang="less">
  260. @import './uni-plate-input';
  261. </style>