const app = getApp() const utils = require("../../utils/util.js") Page({ data: { selectedTag: '', showCustomInput: false, customTagLength: 0, showDrawer: false, showPreview: false, // 是否可以切换水务公司 isChange: true, // 新增数据 waterCompany: '', waterCompanyList: [], //当前用户的默认水务公司的下标 waterCompanyIndex: [0], waterCompanyId: app.globalData.launchPara, showWaterCompanyPicker: false, huHao: '', huMing: '', phone: '', // 添加手机号字段 customTag: '', tagLength: 0, showQueryMethods: false, // 水站联系方式 phoneQueryList: [ ], // 户号预览图地址 ylt: '', isBinding: false // 添加一个标记,表示是否正在绑定中 }, selectTag: function(e) { const tag = e.currentTarget.dataset.tag; this.setData({ selectedTag: tag, showCustomInput: tag === '自定义', customTag: tag === '自定义' ? '' : tag, tagLength: 0 // 重置计数器 }); }, onCustomInput(e) { const value = e.detail.value; this.setData({ customTagLength: value.length, customTag: value }); }, // 显示抽屉 showDrawer() { this.setData({ showDrawer: true }); }, // 隐藏抽屉 hideDrawer() { this.setData({ showDrawer: false }); }, // 拨打电话 makePhoneCall(e) { const phone = e.currentTarget.dataset.phone; // 检查电话号码是否包含分隔符(逗号、顿号等) if (phone.includes('、') || phone.includes(',') || phone.includes(',')) { // 分割电话号码字符串,处理各种可能的分隔符 const phoneNumbers = phone.split(/[、,,]/); // 过滤掉空字符串并去除前后空格 const validPhoneNumbers = phoneNumbers .map(num => num.trim()) .filter(num => num !== ''); if (validPhoneNumbers.length > 1) { // 如果有多个电话号码,显示操作菜单让用户选择 wx.showActionSheet({ itemList: validPhoneNumbers, success: function(res) { // 用户选择了一个号码 wx.makePhoneCall({ phoneNumber: validPhoneNumbers[res.tapIndex] }); }, fail: function(res) { console.log('用户取消选择') } }); } else if (validPhoneNumbers.length === 1) { // 只有一个有效号码时直接拨打 wx.makePhoneCall({ phoneNumber: validPhoneNumbers[0] }); } } else { // 没有分隔符,直接拨打 wx.makePhoneCall({ phoneNumber: phone }); } }, // 修改预览图片方法 previewImage: function() { this.setData({ showPreview: true }); }, // 关闭预览 closePreview: function() { this.setData({ showPreview: false }); }, // 防止点击图片内容时关闭预览 preventBubble: function() { return; }, // 显示水站公司选择器 showWaterCompanyPicker: function() { // 检查水务公司列表是否已加载 if (!this.data.waterCompanyList || this.data.waterCompanyList.length === 0) { wx.showToast({ title: '水务公司数据加载中,请稍后再试', icon: 'none' }); return; } // 确保waterCompanyIndex的有效性 let currentIndex = this.data.waterCompanyIndex; if (!Array.isArray(currentIndex) || currentIndex.length === 0 || currentIndex[0] >= this.data.waterCompanyList.length) { currentIndex = [0]; } this.setData({ showWaterCompanyPicker: true, waterCompanyIndex: currentIndex }); }, // 隐藏水站公司选择器 hideWaterCompanyPicker: function() { this.setData({ showWaterCompanyPicker: false }); }, // 水站公司选择变化 onWaterCompanyChange: function(e) { // 确保传入的值是有效的 if (!e || !e.detail || !e.detail.value) { console.error('水务公司选择变化事件数据无效:', e); return; } // 确保索引在有效范围内 let newIndex = e.detail.value; if (!Array.isArray(newIndex) || newIndex.length === 0) { newIndex = [0]; } // 验证索引是否超出范围 if (newIndex[0] >= this.data.waterCompanyList.length) { newIndex = [0]; console.error('水务公司索引超出范围,已重置为0'); } this.setData({ waterCompanyIndex: newIndex }); }, // 确认水站公司选择 confirmWaterCompany: function(e) { const index = this.data.waterCompanyIndex[0]; // 确保水站公司数据已加载 if (!this.data.waterCompanyList || this.data.waterCompanyList.length === 0 || index >= this.data.waterCompanyList.length) { wx.showToast({ title: '数据加载中,请稍后再试', icon: 'none' }); this.hideWaterCompanyPicker(); return; } // 获取选中的水务公司信息 const selectedCompany = this.data.waterCompanyList[index]; if (!selectedCompany) { wx.showToast({ title: '选择的水务公司无效', icon: 'none' }); this.hideWaterCompanyPicker(); return; } // 使用固定的本地图片地址,而不是动态获取 const yltUrl = '/static_file/jcsfcjtzd.jpg'; // 获取营业点数据 let phoneList = []; try { if (selectedCompany.businessPoint && Array.isArray(selectedCompany.businessPoint)) { phoneList = selectedCompany.businessPoint.map((data, index) => { return { id: index + 1, name: index + 1 + '、' + data.address, phone: data.contactphone, time: data.worktime }; }); } } catch (error) { console.error('处理营业点数据时出错:', error); } debugger // 更新数据状态 this.setData({ waterCompanyIndex: [index], waterCompany: selectedCompany.name, waterCompanyId: selectedCompany.id, showWaterCompanyPicker: false, phoneQueryList: phoneList, ylt: yltUrl }); }, // 户号输入 onHuHaoInput: function(e) { // 使用正则表达式去除非数字字符 const value = e.detail.value.replace(/[^\d]/g, ''); // 更新输入框的值 this.setData({ huHao: value }); }, // 户名输入 onHuMingInput: function(e) { // 使用正则表达式去除所有空格 const value = e.detail.value.replace(/\s/g, ''); this.setData({ huMing: value }); }, // 获取户名 getHuMing: function() { const { huHao, waterCompanyId } = this.data; debugger // 验证户号是否已输入 if (!huHao) { wx.showToast({ title: '请输入户号', icon: 'none' }); return; } // 验证水站公司是否已选择 if (!waterCompanyId) { wx.showToast({ title: '请选择水站公司', icon: 'none' }); return; } // 显示加载中 wx.showLoading({ title: '获取中...', mask: true }); // 调用获取户名接口 wx.request({ url: app.globalData.interfaceUrls.getHuMing, method: 'GET', data: { huHao: huHao, waterCompanyId: waterCompanyId }, header: { 'content-type': 'application/json', // 默认值 'token':app.globalData.userWxInfo.token, 'source':"wc", '!SAAS_LOGIN_TOKEN_!':this.data.waterCompanyList[this.data.waterCompanyIndex[0]].id }, success: (res) => { wx.hideLoading(); if (res.data.code === '200' && res.data.msg) { this.setData({ huMing: res.data.msg }); } else { this.setData({ huMing: '' }); wx.showToast({ title: res.data.message || '未找到户名信息', icon: 'none' }); } }, fail: (error) => { wx.hideLoading(); wx.showToast({ title: '获取户名失败,请稍后重试', icon: 'none' }); console.error('获取户名失败:', error); } }); }, // 手机号输入 onPhoneInput: function(e) { // 使用正则表达式去除非数字字符 const value = e.detail.value.replace(/[^\d]/g, ''); this.setData({ phone: value }); }, // 验证手机号格式 validatePhone: function(phone) { const phoneReg = /^1[3-9]\d{9}$/; return phoneReg.test(phone); }, // 确认绑定 confirmBinding: function() { // 如果已经在处理绑定请求,则直接返回 if (this.data.isBinding) { return; } // 设置绑定状态为true,防止重复点击 this.setData({ isBinding: true }); // 验证必填字段 if (!this.data.waterCompany) { wx.showToast({ title: '请选择水站公司', icon: 'none' }); this.setData({ isBinding: false }); // 重置状态 return; } if (!this.data.huHao) { wx.showToast({ title: '请输入户号', icon: 'none' }); this.setData({ isBinding: false }); // 重置状态 return; } if (!this.data.phone) { wx.showToast({ title: '请输入手机号', icon: 'none' }); this.setData({ isBinding: false }); // 重置状态 return; } if (!this.validatePhone(this.data.phone)) { wx.showToast({ title: '请输入正确的手机号', icon: 'none' }); this.setData({ isBinding: false }); // 重置状态 return; } // 获取最终的标签值 let finalTag = this.data.selectedTag; // debugger if (this.data.selectedTag === '自定义') { finalTag = this.data.customTag || ''; // 使用用户输入的自定义标签 } // 如果选择了自定义但没有输入内容,提示用户 if (this.data.selectedTag === '自定义' && !this.data.customTag) { wx.showToast({ title: '请输入自定义标签名称', icon: 'none' }); this.setData({ isBinding: false }); // 重置状态 return; } // 构建保存的数据对象 const bindingData = { bindWaterCompany: this.data.waterCompanyList[this.data.waterCompanyIndex[0]].id, accountNum: this.data.huHao, accountName: this.data.huMing, phoneNum: this.data.phone, // 添加手机号字段 groupType: finalTag, // 使用处理后的标签值 openId: app.globalData.userWxInfo.openid, otherDsKey: (app.globalData.bindAccountInfo||[]).map(data=>data.dsKey) }; // debugger // 提交数据 wx.showLoading({ title: '绑定中...', }); const that = this; // 保存this引用 // 调用绑定接口 wx.request({ url: app.globalData.interfaceUrls.accountBind, method: 'POST', data: bindingData, header: { 'content-type': 'application/json', // 默认值 'token':app.globalData.userWxInfo.token, 'source':"wc", '!SAAS_LOGIN_TOKEN_!':this.data.waterCompanyList[this.data.waterCompanyIndex[0]].id }, success (res) { wx.hideLoading(); let apiReturnData=res.data if(apiReturnData.code=='200'){ wx.hideLoading(); utils.simleInfo('绑定成功') // 确保正确更新全局数据 const boundAccount = apiReturnData.data[0]; app.globalData.currentAccountInfo = boundAccount; app.globalData.userWxInfo.currentDsKey = boundAccount.dsKey; app.globalData.userWxInfo.username = boundAccount.username; app.globalData.userWxInfo.usernumber = boundAccount.usernumber; app.globalData.userWxInfo.address = boundAccount.address; app.globalData.userWxInfo.groupName = boundAccount.groupName; // 保存到本地存储 wx.setStorageSync('currentHuHao', boundAccount); // 设置标记表示需要刷新首页 wx.setStorageSync('needRefreshHomepage', true); // 设置标记表示已绑定新户号 wx.setStorageSync('justBoundNewAccount', true); // 设置全局刷新标记 app.globalData.refresh = 1; // 确保homepage能够正确加载轮播通知数据 // 增加适当的延迟,确保数据能够正确加载 setTimeout(function(){ // 使用reLaunch确保homepage完全重新加载 wx.reLaunch({ url: '/pages/homepage/homepage', success: function() { // 当跳转成功后,确保homepage可以正确加载数据 // 再次设置justBoundNewAccount标记,确保onLoad和onShow都能正确处理 wx.setStorageSync('justBoundNewAccount', true); } }); },2000) }else{ wx.hideLoading(); setTimeout(() => { wx.showToast({ title: apiReturnData.msg, icon: 'none', duration: 2000, mask: true }); that.setData({ isBinding: false }); // 重置绑定状态 }, 100); } }, fail(error) { wx.hideLoading() utils.simleInfo('登录失败,请稍后再试') that.setData({ isBinding: false }); // 重置绑定状态 } }) }, onLoad: function() { const that = this; // 通过POST请求获取水站列表 wx.showLoading({ title: '加载中...', }); wx.request({ url: app.globalData.interfaceUrls.waterList, method: 'POST', data: {"dsKey":""}, header: { 'content-type': 'application/json', 'token': app.globalData.userWxInfo.token, 'source': "wc" }, success(res) { wx.hideLoading(); if (res.data.code == '200' && res.data.data) { // 获取成功,设置水务公司列表 that.setData({ waterCompanyList: res.data.data }); // 检查是否从FirstBangDing页面跳转过来 const goingToBindNewAccount = wx.getStorageSync('goingToBindNewAccount'); if (goingToBindNewAccount) { // 清除标记 wx.removeStorageSync('goingToBindNewAccount'); // 不需要额外处理,因为FirstBangDing已经设置了必要的标记 } // 延迟设置导航栏,确保页面已完全加载 setTimeout(() => { wx.setNavigationBarTitle({ title: '户号绑定' }); wx.setNavigationBarColor({ frontColor: '#ffffff', backgroundColor: '#0066FF' }); }, 100); // 设置固定的预览图地址 that.setData({ ylt: '/static_file/jcsfcjtzd.jpg' }); // 从app.globalData.dsId获取当前用户的dsid const dsid = app.globalData.currentAccountInfo.dsId; let companyIndex = -1; // 如果dsid不为null,查找匹配的水站公司 if (dsid) { companyIndex = that.data.waterCompanyList.findIndex(data => data.id == dsid); } // 如果没有找到匹配的dsid或dsid为null,则检查其他可能的水站信息 if (companyIndex === -1) { // 先检查当前用户信息中是否存在水站信息 if (app.globalData.userWxInfo && app.globalData.userWxInfo.waterCompanyId) { companyIndex = that.data.waterCompanyList.findIndex(data => data.id == app.globalData.userWxInfo.waterCompanyId); } // 再检查当前账户信息中是否存在水站信息 if (companyIndex === -1 && app.globalData.currentAccountInfo && app.globalData.currentAccountInfo.waterCompanyId) { companyIndex = that.data.waterCompanyList.findIndex(data => data.id == app.globalData.currentAccountInfo.waterCompanyId); } // 如果指定了启动参数,则优先使用启动参数 if(app.globalData.launchPara){ companyIndex = that.data.waterCompanyList.findIndex(data => data.id == app.globalData.launchPara); } } // 如果找到了对应的水站公司 if (companyIndex !== -1) { const company = that.data.waterCompanyList[companyIndex]; if (!company) { console.error('水务公司数据无效'); return; } // 获取营业点数据 let phoneList = []; try { if (company.businessPoint && Array.isArray(company.businessPoint)) { phoneList = company.businessPoint.map((data, index) => { return { id: index+1, name: data.address, phone: data.contactphone, time: data.worktime }; }); } } catch (error) { console.error('处理营业点数据时出错:', error); } that.setData({ waterCompanyIndex: [companyIndex], waterCompany: company.name, waterCompanyId: company.id, isChange: true, phoneQueryList: phoneList }); } } else { wx.showToast({ title: res.data.msg || '获取水务公司列表失败', icon: 'none' }); } }, fail(error) { wx.hideLoading(); wx.showToast({ title: '网络错误,请稍后再试', icon: 'none' }); console.error('获取水务公司列表失败:', error); } }); }, /** * 返回上一页 */ goBack: function () { wx.navigateBack(); }, onTagInput: function(e) { let value = e.detail.value; // 如果超过4个字符,截取前4个字符 if (value.length > 4) { value = value.slice(0, 4); } this.setData({ tagLength: value.length, customTag: value }); }, showQueryMethodsModal() { this.setData({ showQueryMethods: true }); }, closeQueryMethods() { this.setData({ showQueryMethods: false }); }, // 防止抽屉中非列表区域滚动引起整个页面滚动 preventNonListScroll: function(e) { // 由于现在使用scroll-view,我们只需阻止非滚动区域的默认滚动 return true; }, // 防止蒙层触摸事件 preventTouchMove: function(e) { return false; } });