当前位置:   article > 正文

vue中实现cron解析、反解析_vue-cron表达式解析

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 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
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 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
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/410674
推荐阅读
  

闽ICP备14008679号