homepage.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. {
  29. icon: '/static_file/zdlb.png',
  30. text: '账单列表',
  31. url: '/pages/zhangdanlist/zhangdanlist'
  32. },
  33. {
  34. icon: '/static_file/jfjl.png',
  35. text: '缴费记录',
  36. url: '/pages/jiaofeiList/jiaofeiList'
  37. },
  38. {
  39. icon: '/static_file/yhbx.png',
  40. text: '用户报修',
  41. url: '/pages/baoxiudj/baoxiudj'
  42. },
  43. {
  44. icon: '/static_file/tzgg.png',
  45. text: '消息通知',
  46. url: '/pages/tongzhiList/tongzhiList'
  47. },
  48. {
  49. icon: '/static_file/ckys.png',
  50. text: '户号绑定',
  51. url: '/pages/huhaoguanli/huhaoguanli'
  52. },
  53. {
  54. icon: '/static_file/yjfk.png',
  55. text: '投诉建议',
  56. url: '/pages/tousujianyi/tousujianyi'
  57. }
  58. ],
  59. currentIndex:0,
  60. noticeList:[],
  61. // 活动信息
  62. activity: {
  63. title: '净水器活动',
  64. subtitle: '预留入口',
  65. imageUrl: '/static_file/background.png'
  66. },
  67. currentAccountInfo: {},
  68. unreadCount: 0
  69. },
  70. onLoad() {
  71. let refresh= app.globalData.refresh;
  72. if(refresh==1){
  73. wx.showLoading({
  74. title: '加载中...',
  75. });
  76. if(!app.globalData.userWxInfo.token){
  77. wx.hideLoading();
  78. return;
  79. }
  80. const _this = this;
  81. wx.request({
  82. url: app.globalData.interfaceUrls.homePageInfo+app.globalData.currentAccountInfo.usernumber,
  83. method: 'GET',
  84. header: {
  85. 'content-type': 'application/json', // 默认值
  86. 'token':app.globalData.userWxInfo.token,
  87. 'source':"wc",
  88. '!SAAS_LOGIN_TOKEN_!':app.globalData.currentAccountInfo.dsKey
  89. },
  90. success (res) {
  91. debugger
  92. wx.hideLoading();
  93. let apiReturnData=res.data;
  94. apiReturnData.data.swNotificationAnnouncementDos.forEach(data=>{
  95. data.publishtime=data.publishtime.slice(0,10);
  96. })
  97. // 所有的通知公告
  98. app.globalData.notices= apiReturnData.data.swNotificationAnnouncementDos;
  99. // 该用户所有的消息通知
  100. app.globalData.userNoticesList = apiReturnData.data.sysNotifyAnnounceUserDos;
  101. // 需要轮播的公告
  102. let data= apiReturnData.data.swNotificationAnnouncementDos.filter(data=>{
  103. return data.homepageslider=='1';
  104. });
  105. if (app.globalData.userNoticesList) {
  106. // 假设 userNoticesList 中有一个 isRead 属性表示消息是否已读
  107. const unreadCount = app.globalData.userNoticesList.filter(notice => notice.readstate === '2').length;
  108. _this.setData({
  109. unreadCount: unreadCount
  110. });
  111. } else {
  112. _this.setData({
  113. unreadCount: 0
  114. });
  115. }
  116. _this.setData({
  117. billInfo: {
  118. totalAmount: apiReturnData.data.duFees ?? 0, // 总计应缴
  119. waterUsage: apiReturnData.data.waterConsumption ?? 0, // 用水量
  120. balance: apiReturnData.data.accountbalance ?? 0 // 账户余额
  121. },
  122. userInfo:{
  123. name: apiReturnData.data.username,
  124. accountNumber: apiReturnData.data.usernumber,
  125. address: apiReturnData.data.address
  126. },
  127. noticeList :data,
  128. isScrolling: true,
  129. })
  130. },
  131. fail(error) {
  132. wx.hideLoading()
  133. utils.simleInfo('登录失败,请稍后再试')
  134. }
  135. })
  136. }
  137. // 加载账户和账单信息
  138. this.loadAccountInfo();
  139. },
  140. onShow() {
  141. // 检查是否需要刷新
  142. const needRefresh = wx.getStorageSync('needRefreshHomepage');
  143. if (needRefresh) {
  144. // 清除刷新标记
  145. wx.removeStorageSync('needRefreshHomepage');
  146. this.loadPageData();
  147. }
  148. // 检查是否从户号管理页面返回
  149. const backFromHuHao = wx.getStorageSync('backFromHuHao');
  150. if (backFromHuHao) {
  151. wx.removeStorageSync('backFromHuHao');
  152. // 从户号管理页面返回时重新加载全部数据
  153. this.loadHomepageData();
  154. } else {
  155. // 即使不是从户号管理页面返回,也更新未读消息数
  156. this.getUnreadNoticeCount();
  157. }
  158. },
  159. loadPageData: function() {
  160. const _this = this;
  161. wx.showLoading({
  162. title: '加载中...',
  163. });
  164. // 检查token是否存在
  165. if(!app.globalData.userWxInfo.token){
  166. wx.hideLoading();
  167. return;
  168. }
  169. wx.request({
  170. url: app.globalData.interfaceUrls.homePageInfo + app.globalData.currentAccountInfo.usernumber,
  171. method: 'GET',
  172. header: {
  173. 'content-type': 'application/json',
  174. 'token': app.globalData.userWxInfo.token,
  175. 'source': "wc",
  176. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  177. },
  178. success(res) {
  179. wx.hideLoading();
  180. let apiReturnData = res.data;
  181. // 该用户所有的消息通知
  182. app.globalData.userNoticesList = apiReturnData.data.sysNotifyAnnounceUserDos;
  183. // 更新页面数据
  184. _this.setData({
  185. billInfo: {
  186. totalAmount: apiReturnData.data.duFees ?? 0, // 总计应缴
  187. waterUsage: apiReturnData.data.waterConsumption ?? 0, // 用水量
  188. balance: apiReturnData.data.accountbalance ?? 0 // 账户余额
  189. },
  190. userInfo: {
  191. name: apiReturnData.data.username,
  192. accountNumber: apiReturnData.data.usernumber,
  193. address: apiReturnData.data.address
  194. },
  195. });
  196. },
  197. fail(error) {
  198. wx.hideLoading();
  199. wx.showToast({
  200. title: '数据加载失败,请稍后再试',
  201. icon: 'none',
  202. duration: 2000
  203. });
  204. }
  205. });
  206. },
  207. // 切换户号
  208. handleSwitchMeter() {
  209. wx.navigateTo({
  210. url: '/pages/huhaoguanli/huhaoguanli'
  211. })
  212. },
  213. // 充值缴费
  214. handlePayment() {
  215. wx.navigateTo({
  216. // url: '/pages/payment/payment'
  217. })
  218. },
  219. // 功能按钮点击事件
  220. handleFunctionClick(e) {
  221. const index = e.currentTarget.dataset.index
  222. const item = this.data.functionList[index]
  223. if (item && item.url) {
  224. wx.navigateTo({
  225. url: item.url,
  226. fail: function(err) {
  227. console.error('页面跳转失败:', err)
  228. wx.showToast({
  229. title: '功能开发中',
  230. icon: 'none',
  231. duration: 2000
  232. })
  233. }
  234. })
  235. } else {
  236. wx.showToast({
  237. title: '功能开发中',
  238. icon: 'none',
  239. duration: 2000
  240. })
  241. }
  242. },
  243. // 查看账单列表
  244. handleViewBill() {
  245. wx.navigateTo({
  246. // url: '/pages/billList/billList'
  247. })
  248. },
  249. // 查看更多活动
  250. handleMoreActivity() {
  251. wx.navigateTo({
  252. // url: '/pages/activityList/activityList'
  253. })
  254. },
  255. // 添加或修改立即缴费按钮点击事件
  256. goToPayment: function(e) {
  257. // 获取当前账单信息
  258. const billInfo = {
  259. amount: this.data.billInfo.totalAmount, // 应缴金额
  260. amountDue:this.data.billInfo.totalAmount, // 总计应缴
  261. balance: this.data.billInfo.balance // 账户余额
  262. };
  263. // 编码账单信息并传递到缴费页面
  264. const billInfoStr = encodeURIComponent(JSON.stringify(billInfo));
  265. wx.navigateTo({
  266. url: `/pages/lijijiaofei/lijijiaofei?billInfo=${billInfoStr}`
  267. });
  268. },
  269. // 跳转到通知公告列表页面
  270. goToNoticeList: function() {
  271. wx.navigateTo({
  272. url: '/pages/tongzhiList/tongzhiList'
  273. })
  274. },
  275. // 跳转到通知详情页面
  276. goToNoticeDetail: function() {
  277. const currentNotice = this.data.noticeList[this.data.currentIndex];
  278. wx.navigateTo({
  279. url: `/pages/tzxq/tzxq?id=${currentNotice.id}`
  280. });
  281. },
  282. onReady: function() {
  283. this.startNoticeScroll();
  284. },
  285. startNoticeScroll: function() {
  286. if(this.data.noticeList.length==0){
  287. return;
  288. }
  289. let data= (this.data.noticeList||[]).filter(data=>{
  290. return data.homepageslider=='1';
  291. });
  292. const content = data[this.data.currentIndex].scrollingcontent;
  293. const title = data[this.data.currentIndex].noticetitle;
  294. const duration = (title.length + content.length) * 0.5;
  295. this.setData({
  296. scrollDuration: duration,
  297. isScrolling: true,
  298. });
  299. },
  300. onScrollComplete: function() {
  301. this.setData({
  302. isScrolling: false
  303. });
  304. // 等待2秒后切换到下一条
  305. setTimeout(() => {
  306. const nextIndex = (this.data.currentIndex + 1) % this.data.noticeList.length;
  307. this.setData({
  308. currentIndex: nextIndex
  309. }, () => {
  310. this.startNoticeScroll();
  311. });
  312. }, 1000);
  313. },
  314. // 加载账户和账单信息
  315. loadAccountInfo: function() {
  316. const accountInfo = app.globalData.currentAccountInfo || {};
  317. // 检查是否是首次加载
  318. const isFirstLoad = wx.getStorageSync('isFirstLoad') !== 'false';
  319. if (isFirstLoad) {
  320. // 首次加载,查找并设置默认账户
  321. this.loadDefaultAccount();
  322. // 设置标记,表示已不是首次加载
  323. wx.setStorageSync('isFirstLoad', 'false');
  324. } else {
  325. this.setData({
  326. currentAccountInfo: accountInfo
  327. });
  328. }
  329. },
  330. // 加载默认账户
  331. loadDefaultAccount: function() {
  332. if (!app.globalData.userWxInfo.token) {
  333. return;
  334. }
  335. const _this = this;
  336. wx.showLoading({
  337. title: '加载中...',
  338. });
  339. wx.request({
  340. url: app.globalData.interfaceUrls.accountList,
  341. method: 'GET',
  342. header: {
  343. 'content-type': 'application/json',
  344. 'token': app.globalData.userWxInfo.token,
  345. 'source': "wc"
  346. },
  347. success(res) {
  348. wx.hideLoading();
  349. if (res.data && res.data.data) {
  350. // 查找 defaultAccount 为 0 的账户
  351. const defaultAccount = res.data.data.find(account => account.defaultAccount === "0");
  352. if (defaultAccount) {
  353. // 设置为当前账户
  354. app.globalData.currentAccountInfo = defaultAccount;
  355. _this.setData({
  356. currentAccountInfo: defaultAccount
  357. });
  358. // 加载该默认账户的详细信息
  359. _this.loadPageData();
  360. } else {
  361. // 没有找到默认账户,使用当前账户
  362. _this.setData({
  363. currentAccountInfo: app.globalData.currentAccountInfo || {}
  364. });
  365. }
  366. }
  367. },
  368. fail(error) {
  369. wx.hideLoading();
  370. wx.showToast({
  371. title: '加载默认账户失败',
  372. icon: 'none',
  373. duration: 2000
  374. });
  375. // 使用当前账户作为备选
  376. _this.setData({
  377. currentAccountInfo: app.globalData.currentAccountInfo || {}
  378. });
  379. }
  380. });
  381. },
  382. onPullDownRefresh: function() {
  383. // 刷新数据
  384. this.refreshData();
  385. },
  386. // 修改刷新数据的函数
  387. refreshData: function() {
  388. console.log('正在刷新数据...');
  389. this.loadPageData();
  390. // 刷新完成后停止下拉刷新动画
  391. wx.stopPullDownRefresh({
  392. success: (res) => {
  393. wx.showToast({
  394. title: '刷新成功',
  395. icon: 'success',
  396. duration: 1000
  397. });
  398. }
  399. });
  400. },
  401. // 获取未读消息数量的方法
  402. getUnreadNoticeCount: function() {
  403. const _this = this;
  404. // 从服务器获取最新的消息通知数据
  405. if (app.globalData.userWxInfo.token && app.globalData.currentAccountInfo) {
  406. wx.request({
  407. url: app.globalData.interfaceUrls.homePageInfo + app.globalData.currentAccountInfo.usernumber,
  408. method: 'GET',
  409. header: {
  410. 'content-type': 'application/json',
  411. 'token': app.globalData.userWxInfo.token,
  412. 'source': "wc",
  413. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  414. },
  415. success(res) {
  416. if (res.data && res.data.data) {
  417. // 更新全局的用户通知列表
  418. app.globalData.userNoticesList = res.data.data.sysNotifyAnnounceUserDos;
  419. // 计算未读消息数量
  420. const unreadCount = app.globalData.userNoticesList.filter(notice => notice.readstate === '2').length;
  421. _this.setData({
  422. unreadCount: unreadCount
  423. });
  424. }
  425. }
  426. });
  427. } else {
  428. this.setData({
  429. unreadCount: 0
  430. });
  431. }
  432. },
  433. // 新增方法:加载主页全部数据(包括消息通知)
  434. loadHomepageData: function() {
  435. const _this = this;
  436. wx.showLoading({
  437. title: '加载中...',
  438. });
  439. if(!app.globalData.userWxInfo.token){
  440. wx.hideLoading();
  441. return;
  442. }
  443. wx.request({
  444. url: app.globalData.interfaceUrls.homePageInfo + app.globalData.currentAccountInfo.usernumber,
  445. method: 'GET',
  446. header: {
  447. 'content-type': 'application/json',
  448. 'token': app.globalData.userWxInfo.token,
  449. 'source': "wc",
  450. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  451. },
  452. success(res) {
  453. wx.hideLoading();
  454. if (res.data && res.data.data) {
  455. let apiReturnData = res.data;
  456. // 更新消息通知数据
  457. apiReturnData.data.swNotificationAnnouncementDos.forEach(data => {
  458. data.publishtime = data.publishtime.slice(0,10);
  459. });
  460. // 更新全局数据
  461. app.globalData.notices = apiReturnData.data.swNotificationAnnouncementDos;
  462. app.globalData.userNoticesList = apiReturnData.data.sysNotifyAnnounceUserDos;
  463. // 需要轮播的公告
  464. let data = apiReturnData.data.swNotificationAnnouncementDos.filter(data => {
  465. return data.homepageslider == '1';
  466. });
  467. // 更新未读消息数
  468. _this.getUnreadNoticeCount();
  469. // 更新页面数据
  470. _this.setData({
  471. billInfo: {
  472. totalAmount: apiReturnData.data.duFees ?? 0,
  473. waterUsage: apiReturnData.data.waterConsumption ?? 0,
  474. balance: apiReturnData.data.accountbalance ?? 0
  475. },
  476. userInfo: {
  477. name: apiReturnData.data.username,
  478. accountNumber: apiReturnData.data.usernumber,
  479. address: apiReturnData.data.address
  480. },
  481. noticeList: data,
  482. isScrolling: true
  483. });
  484. // 如果有轮播公告,重新开始轮播
  485. if(data && data.length > 0) {
  486. _this.startNoticeScroll();
  487. }
  488. }
  489. },
  490. fail(error) {
  491. wx.hideLoading();
  492. wx.showToast({
  493. title: '数据加载失败',
  494. icon: 'none',
  495. duration: 2000
  496. });
  497. }
  498. });
  499. }
  500. })