huhaobangding.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. this.setData({
  85. showWaterCompanyPicker: true,
  86. waterCompanyIndex: this.data.waterCompanyIndex
  87. });
  88. },
  89. // 隐藏水站公司选择器
  90. hideWaterCompanyPicker: function() {
  91. this.setData({
  92. showWaterCompanyPicker: false
  93. });
  94. },
  95. // 水站公司选择变化
  96. onWaterCompanyChange: function(e) {
  97. this.setData({
  98. waterCompanyIndex: e.detail.value
  99. });
  100. },
  101. // 确认水站公司选择
  102. confirmWaterCompany: function(e) {
  103. const index = this.data.waterCompanyIndex[0];
  104. this.setData({
  105. waterCompanyIndex: this.data.waterCompanyIndex,
  106. waterCompany: this.data.waterCompanyList[index].name,
  107. showWaterCompanyPicker: false,
  108. phoneQueryList: this.data.waterCompanyList[index].businessPoint.map((data,index)=>{
  109. return {
  110. id: index+1,
  111. name: index+1+'、'+data.address,
  112. phone: data.contactphone,
  113. time: data.worktime
  114. }
  115. }),
  116. ylt:app.globalData.imgPreviewUrl+"pagePart/"+this.data.waterCompanyList[index].swCompanyInfo.overduenoticeimage.split("-;-")[0]
  117. });
  118. },
  119. // 户号输入
  120. onHuHaoInput: function(e) {
  121. // 使用正则表达式去除非数字字符
  122. const value = e.detail.value.replace(/[^\d]/g, '');
  123. // 更新输入框的值
  124. this.setData({
  125. huHao: value
  126. });
  127. },
  128. // 户名输入
  129. onHuMingInput: function(e) {
  130. // 使用正则表达式去除所有空格
  131. const value = e.detail.value.replace(/\s/g, '');
  132. this.setData({
  133. huMing: value
  134. });
  135. },
  136. // 确认绑定
  137. confirmBinding: function() {
  138. // 如果已经在处理绑定请求,则直接返回
  139. if (this.data.isBinding) {
  140. return;
  141. }
  142. // 设置绑定状态为true,防止重复点击
  143. this.setData({
  144. isBinding: true
  145. });
  146. // 验证必填字段
  147. if (!this.data.waterCompany) {
  148. wx.showToast({
  149. title: '请选择水站公司',
  150. icon: 'none'
  151. });
  152. this.setData({ isBinding: false }); // 重置状态
  153. return;
  154. }
  155. if (!this.data.huHao) {
  156. wx.showToast({
  157. title: '请输入户号',
  158. icon: 'none'
  159. });
  160. this.setData({ isBinding: false }); // 重置状态
  161. return;
  162. }
  163. if (!this.data.huMing) {
  164. wx.showToast({
  165. title: '请输入户名',
  166. icon: 'none'
  167. });
  168. this.setData({ isBinding: false }); // 重置状态
  169. return;
  170. }
  171. // 获取最终的标签值
  172. let finalTag = this.data.selectedTag;
  173. // debugger
  174. if (this.data.selectedTag === '自定义') {
  175. finalTag = this.data.customTag || ''; // 使用用户输入的自定义标签
  176. }
  177. // 如果选择了自定义但没有输入内容,提示用户
  178. if (this.data.selectedTag === '自定义' && !this.data.customTag) {
  179. wx.showToast({
  180. title: '请输入自定义标签名称',
  181. icon: 'none'
  182. });
  183. this.setData({ isBinding: false }); // 重置状态
  184. return;
  185. }
  186. // 构建保存的数据对象
  187. const bindingData = {
  188. bindWaterCompany: this.data.waterCompanyList[this.data.waterCompanyIndex[0]].id,
  189. accountNum: this.data.huHao,
  190. accountName: this.data.huMing,
  191. groupType: finalTag, // 使用处理后的标签值
  192. openId: app.globalData.userWxInfo.openid,
  193. otherDsKey: (app.globalData.bindAccountInfo||[]).map(data=>data.dsKey)
  194. };
  195. // debugger
  196. // 提交数据
  197. wx.showLoading({
  198. title: '绑定中...',
  199. });
  200. const that = this; // 保存this引用
  201. // 调用绑定接口
  202. wx.request({
  203. url: app.globalData.interfaceUrls.accountBind,
  204. method: 'POST',
  205. data: bindingData,
  206. header: {
  207. 'content-type': 'application/json', // 默认值
  208. 'token':app.globalData.userWxInfo.token,
  209. 'source':"wc",
  210. '!SAAS_LOGIN_TOKEN_!':this.data.waterCompanyList[this.data.waterCompanyIndex[0]].id
  211. },
  212. success (res) {
  213. wx.hideLoading();
  214. let apiReturnData=res.data
  215. if(apiReturnData.code=='200'){
  216. wx.hideLoading();
  217. utils.simleInfo('绑定成功')
  218. app.globalData.currentAccountInfo=apiReturnData.data[0];
  219. setTimeout(function(){
  220. app.globalData.refresh=1;
  221. wx.switchTab({
  222. url: '/pages/homepage/homepage',
  223. });
  224. },2000)
  225. }else{
  226. wx.showToast({
  227. title: apiReturnData.msg,
  228. icon: 'error',
  229. duration: 2000
  230. })
  231. wx.hideLoading();
  232. that.setData({ isBinding: false }); // 重置绑定状态
  233. }
  234. },
  235. fail(error) {
  236. wx.hideLoading()
  237. utils.simleInfo('登录失败,请稍后再试')
  238. that.setData({ isBinding: false }); // 重置绑定状态
  239. }
  240. })
  241. },
  242. onLoad: function() {
  243. // 延迟设置导航栏,确保页面已完全加载
  244. setTimeout(() => {
  245. wx.setNavigationBarTitle({
  246. title: '户号绑定'
  247. });
  248. wx.setNavigationBarColor({
  249. frontColor: '#ffffff',
  250. backgroundColor: '#0066FF'
  251. });
  252. }, 100);
  253. if(app.globalData.launchPara){
  254. this.setData({
  255. waterCompanyIndex:[app.globalData.waterCompanys.findIndex(data=>data.id==app.globalData.launchPara)],
  256. waterCompany: app.globalData.waterCompanys.filter(data=>data.id==app.globalData.launchPara)[0].name,
  257. isChange:false,
  258. phoneQueryList: app.globalData.waterCompanys.filter(data=>data.id==app.globalData.launchPara)[0].businessPoint.map((data,index)=>{
  259. return {
  260. id: index+1,
  261. name: data.address,
  262. phone: data.contactphone,
  263. time: data.worktime
  264. }
  265. }),
  266. ylt:app.globalData.imgPreviewUrl+
  267. app.globalData.currentAccountInfo.dsId+"?fileId="
  268. +app.globalData.waterCompanys.filter(data=>data.id==app.globalData.launchPara)[0].swCompanyInfo.overduenoticeimage.split("-;-")[1]
  269. });
  270. }
  271. },
  272. /**
  273. * 返回上一页
  274. */
  275. goBack: function () {
  276. wx.navigateBack();
  277. },
  278. onTagInput: function(e) {
  279. let value = e.detail.value;
  280. // 如果超过4个字符,截取前4个字符
  281. if (value.length > 4) {
  282. value = value.slice(0, 4);
  283. }
  284. this.setData({
  285. tagLength: value.length,
  286. customTag: value
  287. });
  288. },
  289. showQueryMethodsModal() {
  290. this.setData({
  291. showQueryMethods: true
  292. });
  293. },
  294. closeQueryMethods() {
  295. this.setData({
  296. showQueryMethods: false
  297. });
  298. }
  299. });