管灌系统农户端微信小程序(嘉峪关应用)
pages/createIrrigation/createIrrigation.js
@@ -1,5 +1,8 @@
const app = getApp();
const { get, post } = require('../../api/request');
const {
  get,
  post
} = require('../../api/request');
const dayjs = require('dayjs');
Page({
@@ -47,16 +50,18 @@
     */
    generatePlanCode: function () {
        const now = dayjs();
        const dateStr = now.format('YYYYMMDD');
    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 });
    const planCode = `${dateStr}${-randomNum}`;
    this.setData({
      planCode
    });
    },
    /**
     * 获取项目列表
     */
    fetchProjects: function () {
  fetchProjects: function (isShowDiaolog) {
        return get({
            url: '/wx/irrigation/getSimpleProjects',
            isShowLoding: true
@@ -78,6 +83,11 @@
                    projectList,
                    projectOptions
                });
        if (isShowDiaolog) {
          this.setData({
            projectPickerVisible: true
          });
        }
            } else {
                wx.showToast({
                    title: res.msg || '获取项目列表失败',
@@ -107,14 +117,14 @@
            isShowLoding: true
        }).then(res => {
            if (res.success) {
                console.log('轮灌组数据:', res.content.obj);
        console.log('轮灌组数据:', res.content);
                // 更新选中项目的轮灌组信息
                const projectList = this.data.projectList.map(project => {
                    if (project.id === projectId) {
                        return {
                            ...project,
                            groups: res.content.obj.map(group => ({
              groups: res.content.map(group => ({
                                id: group.groupId,
                                name: group.groupCode,
                                duration: group.defaultDuration || 0,
@@ -175,7 +185,9 @@
     * 时间选择器确认回调
     */
    onTimePickerConfirm: function (e) {
        const { value } = e.detail;
    const {
      value
    } = e.detail;
        this.setData({
            timePickerVisible: false,
            startTime: value
@@ -238,10 +250,14 @@
     * 处理时长输入
     */
    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({
@@ -287,7 +303,9 @@
     * 跳转到轮灌组详情页
     */
    navigateToGroupDetail: function (e) {
        const { groupIndex } = e.currentTarget.dataset;
    const {
      groupIndex
    } = e.currentTarget.dataset;
        // TODO: 实现跳转逻辑
    },
@@ -295,7 +313,11 @@
     * 确认按钮点击事件
     */
    onConfirm: function () {
        const { planCode, startTime, selectedProject } = this.data;
    const {
      planCode,
      startTime,
      selectedProject
    } = this.data;
        if (!planCode) {
            wx.showToast({
@@ -343,9 +365,16 @@
                    title: '创建成功',
                    icon: 'success'
                });
                // 返回上一页
        // 返回上一页,并传递参数指示需要刷新列表并切换到当前计划列表
                setTimeout(() => {
                    wx.navigateBack();
          // 使用全局变量标记需要刷新
          const app = getApp();
          app.globalData.needRefreshIrrigationList = true;
          // 直接返回上一页
          wx.navigateBack({
            delta: 1
          });
                }, 1500);
            } else {
                wx.showToast({
@@ -364,14 +393,15 @@
    // 显示项目选择器
    showProjectPicker() {
        this.setData({
            projectPickerVisible: true
        });
    this.fetchProjects(true)
    },
    // 项目选择器确认
    onProjectPickerConfirm(e) {
        const { value } = e.detail;
    const {
      value
    } = e.detail;
        console.log('选择的项目ID:', value[0]); // 添加日志查看数据
        const selectedProject = this.data.projectList.find(project => project.id === value[0]);
        console.log('找到的项目:', selectedProject); // 添加日志查看数据
@@ -415,18 +445,26 @@
     */
    onGroupListRefresh: function () {
        if (!this.data.selectedProject) {
            this.setData({ isRefreshing: false });
      this.setData({
        isRefreshing: false
      });
            return;
        }
        this.setData({ isRefreshing: true });
    this.setData({
      isRefreshing: true
    });
        this.fetchGroups(this.data.selectedProject.id)
            .then(() => {
                this.setData({ isRefreshing: false });
        this.setData({
          isRefreshing: false
        });
            })
            .catch(() => {
                this.setData({ isRefreshing: false });
        this.setData({
          isRefreshing: false
        });
            });
    },