// template/admission-box/admission-box.js var miucms = require('../../utils/miucms.js'); var common = require('../../utils/commonMethod.js'); const util = require("../../utils/util") const app = getApp() Component({ /** * 组件的属性列表 */ properties: { initFinish: { type: Boolean, value: false, observer(res) { if (res) this.getAdmissionsOfficer() } }, }, /** * 组件的初始数据 */ data: { urls: [], countdownInterval: null, countDown: { days: 0, hours: '00', minutes: '00', seconds: '00', }, admissionCurrent: 0, countDownState: false, }, /** * 组件的方法列表 */ methods: { getAdmissionsOfficer() { util.wxget('/api/project.other/admissionOfficerIn').then(res => { if (res.code != 200) return const data = res.data || [] const today = new Date() today.setHours(0, 0, 0, 0) let list = [] for (var i in data) { const item = data[i]; for (var ii in item) { const ite = item[ii]; list.push(...ite) } } let urls = [] console.log("list", list); list.forEach(element => { element.urls.forEach(ele => { urls.push({ ...ele, logo: element.schoollogo }) }) }) urls.sort((a, b) => b.rank - a.rank); urls.forEach(element => { let mold = null let timestamp = "" if (!element.date || (typeof element.date == 'string' && element.date.indexOf("答疑") >= 0)) mold = 3; else { const date = new Date(element.date); if (!isNaN(date.getTime())) { if (date >= today) mold = 1 else mold = 2 if (Date.now() < date.getTime()) timestamp = true } else mold = 3 } element['mold'] = mold element['timestamp'] = timestamp }) this.setData({ urls, }) this.calculateIndicator() if (urls.length != 0) this.setCountDown(0) }) }, // 0是小 1是大 calculateIndicator() { const urls = this.data.urls const current = this.data.admissionCurrent let length = urls.length let startIndex = current - 2 let endIndex = current + 2 urls[this.data.admissionCurrent]['current'] = 2 if (startIndex < 0) { endIndex = endIndex + Math.abs(startIndex) startIndex = 0 } if (endIndex >= length) { startIndex = length - 5 endIndex = length - 1 } urls.forEach((element, index) => { element['current'] = -1 if (index == startIndex || index == endIndex) element['current'] = 0 if (index > startIndex && index < endIndex) element['current'] = 1 if ((index == startIndex && index == 0) || (index == endIndex && index == length - 1)) element['current'] = 1 if (index == current) element['current'] = 2 }) this.setData({ urls, }) }, bindanimationfinish(e) { const current = e.detail.current || 0 this.setData({ admissionCurrent: current }) this.setCountDown(current) this.calculateIndicator(current) }, setCountDown(index) { const urls = this.data.urls clearTimeout(this.data.countdownInterval); if (urls[index]['timestamp']) this.startCountdown(urls[index]['date']) else { this.setData({ countDownState: false, }) } }, startCountdown(date) { clearTimeout(this.data.countdownInterval); const newTime = new Date().getTime() const dateTime = new Date(date).getTime() let timer = dateTime - newTime const days = Math.floor(timer / (24 * 60 * 60 * 1000)); const hours = Math.floor((timer % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000)); const minutes = Math.floor((timer % (60 * 60 * 1000)) / (60 * 1000)); const seconds = Math.floor(timer % (60 * 1000) / 1000); this.setData({ countDown: { days, hours: hours.toString().padStart(2, '0'), minutes: minutes.toString().padStart(2, '0'), seconds: seconds.toString().padStart(2, '0'), }, countDownState: true, }); timer = timer - 1000 if (timer > 0) this.data.countdownInterval = setTimeout(() => this.startCountdown(date), 1000) }, goPage(e) { const url = e.currentTarget.dataset.url common.goPage(url) }, } })