Commit d5b44402 by Jaxon

feat: 督导统计 - 业务开发

1 parent 392349c6
// components/dateScreen/dateScreen.js
import api from "~/api/api";
import dayjs from "dayjs";
let currentObj;
let weekColumnsCache;
let weekColumnsPromise;
Component({
/**
* 组件的属性列表
*/
properties: {
tabSelected: {
type: Number,
observer: function (n, o) {
if (!((n ?? "") === "")) {
let { timeTypeEnum } = this.data;
const nextIndex = [1, 2].includes(n) ? n : 0;
const nextTimeType = timeTypeEnum[nextIndex];
if (n === 3 && this._hasInitConfig()) {
return;
}
if (this.data.date && this.data.dateCurrent === nextIndex && this.data.timeType === nextTimeType) {
return;
}
if ([1, 2].includes(n)) {
this.setData({
dateCurrent: n,
timeType: nextTimeType,
rightCurrent: true,
leftCurrent: false,
});
this.initDate(nextTimeType);
} else {
this.setData({
dateCurrent: 0,
timeType: nextTimeType,
rightCurrent: true,
leftCurrent: false,
});
this.initDate(nextTimeType);
}
}
},
},
initTimeDimension: {
type: Number,
value: -1,
observer: "_onInitConfigChange",
},
initStartDate: {
type: String,
value: "",
observer: "_onInitConfigChange",
},
initEndDate: {
type: String,
value: "",
observer: "_onInitConfigChange",
},
isSticky: Boolean,
customBackground: String,
setTrackData: Boolean,
},
lifetimes: {
attached: function () {
this._isAttached = true;
this._hasAppliedInit = false;
this._runInitConfigFlow();
},
detached: function () {
this._isAttached = false;
if (this._initConfigTimer) {
clearTimeout(this._initConfigTimer);
this._initConfigTimer = null;
}
},
},
/**
* 组件的初始数据
*/
data: {
// 默认展示今天日期,格式为 月/日
date: "",
minDate: dayjs().subtract(3, "year").valueOf(),
maxDate: dayjs().valueOf(),
dateCurrent: 0,
titleEnum: {
date: "选择日期",
week: "选择周",
"year-month": "选择年月",
},
timeType: "date",
weekList: [],
weekColumns: [],
dateList: [
{
title: "日",
index: 0,
},
{
title: "周",
index: 1,
},
{
title: "月",
index: 2,
},
],
timeTypeEnum: {
0: "date",
1: "week",
2: "year-month",
},
selectedDate: "",
leftCurrent: false,
rightCurrent: true,
},
/**
* 组件的方法列表
*/
methods: {
_onInitConfigChange() {
if (!this._isAttached) return;
this._hasAppliedInit = false;
if (this._initConfigTimer) {
clearTimeout(this._initConfigTimer);
}
this._initConfigTimer = setTimeout(() => {
this._initConfigTimer = null;
this._runInitConfigFlow();
}, 0);
},
_runInitConfigFlow() {
const hasInit = this._hasInitConfig();
if (hasInit) {
const dim = Number(this.data.initTimeDimension);
if (dim === 1) {
this.getWeeklyData().then(() => {
this._tryApplyInitConfig();
});
} else {
this._tryApplyInitConfig();
this.getWeeklyData();
}
} else {
this.initDate();
this.getWeeklyData();
}
},
async getWeeklyData() {
if (Array.isArray(weekColumnsCache) && weekColumnsCache.length) {
this.setData({ weekColumns: weekColumnsCache });
return;
}
if (!weekColumnsPromise) {
weekColumnsPromise = api
.getWeekSelectData()
.then((result) => {
const weekColumns =
result.data?.map((item, index) => ({
label: `${dayjs(item.startDate).format("YYYY/MM/DD")}-${dayjs(item.endDate).format("YYYY/MM/DD")}`,
startDate: dayjs(item.startDate).format("YYYY/MM/DD"),
endDate: dayjs(item.endDate).format("YYYY/MM/DD"),
index,
})) || [];
weekColumnsCache = weekColumns;
return weekColumns;
})
.catch(() => {
return [];
})
.finally(() => {
weekColumnsPromise = null;
});
}
const weekColumns = await weekColumnsPromise;
if (Array.isArray(weekColumns) && weekColumns.length) {
this.setData({ weekColumns });
}
},
// 切换日期类型
selectDate(event) {
const { index } = event.currentTarget.dataset;
const { timeTypeEnum } = this.data;
let eventCode = ["supervisor_dayTime_tab", "supervisor_weekTime_tab", "supervisor_monthTime_tab"];
if (this.data.setTrackData) {
wx.$track.setTrackData(0, eventCode[index], {}, "");
}
this.setData({
dateCurrent: index,
timeType: timeTypeEnum[index],
rightCurrent: true,
leftCurrent: false,
});
this.initDate(timeTypeEnum[index]);
},
getParams() {
let { dateCurrent, date, currentWeek } = this.data;
const obj = {};
switch (dateCurrent) {
case 0:
obj.startDay = obj.endDay = dayjs(date).format("YYYY/MM/DD");
break;
case 1:
obj.startDay = dayjs(currentWeek.startDate).format("YYYY/MM/DD");
obj.endDay = dayjs(currentWeek.endDate).format("YYYY/MM/DD");
break;
case 2:
obj.startDay = dayjs(date).startOf("month").format("YYYY/MM/DD");
obj.endDay = dayjs(date).endOf("month").format("YYYY/MM/DD");
break;
default:
console.warn("[getUsedCarBusinessData] 未知 timeType:", timeType);
}
obj.timeDimension = dateCurrent;
return obj;
},
initLoad() {
this.setData(
{
dateCurrent: 0,
leftCurrent: false,
rightCurrent: true,
},
() => {
this.initDate("date");
},
);
},
initDate(type = "date") {
const now = dayjs();
const setData = (payload) => {
this.setData(payload);
this.triggerEvent("search", this.getParams());
};
switch (type) {
case "date": {
const yesterday = now.subtract(1, "day");
setData({
date: now.format("YYYY/MM/DD"),
currentDate: now.valueOf(),
});
break;
}
case "year-month": {
const lastMonth = now.subtract(1, "month");
setData({
date: now.format("YYYY/MM"),
currentDate: now.valueOf(),
});
break;
}
case "week": {
// const secondWeek = this.data.weekColumns[1];
const firstWeek = this.data.weekColumns[0];
if (firstWeek) {
setData({
currentWeek: firstWeek,
date: `${firstWeek.startDate}-${firstWeek.endDate}`,
});
}
break;
}
default:
console.warn("[initDate] 未知 type:", type);
}
},
_hasInitConfig() {
const dim = Number(this.data.initTimeDimension);
if (![0, 1, 2].includes(dim)) return false;
return true;
},
_normalizeDateStr(input) {
if (!input) return "";
const str = String(input).trim().split(" ")[0];
const normalized = str.replace(/-/g, "/");
const d = dayjs(normalized);
return d.isValid() ? d.format("YYYY/MM/DD") : "";
},
_isSameAsCurrentQuery() {
const dim = Number(this.data.initTimeDimension);
if (![0, 1, 2].includes(dim)) return false;
const start = this._normalizeDateStr(this.data.initStartDate);
const end = this._normalizeDateStr(this.data.initEndDate);
const current = this.getParams();
return Number(current.timeDimension) === dim && this._normalizeDateStr(current.startDay) === start && this._normalizeDateStr(current.endDay) === end;
},
_tryApplyInitConfig() {
if (this._hasAppliedInit) return;
const dim = Number(this.data.initTimeDimension);
if (![0, 1, 2].includes(dim)) return;
if (this._isSameAsCurrentQuery()) {
this._hasAppliedInit = true;
return;
}
const timeTypeEnum = this.data.timeTypeEnum || {};
const timeType = timeTypeEnum[dim] || "date";
const start = this._normalizeDateStr(this.data.initStartDate);
const end = this._normalizeDateStr(this.data.initEndDate);
if (dim === 1 && (!Array.isArray(this.data.weekColumns) || !this.data.weekColumns.length)) {
return;
}
const nextData = {
dateCurrent: dim,
timeType,
};
if (dim === 0) {
const base = start || end;
if (base) {
const d = dayjs(base);
nextData.date = d.format("YYYY/MM/DD");
nextData.currentDate = d.valueOf();
} else {
const now = dayjs();
nextData.date = now.format("YYYY/MM/DD");
nextData.currentDate = now.valueOf();
}
}
if (dim === 2) {
const base = start || end;
if (base) {
const d = dayjs(base);
nextData.date = d.format("YYYY/MM");
nextData.currentDate = d.valueOf();
} else {
const now = dayjs();
nextData.date = now.format("YYYY/MM");
nextData.currentDate = now.valueOf();
}
}
if (dim === 1) {
const weekColumns = this.data.weekColumns || [];
const target = weekColumns.find((i) => i.startDate === start && i.endDate === end) || weekColumns[0];
if (target) {
nextData.currentWeek = target;
nextData.date = `${target.startDate}-${target.endDate}`;
nextData.defaultIndex = target.index;
nextData.rightCurrent = target.index === 0;
nextData.leftCurrent = target.index === weekColumns.length - 1;
}
}
if (dim === 0) {
const today = dayjs().startOf("day");
const min = dayjs(this.data.minDate).startOf("day");
const sel = dayjs(nextData.date).startOf("day");
nextData.rightCurrent = sel.isSame(today, "day");
nextData.leftCurrent = sel.isSame(min, "day");
}
if (dim === 2) {
const current = dayjs().startOf("month");
const min = dayjs(this.data.minDate).startOf("month");
const sel = dayjs(nextData.date);
nextData.rightCurrent = sel.isSame(current, "month");
nextData.leftCurrent = sel.isSame(min, "month");
}
this._hasAppliedInit = true;
this.setData(nextData, () => {
this.triggerEvent("search", this.getParams());
});
},
onDisplayDate() {
const { timeType, date, currentDate } = this.data;
if (timeType == "custom") {
this.setData(
{
show: true,
},
() => {
this._emitPopupVisible(true);
wx.nextTick(() => {
const calendar = this.selectComponent("#calendar");
if (calendar && calendar.__data__ && calendar.__data__.currentDate) {
const _calendar = calendar.__data__.currentDate.map((item) => dayjs(item).format("YYYY/MM/DD"));
const _value = date.split("-");
if (_value[0] != _calendar[0] || _value[1] != _calendar[1]) {
calendar.reset();
}
}
});
},
);
} else {
this.setData(
{
dateShow: true,
},
() => {
this._emitPopupVisible(true);
wx.nextTick(() => {
if (timeType == "date" && currentObj) {
const c_value = currentObj.getValues();
const _date = String(date).split(/[/-]/);
if (c_value[0] != _date[0] || c_value[1] != _date[1] || c_value[2] != _date[2]) {
const picker = this.selectComponent("#datePicker");
this.setData(
{
minDate: dayjs().subtract(3, "year").valueOf(),
maxDate: dayjs().valueOf(),
},
() => {
picker?.picker?.setValues?.(_date);
},
);
}
} else if (timeType == "year-month" && currentObj) {
const c_value = currentObj.getValues();
const _date = String(date).split(/[/-]/);
if (c_value[0] != _date[0] || c_value[1] != _date[1]) {
const picker = this.selectComponent("#datePicker");
this.setData(
{
minDate: dayjs().subtract(3, "year").valueOf(),
maxDate: dayjs().valueOf(),
},
() => {
picker?.picker?.setValues?.(_date);
},
);
}
} else if (timeType == "week") {
const { date, weekColumns } = this.data;
const picker = this.selectComponent("#weekPicker");
const label = date;
const index = weekColumns.findIndex((i) => i.label == label);
setTimeout(() => {
picker?.setColumnIndex?.(0, index);
}, 0);
}
});
},
);
}
},
// 取消
cancel() {
this.setData(
{
dateShow: false,
},
() => {
this._emitPopupVisible(false);
},
);
},
// 年月日确认
confirm() {
const { selectedDate, timeType, currentWeek } = this.data;
const baseDate = selectedDate || this.data.currentDate || dayjs().valueOf();
const formatMap = {
date: () => dayjs(baseDate).format("YYYY/MM/DD"),
"year-month": () => dayjs(baseDate).format("YYYY/MM"),
week: () => `${currentWeek.startDate}-${currentWeek.endDate}`,
};
const valueMap = {
date: () => dayjs(baseDate).valueOf(),
"year-month": () => dayjs(baseDate).valueOf(),
week: () => dayjs(currentWeek?.startDate, "YYYY/MM/DD").valueOf(),
};
// 边界状态统一计算
const calcBoundary = () => {
const { timeType, selectedDate, currentWeek, weekColumns, minDate } = this.data;
let rightCurrent = false;
let leftCurrent = false;
switch (timeType) {
case "date": {
const today = dayjs().startOf("day");
const min = dayjs(minDate).startOf("day");
const sel = dayjs(selectedDate || this.data.currentDate || this.data.date).startOf("day");
rightCurrent = sel.isSame(today, "day");
leftCurrent = sel.isSame(min, "day");
break;
}
case "year-month": {
const current = dayjs().startOf("month");
const min = dayjs(minDate).startOf("month");
const sel = dayjs(selectedDate || this.data.currentDate || this.data.date);
rightCurrent = sel.isSame(current, "month");
leftCurrent = sel.isSame(min, "month");
break;
}
case "week":
if (currentWeek.index === 0) rightCurrent = true;
if (currentWeek.index === weekColumns.length - 1) leftCurrent = true;
break;
default:
console.warn("[confirm] 未知 timeType:", timeType);
}
this.setData({ rightCurrent, leftCurrent });
};
calcBoundary();
this.setData(
{
dateShow: false,
currentDate: (valueMap[timeType] || (() => dayjs().valueOf()))(),
date: (formatMap[timeType] || (() => ""))(),
},
() => {
this._emitPopupVisible(false);
this.triggerEvent("search", this.getParams());
},
);
},
_emitPopupVisible(visible) {
this.triggerEvent("popupvisible", { visible }, { bubbles: true, composed: true });
},
onChange(e) {
currentObj = e.detail;
},
// 日选择
onInput(event) {
// 防止选择之后,取消选择,导致日期不变
this.setData({
selectedDate: event.detail,
});
},
// 周选择
onWeekChange(event) {
const { value } = event.detail;
const currentWeek = value;
this.setData({
currentWeek,
});
},
// 上一个月
last() {
const { timeType, merchantId, date, minDate, current } = this.data;
if (timeType == "year-month") {
// 使用 dayjs 处理年月逻辑
const currentDisplayDate = dayjs(date);
const prevMonth = currentDisplayDate.subtract(1, "month");
const leftCurrent = prevMonth.isSame(dayjs(minDate), "month");
this.setData({
date: prevMonth.format("YYYY/MM"),
currentDate: prevMonth.valueOf(),
rightCurrent: false,
leftCurrent,
});
} else if (timeType == "date") {
// 日选择:使用 dayjs 处理日期减一天
const currentDisplayDate = dayjs(date);
const prevDate = currentDisplayDate.subtract(1, "day");
const leftCurrent = prevDate.isSame(dayjs(minDate), "day");
this.setData({
date: prevDate.format("YYYY/MM/DD"),
currentDate: prevDate.valueOf(),
rightCurrent: false,
leftCurrent,
});
} else if (timeType == "week") {
const { currentWeek, weekColumns } = this.data;
if (currentWeek.index > -1) {
const prevWeek = weekColumns[currentWeek.index + 1];
const prevDate = dayjs(prevWeek.startDate, "YYYY/MM/DD");
this.setData({
date: `${prevWeek.startDate}-${prevWeek.endDate}`,
currentDate: prevDate.valueOf(),
defaultIndex: currentWeek.index + 1,
currentWeek: prevWeek,
leftCurrent: prevWeek.index === weekColumns.length - 1,
rightCurrent: false,
});
}
}
this.triggerEvent("search", this.getParams());
},
// 下一个月
next() {
const { timeType, merchantId, date, current } = this.data;
if (timeType == "year-month") {
// 获取当前显示的日期
const currentDisplayDate = dayjs(date);
// 设置为上一个月
const nextMonth = currentDisplayDate.add(1, "month");
const rightCurrent = nextMonth.isSame(dayjs(), "month");
this.setData({
date: nextMonth.format("YYYY/MM"),
currentDate: nextMonth.valueOf(),
leftCurrent: false,
rightCurrent,
});
} else if (timeType == "date") {
// 日选择
// 获取当前显示的日期
const currentDisplayDate = dayjs(date, "YYYY/MM/DD");
// 设置为下一个月
const nextDate = currentDisplayDate.add(1, "day");
const _date = nextDate.format("YYYY/MM/DD");
const _currentDate = dayjs().format("YYYY/MM/DD");
const rightCurrent = _date == _currentDate;
this.setData({
date: _date,
currentDate: nextDate.valueOf(),
rightCurrent,
leftCurrent: false,
});
} else if (timeType == "week") {
const { currentWeek, weekColumns } = this.data;
if (currentWeek.index < weekColumns.length) {
const nextWeek = weekColumns[currentWeek.index - 1];
const nextDate = dayjs(nextWeek.startDate, "YYYY/MM/DD");
this.setData({
date: `${nextWeek.startDate}-${nextWeek.endDate}`,
currentDate: nextDate.valueOf(),
defaultIndex: currentWeek.index - 1,
currentWeek: nextWeek,
rightCurrent: nextWeek.index == 0,
leftCurrent: false,
});
}
}
this.triggerEvent("search", this.getParams());
},
},
});
{
"component": true,
"usingComponents": {
"van-icon": "@vant/weapp/icon/index",
"van-datetime-picker": "@vant/weapp/datetime-picker/index",
"van-calendar": "@vant/weapp/calendar/index",
"van-picker": "@vant/weapp/picker/index"
}
}
\ No newline at end of file
/* components/dateScreen/dateScreen.wxss */
.date-screen {
display: flex;
align-items: center;
justify-content: center;
.tab-section {
box-sizing: border-box;
width: 278rpx;
height: 56rpx;
display: flex;
align-items: center;
background: #FFFFFF;
border-radius: 56rpx;
font-size: 26rpx;
color: #999999;
padding: 4rpx;
&.isSticky {
background: #F7F8FA;
}
.tab {
flex: 1;
text-align: center;
height: 48rpx;
line-height: 48rpx;
border-radius: 48rpx;
&.active{
color: #2C66F7;
font-weight: 500;
background: #2c66f71a;
}
}
}
.date-section {
box-sizing: border-box;
height: 56rpx;
flex: 1;
max-width: calc(100% - 278rpx);
display: flex;
align-items: center;
justify-content: space-around;
.date-text {
font-size: 26rpx;
text-align: center;
width: 320rpx;
flex-shrink: 0;
}
.arrow {
width: 32rpx;
}
}
}
\ No newline at end of file
<!--components/dateScreen/dateScreen.wxml-->
<view class="date-screen">
<view class="tab-section {{ isSticky ? 'isSticky' : '' }}"
style="background: {{ customBackground }};"
wx:if="{{ !tabSelected || tabSelected == 3 }}">
<block wx:for="{{ dateList }}" wx:key="index">
<view class="tab {{ item.index==dateCurrent?'active':'' }}"
data-index="{{ item.index }}"
bind:tap="selectDate">
{{ item.title }}
</view>
</block>
</view>
<view class="date-section">
<view class="arrow left">
<van-icon wx:if="{{ !leftCurrent }}"
class="arrow-left"
name="arrow-left"
bind:tap="last" />
</view>
<view class="date-text" bind:tap="onDisplayDate">
{{ date }}
</view>
<view class="arrow right">
<van-icon wx:if="{{ !rightCurrent }}"
class="arrow-right"
name="arrow"
bind:tap="next" />
</view>
</view>
</view>
<!-- 年月日 -->
<block wx:if="{{ dateShow }}">
<popup bindcancel="cancel"
bindconfirm="confirm"
show="{{ dateShow }}"
title="{{ titleEnum[timeType] }}"
height="60vh">
<block wx:if="{{ timeType == 'date' || timeType == 'year-month' }}">
<van-datetime-picker id="datePicker"
show-toolbar="{{ false }}"
min-date="{{ minDate }}"
max-date="{{ maxDate }}"
model:value="{{ currentDate }}"
type="{{ timeType=='date'?'date':'year-month' }}"
bind:change="onChange"
bind:input="onInput" />
</block>
<!-- 周 -->
<block wx:if="{{ timeType == 'week' }}">
<van-picker id="weekPicker"
show-toolbar="{{ false }}"
model:value="{{ currentDate }}"
default-index="{{ defaultIndex }}"
value-key="label"
columns="{{ weekColumns }}"
bind:change="onWeekChange" />
</block>
</popup>
</block>
<block wx:if="{{ show }}">
<van-calendar id="calendar"
max-range="{{ 31 }}"
min-date="{{ minDate }}"
max-date="{{ maxDate }}"
show="{{ show }}"
type="range"
color="#2C66F7"
bind:confirm="onConfirm"
bind:close="onClose" />
</block>
......@@ -2,6 +2,7 @@ import { schemaList, schemaCommonBrandList } from "./stationStaffPerformance.dat
import { brandTypeList } from "~/packageIndex/constant/performance.js";
import { getSupervisorStaffApi } from "~/packageIndex/api/performance.js";
const IMG_URL = "https://img02.glsx.com.cn/weapp/resource/miniprogram/supervisor-miniprogram/";
import dayjs from "dayjs";
Page({
data: {
IMG_URL,
......@@ -53,6 +54,12 @@ Page({
}
this.setData({
brandTypeList: wx.$util.deepClone(brandTypeList),
query: {
...this.data.query,
dateType: 0,
startDate: dayjs().startOf("day").format("YYYY-MM-DD"),
endDate: dayjs().endOf("day").format("YYYY-MM-DD"),
},
});
},
// onLoad(options) {
......@@ -104,6 +111,7 @@ Page({
await this.getData();
},
async handleSearch({ detail }) {
console.log("handleSearch");
this.setData({
query: {
...this.data.query,
......
......@@ -2,7 +2,7 @@
"usingComponents": {
"TypeTabs": "/packageIndex/components/typeTabs/typeTabs",
"StatisticsItem": "/packageIndex/components/statisticsItem/statisticsItem",
"date-screen": "/components/dateScreen/dateScreen",
"date-screen": "/packageIndex/components/dateScreen/dateScreen",
"van-search": "@vant/weapp/search/index"
},
"enablePullDownRefresh": true,
......
......@@ -20,9 +20,9 @@
<view class="date-filter-wrapper">
<date-screen customBackground="#f3f3f4"
tabSelected="{{ 3 }}"
initStartDate="{{ query.startDate }}"
initEndDate="{{ query.endDate }}"
initTimeDimension="{{ query.dateType }}"
init-start-date="{{ query.startDate }}"
init-end-date="{{ query.endDate }}"
init-time-dimension="{{ query.dateType }}"
bind:search="handleSearch"></date-screen>
</view>
<van-search value="{{ query.filterKey }}"
......
......@@ -88,6 +88,7 @@ Page({
await this.getData();
},
async handleSearch({ detail }) {
console.log("store search");
this.setData({
query: {
...this.data.query,
......
......@@ -2,7 +2,7 @@
"usingComponents": {
"TypeTabs": "/packageIndex/components/typeTabs/typeTabs",
"StatisticsItem": "/packageIndex/components/statisticsItem/statisticsItem",
"date-screen": "/components/dateScreen/dateScreen",
"date-screen": "/packageIndex/components/dateScreen/dateScreen",
"van-search": "@vant/weapp/search/index",
"WarnList": "/packageIndex/components/warnList/index"
},
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!