master-keyboard.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <template>
  2. <view class="master_wrap" v-show="show" hover-stop-propagation>
  3. <block v-if="keyboardtype == 'number' || keyboardtype == 'digit' || keyboardtype == 'idcard'">
  4. <view class="down_wrap">
  5. <image src="../../static/icon_down.png" @tap.stop="handleCloseKeyboard(false)"></image>
  6. </view>
  7. <view class="kerboard_number">
  8. <view @tap.stop="handleNumberKeyboardClick(item)" :hover-stay-time="100"
  9. :class="item== '-1' ? keyboardtype == 'number' ? 'number_empty' :'number_item' : item=='-2' ? 'number_delete' : 'number_item' "
  10. :hover-class="item== '-1' ? keyboardtype == 'number' ? 'number_empty_active' : 'number_item_active' : item=='-2' ? 'number_delete_active' : 'number_item_active'"
  11. v-for="(item,index) in randomNumberArr" :key="index">
  12. <block v-if="item == '-1'">
  13. <view v-if="keyboardtype == 'digit'">
  14. ·
  15. </view>
  16. <view v-else-if=" keyboardtype == 'idcard'">
  17. X
  18. </view>
  19. </block>
  20. <block v-else-if="item == '-2'">
  21. <image class="delete_img" src="../../static/icon_delete.png"></image>
  22. </block>
  23. <block v-else>
  24. {{item}}
  25. </block>
  26. </view>
  27. </view>
  28. <view class="next-btn">
  29. <view class='number_next' @click="nextBtnClick(0)">上一项</view>
  30. <view class='number_next' @click="nextBtnClick(1)">下一项</view>
  31. </view>
  32. </block>
  33. <block v-else-if="keyboardtype == 'car'">
  34. <view class="keyboard_car">
  35. <image @tap.stop="handleCloseKeyboard(false)" class="car_down" src="../../static/icon_down.png"></image>
  36. <view class="tab_wrap">
  37. <view @tap.stop="handleCarTabItemClick(index)"
  38. :class=" carType === index ?'tab_item_active' : 'tab_item'" hover-class="tab_item_active"
  39. v-for="(item,index) in carTabArr" :key="index">
  40. {{item}}
  41. </view>
  42. </view>
  43. <view class="car_content">
  44. <block v-if="carType === 0">
  45. <view class="car_province">
  46. <view @tap.stop="handleCarKerboardClick(item)"
  47. :class="computedDefaultValueLength ===0 ? 'province_item' :'province_item_disable'"
  48. :hover-class="computedDefaultValueLength ===0 ? 'province_item_active' : 'none'"
  49. :hover-stay-time="100" v-for="(item,index) in sliceProvinceArr" :key="index">
  50. {{item}}
  51. </view>
  52. </view>
  53. <view class="car_province_1">
  54. <view @tap.stop="handleCarKerboardClick(item)"
  55. :class=" (formatCarProvinceFirst(item) === -1 && computedDefaultValueLength ===0 ) ? 'province_item1' :
  56. ((newCar && computedDefaultValueLength ===7 && formatCarProvinceFirst(item) !== -1) ||
  57. (!newCar && computedDefaultValueLength ===6 && formatCarProvinceFirst(item) !== -1) ) ? 'province_item1' : 'province_item_disable'"
  58. :hover-class=" (formatCarProvinceFirst(item) === -1 && computedDefaultValueLength ===0) ? 'province_item_active' : 'none'"
  59. :hover-stay-time="100" :style="{'margin-left' : computeItemSpace + '%'}"
  60. v-for="(item,index) in sliceSecondProvinceArr" :key="index">
  61. {{item}}
  62. </view>
  63. </view>
  64. </block>
  65. <block v-else="carType === 1">
  66. <view class="car_latter">
  67. <view @tap.stop="handleCarKerboardClick(item)"
  68. :class=" (computedDefaultValueLength ===1 && formatCarProvinceSecond(item) >-1 || computedDefaultValueLength ===0) ? 'province_item_disable' : 'latter_item'"
  69. :hover-class=" (computedDefaultValueLength ===1 && formatCarProvinceSecond(item) >-1 || computedDefaultValueLength ===0) ? 'none' : 'province_item_active'"
  70. :hover-stay-time="100" v-for="(item,index) in sliceLatterArr" :key="index">
  71. <text>{{item}}</text>
  72. </view>
  73. </view>
  74. <view class="car_province_1">
  75. <view @tap.stop="handleCarKerboardClick(item)"
  76. :class=" (computedDefaultValueLength ===1 && formatCarProvinceSecond(item) >-1 || computedDefaultValueLength ===0) ? 'province_item_disable' : 'province_item1'"
  77. :hover-class=" (computedDefaultValueLength ===1 && formatCarProvinceSecond(item) >-1 || computedDefaultValueLength ===0) ? 'none' : 'province_item_active'"
  78. :hover-stay-time="100" :style="{'margin-left' : computeItemSpace + '%'}"
  79. v-for="(item,index) in sliceSecondLatterArr" :key="index">
  80. {{item}}
  81. </view>
  82. </view>
  83. </block>
  84. <view @tap.stop="handleCarKerboardDeleteClick" class="car_delete" hover-class="car_delete_active"
  85. :hover-stay-time="100"
  86. :style="{'top':computeDeleteTop + 'rpx','right' :computeItemSpace + '%' }">
  87. <image class="delete_img" src="../../static/icon_delete.png"></image>
  88. </view>
  89. </view>
  90. </view>
  91. </block>
  92. </view>
  93. </template>
  94. <script>
  95. export default {
  96. name: "master-keyboard",
  97. props: {
  98. fatherMethod: {
  99. type: Function,
  100. default: null
  101. },
  102. keyboardtype: {
  103. type: String,
  104. default: 'number' // number=数字键盘 digit=带小数点的数字键盘 idcard=身份证号键盘 car=车牌号键盘
  105. },
  106. defaultValue: {
  107. type: String,
  108. default: ''
  109. },
  110. newCar: {
  111. type: Boolean,
  112. default: false
  113. },
  114. randomNumber: {
  115. type: Boolean,
  116. default: false
  117. }
  118. },
  119. data() {
  120. return {
  121. numberArr: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
  122. carProvinceArr: ['京', '津', '渝', '沪', '冀', '晋', '辽', '吉', '黑', '苏', '浙', '皖', '闽', '赣', '鲁', '豫',
  123. '鳄', '湘', '粤', '琼', '川', '贵', '云', '陕', '甘', '青', '蒙', '桂', '宁', '新', '藏', '使',
  124. '领', '警', '学', '港', '澳'
  125. ],
  126. carLatterArr: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  127. 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R',
  128. 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
  129. ],
  130. carTabArr: ['省份', '字母'],
  131. carType: 0,
  132. show: false,
  133. };
  134. },
  135. computed: {
  136. sliceProvinceArr: function() {
  137. return this.carProvinceArr.slice(0, 30)
  138. },
  139. sliceSecondProvinceArr: function() {
  140. return this.carProvinceArr.slice(30, this.carProvinceArr.length)
  141. },
  142. sliceLatterArr: function() {
  143. return this.carLatterArr.slice(0, 30)
  144. },
  145. sliceSecondLatterArr: function() {
  146. return this.carLatterArr.slice(30, this.carLatterArr.length)
  147. },
  148. computeDeleteTop: function() {
  149. return 80 * 3 + 10 * 3
  150. },
  151. computeItemSpace: function() {
  152. return (1 - 0.095 * 10) / 11 * 100
  153. },
  154. computedDefaultValueLength: function() {
  155. return this.$props.defaultValue.length
  156. },
  157. randomNumberArr: function() {
  158. if(this.$props.randomNumber){
  159. this.numberArr.sort(function() {
  160. return 0.5 - Math.random()
  161. })
  162. }
  163. const size = this.numberArr.length
  164. this.numberArr.splice(size - 1, 0, "-1")
  165. this.numberArr.push("-2")
  166. return this.numberArr
  167. }
  168. },
  169. watch: {
  170. show: function(value) {
  171. }
  172. },
  173. onLoad() {},
  174. methods: {
  175. nextBtnClick(type){
  176. let that = this
  177. if (this.fatherMethod) {
  178. this.fatherMethod(type);
  179. }
  180. },
  181. formatCarProvinceFirst(value) {
  182. const list = ['使', '领', '警', '学', '港', '澳', ]
  183. return list.indexOf(value)
  184. },
  185. formatCarProvinceSecond(value) {
  186. const list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
  187. return list.indexOf(value)
  188. },
  189. handleCloseKeyboard() {
  190. this.open(false)
  191. },
  192. open(value) {
  193. this.show = value
  194. if(!value&&this.keyboardtype == 'car'){
  195. this.carType=0
  196. }
  197. },
  198. //车牌tab点击事件
  199. handleCarTabItemClick(index) {
  200. this.carType = index
  201. },
  202. //车牌键盘点击事件
  203. handleCarKerboardClick(e) {
  204. const keyValue = e
  205. let value = this.$props.defaultValue
  206. if (/^[\u4e00-\u9fa5]*$/.test(value) && value.length >= 2) {
  207. value = ''
  208. }
  209. var pattern = new RegExp("[\u4E00-\u9FA5]+");
  210. var pattern2 = new RegExp("[A-Za-z]+");
  211. var pattern3 = new RegExp("[0-9]+");
  212. if (value.length == 0) {
  213. if (!pattern.test(keyValue) || this.formatCarProvinceFirst(keyValue) !== -1) {
  214. return
  215. }
  216. this.carType = 1
  217. } else if (value.length == 1) {
  218. if (!pattern2.test(keyValue)) {
  219. return
  220. }
  221. } else {
  222. if (value.length === (this.$props.newCar ? 7 : 6) && pattern.test(keyValue) && this
  223. .formatCarProvinceFirst(keyValue) == -1) {
  224. return
  225. }
  226. if (pattern.test(keyValue) && value.length !== (this.$props.newCar ? 7 : 6)) {
  227. return
  228. }
  229. }
  230. if (value.length < 7 && !this.$props.newCar) {
  231. this.$emit('keyboardClick', {
  232. value: value + keyValue
  233. })
  234. }
  235. if (value.length < 8 && this.$props.newCar) {
  236. this.$emit('keyboardClick', {
  237. value: value + keyValue
  238. })
  239. }
  240. },
  241. //车牌键盘删除事件
  242. handleCarKerboardDeleteClick() {
  243. let deleteValue = this.$props.defaultValue
  244. if (deleteValue == '' || (/^[\u4e00-\u9fa5]*$/.test(deleteValue) && deleteValue.length !== 1)) {
  245. return
  246. }
  247. if (deleteValue.length > 0) {
  248. let count = deleteValue.length
  249. this.$emit('keyboardClick', {
  250. value: deleteValue.substr(0, count - 1)
  251. })
  252. }
  253. },
  254. //数字键盘点击事件
  255. handleNumberKeyboardClick(e) {
  256. const keyValue = e
  257. if (this.$props.keyboardtype == 'number' || this.$props.keyboardtype == 'digit' || this.$props
  258. .keyboardtype == 'idcard') {
  259. switch (keyValue) {
  260. case '-1':
  261. if (this.$props.keyboardtype == 'number' || this.$props.defaultValue == '') {
  262. return
  263. }
  264. let value = this.$props.defaultValue
  265. if (/^[\u4e00-\u9fa5]*$/.test(value) || value.indexOf('.') > -1 || value.indexOf('X') > -1) {
  266. return
  267. }
  268. if (this.$props.keyboardtype == 'idcard') {
  269. if (value.length === 17) {
  270. this.$emit('keyboardClick', {
  271. value: value + 'X'
  272. })
  273. }
  274. }
  275. if (this.$props.keyboardtype == 'digit') {
  276. this.$emit('keyboardClick', {
  277. value: value + '.'
  278. })
  279. }
  280. break
  281. case '-2':
  282. let deleteValue = this.$props.defaultValue
  283. if (deleteValue == '' || /^[\u4e00-\u9fa5]*$/.test(deleteValue)) {
  284. return
  285. }
  286. if (deleteValue.length > 0) {
  287. let count = deleteValue.length
  288. this.$emit('keyboardClick', {
  289. value: deleteValue.substr(0, count - 1)
  290. })
  291. }
  292. break
  293. default:
  294. let initValue = this.$props.defaultValue
  295. if (/^[\u4e00-\u9fa5]*$/.test(initValue)) {
  296. initValue = ''
  297. }
  298. if (this.$props.keyboardtype == 'idcard') {
  299. if (initValue.length < 18) {
  300. this.$emit('keyboardClick', {
  301. value: initValue + keyValue
  302. })
  303. }
  304. return
  305. }
  306. this.$emit('keyboardClick', {
  307. value: initValue + keyValue
  308. })
  309. break
  310. }
  311. }
  312. }
  313. }
  314. }
  315. </script>
  316. <style scoped lang="scss">
  317. @import './master-keyboard.scss';
  318. .next-btn{
  319. display: flex;
  320. justify-content: space-evenly;
  321. .number_next{
  322. width: 45%;
  323. }
  324. }
  325. </style>