huhaobangding.js 17 KB

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