huhaobangding.js 13 KB

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