homepage.js 26 KB

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