vue中实现cron解析、反解析

参考:https://github.com/penggy/easy-cron-input

<template>
  <div id="easy-cron-input">
    <div class="cron-select-period">
      <label>选择周期</label>
      <select class="cron-period-select" v-model="period" @change="periodChange">
        <option :value="item" v-for="(item, index) of periodOpts" :key="index">{{item}}</option>
      </select>
    </div>
    <div class="cron-input cron-seconds" v-show="period == ''">
      <p><select class="cron-seconds-select" v-model="nsec" @change="onChange">
          <option v-for="(item, index) of 60" :key="index" :value="item">{{item}}</option>
        </select></p>
    </div>
    <div class="cron-input cron-minutes" v-show="period == ''">
      <p><select class="cron-minutes-select" v-model="nmin" @change="onChange">
          <option v-for="(item, index) of 60" :key="index" :value="item">{{item}}</option>
        </select>
        分钟
      </p>
    </div>
    <div class="cron-input cron-hourly" v-show="period == ''">
      <p><select class="cron-hourly-select" v-model="nhour" @change="onChange">
          <option v-for="(item, index) of 24" :key="index" :value="item">{{item}}</option>
        </select>
        小时
      </p>
    </div>
    <div class="cron-input cron-daily" v-show="period == ''">
      <p><select class="cron-daily-select" v-model="nday" @change="onChange">
          <option v-for="(item, index) of 100" :key="index" :value="item">{{item}}</option>
        </select></p>
    </div>
    <div class="cron-input cron-weekly" v-show="period == ''">
      <p>
        每周
        <input type="checkbox" name="dayOfWeekMon" v-model="dow" value="0" @change="onChange"><input type="checkbox" name="dayOfWeekTue" v-model="dow" value="1" @change="onChange"><input type="checkbox" name="dayOfWeekWed" v-model="dow" value="2" @change="onChange"><input type="checkbox" name="dayOfWeekThu" v-model="dow" value="3" @change="onChange"><input type="checkbox" name="dayOfWeekFri" v-model="dow" value="4" @change="onChange"><input type="checkbox" name="dayOfWeekSat" v-model="dow" value="5" @change="onChange"><input type="checkbox" name="dayOfWeekSun" v-model="dow" value="6" @change="onChange"></p>
    </div>
    <div class="cron-input cron-monthly" v-show="period == ''">
      <p><select class="cron-monthly-month" v-model="nmonth" @change="onChange">
          <option v-for="(item, index) of 12" :key="index" :value="item">{{item}}</option>
        </select><select class="cron-monthly-day" v-model="dom" @change="onChange">
          <option v-for="(item, index) of 31" :key="index" :value="item">{{item}}</option>
        </select></p>
    </div>
    <div class="cron-input cron-yearly" v-show="period == ''">
      <p>
        每年
        <select class="cron-yearly-month" v-model="moy" @change="onChange">
          <option v-for="(item, index) of 12" :key="index" :value="item">{{item}}</option>
        </select><select class="cron-yearly-day" v-model="dom" @change="onChange">
          <option v-for="(item, index) of 31" :key="index" :value="item">{{item}}</option>
        </select></p>
    </div>
    <div class="cron-input cron-start-time" v-show="period == '' || period == '' || period == '' || period == ''">
        <select class="cron-clock-hour" v-model="hour" @change="onChange">
          <option v-for="(item, index) of 24" :key="index" :value="item - 1">{{('00' + (item - 1)).slice(-2)}}</option>
        </select><select class="cron-clock-minute" v-model="min" @change="onChange">
          <option v-for="(item, index) of 60" :key="index" :value="item - 1">{{('00' + (item - 1)).slice(-2)}}</option>
        </select></div>
  </div>
</template>

<script>
export default {
  name: "easy-cron-input",
  props: {
    value: {
      type: String,
      default: "* * * * * *"
    }
  },
  data() {
    return {
      periodOpts: ["秒", "分", "时", "天", "周", "月", "年"],
      nthWeekOpts: ["第一个", "第二个", "第三个", "第四个"],
      dayOfWeekOpts: ["一", "二", "三", "四", "五", "六", "日"],
      period: "秒",
      dow: [],
      dom: 1,
      moy: 1,
      min: 0,
      hour: 0,
      nsec: 1,
      nmin: 1,
      nhour: 1,
      nday: 1,
      nmonth: 1
    };
  },
  mounted() {
    //反解析时传入  0 6 0 */5 * *即可
    this.setExpress(this.value);
    this.onChange();
  },
  methods: {
    periodChange() {
      this.dow = [];
      this.dom = 1;
      this.moy = 1;
      this.min = 0;
      this.hour = 0;
      this.nsec = 1;
      this.nmin = 1;
      this.nhour = 1;
      this.nday = 1;
      this.nmonth = 1;
      this.onChange();      
    },
    onChange() {
      let ex=this.getExpress();
      let pr=this.getText();
      console.log(ex,pr);
    },
    getExpress() {
      var sec = 0; // ignoring seconds by default
      var year = "*"; // every year by default
      var dow = "*",
        dom = "*",
        moy = "*";
      var hour = this.hour,
        min = this.min;
      switch (this.period) {
        case "秒":
          if (this.nsec > 1) {
            sec = "*/" + this.nsec;
          } else {
            sec = "*";
          }
          hour = "*";
          min = "*";
          break;
        case "分":
          if (this.nmin > 1) {
            min = "*/" + this.nmin;
          } else {
            min = "*";
          }
          hour = "*";
          break;
        case "时":
          if (this.nhour > 1) {
            hour = "*/" + this.nhour;
          } else {
            hour = "*";
          }
          min = 0;
          break;
        case "天":
          if (this.nday > 1) {
            dom = "*/" + this.nday;
          }
          break;
        case "周":
          if (this.dow.length > 0 && this.dow.length < 7) {
            dow = this.dow.sort().join(",");
          }
          break;
        case "月":
          if (this.nmonth > 1) {
            moy = "*/" + this.nmonth;
          }
          dom = this.dom;
          break;
        case "年":
          moy = this.moy;
          dom = this.dom;
          break;
        default:
          break;
      }
      return [sec, min, hour, dom, moy, dow].join(" ");
    },
    setExpress(express) {
      express = express || "* * * * * *";
      var crons = express.split(/\s+/);
      if (crons.length == 6) {
        if (crons[0] == "*") {
          this.period = '秒';
          this.nsec = 1;
          return;
        }
        if (crons[0].split("/").length == 2) {
          this.period = '秒';
          this.nsec = crons[0].split('/')[1];
          return;
        }
        if (crons[1] == "*") {
          this.period = '分';
          this.nmin = 1;
          return;
        }
        if (crons[1].split("/").length == 2) {
          this.period = '分';
          this.nmin = crons[1].split('/')[1];
          return;
        }
        if (crons[2] == "*") {
          this.period = '时';
          this.nhour = 1;
          return;
        }
        if (crons[2].split("/").length == 2) {
          this.period = '时';
          this.nhour = crons[2].split('/')[1];
          return;
        }
        if (crons[3] == "*" && crons[5] == "*") {
          this.period = '天';
          this.nday = 1;
          this.hour = crons[2];
          this.min = crons[1];
          return;
        }
        if (crons[3] == "*" && crons[5] != "*") {
          this.period = '周';
          this.dow = crons[5].split(',');
          this.hour = crons[2];
          this.min = crons[1];     
          return;   
        }
        if (crons[3].split("/").length == 2) {
          this.period = '天';
          this.nday = crons[3].split('/')[1];
          this.hour = crons[2];
          this.min = crons[1];  
          return;      
        }
        if (crons[4] == "*") {
          this.period = '月';
          this.nmonth = 1;
          this.dom = crons[3];
          this.hour = crons[2];
          this.min = crons[1];
          return;        
        }
        if (crons[4].split("/").length == 2) {
          this.period = '月';
          this.moy = crons[4].split('/')[1];
          this.dom = crons[3];
          this.hour = crons[2];
          this.min = crons[1];
          return;          
        }
        if (/\d+/.test(crons[4])) {
          this.period = '年';
          this.moy = crons[4];
          this.dom = crons[3];
          this.hour = crons[2];
          this.min = crons[1];
          return;        
        }
      }      
    },
    getText() {
      var text = "";
      switch(this.period) {
        case '秒':
        text += "每";
        text += this.nsec;
        text += "秒";
        break;
        case '分':
        text += "每";
        text += this.nmin;
        text += "分钟";
        break;
        case '时':
        text += "每";
        text += this.nhour;
        text += "分钟";        
        break;
        case '天':
        text += "每";
        text += this.nday;
        text += "天";   
        text += ("00" + this.hour).slice(-2);
        text += "时";
        text += ("00" + this.min).slice(-2);
        text += "分";     
        break;
        case '周':
        if(this.dow.length > 0 && this.dow.length < 7) {
          text += "每周";
          var _dow = this.dow.sort().map(v => {
            return this.dayOfWeekOpts[v];
          })
          text += _dow.join(",");          
        } else {
          text += "每天";
        }
        text += ("00" + this.hour).slice(-2);
        text += "时";
        text += ("00" + this.min).slice(-2);
        text += "分";         
        break;
        case '月':
        text += "每";
        text += this.nmonth;
        text += "月";
        text += this.dom;   
        text += "日";
        text += ("00" + this.hour).slice(-2);
        text += "时";
        text += ("00" + this.min).slice(-2);
        text += "分";         
        break;
        case '年':
        text += "每年";
        text += this.moy;
        text += "月";
        text += this.dom;
        text += "日";   
        text += ("00" + this.hour).slice(-2);
        text += "时";
        text += ("00" + this.min).slice(-2);
        text += "分";         
        break;
        default:
        break;
      }
      return text;
    }    
  }
};
</script>

<style lang="less" scoped>
.cron-select-period {
  padding: 5px 5px 0px 5px;
  display: inline-block;
}
.cron-input {
  padding: 5px 5px 0px 5px;
  display: inline-block;
}
.cron-select-period select,
.cron-input select,
.cron-input input[type="radio"],
.cron-input input[type="checkbox"] {
  margin-left: 5px;
  margin-right: 5px;
}
</style>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值