homepage.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. debugger
  259. // const billInfo = {
  260. // amount: this.data.billInfo.totalAmount, // 应缴金额
  261. // amountDue:this.data.billInfo.totalAmount, // 总计应缴
  262. // balance: this.data.billInfo.balance // 账户余额
  263. // };
  264. // 编码账单信息并传递到缴费页面
  265. // const billInfoStr = encodeURIComponent(JSON.stringify(billInfo));
  266. wx.navigateTo({
  267. url: `/pages/lijijiaofei/lijijiaofei`
  268. // url: `/pages/lijijiaofei/lijijiaofei?billInfo=${billInfoStr}`
  269. });
  270. },
  271. // 跳转到通知公告列表页面
  272. goToNoticeList: function() {
  273. wx.navigateTo({
  274. url: '/pages/tongzhiList/tongzhiList'
  275. })
  276. },
  277. // 跳转到通知详情页面
  278. goToNoticeDetail: function() {
  279. const currentNotice = this.data.noticeList[this.data.currentIndex];
  280. wx.navigateTo({
  281. url: `/pages/tzxq/tzxq?id=${currentNotice.id}`
  282. });
  283. },
  284. onReady: function() {
  285. this.startNoticeScroll();
  286. },
  287. startNoticeScroll: function() {
  288. if(this.data.noticeList.length==0){
  289. return;
  290. }
  291. let data= (this.data.noticeList||[]).filter(data=>{
  292. return data.homepageslider=='1';
  293. });
  294. const content = data[this.data.currentIndex].scrollingcontent;
  295. const title = data[this.data.currentIndex].noticetitle;
  296. const duration = (title.length + content.length) * 0.5;
  297. this.setData({
  298. scrollDuration: duration,
  299. isScrolling: true,
  300. });
  301. },
  302. onScrollComplete: function() {
  303. this.setData({
  304. isScrolling: false
  305. });
  306. // 等待2秒后切换到下一条
  307. setTimeout(() => {
  308. const nextIndex = (this.data.currentIndex + 1) % this.data.noticeList.length;
  309. this.setData({
  310. currentIndex: nextIndex
  311. }, () => {
  312. this.startNoticeScroll();
  313. });
  314. }, 1000);
  315. },
  316. // 加载账户和账单信息
  317. loadAccountInfo: function() {
  318. const accountInfo = app.globalData.currentAccountInfo || {};
  319. // 检查是否是首次加载
  320. const isFirstLoad = wx.getStorageSync('isFirstLoad') !== 'false';
  321. if (isFirstLoad) {
  322. // 首次加载,查找并设置默认账户
  323. this.loadDefaultAccount();
  324. // 设置标记,表示已不是首次加载
  325. wx.setStorageSync('isFirstLoad', 'false');
  326. } else {
  327. this.setData({
  328. currentAccountInfo: accountInfo
  329. });
  330. }
  331. },
  332. // 加载默认账户
  333. loadDefaultAccount: function() {
  334. if (!app.globalData.userWxInfo.token) {
  335. return;
  336. }
  337. const _this = this;
  338. wx.showLoading({
  339. title: '加载中...',
  340. });
  341. wx.request({
  342. url: app.globalData.interfaceUrls.accountList,
  343. method: 'GET',
  344. header: {
  345. 'content-type': 'application/json',
  346. 'token': app.globalData.userWxInfo.token,
  347. 'source': "wc"
  348. },
  349. success(res) {
  350. wx.hideLoading();
  351. if (res.data && res.data.data) {
  352. // 查找 defaultAccount 为 0 的账户
  353. const defaultAccount = res.data.data.find(account => account.defaultAccount === "0");
  354. if (defaultAccount) {
  355. // 设置为当前账户
  356. app.globalData.currentAccountInfo = defaultAccount;
  357. _this.setData({
  358. currentAccountInfo: defaultAccount
  359. });
  360. // 加载该默认账户的详细信息
  361. _this.loadPageData();
  362. } else {
  363. // 没有找到默认账户,使用当前账户
  364. _this.setData({
  365. currentAccountInfo: app.globalData.currentAccountInfo || {}
  366. });
  367. }
  368. }
  369. },
  370. fail(error) {
  371. wx.hideLoading();
  372. wx.showToast({
  373. title: '加载默认账户失败',
  374. icon: 'none',
  375. duration: 2000
  376. });
  377. // 使用当前账户作为备选
  378. _this.setData({
  379. currentAccountInfo: app.globalData.currentAccountInfo || {}
  380. });
  381. }
  382. });
  383. },
  384. onPullDownRefresh: function() {
  385. // 刷新数据
  386. this.refreshData();
  387. },
  388. // 修改刷新数据的函数
  389. refreshData: function() {
  390. console.log('正在刷新数据...');
  391. this.loadPageData();
  392. // 刷新完成后停止下拉刷新动画
  393. wx.stopPullDownRefresh({
  394. success: (res) => {
  395. wx.showToast({
  396. title: '刷新成功',
  397. icon: 'success',
  398. duration: 1000
  399. });
  400. }
  401. });
  402. },
  403. // 获取未读消息数量的方法
  404. getUnreadNoticeCount: function() {
  405. const _this = this;
  406. // 从服务器获取最新的消息通知数据
  407. if (app.globalData.userWxInfo.token && app.globalData.currentAccountInfo) {
  408. wx.request({
  409. url: app.globalData.interfaceUrls.homePageInfo + app.globalData.currentAccountInfo.usernumber,
  410. method: 'GET',
  411. header: {
  412. 'content-type': 'application/json',
  413. 'token': app.globalData.userWxInfo.token,
  414. 'source': "wc",
  415. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  416. },
  417. success(res) {
  418. if (res.data && res.data.data) {
  419. // 更新全局的用户通知列表
  420. app.globalData.userNoticesList = res.data.data.sysNotifyAnnounceUserDos;
  421. // 计算未读消息数量
  422. const unreadCount = app.globalData.userNoticesList.filter(notice => notice.readstate === '2').length;
  423. _this.setData({
  424. unreadCount: unreadCount
  425. });
  426. }
  427. }
  428. });
  429. } else {
  430. this.setData({
  431. unreadCount: 0
  432. });
  433. }
  434. },
  435. // 新增方法:加载主页全部数据(包括消息通知)
  436. loadHomepageData: function() {
  437. const _this = this;
  438. wx.showLoading({
  439. title: '加载中...',
  440. });
  441. if(!app.globalData.userWxInfo.token){
  442. wx.hideLoading();
  443. return;
  444. }
  445. wx.request({
  446. url: app.globalData.interfaceUrls.homePageInfo + app.globalData.currentAccountInfo.usernumber,
  447. method: 'GET',
  448. header: {
  449. 'content-type': 'application/json',
  450. 'token': app.globalData.userWxInfo.token,
  451. 'source': "wc",
  452. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  453. },
  454. success(res) {
  455. wx.hideLoading();
  456. if (res.data && res.data.data) {
  457. let apiReturnData = res.data;
  458. // 更新消息通知数据
  459. apiReturnData.data.swNotificationAnnouncementDos.forEach(data => {
  460. data.publishtime = data.publishtime.slice(0,10);
  461. });
  462. // 更新全局数据
  463. app.globalData.notices = apiReturnData.data.swNotificationAnnouncementDos;
  464. app.globalData.userNoticesList = apiReturnData.data.sysNotifyAnnounceUserDos;
  465. // 需要轮播的公告
  466. let data = apiReturnData.data.swNotificationAnnouncementDos.filter(data => {
  467. return data.homepageslider == '1';
  468. });
  469. // 更新未读消息数
  470. _this.getUnreadNoticeCount();
  471. // 更新页面数据
  472. _this.setData({
  473. billInfo: {
  474. totalAmount: apiReturnData.data.duFees ?? 0,
  475. waterUsage: apiReturnData.data.waterConsumption ?? 0,
  476. balance: apiReturnData.data.accountbalance ?? 0
  477. },
  478. userInfo: {
  479. name: apiReturnData.data.username,
  480. accountNumber: apiReturnData.data.usernumber,
  481. address: apiReturnData.data.address
  482. },
  483. noticeList: data,
  484. isScrolling: true
  485. });
  486. // 如果有轮播公告,重新开始轮播
  487. if(data && data.length > 0) {
  488. _this.startNoticeScroll();
  489. }
  490. }
  491. },
  492. fail(error) {
  493. wx.hideLoading();
  494. wx.showToast({
  495. title: '数据加载失败',
  496. icon: 'none',
  497. duration: 2000
  498. });
  499. }
  500. });
  501. }
  502. })