homepage.js 26 KB

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