123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- const app = getApp()
- const utils = require("../../utils/util.js")
- Page({
- data: {
- selectedTag: '',
- showCustomInput: false,
- customTagLength: 0,
- showDrawer: false,
- showPreview: false,
- // 是否可以切换水务公司
- isChange: true,
- // 新增数据
- waterCompany: '',
- waterCompanyList: app.globalData.waterCompanys,
- //当前用户的默认水务公司的下标
- 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;
- 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) {
- // 如果列表为空,尝试重新获取全局数据
- if (app.globalData.waterCompanys && app.globalData.waterCompanys.length > 0) {
- this.setData({
- waterCompanyList: app.globalData.waterCompanys
- });
- } else {
- 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,
- 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
- });
- },
- // 手机号输入
- 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;
-
- setTimeout(function(){
- // 使用reLaunch确保homepage完全重新加载
- wx.reLaunch({
- url: '/pages/homepage/homepage',
- });
- },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() {
- // 延迟设置导航栏,确保页面已完全加载
- setTimeout(() => {
- wx.setNavigationBarTitle({
- title: '户号绑定'
- });
- wx.setNavigationBarColor({
- frontColor: '#ffffff',
- backgroundColor: '#0066FF'
- });
- }, 100);
-
- // 设置固定的预览图地址
- this.setData({
- ylt: '/static_file/jcsfcjtzd.jpg'
- });
-
- // 确保水务公司列表已加载
- if (!this.data.waterCompanyList || this.data.waterCompanyList.length === 0) {
- if (app.globalData.waterCompanys && app.globalData.waterCompanys.length > 0) {
- this.setData({
- waterCompanyList: app.globalData.waterCompanys
- });
- } else {
- console.error('水务公司数据未加载');
- // 可以在这里添加重试逻辑或者提示用户刷新页面
- }
- }
-
- if(app.globalData.launchPara){
- // 在设置数据前检查水务公司是否存在
- const companyIndex = app.globalData.waterCompanys.findIndex(data => data.id == app.globalData.launchPara);
- if (companyIndex === -1) {
- console.error('未找到指定的水务公司');
- return;
- }
-
- const company = app.globalData.waterCompanys[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);
- }
-
- this.setData({
- waterCompanyIndex: [companyIndex],
- waterCompany: company.name,
- isChange: false,
- phoneQueryList: phoneList
- });
- }
- },
- /**
- * 返回上一页
- */
- 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;
- }
- });
|