| const app = getApp(); | 
| const storage = require('../../utils/storage.js'); | 
| const { | 
|   post | 
| } = require('../../api/request.js'); | 
| const { PROJECT_CONFIG } = require('../../utils/projectConfig.js'); | 
| const { setBaseUrl } = require('../../api/config.js'); | 
|   | 
| Page({ | 
|   | 
|   /** | 
|    * 页面的初始数据 | 
|    */ | 
|   data: { | 
|     phone: '', | 
|     verificationCode: '', | 
|     codeText: '获取验证码', | 
|     counting: false, | 
|     countDown: 60, | 
|     projectName: '嘉峪关项目', // 默认项目名称 | 
|     selectedProject: 'JYG', // 默认项目代码 | 
|     showErrorDialog: false, | 
|     fromBack: false,  // 标记是否是从返回按钮返回的 | 
|     manualNavigate: false // 标记是否是通过编程方式导航的 | 
|   }, | 
|   | 
|   /** | 
|    * 绑定手机号输入 | 
|    */ | 
|   bindPhoneInput: function (e) { | 
|     this.setData({ | 
|       phone: e.detail.value | 
|     }); | 
|   }, | 
|   | 
|   /** | 
|    * 绑定验证码输入 | 
|    */ | 
|   bindCodeInput: function (e) { | 
|     this.setData({ | 
|       verificationCode: e.detail.value | 
|     }); | 
|   }, | 
|   | 
|   /** | 
|    * 发送验证码 | 
|    */ | 
|   sendVerificationCode: function () { | 
|     const { | 
|       phone, | 
|       counting | 
|     } = this.data; | 
|   | 
|     // 如果正在倒计时,则不允许再次发送 | 
|     if (counting) { | 
|       return; | 
|     } | 
|   | 
|     // 验证手机号格式 | 
|     if (!/^1\d{10}$/.test(phone)) { | 
|       wx.showToast({ | 
|         title: '请输入正确的手机号', | 
|         icon: 'none' | 
|       }); | 
|       return; | 
|     } | 
|   | 
|   | 
|   | 
|     // 发送验证码请求 | 
|     this.postCode(); | 
|   }, | 
|   | 
|   /** | 
|    * 开始倒计时 | 
|    */ | 
|   startCountDown: function () { | 
|     this.setData({ | 
|       counting: true, | 
|       countDown: 60 | 
|     }); | 
|   | 
|     const timer = setInterval(() => { | 
|       if (this.data.countDown <= 1) { | 
|         clearInterval(timer); | 
|         this.setData({ | 
|           counting: false, | 
|           codeText: '获取验证码' | 
|         }); | 
|       } else { | 
|         this.setData({ | 
|           countDown: this.data.countDown - 1, | 
|           codeText: `${this.data.countDown - 1}秒后重发` | 
|         }); | 
|       } | 
|     }, 1000); | 
|   | 
|     // 保存timer引用,以便在页面卸载时清除 | 
|     this.countDownTimer = timer; | 
|   }, | 
|   | 
|   /** | 
|    * 停止倒计时 | 
|    */ | 
|   stopCountDown: function () { | 
|     if (this.countDownTimer) { | 
|       clearInterval(this.countDownTimer); | 
|     } | 
|     this.setData({ | 
|       counting: false, | 
|       codeText: '获取验证码' | 
|     }); | 
|   }, | 
|   | 
|   /** | 
|    * 登录 | 
|    */ | 
|   login: function () { | 
|     const { | 
|       phone, | 
|       verificationCode | 
|     } = this.data; | 
|   | 
|     // 验证手机号和验证码 | 
|     if (!/^1\d{10}$/.test(phone)) { | 
|       wx.showToast({ | 
|         title: '请输入正确的手机号', | 
|         icon: 'none' | 
|       }); | 
|       return; | 
|     } | 
|   | 
|     if (!/^\d{6}$/.test(verificationCode)) { | 
|       wx.showToast({ | 
|         title: '请输入6位验证码', | 
|         icon: 'none' | 
|       }); | 
|       return; | 
|     } | 
|   | 
|     // 显示加载中 | 
|     wx.showLoading({ | 
|       title: '登录中...', | 
|       mask: true | 
|     }); | 
|      | 
|     // 标记为手动导航,确保不会触发返回逻辑 | 
|     console.log('登录按钮点击,设置manualNavigate=true'); | 
|     this.setData({ | 
|       manualNavigate: true | 
|     }); | 
|      | 
|     this.wsLogin(); | 
|   }, | 
|   | 
|   /** | 
|    * 跳转到注册页面 | 
|    */ | 
|   goToRegister: function () { | 
|     wx.showToast({ | 
|       title: '注册功能暂未开放', | 
|       icon: 'none', | 
|       duration: 2000 | 
|     }); | 
|   }, | 
|   | 
|   /** | 
|    * 跳转到忘记密码页面 | 
|    */ | 
|   goToForgetPassword: function () { | 
|     wx.showToast({ | 
|       title: '找回密码功能暂未开放', | 
|       icon: 'none', | 
|       duration: 2000 | 
|     }); | 
|   }, | 
|   | 
|   /** | 
|    * 跳转到账号密码登录页面 | 
|    */ | 
|   goToAccountLogin: function() { | 
|     // 获取当前项目信息,传递给accountLogin页面 | 
|     const { selectedProject, projectName } = this.data; | 
|      | 
|     // 跳转到账号密码登录页面 | 
|     wx.navigateTo({ | 
|       url: `/pages/accountLogin/accountLogin?project=${selectedProject}&projectName=${projectName}` | 
|     }); | 
|   }, | 
|   | 
|   /** | 
|    * 生命周期函数--监听页面加载 | 
|    */ | 
|   onLoad: function (options) { | 
|     console.log('login页面加载,options:', options); | 
|      | 
|     // 设置页面数据 | 
|     this.setData({ | 
|       fromBack: false,  // 标记是否是从返回按钮返回的 | 
|       manualNavigate: false // 标记是否是通过编程方式导航的 | 
|     }); | 
|      | 
|     // 从URL参数获取项目信息 | 
|     if (options && options.project) { | 
|       const project = options.project; | 
|       // 直接使用传入的项目名称,不再手动转换 | 
|       const projectName = options.projectName || '默认项目'; | 
|        | 
|       console.log(`从URL参数获取项目信息: ${project} (${projectName})`); | 
|        | 
|       this.setData({ | 
|         selectedProject: project, | 
|         projectName: projectName | 
|       }); | 
|        | 
|       // 同步更新全局项目设置 | 
|       if (getApp().globalData) { | 
|         getApp().globalData.selectedProject = project; | 
|          | 
|         // 设置对应的tag | 
|         if (project === 'JYG') { | 
|           getApp().globalData.tag = 'ym'; | 
|         } else if (project === 'MQ') { | 
|           getApp().globalData.tag = 'mq'; | 
|         } | 
|          | 
|         // 更新 BASEURL | 
|         try { | 
|           // 使用导入的setBaseUrl函数设置baseUrl | 
|           setBaseUrl(project); | 
|            | 
|           // 从PROJECT_CONFIG中获取serverUrl | 
|           const serverUrl = PROJECT_CONFIG[project]?.serverUrl; | 
|           if (serverUrl) { | 
|             getApp().globalData.baseUrl = serverUrl; | 
|             console.log('设置baseUrl成功:', serverUrl); | 
|           } | 
|         } catch (e) { | 
|           console.error('设置baseUrl失败:', e); | 
|         } | 
|       } | 
|        | 
|       // 检查项目配置的登录方式是否与当前页面匹配 | 
|       const projectConfig = PROJECT_CONFIG[project]; | 
|       if (projectConfig && projectConfig.loginType === 'account') { | 
|         console.log(`当前项目配置的登录方式为account,自动跳转到账号密码登录页面`); | 
|          | 
|         // 设置手动导航标记,避免触发返回逻辑 | 
|         this.setData({ manualNavigate: true }); | 
|          | 
|         // 延迟执行跳转,确保页面完全加载 | 
|         setTimeout(() => { | 
|           wx.redirectTo({ | 
|             url: `/pages/accountLogin/accountLogin?project=${project}&projectName=${projectName}` | 
|           }); | 
|         }, 100); | 
|       } | 
|     } else { | 
|       console.log('未从URL获取到项目信息,使用默认值'); | 
|     } | 
|   }, | 
|   | 
|   /** | 
|    * 生命周期函数--监听页面初次渲染完成 | 
|    */ | 
|   onReady() { | 
|   | 
|   }, | 
|   | 
|   /** | 
|    * 生命周期函数--监听页面显示 | 
|    */ | 
|   onShow() { | 
|   | 
|   }, | 
|   | 
|   /** | 
|    * 生命周期函数--监听页面隐藏 | 
|    */ | 
|   onHide() { | 
|     // 如果不是通过编程方式导航,则可能是点击了返回按钮 | 
|     // 注意:在某些情况下,微信可能会直接调用onUnload而跳过onHide | 
|     if (!this.data.manualNavigate) { | 
|       console.log('页面隐藏,可能是点击了返回按钮'); | 
|       this.setData({ | 
|         fromBack: true | 
|       }); | 
|        | 
|       // 如果页面隐藏但未登录,记录一个时间戳,帮助识别是否是返回操作 | 
|       if (!getApp().globalData.isLoggedIn) { | 
|         this.hideTimestamp = Date.now(); | 
|       } | 
|     } else { | 
|       console.log('页面隐藏,是通过代码导航的'); | 
|     } | 
|   }, | 
|   | 
|   /** | 
|    * 生命周期函数--监听页面卸载 | 
|    */ | 
|   onUnload() { | 
|     // 清除验证码倒计时定时器 | 
|     if (this.countDownTimer) { | 
|       clearInterval(this.countDownTimer); | 
|       this.countDownTimer = null; | 
|     } | 
|      | 
|     // 记录退出登录页面的情况 | 
|     console.log('登录页面卸载,fromBack:', this.data.fromBack, 'manualNavigate:', this.data.manualNavigate, '登录状态:', !!getApp().globalData.isLoggedIn); | 
|      | 
|     // 如果是登录成功,不执行返回逻辑 | 
|     if (getApp().globalData.isLoggedIn) { | 
|       console.log('用户已登录,无需执行返回逻辑'); | 
|       return; | 
|     } | 
|      | 
|     // 检查是否是通过编程方式明确设置了导航 | 
|     // 如果没有明确设置,就假定是返回操作 | 
|     if (!this.data.manualNavigate) { | 
|       console.log('页面卸载时未设置manualNavigate,假定是返回按钮操作'); | 
|        | 
|       // 尝试使用wx.navigateBack返回上一页(如果可行) | 
|       const pages = getCurrentPages(); | 
|       if (pages.length > 1) { | 
|         console.log('检测到有上一页,使用navigateBack返回'); | 
|         wx.navigateBack({ | 
|           delta: 1, | 
|           success: () => { | 
|             // 通过eventChannel向上一个页面传递数据 | 
|             const eventChannel = pages[pages.length - 2].getOpenerEventChannel(); | 
|             if (eventChannel && eventChannel.emit) { | 
|               eventChannel.emit('fromLogin', { fromLogin: true }); | 
|             } | 
|             console.log('已成功返回到上一页面'); | 
|           } | 
|         }); | 
|       } else { | 
|         console.log('无上一页,使用备用返回方式'); | 
|         // 没有上一页,使用备用方法 | 
|         this.backupReturnToHome(); | 
|       } | 
|     } else { | 
|       console.log('通过编程方式离开登录页面'); | 
|     } | 
|   }, | 
|    | 
|    | 
|      | 
|   | 
|   | 
|   /** | 
|    * 页面相关事件处理函数--监听用户下拉动作 | 
|    */ | 
|   onPullDownRefresh() { | 
|   | 
|   }, | 
|   | 
|   /** | 
|    * 页面上拉触底事件的处理函数 | 
|    */ | 
|   onReachBottom() { | 
|   | 
|   }, | 
|   | 
|   /** | 
|    * 用户点击右上角分享 | 
|    */ | 
|   onShareAppMessage() { | 
|   | 
|   }, | 
|   //获取验证码 | 
|   postCode: function () { | 
|     const params = { | 
|       url: 'wx/client/send_sms?phoneNumber=' + this.data.phone | 
|     }; | 
|     post(params) | 
|       .then((data) => { | 
|         wx.showToast({ | 
|           title: '验证码已发送', | 
|           icon: 'success', | 
|           duration: 2000, | 
|         }); | 
|         this.setData({ | 
|           codeSent: true, | 
|         }); | 
|     // 开始倒计时 | 
|     this.startCountDown(); | 
|       }) | 
|       .catch((error) => { | 
|         wx.showToast({ | 
|           title: error.msg, | 
|           icon: 'none' | 
|         }); | 
|         console.error('Failed to add item:', error); | 
|       }); | 
|   }, | 
|   wsLogin() { | 
|     // 标记为手动导航 | 
|     console.log('wsLogin调用,设置manualNavigate=true'); | 
|     this.setData({ | 
|       manualNavigate: true | 
|     }); | 
|      | 
|     wx.login({ | 
|       success: res => { | 
|         if (res.code) { | 
|           console.log('微信登录成功,获取到的code:', res.code); | 
|           // 发送 res.code 到后台服务器换取 openId, sessionKey, unionId | 
|           this.verify(res.code) | 
|         } else { | 
|           console.log('微信登录失败!' + res.errMsg); | 
|           // 登录失败时重置状态 | 
|           this.setData({ | 
|             manualNavigate: false | 
|           }); | 
|         } | 
|       }, | 
|       fail: err => { | 
|         console.error('微信登录API调用失败:', err); | 
|         // 登录失败,重置导航标记 | 
|         this.setData({ | 
|           manualNavigate: false | 
|         }); | 
|       } | 
|     }); | 
|   }, | 
|   //用户绑定 | 
|   verify(wxCode) { | 
|     console.log('verify调用,确认manualNavigate=', this.data.manualNavigate); | 
|      | 
|     const params = { | 
|       url: 'wx/client/verify', | 
|       data: { | 
|         phoneNumber: this.data.phone, | 
|         securityCode: this.data.verificationCode, | 
|         code: wxCode | 
|       } | 
|     }; | 
|      | 
|     post(params) | 
|       .then((data) => { | 
|         wx.hideLoading(); | 
|          | 
|         console.log('验证成功,准备保存用户数据'); | 
|          | 
|         // 确保全局对象已初始化 | 
|         if (!getApp().globalData) { | 
|           getApp().globalData = {}; | 
|         } | 
|          | 
|         // 保存会话ID和客户端ID | 
|         getApp().globalData.sessionId = String(data.content.sessionId); | 
|         storage.setItem("sessionId", String(data.content.sessionId)); | 
|         getApp().globalData.clientId = String(data.content.clientId); | 
|         storage.setItem("clientId", String(data.content.clientId)); | 
|          | 
|          | 
|         // 保存用户信息 | 
|         const userInfo = { | 
|           sessionId: String(data.content.sessionId), | 
|           clientId: String(data.content.clientId), | 
|           phone: this.data.phone, | 
|           token: data.content.token || '', | 
|           project: this.data.selectedProject | 
|         }; | 
|          | 
|         // 保存到全局变量 | 
|         getApp().globalData.userInfo = userInfo; | 
|         getApp().globalData.isLoggedIn = true; | 
|          | 
|         // 保存到本地存储 | 
|         storage.setItem("userInfo", JSON.stringify(userInfo)) | 
|           .then(() => { | 
|             console.log('用户信息保存成功'); | 
|             return storage.setItem("isLoggedIn", "true"); | 
|           }) | 
|           .then(() => { | 
|             // 保存userData信息,包含sessionId和tag | 
|             const userData = JSON.stringify({ | 
|               sessionId: String(data.content.sessionId), | 
|               tag: this.data.selectedProject === 'JYG' ? 'ym' : (this.data.selectedProject === 'MQ' ? 'mq' : 'ym'), | 
|               project: this.data.selectedProject, | 
|               phone: this.data.phone, | 
|               clientName: data.content.clientName || '', | 
|               clientId: String(data.content.clientId) | 
|             }); | 
|             return storage.setItem("userData", userData); | 
|           }) | 
|           .then(() => { | 
|             console.log('登录状态和项目信息保存成功'); | 
|             this.bindSuccess(); | 
|           }) | 
|           .catch(err => { | 
|             console.warn('保存用户信息过程中出错,但不影响继续操作:', err); | 
|             this.bindSuccess(); | 
|           }); | 
|       }) | 
|       .catch((error) => { | 
|         wx.hideLoading(); | 
|         console.error('验证请求失败:', error); | 
|          | 
|         // 验证失败,需要重置导航标记 | 
|         console.log('验证失败,重置manualNavigate=false'); | 
|         this.setData({ | 
|           manualNavigate: false | 
|         }); | 
|          | 
|         wx.showToast({ | 
|           title: error.msg || '验证失败', | 
|           icon: 'error', | 
|           duration: 3000, | 
|         }); | 
|       }); | 
|   }, | 
|   bindSuccess: function () { | 
|     // 设置标记,表示通过编程方式导航 | 
|     console.log('绑定成功,最终确认manualNavigate=true'); | 
|     this.setData({ | 
|       manualNavigate: true, | 
|       fromBack: false // 确保不会被识别为返回操作 | 
|     }); | 
|      | 
|     // 设置全局登录状态 | 
|     getApp().globalData.isLoggedIn = true; | 
|      | 
|      | 
|     wx.showToast({ | 
|       title: '绑定成功', | 
|       icon: 'success', | 
|       duration: 1500, | 
|       mask: true, | 
|       success: () => { | 
|         // 延迟跳转,确保Toast显示完成 | 
|         setTimeout(() => { | 
|           console.log('登录成功,准备返回到首页'); | 
|            | 
|           // 获取当前页面栈 | 
|           const pages = getCurrentPages(); | 
|            | 
|           // 检查是否有上一个页面可返回 | 
|           if (pages.length > 1) { | 
|             // 应用一个技巧,先设置前一个页面的参数 | 
|             try { | 
|               if (pages.length > 1) { | 
|                 const prevPage = pages[pages.length - 2]; | 
|                 if (prevPage && prevPage.options) { | 
|                   // 设置 fromLogin 参数 | 
|                   prevPage.options.fromLogin = 'true'; | 
|                    | 
|                   // 如果页面有onLoad方法,则可能需要传递参数 | 
|                   if (typeof prevPage.onLoad === 'function') { | 
|                     let newOptions = {...prevPage.options}; | 
|                     newOptions.fromLogin = 'true'; | 
|                     prevPage.onLoad(newOptions); | 
|                   } | 
|                 } | 
|               } | 
|             } catch (e) { | 
|               console.error('设置前一页参数失败:', e); | 
|             } | 
|              | 
|             // 返回到上一个页面 | 
|             wx.navigateBack({ | 
|               delta: 1, | 
|               success: () => { | 
|                 console.log('已成功返回到上一页面'); | 
|               }, | 
|               fail: (err) => { | 
|                 console.error('navigateBack失败:', err); | 
|                 // 如果返回失败,使用重定向 | 
|                 wx.redirectTo({ | 
|                   url: '/pages/home/home?fromLogin=true' | 
|                 }); | 
|               } | 
|             }); | 
|           } else { | 
|             // 如果没有上一个页面,则重新打开home页面 | 
|             wx.redirectTo({ | 
|               url: '/pages/home/home?fromLogin=true' | 
|             }); | 
|           } | 
|         }, 1500); | 
|       } | 
|     }); | 
|   }, | 
| }) |