From d18a0c9fe708dcf58253b54ef2938b5e864b24df Mon Sep 17 00:00:00 2001
From: zuoxiao <zuoxiao>
Date: 星期一, 28 四月 2025 14:39:30 +0800
Subject: [PATCH] 优化水摄入页面的确认弹窗逻辑,调整showConfirm状态的设置位置,并移除不必要的代码注释,提升代码可读性和维护性。
---
pages/createIrrigation/createIrrigation.js | 437 +++++++++++++++++++++++++++++++++++++----------------
1 files changed, 303 insertions(+), 134 deletions(-)
diff --git a/pages/createIrrigation/createIrrigation.js b/pages/createIrrigation/createIrrigation.js
index ce7e83b..14c2a4c 100644
--- a/pages/createIrrigation/createIrrigation.js
+++ b/pages/createIrrigation/createIrrigation.js
@@ -1,4 +1,9 @@
const app = getApp();
+const {
+ get,
+ post
+} = require('../../api/request');
+const dayjs = require('dayjs');
Page({
/**
@@ -10,119 +15,152 @@
startTime: '', // 鐏屾簤寮�濮嬫椂闂�
pickerValue: '', // 鏃堕棿閫夋嫨鍣ㄧ殑鍊�
timePickerVisible: false, // 鏃堕棿閫夋嫨鍣ㄦ槸鍚﹀彲瑙�
+ timeInfoVisible: false, // 鏃堕棿鎻愮ず寮圭獥鏄惁鍙
// 椤圭洰閫夋嫨鍣ㄧ浉鍏�
projectPickerVisible: false,
projectPickerValue: [],
selectedProject: null,
projectOptions: [],
totalDuration: 0, // 娣诲姞鎬荤亴婧夋椂闂�
- // 娴嬭瘯鏁版嵁
- projectList: [
- {
- id: 1,
- name: '娴嬭瘯椤圭洰涓�',
- groups: [
- {
- id: 101,
- name: '杞亴缁凙',
- duration: 30,
- selected: false
- },
- {
- id: 102,
- name: '杞亴缁凚',
- duration: 45,
- selected: false
- },
- {
- id: 103,
- name: '杞亴缁凜',
- duration: 60,
- selected: false
- }
- ]
- },
- {
- id: 2,
- name: '娴嬭瘯椤圭洰浜�',
- groups: [
- {
- id: 201,
- name: '杞亴缁�1',
- duration: 40,
- selected: false
- },
- {
- id: 202,
- name: '杞亴缁�2',
- duration: 50,
- selected: false
- }
- ]
- },
- {
- id: 3,
- name: '娴嬭瘯椤圭洰涓�',
- groups: [
- {
- id: 301,
- name: '涓滃尯杞亴缁�',
- duration: 35,
- selected: false
- },
- {
- id: 302,
- name: '瑗垮尯杞亴缁�',
- duration: 55,
- selected: false
- },
- {
- id: 303,
- name: '鍗楀尯杞亴缁�',
- duration: 25,
- selected: false
- }
- ]
- }
- ]
+ // 椤圭洰鍒楄〃
+ projectList: [],
+ // 鏃堕棿閫夋嫨鍣ㄩ�夐」
+ timeOptions: [],
+ // 杞亴缁勫垪琛ㄥ埛鏂扮姸鎬�
+ isRefreshing: false
},
/**
* 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰鍔犺浇
*/
onLoad: function (options) {
- // 鍒濆鍖栭」鐩�夋嫨鍣ㄩ�夐」
- const projectOptions = this.data.projectList.map(project => ({
- label: project.name,
- value: project.id
- }));
-
+ // 鐢熸垚璁″垝缂栧彿
+ this.generatePlanCode();
+ // 璁剧疆鏃堕棿閫夋嫨鍣ㄧ殑鍒濆鍊�
+ const now = dayjs();
this.setData({
- projectOptions: projectOptions // 鐩存帴浣跨敤涓�缁存暟缁勶紝涓嶉渶瑕佸寘瑁呮垚浜岀淮鏁扮粍
+ pickerValue: now.add(8.5, 'hour').format('YYYY-MM-DD HH:mm')
+ });
+ // 鑾峰彇椤圭洰鍒楄〃
+ this.fetchProjects();
+ },
+
+ /**
+ * 鐢熸垚璁″垝缂栧彿
+ */
+ generatePlanCode: function () {
+ const now = dayjs();
+ const dateStr = now.format('YYYY-MM-DD');
+ const randomNum = Math.floor(Math.random() * 10000).toString().padStart(4, '0');
+ const planCode = `${dateStr}${-randomNum}`;
+ this.setData({
+ planCode
});
},
/**
- * 鑾峰彇椤圭洰鍜岃疆鐏岀粍鏁版嵁
+ * 鑾峰彇椤圭洰鍒楄〃
*/
- fetchProjectsAndGroups: function () {
- // 杩欓噷鍙互娣诲姞API璇锋眰閫昏緫锛岃幏鍙栫湡瀹炴暟鎹�
- // wx.request({
- // url: 'your-api-url',
- // success: (res) => {
- // this.setData({
- // projectList: res.data
- // });
- // }
- // });
+ fetchProjects: function (isShowDiaolog) {
+ return get({
+ url: '/wx/irrigation/getSimpleProjects',
+ isShowLoding: true
+ }).then(res => {
+ if (res.success) {
+ const projectList = res.content.map(project => ({
+ id: project.projectId,
+ name: project.projectName,
+ groupCount: project.groupCount,
+ groups: []
+ }));
+
+ const projectOptions = projectList.map(project => ({
+ label: project.name,
+ value: project.id
+ }));
+
+ this.setData({
+ projectList,
+ projectOptions
+ });
+ if (isShowDiaolog) {
+ this.setData({
+ projectPickerVisible: true
+ });
+ }
+ } else {
+ wx.showToast({
+ title: res.msg || '鑾峰彇椤圭洰鍒楄〃澶辫触',
+ icon: 'none'
+ });
+ return Promise.reject(new Error(res.msg || '鑾峰彇椤圭洰鍒楄〃澶辫触'));
+ }
+ }).catch(err => {
+ console.error('鑾峰彇椤圭洰鍒楄〃澶辫触锛�', err);
+ wx.showToast({
+ title: '鑾峰彇椤圭洰鍒楄〃澶辫触',
+ icon: 'none'
+ });
+ return Promise.reject(err);
+ });
},
/**
- * 澶勭悊璁″垝缂栧彿杈撳叆
+ * 鑾峰彇杞亴缁勫垪琛�
*/
- onPlanCodeInput: function (e) {
- this.setData({
- planCode: e.detail.value
+ fetchGroups: function (projectId) {
+ return get({
+ url: '/wx/irrigation/getSimpleGroups',
+ data: {
+ projectId: projectId,
+ },
+ isShowLoding: true
+ }).then(res => {
+ if (res.success) {
+ console.log('杞亴缁勬暟鎹細', res.content);
+
+ // 鏇存柊閫変腑椤圭洰鐨勮疆鐏岀粍淇℃伅
+ const projectList = this.data.projectList.map(project => {
+ if (project.id === projectId) {
+ return {
+ ...project,
+ groups: res.content.map(group => ({
+ id: group.groupId,
+ name: group.groupCode,
+ duration: group.defaultDuration || 0,
+ selected: false,
+ intakeCount: group.intakeCount
+ }))
+ };
+ }
+ return project;
+ });
+
+ // 鏇存柊閫変腑鐨勯」鐩�
+ const selectedProject = projectList.find(project => project.id === projectId);
+ console.log('鏇存柊鍚庣殑閫変腑椤圭洰锛�', selectedProject);
+
+ this.setData({
+ projectList,
+ selectedProject
+ }, () => {
+ // 璁$畻鎬绘椂闂�
+ this.calculateTotalDuration();
+ });
+ } else {
+ wx.showToast({
+ title: res.msg || '鑾峰彇杞亴缁勫垪琛ㄥけ璐�',
+ icon: 'none'
+ });
+ return Promise.reject(new Error(res.msg || '鑾峰彇杞亴缁勫垪琛ㄥけ璐�'));
+ }
+ }).catch(err => {
+ console.error('鑾峰彇杞亴缁勫垪琛ㄥけ璐ワ細', err);
+ wx.showToast({
+ title: '鑾峰彇杞亴缁勫垪琛ㄥけ璐�',
+ icon: 'none'
+ });
+ return Promise.reject(err);
});
},
@@ -130,6 +168,14 @@
* 鏄剧ず鏃堕棿閫夋嫨鍣�
*/
showTimePicker: function () {
+ // 濡傛灉娌℃湁閫夋嫨鏃堕棿锛屼娇鐢ㄥ綋鍓嶆椂闂�
+ if (!this.data.pickerValue) {
+ const now = dayjs();
+ this.setData({
+ pickerValue: now.format('YYYY-MM-DD HH:mm')
+ });
+ }
+
this.setData({
timePickerVisible: true
});
@@ -139,7 +185,9 @@
* 鏃堕棿閫夋嫨鍣ㄧ‘璁ゅ洖璋�
*/
onTimePickerConfirm: function (e) {
- const { value } = e.detail;
+ const {
+ value
+ } = e.detail;
this.setData({
timePickerVisible: false,
startTime: value
@@ -161,7 +209,7 @@
toggleProject: function (e) {
const index = e.currentTarget.dataset.index;
const currentValue = this.data.projectList[index].expanded;
-
+
// 鍒涘缓鏂扮殑椤圭洰鍒楄〃锛屽厛灏嗘墍鏈夐」鐩涓烘姌鍙犵姸鎬�
const newProjectList = this.data.projectList.map((item, idx) => {
return {
@@ -169,13 +217,13 @@
expanded: false
};
});
-
+
// 濡傛灉褰撳墠鐐瑰嚮鐨勯」鐩凡缁忔槸灞曞紑鐘舵�侊紝鍒欎繚鎸佹墍鏈夐」鐩姌鍙�
// 鍚﹀垯锛屽皢褰撳墠鐐瑰嚮鐨勯」鐩涓哄睍寮�鐘舵��
if (!currentValue) {
newProjectList[index].expanded = true;
}
-
+
this.setData({
projectList: newProjectList
});
@@ -189,11 +237,11 @@
const groupIndex = e.currentTarget.dataset.groupIndex;
const key = `projectList[${projectIndex}].groups[${groupIndex}].selected`;
const currentValue = this.data.projectList[projectIndex].groups[groupIndex].selected;
-
+
this.setData({
[key]: !currentValue
});
-
+
// 鏇存柊椤圭洰鎬绘椂闀�
this.updateProjectTotalDuration(projectIndex);
},
@@ -202,12 +250,16 @@
* 澶勭悊鏃堕暱杈撳叆
*/
onDurationInput: function (e) {
- const { groupIndex } = e.currentTarget.dataset;
+ const {
+ groupIndex
+ } = e.currentTarget.dataset;
const duration = parseInt(e.detail.value) || 0;
-
- const selectedProject = { ...this.data.selectedProject };
+
+ const selectedProject = {
+ ...this.data.selectedProject
+ };
selectedProject.groups[groupIndex].duration = duration;
-
+
this.setData({
selectedProject
}, () => {
@@ -222,14 +274,14 @@
updateProjectTotalDuration: function (projectIndex) {
const project = this.data.projectList[projectIndex];
let totalDuration = 0;
-
+
// 璁$畻鎵�鏈夐�変腑鐨勮疆鐏岀粍鐨勬椂闀挎�诲拰
project.groups.forEach(group => {
if (group.selected) {
totalDuration += parseInt(group.duration) || 0;
}
});
-
+
// 鏇存柊椤圭洰鎬绘椂闀�
const totalDurationKey = `projectList[${projectIndex}].totalDuration`;
this.setData({
@@ -240,8 +292,11 @@
/**
* 闃绘浜嬩欢鍐掓场
*/
- stopPropagation: function () {
- // 闃绘浜嬩欢鍐掓场锛岄槻姝㈢偣鍑昏緭鍏ユ鏃惰Е鍙戠埗鍏冪礌鐨勭偣鍑讳簨浠�
+ stopPropagation: function (e) {
+ if (e && e.stopPropagation) {
+ e.stopPropagation();
+ }
+ return false;
},
/**
@@ -249,15 +304,36 @@
*/
navigateToGroupDetail: function (e) {
const { groupIndex } = e.currentTarget.dataset;
- // TODO: 瀹炵幇璺宠浆閫昏緫
+ const group = this.data.selectedProject.groups[groupIndex];
+
+ // 鏋勫缓URL鍙傛暟
+ const params = {
+ projectName: this.data.selectedProject.name,
+ groupName: group.name,
+ groupId: group.id
+ };
+
+ // 鏋勫缓URL鏌ヨ瀛楃涓�
+ const queryString = Object.keys(params)
+ .map(key => `${key}=${encodeURIComponent(params[key])}`)
+ .join('&');
+
+ // 璺宠浆鍒拌疆鐏岀粍璇︽儏椤�
+ wx.navigateTo({
+ url: `/pages/groupDetail/groupDetail?${queryString}`
+ });
},
/**
* 纭鎸夐挳鐐瑰嚮浜嬩欢
*/
onConfirm: function () {
- const { planCode, startTime, selectedProject } = this.data;
-
+ const {
+ planCode,
+ startTime,
+ selectedProject
+ } = this.data;
+
if (!planCode) {
wx.showToast({
title: '璇疯緭鍏ヨ鍒掔紪鍙�',
@@ -265,15 +341,7 @@
});
return;
}
-
- if (!startTime) {
- wx.showToast({
- title: '璇烽�夋嫨鐏屾簤寮�濮嬫椂闂�',
- icon: 'none'
- });
- return;
- }
-
+
if (!selectedProject) {
wx.showToast({
title: '璇烽�夋嫨椤圭洰',
@@ -282,33 +350,86 @@
return;
}
- // TODO: 瀹炵幇纭閫昏緫
- console.log('鎻愪氦鏁版嵁锛�', {
- planCode,
- startTime,
- project: selectedProject
+ // 鏋勫缓涓婃姤鏁版嵁
+ const schedules = selectedProject.groups.map(group => ({
+ groupId: group.id,
+ duration: parseInt(group.duration) || 0
+ }));
+
+ const requestData = {
+ projectId: selectedProject.id,
+ planName: planCode,
+ startupMode: startTime ? 2 : 1,
+ operatorId: app.globalData.clientId,
+ schedules: schedules
+ };
+
+ // 濡傛灉鏈夊紑濮嬫椂闂达紝娣诲姞鍒拌姹傛暟鎹腑
+ if (startTime) {
+ requestData.planStartTime = startTime;
+ }
+
+ // 鍙戦�佽姹�
+ post({
+ url: '/wx/plan/createPlan',
+ data: requestData,
+ isShowLoding: true
+ }).then(res => {
+ if (res.success) {
+ wx.showToast({
+ title: '鍒涘缓鎴愬姛',
+ icon: 'success'
+ });
+ // 杩斿洖涓婁竴椤碉紝骞朵紶閫掑弬鏁版寚绀洪渶瑕佸埛鏂板垪琛ㄥ苟鍒囨崲鍒板綋鍓嶈鍒掑垪琛�
+ setTimeout(() => {
+ // 浣跨敤鍏ㄥ眬鍙橀噺鏍囪闇�瑕佸埛鏂�
+ const app = getApp();
+ app.globalData.needRefreshIrrigationList = true;
+
+ // 鐩存帴杩斿洖涓婁竴椤�
+ wx.navigateBack({
+ delta: 1
+ });
+ }, 1500);
+ } else {
+ wx.showToast({
+ title: res.msg || '鍒涘缓澶辫触',
+ icon: 'none'
+ });
+ }
+ }).catch(err => {
+ console.error('鍒涘缓璁″垝澶辫触锛�', err);
+ wx.showToast({
+ title: '鍒涘缓澶辫触',
+ icon: 'none'
+ });
});
},
// 鏄剧ず椤圭洰閫夋嫨鍣�
showProjectPicker() {
- this.setData({
- projectPickerVisible: true
- });
+ this.fetchProjects(true)
+
},
// 椤圭洰閫夋嫨鍣ㄧ‘璁�
onProjectPickerConfirm(e) {
- const { value } = e.detail;
+ const {
+ value
+ } = e.detail;
+ console.log('閫夋嫨鐨勯」鐩甀D锛�', value[0]); // 娣诲姞鏃ュ織鏌ョ湅鏁版嵁
const selectedProject = this.data.projectList.find(project => project.id === value[0]);
-
+ console.log('鎵惧埌鐨勯」鐩細', selectedProject); // 娣诲姞鏃ュ織鏌ョ湅鏁版嵁
+
this.setData({
projectPickerVisible: false,
selectedProject: selectedProject,
projectPickerValue: value
}, () => {
- // 閫夋嫨椤圭洰鍚庤绠楁�绘椂闂�
- this.calculateTotalDuration();
+ // 閫夋嫨椤圭洰鍚庤幏鍙栬疆鐏岀粍鍒楄〃
+ if (selectedProject) {
+ this.fetchGroups(selectedProject.id);
+ }
});
},
@@ -319,16 +440,64 @@
});
},
- // 璁$畻鎬荤亴婧夋椂闂�
- calculateTotalDuration() {
+ /**
+ * 璁$畻鎬荤亴婧夋椂闂�
+ */
+ calculateTotalDuration: function () {
if (!this.data.selectedProject) return;
-
+
const totalDuration = this.data.selectedProject.groups.reduce((sum, group) => {
return sum + (parseInt(group.duration) || 0);
}, 0);
-
+
this.setData({
totalDuration
});
},
-});
\ No newline at end of file
+
+ /**
+ * 杞亴缁勫垪琛ㄤ笅鎷夊埛鏂�
+ */
+ onGroupListRefresh: function () {
+ if (!this.data.selectedProject) {
+ this.setData({
+ isRefreshing: false
+ });
+ return;
+ }
+
+ this.setData({
+ isRefreshing: true
+ });
+
+ this.fetchGroups(this.data.selectedProject.id)
+ .then(() => {
+ this.setData({
+ isRefreshing: false
+ });
+ })
+ .catch(() => {
+ this.setData({
+ isRefreshing: false
+ });
+ });
+ },
+
+ /**
+ * 鏄剧ず鏃堕棿鎻愮ず寮圭獥
+ */
+ showTimeInfo: function () {
+ this.setData({
+ timeInfoVisible: true
+ });
+ },
+
+ /**
+ * 鍏抽棴鏃堕棿鎻愮ず寮圭獥
+ */
+ onTimeInfoConfirm: function () {
+ this.setData({
+ timeInfoVisible: false
+ });
+ },
+});
\ No newline at end of file
--
Gitblit v1.8.0