最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • 代理模式-缓存代理实现日期选择器

    正文概述 掘金(邢黎)   2020-12-23   369

    缓存代理

    保存输入和结果,建立映射关系,下次输入相同时可以直接从缓存读取结果,避免复杂耗时的判断和操作

    日期选择器主要难点

    • 平闰年切换2月天数数组
    • 如果是首年/末年,更新月份数组,如果月份数组里找不到当前月,则重置月份
      • 如果当前月份为首年首月/末年末月,更新日期数组, 如果日期数组里找不到当前日期,则重置日期
    • 重置月份和日期的规则
      • 如果当前月 < 月份数组第一位, 则重置为月份数组第一位,例如startDate = 2019/3/10,当前选择结果为2020/2/11,重置后的结果为2019/3/11
      • 如果当前月 > 月份数组最后一位, 则重置为月份数组最后一位,例如当前结果为 2020/2/29,将年份切换到2019年,2月只有28天,所以重置后的结果为2019/2/28

    优化方案

    每次切换时间都生成一个mapKey,值为当前的年月日,然后进行计算和重置生成最终的年月日以及月日选项数组,然后把mapKey和最终的结果关联起来。这样下一次切换时如果年月日这个mapKey存在,则直接得到最终的结果。

    const mapKey = `${this.tempDate.year}/${this.tempDate.month}/${this.tempDate.day}`;
    let tmpMap = this.map[mapKey];
    // 如果有缓存,则直接返回月、月数组、日、日数组
    if (tmpMap) {
      this.date.day = tmpMap.dayArr;
      this.tempDate.day = tmpMap.day;
      this.date.month = tmpMap.monthArr;
      this.tempDate.month = tmpMap.month;
      console.log("useMap", tmpMap);
    } else {
      // 初始化每月天数数组为平年
      let arr: string[] = this.ordinaryMonth;
      let tempDateM = parseInt(this.tempDate.month, 10);
      const tempDateY = parseInt(this.tempDate.year, 10);
      if (tempDateM === 2 && DateExt.prototype.isLeapYear(tempDateY)) {
        // 初每月天数数组改为平年
        arr = this.leapMonth;
      }
      // 根据当前月份从字典取出当前月份天数数组
      let dayArr = this.dayDict[parseInt(arr[tempDateM - 1], 10)];
      // 标记首年末年
      let specialY: any = false;
      // 根据当前年份更新月份数组
      if (tempDateY === this.startDateExt.year) {
        this.date.month = this.startDateExt.monthArr;
        specialY = this.startDateExt;
      } else if (tempDateY === this.endDateExt.year) {
        this.date.month = this.endDateExt.monthArr;
        specialY = this.endDateExt;
      } else {
        this.date.month = this.monthDict;
      }
      // 对月年份为首年末年的情况,当前月份小于首月则重置成首月,大于末月则重置为末月, 处于首月末月的时候需要重置日期数组
      if (specialY) {
        if (
          this.tempDate.month >= this.date.month[this.date.month.length - 1]
        ) {
          this.tempDate.month = this.date.month[this.date.month.length - 1];
          this.date.day = specialY.dayArr;
        } else if (this.tempDate.month <= this.date.month[0]) {
          this.tempDate.month = this.date.month[0];
          this.date.day = specialY.dayArr;
        }
      } else {
        this.date.day = dayArr;
      }
      // 当前日期小于首日则重置为首日,大于末日则重置为末日
      if (this.tempDate.day >= this.date.day[this.date.day.length - 1]) {
        this.tempDate.day = this.date.day[this.date.day.length - 1];
      } else if (this.tempDate.day <= this.date.day[0]) {
        this.tempDate.day = this.date.day[0];
      }
      this.map[mapKey] = {
        month: this.tempDate.month,
        monthArr: this.date.month,
        day: this.tempDate.day,
        dayArr: this.date.day,
      };
    }
    this.resetOption("month");
    this.resetOption("day");
    

    起源地下载网 » 代理模式-缓存代理实现日期选择器

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元