Newer
Older
smartKitchenMiniProgram / packageA / home_center / common_panel / components / Raw / index.js
dutingting on 22 Nov 2022 2 KB 1.0.1
// pages/home_center/common_panel/components/Integer/index.js
import { getDateTime, getZoneName } from '../../../../../common/utils';
import { setTimeAdd, getTimeList, delTimeList, delAllTimeList } from '../../../../../utils/api/dingshi';
import { getDeviceDetails } from '../../../../../utils/api/device-api'
Component({
  options: {
    styleIsolation: 'shared'
  },
  /**
   * 组件的属性列表
   */
  properties: {
		device_id: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    minDate: new Date().getTime(),
		currentDate: new Date().getTime(),
		closeTime: '',
  },
	async attached() {
		const { device_id } = this.properties;
		//获取定时任务列表
		const responseList = await getTimeList(device_id);
		const timeList = responseList[0].groups;
		const delcategory = responseList[0].category.category;
		//删除已经完成的定时任务
		timeList.forEach(item => {
			if(item.timers[0].status === 0) {
				const param = {
					device_id: device_id,
					category: delcategory,
					group_id: item.id
				}
				delTimeList(param);
			}
		});
	},
  methods: {
		//点击定时关闭
		timerClose() {
			this.setData({
				show: true,
			})
		},
		closePopup() {
			this.setData({
				show: false,
			})
		},
		onConfirm: async function(event) {
			let sq = getZoneName();
			console.log('时区', sq)
			if(event.detail <= Date.now()) {
				wx.showToast({
					title: `选择的时间应大于当前`,
					icon: 'none'
				});
				return false;
			}
			this.setData({
				currentDate: event.detail
			})
			//关闭弹出框
			this.closePopup();
			const { device_id } = this.properties;
			const responseList = await getTimeList(device_id);
			const timeList = responseList[0].groups;
			if(timeList.length >= 30) {
				wx.showToast({
					title: `定时任务最多只能添加30个`,
					icon: 'none'
				});
				return false;
			}
			//获取设备详情
			const res = await getDeviceDetails(device_id);
			const {str1: date, str2: time} = (getDateTime(event.detail));
			this.setData({
				closeTime: date + ' ' + time
			})
			//添加定时任务的参数
			const param = {
				device_id: res.id,
				loops: "0000000",
				category: res.category,
				timezone_id: sq,
				time_zone: "+8:00",
				instruct:[{
					functions:[
						{
							code: "switch",
							value: false
						},
					],
					date: date,
					time: time
					}
				]
			}
			//添加定时任务
			const setTimeAddRes = await setTimeAdd(param);
			wx.showToast({
				title: '设置成功',
				icon: 'none'
			});
		}
  }
})