homepage.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. const app = getApp();
  2. Page({
  3. data: {
  4. images: {
  5. logo: '/static_file/logo.png',
  6. background: '/static_file/background.png',
  7. card: '/static_file/card.png',
  8. tzd: '/static_file/backgrountzdd.png',
  9. phone: '/static_file/phone.png',
  10. yl: '/static_file/background.yl',
  11. jcsfcjtzd: '/static_file/jcsfcjtzd.png',
  12. kapiantubiao: '/static_file/kapiantubiao.png'
  13. },
  14. // 用户信息
  15. userInfo: {
  16. name: "",
  17. accountNumber: "",
  18. address: ""
  19. },
  20. // 账单信息
  21. billInfo: {
  22. totalAmount: "", // 总计应缴
  23. waterUsage: "", // 用水量
  24. balance: "" // 账户余额
  25. },
  26. // 功能按钮配置
  27. functionList: [{
  28. icon: '/static_file/zdlb.png',
  29. text: '账单列表',
  30. url: '/pages/zhangdanlist/zhangdanlist'
  31. },
  32. {
  33. icon: '/static_file/jfjl.png',
  34. text: '缴费记录',
  35. url: '/pages/jiaofeiList/jiaofeiList'
  36. },
  37. {
  38. icon: '/static_file/yhbx.png',
  39. text: '用户报修',
  40. url: '/pages/baoxiudj/baoxiudj'
  41. },
  42. {
  43. icon: '/static_file/tzgg.png',
  44. text: '消息通知',
  45. url: '/pages/tongzhiList/tongzhiList'
  46. },
  47. {
  48. icon: '/static_file/ckys.png',
  49. text: '户号绑定',
  50. url: '/pages/huhaoguanli/huhaoguanli'
  51. },
  52. {
  53. icon: '/static_file/yjfk.png',
  54. text: '投诉建议',
  55. url: '/pages/tousujianyi/tousujianyi'
  56. }
  57. ],
  58. currentIndex: 0,
  59. noticeList: [],
  60. // 活动信息
  61. activity: {
  62. title: '净水器活动',
  63. subtitle: '预留入口',
  64. imageUrl: '/static_file/background.png'
  65. },
  66. currentAccountInfo: {},
  67. unreadCount: 0
  68. },
  69. onLoad() {
  70. // 检查是否刚刚绑定了新户号
  71. const justBoundNewAccount = wx.getStorageSync('justBoundNewAccount');
  72. if (justBoundNewAccount) {
  73. // 清除标记
  74. wx.removeStorageSync('justBoundNewAccount');
  75. // 直接加载页面数据
  76. this.loadPageData();
  77. return;
  78. }
  79. // 检查是否所有户号已解绑
  80. const allAccountsUnbound = wx.getStorageSync('allAccountsUnbound');
  81. if (allAccountsUnbound) {
  82. // 清除标记
  83. wx.removeStorageSync('allAccountsUnbound');
  84. // 跳转到首次绑定页面
  85. wx.redirectTo({
  86. url: '/pages/FirstBangDing/FirstBangDing',
  87. });
  88. return;
  89. }
  90. // 检查全局用户信息是否存在
  91. if (!app.globalData.currentAccountInfo ||
  92. !app.globalData.currentAccountInfo.usernumber ||
  93. app.globalData.userWxInfo.currentDsKey === "") {
  94. wx.redirectTo({
  95. url: '/pages/FirstBangDing/FirstBangDing',
  96. });
  97. return;
  98. }
  99. let refresh = app.globalData.refresh;
  100. if (refresh == 1) {
  101. wx.showLoading({
  102. title: '加载中...',
  103. });
  104. if (!app.globalData.userWxInfo.token) {
  105. wx.hideLoading();
  106. return;
  107. }
  108. const _this = this;
  109. wx.request({
  110. url: app.globalData.interfaceUrls.homePageInfo + app.globalData.currentAccountInfo.usernumber,
  111. method: 'GET',
  112. header: {
  113. 'content-type': 'application/json', // 默认值
  114. 'token': app.globalData.userWxInfo.token,
  115. 'source': "wc",
  116. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  117. },
  118. success(res) {
  119. wx.hideLoading();
  120. let apiReturnData = res.data;
  121. apiReturnData.data.swNotificationAnnouncementDos.forEach(data => {
  122. data.publishtime = data.publishtime.slice(0, 10);
  123. })
  124. // 所有的通知公告
  125. app.globalData.notices = apiReturnData.data.swNotificationAnnouncementDos;
  126. // 该用户所有的消息通知
  127. app.globalData.userNoticesList = apiReturnData.data.sysNotifyAnnounceUserDos;
  128. // 需要轮播的公告
  129. let data = apiReturnData.data.swNotificationAnnouncementDos.filter(data => {
  130. return data.homepageslider == '1';
  131. });
  132. if (app.globalData.userNoticesList) {
  133. // 假设 userNoticesList 中有一个 isRead 属性表示消息是否已读
  134. const unreadCount = app.globalData.userNoticesList.filter(notice => notice.readstate === '2').length;
  135. _this.setData({
  136. unreadCount: unreadCount
  137. });
  138. } else {
  139. _this.setData({
  140. unreadCount: 0
  141. });
  142. }
  143. _this.setData({
  144. billInfo: {
  145. totalAmount: apiReturnData.data.duFees ?? 0, // 总计应缴
  146. waterUsage: apiReturnData.data.waterConsumption ?? 0, // 用水量
  147. balance: apiReturnData.data.accountbalance ?? 0 // 账户余额
  148. },
  149. userInfo: {
  150. name: apiReturnData.data.username,
  151. accountNumber: apiReturnData.data.usernumber,
  152. address: apiReturnData.data.address
  153. },
  154. noticeList: data,
  155. isScrolling: true,
  156. })
  157. },
  158. fail(error) {
  159. wx.hideLoading()
  160. utils.simleInfo('登录失败,请稍后再试')
  161. }
  162. })
  163. }
  164. // 加载账户和账单信息
  165. this.loadAccountInfo();
  166. },
  167. onShow() {
  168. // 检查是否刚刚绑定了新户号
  169. const justBoundNewAccount = wx.getStorageSync('justBoundNewAccount');
  170. if (justBoundNewAccount) {
  171. // 清除标记
  172. wx.removeStorageSync('justBoundNewAccount');
  173. // 直接加载页面数据
  174. this.loadPageData();
  175. return;
  176. }
  177. // 首先检查全局用户信息是否存在,如果没有则跳转到首次绑定页面
  178. if (!app.globalData.currentAccountInfo ||
  179. !app.globalData.currentAccountInfo.usernumber ||
  180. app.globalData.userWxInfo.currentDsKey === "") {
  181. wx.redirectTo({
  182. url: '/pages/FirstBangDing/FirstBangDing',
  183. });
  184. return;
  185. }
  186. // 检查是否需要刷新
  187. const needRefresh = wx.getStorageSync('needRefreshHomepage');
  188. if (needRefresh) {
  189. // 清除刷新标记
  190. wx.removeStorageSync('needRefreshHomepage');
  191. this.loadPageData();
  192. }
  193. // 检查是否从户号管理页面返回
  194. const backFromHuHao = wx.getStorageSync('backFromHuHao');
  195. if (backFromHuHao) {
  196. wx.removeStorageSync('backFromHuHao');
  197. // 从户号管理页面返回时重新加载全部数据
  198. this.loadHomepageData();
  199. } else {
  200. // 即使不是从户号管理页面返回,也更新未读消息数
  201. this.getUnreadNoticeCount();
  202. }
  203. },
  204. loadPageData: function () {
  205. const _this = this;
  206. wx.showLoading({
  207. title: '加载中...',
  208. });
  209. // 检查token和账户信息是否存在
  210. if (!app.globalData.userWxInfo.token ||
  211. !app.globalData.currentAccountInfo ||
  212. !app.globalData.currentAccountInfo.usernumber ||
  213. !app.globalData.userWxInfo.currentDsKey) {
  214. wx.hideLoading();
  215. console.error('缺少必要的账户信息');
  216. // 尝试从本地存储恢复账户信息
  217. const storedHuHao = wx.getStorageSync('currentHuHao');
  218. if (storedHuHao && storedHuHao.usernumber) {
  219. app.globalData.currentAccountInfo = storedHuHao;
  220. app.globalData.userWxInfo.currentDsKey = storedHuHao.dsKey;
  221. // 重新尝试加载
  222. setTimeout(() => {
  223. this.loadPageData();
  224. }, 100);
  225. return;
  226. }
  227. // 如果无法恢复,则跳转到首次绑定页面
  228. wx.redirectTo({
  229. url: '/pages/FirstBangDing/FirstBangDing',
  230. });
  231. return;
  232. }
  233. wx.request({
  234. url: app.globalData.interfaceUrls.homePageInfo + app.globalData.currentAccountInfo.usernumber,
  235. method: 'GET',
  236. header: {
  237. 'content-type': 'application/json',
  238. 'token': app.globalData.userWxInfo.token,
  239. 'source': "wc",
  240. '!SAAS_LOGIN_TOKEN_!': app.globalData.userWxInfo.currentDsKey
  241. },
  242. success(res) {
  243. wx.hideLoading();
  244. let apiReturnData = res.data;
  245. // 该用户所有的消息通知
  246. app.globalData.userNoticesList = apiReturnData.data.sysNotifyAnnounceUserDos;
  247. // 更新页面数据
  248. _this.setData({
  249. billInfo: {
  250. totalAmount: apiReturnData.data.duFees ?? 0, // 总计应缴
  251. waterUsage: apiReturnData.data.waterConsumption ?? 0, // 用水量
  252. balance: apiReturnData.data.accountbalance ?? 0 // 账户余额
  253. },
  254. userInfo: {
  255. name: apiReturnData.data.username,
  256. accountNumber: apiReturnData.data.usernumber,
  257. address: apiReturnData.data.address
  258. },
  259. });
  260. },
  261. fail(error) {
  262. wx.hideLoading();
  263. wx.showToast({
  264. title: '数据加载失败,请稍后再试',
  265. icon: 'none',
  266. duration: 2000
  267. });
  268. }
  269. });
  270. },
  271. // 切换户号
  272. handleSwitchMeter() {
  273. wx.navigateTo({
  274. url: '/pages/huhaoguanli/huhaoguanli'
  275. })
  276. },
  277. // 充值缴费
  278. handlePayment() {
  279. wx.navigateTo({
  280. // url: '/pages/payment/payment'
  281. })
  282. },
  283. // 功能按钮点击事件
  284. handleFunctionClick(e) {
  285. const index = e.currentTarget.dataset.index
  286. const item = this.data.functionList[index]
  287. if (item && item.url) {
  288. wx.navigateTo({
  289. url: item.url,
  290. fail: function (err) {
  291. console.error('页面跳转失败:', err)
  292. wx.showToast({
  293. title: '功能开发中',
  294. icon: 'none',
  295. duration: 2000
  296. })
  297. }
  298. })
  299. } else {
  300. wx.showToast({
  301. title: '功能开发中',
  302. icon: 'none',
  303. duration: 2000
  304. })
  305. }
  306. },
  307. // 查看账单列表
  308. handleViewBill() {
  309. wx.navigateTo({
  310. // url: '/pages/billList/billList'
  311. })
  312. },
  313. // 查看更多活动
  314. handleMoreActivity() {
  315. wx.navigateTo({
  316. // url: '/pages/activityList/activityList'
  317. })
  318. },
  319. // 添加或修改立即缴费按钮点击事件
  320. goToPayment: function (e) {
  321. wx.navigateTo({
  322. url: `/pages/lijijiaofei/lijijiaofei`
  323. });
  324. },
  325. // 跳转到通知公告列表页面
  326. goToNoticeList: function () {
  327. wx.navigateTo({
  328. url: '/pages/tongzhiList/tongzhiList'
  329. })
  330. },
  331. // 跳转到通知详情页面
  332. goToNoticeDetail: function () {
  333. const currentNotice = this.data.noticeList[this.data.currentIndex];
  334. wx.navigateTo({
  335. url: `/pages/tzxq/tzxq?id=${currentNotice.id}`
  336. });
  337. },
  338. onReady: function () {
  339. this.startNoticeScroll();
  340. },
  341. startNoticeScroll: function () {
  342. if (this.data.noticeList.length == 0) {
  343. return;
  344. }
  345. let data = (this.data.noticeList || []).filter(data => {
  346. return data.homepageslider == '1';
  347. });
  348. const content = data[this.data.currentIndex].scrollingcontent;
  349. const title = data[this.data.currentIndex].noticetitle;
  350. const duration = (title.length + content.length) * 0.5;
  351. this.setData({
  352. scrollDuration: duration,
  353. isScrolling: true,
  354. });
  355. },
  356. onScrollComplete: function () {
  357. this.setData({
  358. isScrolling: false
  359. });
  360. // 等待2秒后切换到下一条
  361. setTimeout(() => {
  362. const nextIndex = (this.data.currentIndex + 1) % this.data.noticeList.length;
  363. this.setData({
  364. currentIndex: nextIndex
  365. }, () => {
  366. this.startNoticeScroll();
  367. });
  368. }, 1000);
  369. },
  370. // 加载账户和账单信息
  371. loadAccountInfo: function () {
  372. const accountInfo = app.globalData.currentAccountInfo || {};
  373. // 检查账户信息是否为空(解绑后返回的情况)
  374. if (!accountInfo || !accountInfo.usernumber) {
  375. // 检查是否已全部解绑
  376. const allAccountsUnbound = wx.getStorageSync('allAccountsUnbound');
  377. if (allAccountsUnbound) {
  378. // 清除标记
  379. wx.removeStorageSync('allAccountsUnbound');
  380. // 跳转到首次绑定页面
  381. wx.redirectTo({
  382. url: '/pages/FirstBangDing/FirstBangDing',
  383. });
  384. return;
  385. }
  386. // 尝试请求账户列表,查看是否有其他账户
  387. this.checkAndFetchAccounts();
  388. return;
  389. }
  390. // 检查是否是首次加载
  391. const isFirstLoad = wx.getStorageSync('isFirstLoad') !== 'false';
  392. // 检查是否在户号管理页面切换过账户
  393. const hasUserSwitchedAccount = wx.getStorageSync('hasUserSwitchedAccount') === true;
  394. if (isFirstLoad && !hasUserSwitchedAccount) {
  395. // 首次加载且未切换过账户,查找并设置默认账户
  396. this.loadDefaultAccount();
  397. // 设置标记,表示已不是首次加载
  398. wx.setStorageSync('isFirstLoad', 'false');
  399. } else {
  400. if (!isFirstLoad && hasUserSwitchedAccount) {
  401. // 未切换过账户,查找并设置默认账户
  402. this.loadDefaultAccount();
  403. } else {
  404. // 非首次加载或已在户号管理中切换过账户,直接使用当前账户
  405. this.setData({
  406. currentAccountInfo: accountInfo
  407. });
  408. // 如果是从户号管理页面切换了账户,需要清除该标记
  409. if (hasUserSwitchedAccount) {
  410. wx.removeStorageSync('hasUserSwitchedAccount');
  411. }
  412. }
  413. }
  414. },
  415. // 检查并获取账户列表
  416. checkAndFetchAccounts: function() {
  417. if (!app.globalData.userWxInfo.token) {
  418. // 没有token,直接跳转到首次绑定页面
  419. wx.redirectTo({
  420. url: '/pages/FirstBangDing/FirstBangDing',
  421. });
  422. return;
  423. }
  424. const _this = this;
  425. wx.showLoading({
  426. title: '加载中...',
  427. });
  428. wx.request({
  429. url: app.globalData.interfaceUrls.accountList,
  430. method: 'GET',
  431. header: {
  432. 'content-type': 'application/json',
  433. 'token': app.globalData.userWxInfo.token,
  434. 'source': "wc"
  435. },
  436. success(res) {
  437. wx.hideLoading();
  438. if (res.data && res.data.data && res.data.data.length > 0) {
  439. // 找到了账户,设置默认账户
  440. const defaultAccount = res.data.data.find(account => account.defaultAccount === "0") || res.data.data[0];
  441. // 更新全局账户信息
  442. app.globalData.currentAccountInfo = defaultAccount;
  443. app.globalData.userWxInfo.currentDsKey = defaultAccount.dsKey;
  444. app.globalData.userWxInfo.username = defaultAccount.username;
  445. app.globalData.userWxInfo.usernumber = defaultAccount.usernumber;
  446. app.globalData.userWxInfo.address = defaultAccount.address;
  447. app.globalData.userWxInfo.groupName = defaultAccount.groupName;
  448. // 保存到本地存储
  449. wx.setStorageSync('currentHuHao', defaultAccount);
  450. // 更新页面数据并加载详细信息
  451. _this.setData({
  452. currentAccountInfo: defaultAccount
  453. });
  454. _this.loadPageData();
  455. } else {
  456. // 没有找到任何账户,跳转到首次绑定页面
  457. wx.redirectTo({
  458. url: '/pages/FirstBangDing/FirstBangDing',
  459. });
  460. }
  461. },
  462. fail(error) {
  463. wx.hideLoading();
  464. // 请求失败,跳转到首次绑定页面
  465. wx.redirectTo({
  466. url: '/pages/FirstBangDing/FirstBangDing',
  467. });
  468. }
  469. });
  470. },
  471. // 加载默认账户
  472. loadDefaultAccount: function () {
  473. if (!app.globalData.userWxInfo.token) {
  474. return;
  475. }
  476. const _this = this;
  477. wx.showLoading({
  478. title: '加载中...',
  479. });
  480. wx.request({
  481. url: app.globalData.interfaceUrls.accountList,
  482. method: 'GET',
  483. header: {
  484. 'content-type': 'application/json',
  485. 'token': app.globalData.userWxInfo.token,
  486. 'source': "wc"
  487. },
  488. success(res) {
  489. wx.hideLoading();
  490. if (res.data && res.data.data) {
  491. // 查找 defaultAccount 为 0 的账户
  492. const defaultAccount = res.data.data.find(account => account.defaultAccount === "0");
  493. debugger
  494. if (defaultAccount) {
  495. // 设置为当前账户
  496. app.globalData.currentAccountInfo = defaultAccount;
  497. _this.setData({
  498. currentAccountInfo: defaultAccount
  499. });
  500. // 加载该默认账户的详细信息
  501. _this.loadPageData();
  502. } else {
  503. // 没有找到默认账户,使用当前账户
  504. _this.setData({
  505. currentAccountInfo: app.globalData.currentAccountInfo || {}
  506. });
  507. }
  508. }
  509. },
  510. fail(error) {
  511. wx.hideLoading();
  512. wx.showToast({
  513. title: '加载默认账户失败',
  514. icon: 'none',
  515. duration: 2000
  516. });
  517. // 使用当前账户作为备选
  518. _this.setData({
  519. currentAccountInfo: app.globalData.currentAccountInfo || {}
  520. });
  521. }
  522. });
  523. },
  524. onPullDownRefresh: function () {
  525. // 刷新数据
  526. this.refreshData();
  527. },
  528. // 修改刷新数据的函数
  529. refreshData: function () {
  530. console.log('正在刷新数据...');
  531. this.loadPageData();
  532. // 刷新完成后停止下拉刷新动画
  533. wx.stopPullDownRefresh({
  534. success: (res) => {
  535. wx.showToast({
  536. title: '刷新成功',
  537. icon: 'success',
  538. duration: 1000
  539. });
  540. }
  541. });
  542. },
  543. // 获取未读消息数量的方法
  544. getUnreadNoticeCount: function () {
  545. const _this = this;
  546. // 从服务器获取最新的消息通知数据
  547. if (app.globalData.userWxInfo.token && app.globalData.currentAccountInfo) {
  548. wx.request({
  549. url: app.globalData.interfaceUrls.homePageInfo + app.globalData.currentAccountInfo.usernumber,
  550. method: 'GET',
  551. header: {
  552. 'content-type': 'application/json',
  553. 'token': app.globalData.userWxInfo.token,
  554. 'source': "wc",
  555. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  556. },
  557. success(res) {
  558. if (res.data && res.data.data) {
  559. // 更新全局的用户通知列表
  560. app.globalData.userNoticesList = res.data.data.sysNotifyAnnounceUserDos;
  561. // 计算未读消息数量
  562. const unreadCount = app.globalData.userNoticesList.filter(notice => notice.readstate === '2').length;
  563. _this.setData({
  564. unreadCount: unreadCount
  565. });
  566. }
  567. }
  568. });
  569. } else {
  570. this.setData({
  571. unreadCount: 0
  572. });
  573. }
  574. },
  575. // 新增方法:加载主页全部数据(包括消息通知)
  576. loadHomepageData: function () {
  577. const _this = this;
  578. wx.showLoading({
  579. title: '加载中...',
  580. });
  581. if (!app.globalData.userWxInfo.token) {
  582. wx.hideLoading();
  583. return;
  584. }
  585. wx.request({
  586. url: app.globalData.interfaceUrls.homePageInfo + app.globalData.currentAccountInfo.usernumber,
  587. method: 'GET',
  588. header: {
  589. 'content-type': 'application/json',
  590. 'token': app.globalData.userWxInfo.token,
  591. 'source': "wc",
  592. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  593. },
  594. success(res) {
  595. wx.hideLoading();
  596. if (res.data && res.data.data) {
  597. let apiReturnData = res.data;
  598. // 更新消息通知数据
  599. apiReturnData.data.swNotificationAnnouncementDos.forEach(data => {
  600. data.publishtime = data.publishtime.slice(0, 10);
  601. });
  602. // 更新全局数据
  603. app.globalData.notices = apiReturnData.data.swNotificationAnnouncementDos;
  604. app.globalData.userNoticesList = apiReturnData.data.sysNotifyAnnounceUserDos;
  605. // 需要轮播的公告
  606. let data = apiReturnData.data.swNotificationAnnouncementDos.filter(data => {
  607. return data.homepageslider == '1';
  608. });
  609. // 更新未读消息数
  610. _this.getUnreadNoticeCount();
  611. // 更新页面数据
  612. _this.setData({
  613. billInfo: {
  614. totalAmount: apiReturnData.data.duFees ?? 0,
  615. waterUsage: apiReturnData.data.waterConsumption ?? 0,
  616. balance: apiReturnData.data.accountbalance ?? 0
  617. },
  618. userInfo: {
  619. name: apiReturnData.data.username,
  620. accountNumber: apiReturnData.data.usernumber,
  621. address: apiReturnData.data.address
  622. },
  623. noticeList: data,
  624. isScrolling: true
  625. });
  626. // 如果有轮播公告,重新开始轮播
  627. if (data && data.length > 0) {
  628. _this.startNoticeScroll();
  629. }
  630. }
  631. },
  632. fail(error) {
  633. wx.hideLoading();
  634. wx.showToast({
  635. title: '数据加载失败',
  636. icon: 'none',
  637. duration: 2000
  638. });
  639. }
  640. });
  641. }
  642. })