小程序封装自定义键盘-车牌号键盘

<template>
  <view class="license">
    <view class="wrapperInfo">
    <view class="basicsInfo_license">
      <view class="basicsInfo_">请输入要办理的车牌号</view>
       <view class="license_s">
         <view :class="item.way === 'OIL' 
                  ? licenseDataIndex === index 
                  ? 'board boardYes' : 'board boardNo'
                  : licenseDataIndex === 7
                  ? 'boardFar boardFar-Yes'
                  : 'boardFar boardFar-No' " 
                  v-for="(item, index) in license" 
                  @click="typingInfo(index)"
                  :key="index">
                     {{ item.name }}
                    <text v-if="item.name === '' && item.way === 'NEW'" class="jiaFar" >+</text>
                    <text v-if="item.name === '' && item.way === 'NEW'" class="textar" >新能源</text>
         </view>
       </view>
      </view>
    </view>

    <chepan :startUsingKey = "startUsingKey" 
            :addType="addType" :keyNumber="keyNumber"
            @transmitAdd="transmitAdd"  @keyDelete = "keyDelete"
            @closeKye = "closeKye"
    />
  </view>
</template>

<script>
import chepan from './test.vue'
export default {
 name: 'test',
 components:{chepan},
 data() {
 return {
     // 车牌框
      license: [
        { name: "", way: "OIL" }, //京
        { name: "", way: "OIL" }, //f
        { name: "", way: "OIL" }, //0
        { name: "", way: "OIL" }, //9
        { name: "", way: "OIL" }, //2
        { name: "", way: "OIL" }, //3
        { name: "", way: "OIL" }, //k
        { name: "", way: "NEW" }, //?
      ],
      licenseDataIndex:null,
      startUsingKey:false, // 显示键盘
      addType:true , //显示不同的键盘字或字母
      keyNumber: null, //输入第几个职工
      // 处理车牌 传给后端的值
      restsFrom:{
        licenseData:[]
      }
 };
 },
 created() {

 },
 mounted() {

 },
 methods: {

  // 点击输入车牌
  typingInfo(index){
    console.log('按键下标', index)
    if(index === 0 ){
        this.licenseDataIndex = 0;
        this.addType = true;
    }else if (index < 8) {
        this.licenseDataIndex = index;
        this.addType = false;
      }
     this.keyNumber = index;
     this.startUsingKey = true
  },

 // 进行自动下步
  transmitAdd(item){
      console.log(item,'子组件传进来的值');
      const { keyNumber } = this;
      this.license[keyNumber].name = item;
      if (keyNumber != 7) {
        // 选中一次递进
        this.typingInfo(keyNumber + 1);
      }
      this.takeKeyData();
  },

  // 删除键盘输入内容
    keyDelete() {
      const { keyNumber } = this;
      this.license[keyNumber].name = "";
      if (Number(keyNumber) > 0) {
        // 选中一次递减
        this.typingInfo(keyNumber - 1);
      }
      this.takeKeyData();
    },

  // 点确定和取消 删除第一个车牌汉字
  closeKye(e){
     console.log('点确定和取消 删除第一个车牌汉字', e)
      this.startUsingKey = e; //关闭
      this.keyNumber = null;  //清除下标
      this.licenseDataIndex = null;  //清除下标
      this.addType == true;  // 变成汉字键盘
  },

  // 车牌处理
  takeKeyData(){
    this.restsFrom.licenseData = [];
      this.license.forEach((item, index) => {
        if (item.name) {
          this.restsFrom.licenseData.push(item.name);
        }
      });
      console.log(this.restsFrom.licenseData, "车牌处理传给后端的值");
  },
 }
};
</script>

<style scoped lang="scss">
 .license{
    background: #f7f7f7;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    justify-content: space-between;
    width: 100%;
    height: 100%;
    .wrapperInfo {
    height: calc(100% - 270rpx);
    overflow-y: scroll;
    box-sizing: border-box;
    padding: 24rpx 24rpx 222rpx;
    .basicsInfo_license{
       box-sizing: border-box;
       background: #ffffff;
       padding: 48rpx 24rpx 20rpx 36rpx;
       border-radius: 24rpx;
      .basicsInfo_{
        padding-top: 30rpx;
        font-size: 30rpx;
        color: #000;
        text-align: center;
      }
     .license_s{
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: 32rpx;
        .board{
            width: 64rpx;
            height: 92rpx;
            box-sizing: border-box;
            border-radius: 8rpx;
            opacity: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 36rpx;
            font-family: PingFang SC-Regular, PingFang SC;
            font-weight: 400;
        }
        .boardYes {
            background: #ffffff;
            border: 2rpx solid #26cf6d;
            color: #26cf6d;
         }
          .boardNo {
            background: #ffffff;
            border: 2rpx solid #efefef;
            color: #1c1c1c;
          }
         .boardFar {
            box-sizing: border-box;
            border-radius: 8rpx;
            opacity: 1;
            font-weight: 400;
            .jiaFar {
              margin-bottom: 8rpx;
              font-size: 25rpx;
              padding: 0rpx;
              color: #26cf6d;
            }
            .textar {
              font-size: 17rpx;
              font-family: PingFang SC-Regular, PingFang SC;
              font-weight: 400;
              color: #26cf6d;
              padding: 0rpx;
              margin: 0rpx;
            }
          }
          .boardFar-No {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            background: #ebfff3;
            // padding: 17rpx 10rpx 17rpx;
            width: 64rpx;
            font-size: 36rpx;

            height: 92rpx;
            box-sizing: border-box;
          }
          .boardFar-Yes {
            width: 64rpx;
            height: 92rpx;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            background: #ebfff3;
            border: 2rpx dashed #26cf6d;
            font-size: 36rpx;
          }
       }
      }
    }
 }
</style>


//组件
<template>
  <view v-if="startUsingKey" class="keyBoard-max">
    <view class="keyboard">
      <view class="keyboard-top">
        <view class="buts" @click="closeKyes">取消</view>
        <view class="buts">车牌号键盘</view>
        <view style="color: #303133" class="buts" @click="closeKyes">确定</view>
      </view>
      <!-- 按键区 -->
      <view class="keyboard-bott">
         <view  v-if="addType" class="chineseKeys">
           <!-- 第一行 -->
           <view class="keys">
            <view
              @click="inputKeyboard(item)"
              class="chinese-key"
              v-for="(item, index) in provincesOne"
              :key="index"
              >
                 {{ item }}
              </view>
           </view>
             <!-- 第二行 -->
           <view class="keys">
            <view
              @click="inputKeyboard(item)"
              class="chinese-key"
              v-for="(item, index) in provincesTwo"
              :key="index"
              >
                 {{ item }}
              </view>
           </view>
             <!-- 第三行 -->
           <view class="keys">
            <view
              @click="inputKeyboard(item)"
              class="chinese-key"
              v-for="(item, index) in provincesThre"
              :key="index"
              >
                 {{ item }}
              </view>
           </view>
                <!-- 第四行 -->
           <view class="keys">
            <view @click="inputKeyboard(item)" class="chinese-key">
                 {{ provincesbut }}
            </view>

            <view class="chinese-key-delete" @click="keyDeletes"></view>  
           </view>
         </view>

          <!-- 数字 和 字母 -->
          <view v-if="!addType" class="alphanumericKey">
           <!-- 第一行数字 -->
            <view  class="keys">
                <view
                  @click="inputKeyboard(item)"
                  :class="Number(keyNumber) === 1  ? 'chinese-key chinese-key-no' : 'chinese-key'"
                  v-for="(item, index) in numbersOne"
                  :key="index"
                 >
                  {{ item }}
                 </view>
             </view>
               <!-- 第二行字母 -->
            <view  class="keys">
                <view
                 :class=" Number(keyNumber) > 1 && item === 'O'? 'chinese-key chinese-key-no' : 'chinese-key'"
                  @click="inputKeyboard(item)"
                  class="chinese-key"
                  v-for="(item, index) in numbersTwo"
                  :key="index"
                 >
                  {{ item }}
                 </view>
             </view>
               <!-- 第三行字母 -->
             <view  class="keys">
                <view
                  @click="inputKeyboard(item)"
                  class="chinese-key"
                  v-for="(item, index) in numbersThree"
                  :key="index"
                 >
                  {{ item }}
                 </view>
              </view>
                <!-- 第四行字母 -->
              <view  class="keys">
                 <view
                  @click="inputKeyboard(item)"
                  v-for="(item, index) in numbersbut"
                  :key="index"
                  :class="Number(keyNumber) != 6 && (item === '港' || item === '澳' || item === '学')
                    ? 'chinese-key chinese-key-no'
                    : 'chinese-key'"
                 >
                  {{ item }}
                 </view>
                 <view class="chinese-key-delete" @click="keyDeletes"></view>
              </view>
          </view>
        </view>
      </view>
  </view>
</template>

<script>
export default {
 name: 'test',
 components: '',
 props:{
    startUsingKey:{
      type: Boolean,
      default: false,
    },
    addType:{
      type: Boolean,
      default: true,
    },
    keyNumber:{
      default: null,
    },
 },
 data() {
 return {
    // 省份简写  (31)
      provincesOne: [  "京", "沪","粤", "津","冀","晋","蒙","辽","吉", "黑",  ],
      provincesTwo: [ "苏",  "浙","皖","闽", "赣", "鲁", "豫","鄂","湘", "桂", ],
      provincesThre: [ "琼", "渝", "川",  "贵", "云",  "藏","陕","甘", "青","宁",],
      provincesbut: ["新"],
      // 车牌输入  (10数字 + 25字母 + 3特殊汉子)
      numbersOne: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
      numbersTwo: ["A", "B", "C", "D", "E", "F", "G", "H", "J", "O"],
      numbersThree: ["L", "M", "N", "K", "P", "Q", "R", "S", "T", "U"],
      numbersbut: ["港", "澳", "学", "V", "W", "X", "Y", "Z"]
 };
 },
 created() {

 },
 mounted() {

 },
 methods: {
  //  汉字按键
   inputKeyboard(text){
    console.log(text,'汉字按键');
     let ind = Number(this.keyNumber);
      console.log(ind,'父组件下标');
      if (ind > 1 && ind != 6) {
        console.log(11111111111);
        if ((text != "港") & (text != "澳") & (text != "学") && text != "O") {
          this.$emit("transmitAdd", text);
        }
      } else if (ind != 6) {
         console.log(22222222222);
        if ((text != "港") & (text != "澳") & (text != "学")) {
          this.$emit("transmitAdd", text);
        }
      } else if (ind > 1) {
         console.log(33333333333);
        if (text != "O") {
          this.$emit("transmitAdd", text);
        }
      }
   },
  //  删除
  keyDeletes(){
     this.$emit('keyDelete')
  },
  //  取消  确定
  closeKyes(){
    this.$emit("closeKye",false);
  },
 }
};
</script>

<style scoped lang="scss">
 .keyBoard-max{
      box-shadow: 0px 2px 8px 0px rgba(171, 171, 171, 0.5);
      position: fixed;
      bottom: 0rpx;
      z-index: 999;
      width: 100%;
      height: 604rpx;
      box-sizing: border-box;
      padding-bottom: 68rpx;
      background-color: #ffffff;
   .keyboard{
      box-sizing: border-box;
      width: 100%;
      background: #f7f7f7;
     .keyboard-top{
          display: flex;
          justify-content: space-between;
          align-items: center;
          width: 100%;
          box-sizing: border-box;
          padding: 22rpx 32rpx;
          background-color: #ffffff;
       .buts{
          font-size: 32rpx;
          font-family: PingFang SC-Regular, PingFang SC;
          font-weight: 400;
          color: #909399;
       }
     }

     .keyboard-bott{
        box-sizing: border-box;
        width: 100%;
        background: #f7f7f7;
      .chineseKeys{
        width: 100%;
        height: 100%;
        box-sizing: border-box;
        padding: 16rpx 22rpx 16rpx 24rpx;
      }
      .alphanumericKey{
        width: 100%;
        height: 100%;
        box-sizing: border-box;
        padding: 16rpx 22rpx 16rpx 24rpx;
      }

        .keys{
            width: 100%;
            height: 92rpx;
            margin-bottom: 16rpx;
            box-sizing: border-box;
            display: flex;
            justify-content: space-between;
        }
        .chinese-key{
          width: 64rpx;
          height: 92rpx;
          background: #ffffff;
          box-shadow: 0rpx 2rpx 0rpx 0rpx #898a8d;
          border-radius: 8rpx;
          opacity: 1;
          color: #303133;
          font-size: 36rpx;
          font-weight: 400;
          display: flex;
          margin-bottom: 16rpx;
          align-items: center;
          justify-content: center;
        }
        .chinese-key-no {
          color: #bababa !important;
        }
     }
   }
   // 删除键
      .chinese-key-delete {
        width: 134rpx;
        height: 92rpx;
        box-shadow: 0rpx 2rpx 0rpx 0rpx #898a8d;
        border-radius: 8rpx;
        opacity: 1;
        background: #c0c4cc
          url("https://static.igoodsale.com/ETC/chaxun-jianpan-shancu.png")
          no-repeat center;
        background-size: 55%;
      }
 }
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值