当前位置: 首页 > article >正文

uniapp 小程序 周选择器

这里贴出来的是子组件的代码,父组件只是打开了一下popup

//  打开了一下popup
$refs.popup.open('bottom')
如果不想用子组件的话,直接打开popup就可以用
<template>
  <uni-popup ref="popup" type="bottom" background-color="#fff">
    <view class="my_popup">
      <view class="my_selectDeta">
        <view class="my_button" @click="iconClose">
          取消
        </view>
        <view class="my_button" type="primary" @click="confirm">
          确定
        </view>
      </view>
      <picker-view :value="selectValue" class="picker-view" @change="pickerChange">
        <picker-view-column>
          <view v-for="(item, index) in years" :key="index" class="year-item">
            {{ item }}年
          </view>
        </picker-view-column>

        <picker-view-column>
          <view v-for="(item, index) in weeks" :key="index" class="weeks-item">
            {{ `第${index + 1}周(` + item + ')' }}
          </view>
        </picker-view-column>
      </picker-view>
    </view>
  </uni-popup>
</template>

<script>
export default {
  props: {},
  data() {
    return {
      selectValue: [0, 0],
      years: [],
      year: '', // 当前年
      // weeks: [],
      week: '', // 当前周
      myDate: new Date(),
    }
  },
  // watch: {
  //   year: {
  //     handler(newValue) {
  //       const index = this.years.findIndex((item) => item == newValue);
  //       this.selectValue = [index, 0];
  //     },
  //     deep: true // 深度监听父组件传过来对象变化
  //   },
  // },
  computed: {
    weeks() {
      const ONE_DAY = 24 * 3600 * 1000;
      let firstDay =
        new Date(this.year + '/01/01').getDay() == 0
          ? 7
          : new Date(this.year + '/01/01').getDay();
      let weeklist = [];
      let firstweekday = '';
      let endweekday = new Date(this.year + '/12/28').getTime();
      if (firstDay > 4) {
        firstweekday =
          new Date(this.year + '/01/01').getTime() +
          (8 - firstDay) * ONE_DAY;
      } else if (firstDay <= 4) {
        firstweekday =
          new Date(this.year + '/01/01').getTime() -
          (firstDay - 1) * ONE_DAY;
      }
      for (let i = 0; i < 54; i++) {
        let numWeek = i * 7 * ONE_DAY;
        let firstday = firstweekday + numWeek;
        let endday = firstday + 6 * ONE_DAY;
        if (firstday <= endweekday) {
          weeklist.push(
            `${uni.$u.timeFormat(firstday, 'mm/dd')}-${uni.$u.timeFormat(endday, 'mm/dd')}`
          );
        }
      }
      console.log("computed-weeklist", weeklist)
      return weeklist;
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    pickerChange(e) {
      const currentData = e.detail.value
      this.year = this.years[currentData[0]];
      this.week = this.weeks[currentData[1]];
      console.log("pickerChange", e, this.year, this.week);
    },
    // 初始化时的默认当前周
    init(data = new Date()) {
      for (let i = this.myDate.getFullYear(); i <= this.myDate.getFullYear() + 10; i++) {
        this.years.push(i);
      }
      let beginTime = uni.$u.timeFormat(this.getWeek(0, data), 'mm/dd')
      let endTime = uni.$u.timeFormat(this.getWeek(1, data), 'mm/dd');
      this.year = data ? new Date(data).getFullYear() : this.myDate.getFullYear()
      this.week = `${beginTime}-${endTime}`
      this.selectValue = [this.years.indexOf(this.year), this.weeks.indexOf(this.week)]
      console.log('weeks-init', this.year, this.years, this.week, this.weeks, beginTime, endTime, this.selectValue);
      // this.$nextTick(() => {
      //   this.selectValue = [this.years.indexOf(this.year), this.weeks.indexOf(this.week)]
      // })
      // this.$emit('changeWeekTime', this.dateObj)
    },
    // 获取当前周
    getWeek(type, data = "") {
      let now = new Date(data)
      let day = now.getDay() //返回星期几的某一天;
      if (!type) {
        let dayNumber = day == 0 ? 6 : day - 1
        now.setDate(now.getDate() - dayNumber)
      } else {
        let dayNumber = day == 0 ? 0 : 7 - day
        now.setDate(now.getDate() + dayNumber)
      }
      let date = now.getDate()
      let month = now.getMonth() + 1
      //年-月-日
      let s = now.getFullYear() + '-' + (month < 10 ? '0' + month : month) + '-' + (date < 10 ? '0' +
        date :
        date)
      let datebefore = now
      return datebefore
    },
    iconClose() {
      this.$refs.popup.close()
    },
    changeDateObj() {
      const [firstWeek, lastWeek] = this.week
        .split('-')
        .map((item) => item.replace('/', '-'));
      return this.year + '-' + firstWeek + '至' + this.year + '-' + lastWeek
    },
    confirm() {
      console.log("confirm", this.year, this.week)
      let obj = {
        type: '4',
        date: this.changeDateObj()
      }
      uni.setStorageSync('weChatData', obj)
      uni.reLaunch({
        url: '/hxz/weChat/index'
      })
      this.iconClose();
    }
  },
}
</script>
<style scoped lang="scss">
.my_popup {
  height: 550rpx;
  border-radius: 8rpx 8rpx 0 0;
  position: relative;
  font-weight: 500;
  color: #1b1d21;

  .my_selectDeta {
    display: flex;
    justify-content: space-between;
    padding: 20rpx 40rpx;
    box-sizing: border-box;
    color: rgb(96, 98, 102);
    border-bottom: 1px solid #f5f7f8;
  }

  .my_button {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .picker-view {
    width: 100%;
    height: 600rpx;
  }

}

.year-item,
.weeks-item {
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>



http://www.kler.cn/a/385676.html

相关文章:

  • 【电力系统】永磁同步电机调速系统带有扰动观测器
  • 速盾:cdn和反向代理的关系是什么?
  • Elasticsearch中什么是倒排索引?
  • PCL 点云分割 基于CPC算法的分割
  • goframe开发一个企业网站 rabbitmq队例15
  • 讲讲关于SNMP与智能PDU插座
  • 【机器学习】平均绝对误差(MAE:Mean Absolute Error)
  • stm32cubeide 1.16.1 在ubuntu 24.04上的安装
  • Intern大模型训练营(五):书生大模型全链路开源体系笔记
  • Python代码主要实现了一个基于Transformer和LSTM的混合模型,用于对给定数据集进行二分类任务
  • 用 Python 从零开始创建神经网络(一)
  • MeterSphere接口自动化-ForEach循环
  • 五分钟使用 CocosCreator 快速部署 TON 游戏:开发基于 ZKP 的游戏
  • 【dvwa靶场:XSS系列】XSS (Stored)低-中-高级别,通关啦
  • 华为大咖说 | 浅谈智能运维技术
  • 【1】 Kafka快速入门-从原理到实践
  • 一七七、window.performance API使用介绍
  • SQL pta习题
  • 我谈正态分布——正态偏态
  • Stored procedures in PostgreSQL
  • C++入门基础知识142—【关于C++ 友元函数】
  • 国产操作系统ctyun下安装Informix SDK开发包的方法
  • Notepad++ 更改字体大小和颜色
  • [每周一更]-(第122期):模拟面试|数据库面试思路解析
  • 类文件结构详解
  • Leecode热题100-543.二叉树的直径