huhaobangding.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. const app = getApp()
  2. const utils = require("../../utils/util.js")
  3. Page({
  4. data: {
  5. selectedTag: '',
  6. showCustomInput: false,
  7. customTagLength: 0,
  8. showDrawer: false,
  9. showPreview: false,
  10. // 是否可以切换水务公司
  11. isChange: true,
  12. // 新增数据
  13. waterCompany: '',
  14. waterCompanyList: [],
  15. //当前用户的默认水务公司的下标
  16. waterCompanyIndex: [0],
  17. waterCompanyId: app.globalData.launchPara,
  18. showWaterCompanyPicker: false,
  19. huHao: '',
  20. huMing: '',
  21. phone: '', // 添加手机号字段
  22. customTag: '',
  23. tagLength: 0,
  24. showQueryMethods: false,
  25. // 水站联系方式
  26. phoneQueryList: [
  27. ],
  28. // 户号预览图地址
  29. ylt: '',
  30. isBinding: false // 添加一个标记,表示是否正在绑定中
  31. },
  32. selectTag: function(e) {
  33. const tag = e.currentTarget.dataset.tag;
  34. this.setData({
  35. selectedTag: tag,
  36. showCustomInput: tag === '自定义',
  37. customTag: tag === '自定义' ? '' : tag,
  38. tagLength: 0 // 重置计数器
  39. });
  40. },
  41. onCustomInput(e) {
  42. const value = e.detail.value;
  43. this.setData({
  44. customTagLength: value.length,
  45. customTag: value
  46. });
  47. },
  48. // 显示抽屉
  49. showDrawer() {
  50. this.setData({
  51. showDrawer: true
  52. });
  53. },
  54. // 隐藏抽屉
  55. hideDrawer() {
  56. this.setData({
  57. showDrawer: false
  58. });
  59. },
  60. // 拨打电话
  61. makePhoneCall(e) {
  62. const phone = e.currentTarget.dataset.phone;
  63. // 检查电话号码是否包含分隔符(逗号、顿号等)
  64. if (phone.includes('、') || phone.includes(',') || phone.includes(',')) {
  65. // 分割电话号码字符串,处理各种可能的分隔符
  66. const phoneNumbers = phone.split(/[、,,]/);
  67. // 过滤掉空字符串并去除前后空格
  68. const validPhoneNumbers = phoneNumbers
  69. .map(num => num.trim())
  70. .filter(num => num !== '');
  71. if (validPhoneNumbers.length > 1) {
  72. // 如果有多个电话号码,显示操作菜单让用户选择
  73. wx.showActionSheet({
  74. itemList: validPhoneNumbers,
  75. success: function(res) {
  76. // 用户选择了一个号码
  77. wx.makePhoneCall({
  78. phoneNumber: validPhoneNumbers[res.tapIndex]
  79. });
  80. },
  81. fail: function(res) {
  82. console.log('用户取消选择')
  83. }
  84. });
  85. } else if (validPhoneNumbers.length === 1) {
  86. // 只有一个有效号码时直接拨打
  87. wx.makePhoneCall({
  88. phoneNumber: validPhoneNumbers[0]
  89. });
  90. }
  91. } else {
  92. // 没有分隔符,直接拨打
  93. wx.makePhoneCall({
  94. phoneNumber: phone
  95. });
  96. }
  97. },
  98. // 修改预览图片方法
  99. previewImage: function() {
  100. this.setData({
  101. showPreview: true
  102. });
  103. },
  104. // 关闭预览
  105. closePreview: function() {
  106. this.setData({
  107. showPreview: false
  108. });
  109. },
  110. // 防止点击图片内容时关闭预览
  111. preventBubble: function() {
  112. return;
  113. },
  114. // 显示水站公司选择器
  115. showWaterCompanyPicker: function() {
  116. // 检查水务公司列表是否已加载
  117. if (!this.data.waterCompanyList || this.data.waterCompanyList.length === 0) {
  118. wx.showToast({
  119. title: '水务公司数据加载中,请稍后再试',
  120. icon: 'none'
  121. });
  122. return;
  123. }
  124. // 确保waterCompanyIndex的有效性
  125. let currentIndex = this.data.waterCompanyIndex;
  126. if (!Array.isArray(currentIndex) || currentIndex.length === 0 ||
  127. currentIndex[0] >= this.data.waterCompanyList.length) {
  128. currentIndex = [0];
  129. }
  130. this.setData({
  131. showWaterCompanyPicker: true,
  132. waterCompanyIndex: currentIndex
  133. });
  134. },
  135. // 隐藏水站公司选择器
  136. hideWaterCompanyPicker: function() {
  137. this.setData({
  138. showWaterCompanyPicker: false
  139. });
  140. },
  141. // 水站公司选择变化
  142. onWaterCompanyChange: function(e) {
  143. // 确保传入的值是有效的
  144. if (!e || !e.detail || !e.detail.value) {
  145. console.error('水务公司选择变化事件数据无效:', e);
  146. return;
  147. }
  148. // 确保索引在有效范围内
  149. let newIndex = e.detail.value;
  150. if (!Array.isArray(newIndex) || newIndex.length === 0) {
  151. newIndex = [0];
  152. }
  153. // 验证索引是否超出范围
  154. if (newIndex[0] >= this.data.waterCompanyList.length) {
  155. newIndex = [0];
  156. console.error('水务公司索引超出范围,已重置为0');
  157. }
  158. this.setData({
  159. waterCompanyIndex: newIndex
  160. });
  161. },
  162. // 确认水站公司选择
  163. confirmWaterCompany: function(e) {
  164. const index = this.data.waterCompanyIndex[0];
  165. // 确保水站公司数据已加载
  166. if (!this.data.waterCompanyList || this.data.waterCompanyList.length === 0 || index >= this.data.waterCompanyList.length) {
  167. wx.showToast({
  168. title: '数据加载中,请稍后再试',
  169. icon: 'none'
  170. });
  171. this.hideWaterCompanyPicker();
  172. return;
  173. }
  174. // 获取选中的水务公司信息
  175. const selectedCompany = this.data.waterCompanyList[index];
  176. if (!selectedCompany) {
  177. wx.showToast({
  178. title: '选择的水务公司无效',
  179. icon: 'none'
  180. });
  181. this.hideWaterCompanyPicker();
  182. return;
  183. }
  184. // 使用固定的本地图片地址,而不是动态获取
  185. const yltUrl = '/static_file/jcsfcjtzd.jpg';
  186. // 获取营业点数据
  187. let phoneList = [];
  188. try {
  189. if (selectedCompany.businessPoint && Array.isArray(selectedCompany.businessPoint)) {
  190. phoneList = selectedCompany.businessPoint.map((data, index) => {
  191. return {
  192. id: index + 1,
  193. name: index + 1 + '、' + data.address,
  194. phone: data.contactphone,
  195. time: data.worktime
  196. };
  197. });
  198. }
  199. } catch (error) {
  200. console.error('处理营业点数据时出错:', error);
  201. }
  202. debugger
  203. // 更新数据状态
  204. this.setData({
  205. waterCompanyIndex: [index],
  206. waterCompany: selectedCompany.name,
  207. waterCompanyId: selectedCompany.id,
  208. showWaterCompanyPicker: false,
  209. phoneQueryList: phoneList,
  210. ylt: yltUrl
  211. });
  212. },
  213. // 户号输入
  214. onHuHaoInput: function(e) {
  215. // 使用正则表达式去除非数字字符
  216. const value = e.detail.value.replace(/[^\d]/g, '');
  217. // 更新输入框的值
  218. this.setData({
  219. huHao: value
  220. });
  221. },
  222. // 户名输入
  223. onHuMingInput: function(e) {
  224. // 使用正则表达式去除所有空格
  225. const value = e.detail.value.replace(/\s/g, '');
  226. this.setData({
  227. huMing: value
  228. });
  229. },
  230. // 获取户名
  231. getHuMing: function() {
  232. const { huHao, waterCompanyId } = this.data;
  233. debugger
  234. // 验证户号是否已输入
  235. if (!huHao) {
  236. wx.showToast({
  237. title: '请输入户号',
  238. icon: 'none'
  239. });
  240. return;
  241. }
  242. // 验证水站公司是否已选择
  243. if (!waterCompanyId) {
  244. wx.showToast({
  245. title: '请选择水站公司',
  246. icon: 'none'
  247. });
  248. return;
  249. }
  250. // 显示加载中
  251. wx.showLoading({
  252. title: '获取中...',
  253. mask: true
  254. });
  255. // 调用获取户名接口
  256. wx.request({
  257. url: app.globalData.interfaceUrls.getHuMing,
  258. method: 'GET',
  259. data: {
  260. huHao: huHao,
  261. waterCompanyId: waterCompanyId
  262. },
  263. header: {
  264. 'content-type': 'application/json', // 默认值
  265. 'token':app.globalData.userWxInfo.token,
  266. 'source':"wc",
  267. '!SAAS_LOGIN_TOKEN_!':this.data.waterCompanyList[this.data.waterCompanyIndex[0]].id
  268. },
  269. success: (res) => {
  270. wx.hideLoading();
  271. if (res.data.code === '200' && res.data.msg) {
  272. this.setData({
  273. huMing: res.data.msg
  274. });
  275. } else {
  276. this.setData({
  277. huMing: ''
  278. });
  279. wx.showToast({
  280. title: res.data.message || '未找到户名信息',
  281. icon: 'none'
  282. });
  283. }
  284. },
  285. fail: (error) => {
  286. wx.hideLoading();
  287. wx.showToast({
  288. title: '获取户名失败,请稍后重试',
  289. icon: 'none'
  290. });
  291. console.error('获取户名失败:', error);
  292. }
  293. });
  294. },
  295. // 手机号输入
  296. onPhoneInput: function(e) {
  297. // 使用正则表达式去除非数字字符
  298. const value = e.detail.value.replace(/[^\d]/g, '');
  299. this.setData({
  300. phone: value
  301. });
  302. },
  303. // 验证手机号格式
  304. validatePhone: function(phone) {
  305. const phoneReg = /^1[3-9]\d{9}$/;
  306. return phoneReg.test(phone);
  307. },
  308. // 确认绑定
  309. confirmBinding: function() {
  310. // 如果已经在处理绑定请求,则直接返回
  311. if (this.data.isBinding) {
  312. return;
  313. }
  314. // 设置绑定状态为true,防止重复点击
  315. this.setData({
  316. isBinding: true
  317. });
  318. // 验证必填字段
  319. if (!this.data.waterCompany) {
  320. wx.showToast({
  321. title: '请选择水站公司',
  322. icon: 'none'
  323. });
  324. this.setData({ isBinding: false }); // 重置状态
  325. return;
  326. }
  327. if (!this.data.huHao) {
  328. wx.showToast({
  329. title: '请输入户号',
  330. icon: 'none'
  331. });
  332. this.setData({ isBinding: false }); // 重置状态
  333. return;
  334. }
  335. if (!this.data.phone) {
  336. wx.showToast({
  337. title: '请输入手机号',
  338. icon: 'none'
  339. });
  340. this.setData({ isBinding: false }); // 重置状态
  341. return;
  342. }
  343. if (!this.validatePhone(this.data.phone)) {
  344. wx.showToast({
  345. title: '请输入正确的手机号',
  346. icon: 'none'
  347. });
  348. this.setData({ isBinding: false }); // 重置状态
  349. return;
  350. }
  351. // 获取最终的标签值
  352. let finalTag = this.data.selectedTag;
  353. // debugger
  354. if (this.data.selectedTag === '自定义') {
  355. finalTag = this.data.customTag || ''; // 使用用户输入的自定义标签
  356. }
  357. // 如果选择了自定义但没有输入内容,提示用户
  358. if (this.data.selectedTag === '自定义' && !this.data.customTag) {
  359. wx.showToast({
  360. title: '请输入自定义标签名称',
  361. icon: 'none'
  362. });
  363. this.setData({ isBinding: false }); // 重置状态
  364. return;
  365. }
  366. // 构建保存的数据对象
  367. const bindingData = {
  368. bindWaterCompany: this.data.waterCompanyList[this.data.waterCompanyIndex[0]].id,
  369. accountNum: this.data.huHao,
  370. accountName: this.data.huMing,
  371. phoneNum: this.data.phone, // 添加手机号字段
  372. groupType: finalTag, // 使用处理后的标签值
  373. openId: app.globalData.userWxInfo.openid,
  374. otherDsKey: (app.globalData.bindAccountInfo||[]).map(data=>data.dsKey)
  375. };
  376. // debugger
  377. // 提交数据
  378. wx.showLoading({
  379. title: '绑定中...',
  380. });
  381. const that = this; // 保存this引用
  382. // 调用绑定接口
  383. wx.request({
  384. url: app.globalData.interfaceUrls.accountBind,
  385. method: 'POST',
  386. data: bindingData,
  387. header: {
  388. 'content-type': 'application/json', // 默认值
  389. 'token':app.globalData.userWxInfo.token,
  390. 'source':"wc",
  391. '!SAAS_LOGIN_TOKEN_!':this.data.waterCompanyList[this.data.waterCompanyIndex[0]].id
  392. },
  393. success (res) {
  394. wx.hideLoading();
  395. let apiReturnData=res.data
  396. if(apiReturnData.code=='200'){
  397. wx.hideLoading();
  398. utils.simleInfo('绑定成功')
  399. // 确保正确更新全局数据
  400. const boundAccount = apiReturnData.data[0];
  401. app.globalData.currentAccountInfo = boundAccount;
  402. app.globalData.userWxInfo.currentDsKey = boundAccount.dsKey;
  403. app.globalData.userWxInfo.username = boundAccount.username;
  404. app.globalData.userWxInfo.usernumber = boundAccount.usernumber;
  405. app.globalData.userWxInfo.address = boundAccount.address;
  406. app.globalData.userWxInfo.groupName = boundAccount.groupName;
  407. // 保存到本地存储
  408. wx.setStorageSync('currentHuHao', boundAccount);
  409. // 设置标记表示需要刷新首页
  410. wx.setStorageSync('needRefreshHomepage', true);
  411. // 设置标记表示已绑定新户号
  412. wx.setStorageSync('justBoundNewAccount', true);
  413. // 设置全局刷新标记
  414. app.globalData.refresh = 1;
  415. // 确保homepage能够正确加载轮播通知数据
  416. // 增加适当的延迟,确保数据能够正确加载
  417. setTimeout(function(){
  418. // 使用reLaunch确保homepage完全重新加载
  419. wx.reLaunch({
  420. url: '/pages/homepage/homepage',
  421. success: function() {
  422. // 当跳转成功后,确保homepage可以正确加载数据
  423. // 再次设置justBoundNewAccount标记,确保onLoad和onShow都能正确处理
  424. wx.setStorageSync('justBoundNewAccount', true);
  425. }
  426. });
  427. },2000)
  428. }else{
  429. wx.hideLoading();
  430. setTimeout(() => {
  431. wx.showToast({
  432. title: apiReturnData.msg,
  433. icon: 'none',
  434. duration: 2000,
  435. mask: true
  436. });
  437. that.setData({ isBinding: false }); // 重置绑定状态
  438. }, 100);
  439. }
  440. },
  441. fail(error) {
  442. wx.hideLoading()
  443. utils.simleInfo('登录失败,请稍后再试')
  444. that.setData({ isBinding: false }); // 重置绑定状态
  445. }
  446. })
  447. },
  448. onLoad: function() {
  449. const that = this;
  450. // 通过POST请求获取水站列表
  451. wx.showLoading({
  452. title: '加载中...',
  453. });
  454. wx.request({
  455. url: app.globalData.interfaceUrls.waterList,
  456. method: 'POST',
  457. data: {"dsKey":""},
  458. header: {
  459. 'content-type': 'application/json',
  460. 'token': app.globalData.userWxInfo.token,
  461. 'source': "wc"
  462. },
  463. success(res) {
  464. wx.hideLoading();
  465. if (res.data.code == '200' && res.data.data) {
  466. // 获取成功,设置水务公司列表
  467. that.setData({
  468. waterCompanyList: res.data.data
  469. });
  470. // 检查是否从FirstBangDing页面跳转过来
  471. const goingToBindNewAccount = wx.getStorageSync('goingToBindNewAccount');
  472. if (goingToBindNewAccount) {
  473. // 清除标记
  474. wx.removeStorageSync('goingToBindNewAccount');
  475. // 不需要额外处理,因为FirstBangDing已经设置了必要的标记
  476. }
  477. // 延迟设置导航栏,确保页面已完全加载
  478. setTimeout(() => {
  479. wx.setNavigationBarTitle({
  480. title: '户号绑定'
  481. });
  482. wx.setNavigationBarColor({
  483. frontColor: '#ffffff',
  484. backgroundColor: '#0066FF'
  485. });
  486. }, 100);
  487. // 设置固定的预览图地址
  488. that.setData({
  489. ylt: '/static_file/jcsfcjtzd.jpg'
  490. });
  491. // 从app.globalData.dsId获取当前用户的dsid
  492. const dsid = app.globalData.currentAccountInfo.dsId;
  493. let companyIndex = -1;
  494. // 如果dsid不为null,查找匹配的水站公司
  495. if (dsid) {
  496. companyIndex = that.data.waterCompanyList.findIndex(data => data.id == dsid);
  497. }
  498. // 如果没有找到匹配的dsid或dsid为null,则检查其他可能的水站信息
  499. if (companyIndex === -1) {
  500. // 先检查当前用户信息中是否存在水站信息
  501. if (app.globalData.userWxInfo && app.globalData.userWxInfo.waterCompanyId) {
  502. companyIndex = that.data.waterCompanyList.findIndex(data => data.id == app.globalData.userWxInfo.waterCompanyId);
  503. }
  504. // 再检查当前账户信息中是否存在水站信息
  505. if (companyIndex === -1 && app.globalData.currentAccountInfo && app.globalData.currentAccountInfo.waterCompanyId) {
  506. companyIndex = that.data.waterCompanyList.findIndex(data => data.id == app.globalData.currentAccountInfo.waterCompanyId);
  507. }
  508. // 如果指定了启动参数,则优先使用启动参数
  509. if(app.globalData.launchPara){
  510. companyIndex = that.data.waterCompanyList.findIndex(data => data.id == app.globalData.launchPara);
  511. }
  512. }
  513. // 如果找到了对应的水站公司
  514. if (companyIndex !== -1) {
  515. const company = that.data.waterCompanyList[companyIndex];
  516. if (!company) {
  517. console.error('水务公司数据无效');
  518. return;
  519. }
  520. // 获取营业点数据
  521. let phoneList = [];
  522. try {
  523. if (company.businessPoint && Array.isArray(company.businessPoint)) {
  524. phoneList = company.businessPoint.map((data, index) => {
  525. return {
  526. id: index+1,
  527. name: data.address,
  528. phone: data.contactphone,
  529. time: data.worktime
  530. };
  531. });
  532. }
  533. } catch (error) {
  534. console.error('处理营业点数据时出错:', error);
  535. }
  536. that.setData({
  537. waterCompanyIndex: [companyIndex],
  538. waterCompany: company.name,
  539. waterCompanyId: company.id,
  540. isChange: true,
  541. phoneQueryList: phoneList
  542. });
  543. }
  544. } else {
  545. wx.showToast({
  546. title: res.data.msg || '获取水务公司列表失败',
  547. icon: 'none'
  548. });
  549. }
  550. },
  551. fail(error) {
  552. wx.hideLoading();
  553. wx.showToast({
  554. title: '网络错误,请稍后再试',
  555. icon: 'none'
  556. });
  557. console.error('获取水务公司列表失败:', error);
  558. }
  559. });
  560. },
  561. /**
  562. * 返回上一页
  563. */
  564. goBack: function () {
  565. wx.navigateBack();
  566. },
  567. onTagInput: function(e) {
  568. let value = e.detail.value;
  569. // 如果超过4个字符,截取前4个字符
  570. if (value.length > 4) {
  571. value = value.slice(0, 4);
  572. }
  573. this.setData({
  574. tagLength: value.length,
  575. customTag: value
  576. });
  577. },
  578. showQueryMethodsModal() {
  579. this.setData({
  580. showQueryMethods: true
  581. });
  582. },
  583. closeQueryMethods() {
  584. this.setData({
  585. showQueryMethods: false
  586. });
  587. },
  588. // 防止抽屉中非列表区域滚动引起整个页面滚动
  589. preventNonListScroll: function(e) {
  590. // 由于现在使用scroll-view,我们只需阻止非滚动区域的默认滚动
  591. return true;
  592. },
  593. // 防止蒙层触摸事件
  594. preventTouchMove: function(e) {
  595. return false;
  596. }
  597. });