huhaobangding.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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: app.globalData.waterCompanys,
  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. // 如果列表为空,尝试重新获取全局数据
  88. if (app.globalData.waterCompanys && app.globalData.waterCompanys.length > 0) {
  89. this.setData({
  90. waterCompanyList: app.globalData.waterCompanys
  91. });
  92. } else {
  93. wx.showToast({
  94. title: '水务公司数据加载中,请稍后再试',
  95. icon: 'none'
  96. });
  97. return;
  98. }
  99. }
  100. // 确保waterCompanyIndex的有效性
  101. let currentIndex = this.data.waterCompanyIndex;
  102. if (!Array.isArray(currentIndex) || currentIndex.length === 0 ||
  103. currentIndex[0] >= this.data.waterCompanyList.length) {
  104. currentIndex = [0];
  105. }
  106. this.setData({
  107. showWaterCompanyPicker: true,
  108. waterCompanyIndex: currentIndex
  109. });
  110. },
  111. // 隐藏水站公司选择器
  112. hideWaterCompanyPicker: function() {
  113. this.setData({
  114. showWaterCompanyPicker: false
  115. });
  116. },
  117. // 水站公司选择变化
  118. onWaterCompanyChange: function(e) {
  119. // 确保传入的值是有效的
  120. if (!e || !e.detail || !e.detail.value) {
  121. console.error('水务公司选择变化事件数据无效:', e);
  122. return;
  123. }
  124. // 确保索引在有效范围内
  125. let newIndex = e.detail.value;
  126. if (!Array.isArray(newIndex) || newIndex.length === 0) {
  127. newIndex = [0];
  128. }
  129. // 验证索引是否超出范围
  130. if (newIndex[0] >= this.data.waterCompanyList.length) {
  131. newIndex = [0];
  132. console.error('水务公司索引超出范围,已重置为0');
  133. }
  134. this.setData({
  135. waterCompanyIndex: newIndex
  136. });
  137. },
  138. // 确认水站公司选择
  139. confirmWaterCompany: function(e) {
  140. const index = this.data.waterCompanyIndex[0];
  141. // 确保水站公司数据已加载
  142. if (!this.data.waterCompanyList || this.data.waterCompanyList.length === 0 || index >= this.data.waterCompanyList.length) {
  143. wx.showToast({
  144. title: '数据加载中,请稍后再试',
  145. icon: 'none'
  146. });
  147. this.hideWaterCompanyPicker();
  148. return;
  149. }
  150. // 获取选中的水务公司信息
  151. const selectedCompany = this.data.waterCompanyList[index];
  152. if (!selectedCompany) {
  153. wx.showToast({
  154. title: '选择的水务公司无效',
  155. icon: 'none'
  156. });
  157. this.hideWaterCompanyPicker();
  158. return;
  159. }
  160. // 使用固定的本地图片地址,而不是动态获取
  161. const yltUrl = '/static_file/jcsfcjtzd.jpg';
  162. // 获取营业点数据
  163. let phoneList = [];
  164. try {
  165. if (selectedCompany.businessPoint && Array.isArray(selectedCompany.businessPoint)) {
  166. phoneList = selectedCompany.businessPoint.map((data, index) => {
  167. return {
  168. id: index + 1,
  169. name: index + 1 + '、' + data.address,
  170. phone: data.contactphone,
  171. time: data.worktime
  172. };
  173. });
  174. }
  175. } catch (error) {
  176. console.error('处理营业点数据时出错:', error);
  177. }
  178. debugger
  179. // 更新数据状态
  180. this.setData({
  181. waterCompanyIndex: [index],
  182. waterCompany: selectedCompany.name,
  183. showWaterCompanyPicker: false,
  184. phoneQueryList: phoneList,
  185. ylt: yltUrl
  186. });
  187. },
  188. // 户号输入
  189. onHuHaoInput: function(e) {
  190. // 使用正则表达式去除非数字字符
  191. const value = e.detail.value.replace(/[^\d]/g, '');
  192. // 更新输入框的值
  193. this.setData({
  194. huHao: value
  195. });
  196. },
  197. // 户名输入
  198. onHuMingInput: function(e) {
  199. // 使用正则表达式去除所有空格
  200. const value = e.detail.value.replace(/\s/g, '');
  201. this.setData({
  202. huMing: value
  203. });
  204. },
  205. // 手机号输入
  206. onPhoneInput: function(e) {
  207. // 使用正则表达式去除非数字字符
  208. const value = e.detail.value.replace(/[^\d]/g, '');
  209. this.setData({
  210. phone: value
  211. });
  212. },
  213. // 验证手机号格式
  214. validatePhone: function(phone) {
  215. const phoneReg = /^1[3-9]\d{9}$/;
  216. return phoneReg.test(phone);
  217. },
  218. // 确认绑定
  219. confirmBinding: function() {
  220. // 如果已经在处理绑定请求,则直接返回
  221. if (this.data.isBinding) {
  222. return;
  223. }
  224. // 设置绑定状态为true,防止重复点击
  225. this.setData({
  226. isBinding: true
  227. });
  228. // 验证必填字段
  229. if (!this.data.waterCompany) {
  230. wx.showToast({
  231. title: '请选择水站公司',
  232. icon: 'none'
  233. });
  234. this.setData({ isBinding: false }); // 重置状态
  235. return;
  236. }
  237. if (!this.data.huHao) {
  238. wx.showToast({
  239. title: '请输入户号',
  240. icon: 'none'
  241. });
  242. this.setData({ isBinding: false }); // 重置状态
  243. return;
  244. }
  245. if (!this.data.phone) {
  246. wx.showToast({
  247. title: '请输入手机号',
  248. icon: 'none'
  249. });
  250. this.setData({ isBinding: false }); // 重置状态
  251. return;
  252. }
  253. if (!this.validatePhone(this.data.phone)) {
  254. wx.showToast({
  255. title: '请输入正确的手机号',
  256. icon: 'none'
  257. });
  258. this.setData({ isBinding: false }); // 重置状态
  259. return;
  260. }
  261. // 获取最终的标签值
  262. let finalTag = this.data.selectedTag;
  263. // debugger
  264. if (this.data.selectedTag === '自定义') {
  265. finalTag = this.data.customTag || ''; // 使用用户输入的自定义标签
  266. }
  267. // 如果选择了自定义但没有输入内容,提示用户
  268. if (this.data.selectedTag === '自定义' && !this.data.customTag) {
  269. wx.showToast({
  270. title: '请输入自定义标签名称',
  271. icon: 'none'
  272. });
  273. this.setData({ isBinding: false }); // 重置状态
  274. return;
  275. }
  276. // 构建保存的数据对象
  277. const bindingData = {
  278. bindWaterCompany: this.data.waterCompanyList[this.data.waterCompanyIndex[0]].id,
  279. accountNum: this.data.huHao,
  280. accountName: this.data.huMing,
  281. phoneNum: this.data.phone, // 添加手机号字段
  282. groupType: finalTag, // 使用处理后的标签值
  283. openId: app.globalData.userWxInfo.openid,
  284. otherDsKey: (app.globalData.bindAccountInfo||[]).map(data=>data.dsKey)
  285. };
  286. // debugger
  287. // 提交数据
  288. wx.showLoading({
  289. title: '绑定中...',
  290. });
  291. const that = this; // 保存this引用
  292. // 调用绑定接口
  293. wx.request({
  294. url: app.globalData.interfaceUrls.accountBind,
  295. method: 'POST',
  296. data: bindingData,
  297. header: {
  298. 'content-type': 'application/json', // 默认值
  299. 'token':app.globalData.userWxInfo.token,
  300. 'source':"wc",
  301. '!SAAS_LOGIN_TOKEN_!':this.data.waterCompanyList[this.data.waterCompanyIndex[0]].id
  302. },
  303. success (res) {
  304. wx.hideLoading();
  305. let apiReturnData=res.data
  306. if(apiReturnData.code=='200'){
  307. wx.hideLoading();
  308. utils.simleInfo('绑定成功')
  309. // 确保正确更新全局数据
  310. const boundAccount = apiReturnData.data[0];
  311. app.globalData.currentAccountInfo = boundAccount;
  312. app.globalData.userWxInfo.currentDsKey = boundAccount.dsKey;
  313. app.globalData.userWxInfo.username = boundAccount.username;
  314. app.globalData.userWxInfo.usernumber = boundAccount.usernumber;
  315. app.globalData.userWxInfo.address = boundAccount.address;
  316. app.globalData.userWxInfo.groupName = boundAccount.groupName;
  317. // 保存到本地存储
  318. wx.setStorageSync('currentHuHao', boundAccount);
  319. // 设置标记表示需要刷新首页
  320. wx.setStorageSync('needRefreshHomepage', true);
  321. // 设置标记表示已绑定新户号
  322. wx.setStorageSync('justBoundNewAccount', true);
  323. // 设置全局刷新标记
  324. app.globalData.refresh = 1;
  325. setTimeout(function(){
  326. // 使用reLaunch确保homepage完全重新加载
  327. wx.reLaunch({
  328. url: '/pages/homepage/homepage',
  329. });
  330. },2000)
  331. }else{
  332. wx.hideLoading();
  333. setTimeout(() => {
  334. wx.showToast({
  335. title: apiReturnData.msg,
  336. icon: 'none',
  337. duration: 2000,
  338. mask: true
  339. });
  340. that.setData({ isBinding: false }); // 重置绑定状态
  341. }, 100);
  342. }
  343. },
  344. fail(error) {
  345. wx.hideLoading()
  346. utils.simleInfo('登录失败,请稍后再试')
  347. that.setData({ isBinding: false }); // 重置绑定状态
  348. }
  349. })
  350. },
  351. onLoad: function() {
  352. // 延迟设置导航栏,确保页面已完全加载
  353. setTimeout(() => {
  354. wx.setNavigationBarTitle({
  355. title: '户号绑定'
  356. });
  357. wx.setNavigationBarColor({
  358. frontColor: '#ffffff',
  359. backgroundColor: '#0066FF'
  360. });
  361. }, 100);
  362. // 设置固定的预览图地址
  363. this.setData({
  364. ylt: '/static_file/jcsfcjtzd.jpg'
  365. });
  366. // 确保水务公司列表已加载
  367. if (!this.data.waterCompanyList || this.data.waterCompanyList.length === 0) {
  368. if (app.globalData.waterCompanys && app.globalData.waterCompanys.length > 0) {
  369. this.setData({
  370. waterCompanyList: app.globalData.waterCompanys
  371. });
  372. } else {
  373. console.error('水务公司数据未加载');
  374. // 可以在这里添加重试逻辑或者提示用户刷新页面
  375. }
  376. }
  377. if(app.globalData.launchPara){
  378. // 在设置数据前检查水务公司是否存在
  379. const companyIndex = app.globalData.waterCompanys.findIndex(data => data.id == app.globalData.launchPara);
  380. if (companyIndex === -1) {
  381. console.error('未找到指定的水务公司');
  382. return;
  383. }
  384. const company = app.globalData.waterCompanys[companyIndex];
  385. if (!company) {
  386. console.error('水务公司数据无效');
  387. return;
  388. }
  389. // 获取营业点数据
  390. let phoneList = [];
  391. try {
  392. if (company.businessPoint && Array.isArray(company.businessPoint)) {
  393. phoneList = company.businessPoint.map((data, index) => {
  394. return {
  395. id: index+1,
  396. name: data.address,
  397. phone: data.contactphone,
  398. time: data.worktime
  399. };
  400. });
  401. }
  402. } catch (error) {
  403. console.error('处理营业点数据时出错:', error);
  404. }
  405. this.setData({
  406. waterCompanyIndex: [companyIndex],
  407. waterCompany: company.name,
  408. isChange: false,
  409. phoneQueryList: phoneList
  410. });
  411. }
  412. },
  413. /**
  414. * 返回上一页
  415. */
  416. goBack: function () {
  417. wx.navigateBack();
  418. },
  419. onTagInput: function(e) {
  420. let value = e.detail.value;
  421. // 如果超过4个字符,截取前4个字符
  422. if (value.length > 4) {
  423. value = value.slice(0, 4);
  424. }
  425. this.setData({
  426. tagLength: value.length,
  427. customTag: value
  428. });
  429. },
  430. showQueryMethodsModal() {
  431. this.setData({
  432. showQueryMethods: true
  433. });
  434. },
  435. closeQueryMethods() {
  436. this.setData({
  437. showQueryMethods: false
  438. });
  439. },
  440. // 防止抽屉中非列表区域滚动引起整个页面滚动
  441. preventNonListScroll: function(e) {
  442. // 由于现在使用scroll-view,我们只需阻止非滚动区域的默认滚动
  443. return true;
  444. },
  445. // 防止蒙层触摸事件
  446. preventTouchMove: function(e) {
  447. return false;
  448. }
  449. });