crew.js 9.84 KB
// pages/tab/crew/crew.js
import dayjs from "dayjs";
import api from "~/api/crew/index";
import indexApi from "~/api/index/index";
const IMG_URL = "https://img02.glsx.com.cn/weapp/resource/miniprogram/supervisor-miniprogram/";
const sceneMaps = {
	// 1延保 2轻改 3权益
	1: 1,
	2: 3,
	3: 2,
};
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		IMG_URL,
		navBarHeight: 84,
		tabIndex: 0,
		tabList: [
			{
				text: "业绩",
				value: "performance",
			},
			{
				text: "行为",
				value: "behavior",
			},
			{
				text: "培训",
				value: "training",
			},
		],
		sceneTabIndex: 0,
		sceneTabList: [
			{
				text: "全部",
				icon: "all-icon",
				value: "",
			},
			{
				text: "延保",
				icon: "yanbao-icon",
				value: "1",
			},
			{
				text: "权益",
				icon: "quanyi-icon",
				value: "2",
			},
			{
				text: "轻改",
				icon: "qinggai-icon",
				value: "3",
			},
		],
		staticList: [
			{
				label: "驻店人员",
				width: "208",
				key: "staffCount",
				background: "linear-gradient(90deg, #42CCCF 0%, #36D9A5 100%)",
			},
			{
				label: "门店数量",
				width: "192",
				key: "storeCount",
				background: "linear-gradient(90deg, #5E69FF 0%, #4079FF 100%)",
			},
			{
				label: "总产值",
				key: "totalOutput",
				background: "linear-gradient(90deg, #3D76FC 0%, #4DA6FF 100%)",
			},
		],
		AIContent: "",
		staticData: {},
		employeeList: [],
		mode: "performance",
		sortField: 1,
		sortOrder: 1,
	},

	onTabItemTap(e) {
		// 点击埋点
		wx.$track.setTrackData(0, "supervisor_crew_page", {}, "");
	},

	navbarReady(e) {
		let navBarHeight = e.detail.height;
		this.setData({
			navBarHeight,
		});
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {
		const query = wx.createSelectorQuery().in(this);
		query.select(".top-box").boundingClientRect();
		query.exec((res) => {
			this.setData({ topBoxHeight: res[0]?.height });
		});
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		this.getAIAnalyse();
		this.getEmployeeRank();
		if (this.data.tabIndex == 2) {
			// 请求培训的统计数据
			this.getTrainingStaticsData();
		}
	},

	formatThousands(v) {
		const s = String(v);
		if (!s) return s;
		const isNeg = s[0] === "-";
		const raw = isNeg ? s.slice(1) : s;
		const [intPart, decPart] = raw.split(".");
		const intFmt = String(intPart || "0").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
		const res = decPart ? `${intFmt}.${decPart}` : intFmt;
		return isNeg ? `-${res}` : res;
	},

	// 切换类型
	changeTab(e) {
		let { target } = e.currentTarget.dataset;
		let { tabList } = this.data;
		let eventCode = ["supervisor_performance_tab", "supervisor_behavior_tab", "supervisor_training_tab"];
		const index = tabList?.findIndex((val) => val.value == target.value);
		wx.$track.setTrackData(0, eventCode[index], {}, "");
		this.setData(
			{
				tabIndex: index,
				mode: target.value,
				// sceneTabIndex: 0,
				sortField: index == 1 ? 4 : 1, // 行为排序初始为4成交数
				sortOrder: 1,
			},
			() => {
				// const comp = this.selectComponent('#date')
				// if (comp) comp.initLoad('date')
				const sortComp = this.selectComponent("#employee-ranking");
				if (sortComp) sortComp.initLoad();
				setTimeout(() => {
					this.getAIAnalyse();
					this.getEmployeeRank();
					if (index == 2) {
						this.getTrainingStaticsData(); // 培训tab 多请求一次业绩接口获取统计数据
					}
				});
			},
		);
	},

	// 切换业务
	changeSceneTab(e) {
		let { target } = e.currentTarget.dataset;
		let { sceneTabList } = this.data;
		const index = sceneTabList?.findIndex((val) => val.value == target.value);
		let eventCode = ["supervisor_warranty_tab", "supervisor_benefits_tab", "supervisor_lightMod_tab"];
		wx.$track.setTrackData(0, eventCode[index], {}, "");
		this.setData(
			{
				sceneTabIndex: index,
			},
			() => {
				this.getAIAnalyse();
				this.getEmployeeRank();
				this.getTrainingStaticsData();
			},
		);
	},
	// 排序变化
	onEmployeeSortChange(e) {
		const { rankfield, rankType } = e.detail;
		wx.$track.setTrackData(0, `supervisor_${rankfield}_${rankType}`, {}, "");
		this.setData({
			sortField: rankfield,
			sortOrder: rankType == "desc" ? 1 : 0,
		});
		this.getEmployeeRank();
	},

	search(e) {
		this.setData({
			startStr: e.detail.startDay?.replace(/\//g, "-"),
			endStr: e.detail.endDay?.replace(/\//g, "-"),
			timeDimension: e.detail.timeDimension,
		});
		this.getAIAnalyse();
		this.getEmployeeRank();
		this.getTrainingStaticsData();
	},
	getAIAnalyse() {
		let { startStr, endStr, timeDimension, tabIndex, sceneTabIndex } = this.data;
		const apievents = ["getDashboardAIResult", "getBehaviorAIResult", "getAIAnslyseData"];
		let params =
			tabIndex == 2
				? {
						type: tabIndex + 1,
						timeDimension: timeDimension + 1,
						startDate: startStr,
						endDate: endStr,
					}
				: {
						bizs: sceneTabIndex > 0 ? sceneMaps[sceneTabIndex] : undefined,
						dateType: timeDimension,
						startDate: startStr,
						endDate: endStr,
					};
		this.setData({ AILoading: true, AIContent: "" });
		api[apievents[tabIndex]](params)
			.then((res) => {
				this.setData({ AILoading: false });
				let { code, data, message } = res;
				if (code == 0) {
					if (tabIndex == 2) {
						this.setData({
							AIContent: data?.aiGenerateResult ? `${JSON.parse(data?.aiGenerateResult).current_analysis}${JSON.parse(data?.aiGenerateResult).suggestions}` : "",
						});
					} else {
						this.setData({
							AIContent: data?.aiAnalysisSuggestion ? `${JSON.parse(data?.aiAnalysisSuggestion).current_analysis}${JSON.parse(data?.aiAnalysisSuggestion).suggestions}` : "",
						});
					}
				} else {
					wx.$util.showToast(message);
				}
			})
			.catch(() => {
				this.setData({ AILoading: false });
			});
	},
	getTrainingStaticsData() {
		let { sceneTabIndex, startStr, endStr, sortField, sortOrder, timeDimension } = this.data;
		api
			.getDashboardData({
				bizs: sceneTabIndex > 0 ? sceneMaps[sceneTabIndex] : undefined,
				dateType: timeDimension,
				startDate: startStr,
				endDate: endStr,
				sortField: 1,
				sortDirection: 1,
			})
			.then((res) => {
				let { code, data, message } = res;
				if (code == 0) {
					if (data?.metrics) {
						data.metrics.staffCount = (data.metrics.staffCount ?? "") === "" ? "-" : this.formatThousands(data.metrics.staffCount);
						data.metrics.storeCount = (data.metrics.storeCount ?? "") === "" ? "-" : this.formatThousands(data.metrics.storeCount);
						data.metrics.totalOutput = (data.metrics.totalOutput ?? "") === "" ? "-" : this.formatThousands(data.metrics.totalOutput);
					}
					this.setData({
						staticData: data?.metrics,
					});
				} else {
					wx.$util.showToast(message);
				}
			});
	},
	getEmployeeRank(noAI) {
		let { tabIndex, sceneTabIndex, startStr, endStr, sortField, sortOrder, timeDimension } = this.data;
		const sortComp = this.selectComponent("#employee-ranking");
		if (sortComp) {
			sortComp.clear();
		}
		this.setData({
			employeeList: [],
		});
		const event = ["getDashboardData", "getDashboardBehavior", "getTrainingStaffList"];
		let params =
			tabIndex == 2
				? {
						businessType: sceneTabIndex > 0 ? sceneMaps[sceneTabIndex] : undefined,
						startDate: startStr,
						endDate: endStr,
						sortField,
						sortOrder,
					}
				: {
						bizs: sceneTabIndex > 0 ? sceneMaps[sceneTabIndex] : undefined,
						dateType: timeDimension,
						startDate: startStr,
						endDate: endStr,
						sortField,
						sortDirection: sortOrder,
					};
		wx.$util.showLoading();
		api[event[tabIndex]](params).then((res) => {
			wx.$util.hideLoading();
			let { code, data, message } = res;
			if (code == 0) {
				if (data?.metrics) {
					data.metrics.staffCount = (data.metrics.staffCount ?? "") === "" ? "-" : this.formatThousands(data.metrics.staffCount);
					data.metrics.storeCount = (data.metrics.storeCount ?? "") === "" ? "-" : this.formatThousands(data.metrics.storeCount);
					data.metrics.totalOutput = (data.metrics.totalOutput ?? "") === "" ? "-" : this.formatThousands(data.metrics.totalOutput);
				}
				switch (tabIndex) {
					case 0:
						this.setData({
							staticData: data?.metrics,
							employeeList: data?.employeeList || [],
						});
						break;
					case 1:
						this.setData({
							staticData: data?.metrics,
							employeeList: data?.employeeList || [],
						});
						break;
					case 2:
						data?.forEach((val) => {
							val.employeePhone = val.phone;
						});
						this.setData({
							employeeList: data || [],
						});
				}
			} else {
				wx.$util.showToast(message);
			}
		});
	},
	handleNavigatePerformance(e) {
		const { key } = e.currentTarget.dataset;
		const urlMap = {
			staffCount: "/packageIndex/pages/stationStaffPerformance/stationStaffPerformance",
			storeCount: "/packageIndex/pages/storePerformance/storePerformance",
			shopCount: "/packageIndex/pages/storePerformance/storePerformance",
		};
		const url = urlMap[key];
		if (!url) return;
		wx.navigateTo({ url });
	},
	// 跳转员工个人看板
	onEmployeeTap(e) {
		const employee = e.detail.employee;
		wx.$track.setTrackData(0, "supervisor_staffDetail", {}, "");
		let { startStr, endStr, timeDimension, sceneTabIndex } = this.data;
		let query = {
			...employee,
			scene: sceneTabIndex > 0 ? sceneMaps[sceneTabIndex] : "",
			startDate: startStr,
			endDate: endStr,
			timeDimension: timeDimension,
		};
		const encoded = encodeURIComponent(JSON.stringify(query));
		wx.navigateTo({
			url: `/packageCrew/pages/peopleOverview/peopleOverview?query=${encoded}`,
		});
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {},
});