homepage.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. this.getUnreadNoticeCount();
  150. // 检查是否从户号管理页面返回
  151. const backFromHuHao = wx.getStorageSync('backFromHuHao');
  152. if (backFromHuHao) {
  153. wx.removeStorageSync('backFromHuHao');
  154. // 从户号管理页面返回时重新加载全部数据
  155. this.loadHomepageData();
  156. }
  157. },
  158. loadPageData: function() {
  159. const _this = this;
  160. wx.showLoading({
  161. title: '加载中...',
  162. });
  163. // 检查token是否存在
  164. if(!app.globalData.userWxInfo.token){
  165. wx.hideLoading();
  166. return;
  167. }
  168. wx.request({
  169. url: app.globalData.interfaceUrls.homePageInfo + app.globalData.currentAccountInfo.usernumber,
  170. method: 'GET',
  171. header: {
  172. 'content-type': 'application/json',
  173. 'token': app.globalData.userWxInfo.token,
  174. 'source': "wc",
  175. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  176. },
  177. success(res) {
  178. wx.hideLoading();
  179. let apiReturnData = res.data;
  180. // 该用户所有的消息通知
  181. app.globalData.userNoticesList = apiReturnData.data.sysNotifyAnnounceUserDos;
  182. // 更新页面数据
  183. _this.setData({
  184. billInfo: {
  185. totalAmount: apiReturnData.data.duFees ?? 0, // 总计应缴
  186. waterUsage: apiReturnData.data.waterConsumption ?? 0, // 用水量
  187. balance: apiReturnData.data.accountbalance ?? 0 // 账户余额
  188. },
  189. userInfo: {
  190. name: apiReturnData.data.username,
  191. accountNumber: apiReturnData.data.usernumber,
  192. address: apiReturnData.data.address
  193. },
  194. });
  195. },
  196. fail(error) {
  197. wx.hideLoading();
  198. wx.showToast({
  199. title: '数据加载失败,请稍后再试',
  200. icon: 'none',
  201. duration: 2000
  202. });
  203. }
  204. });
  205. },
  206. // 切换户号
  207. handleSwitchMeter() {
  208. wx.navigateTo({
  209. url: '/pages/huhaoguanli/huhaoguanli'
  210. })
  211. },
  212. // 充值缴费
  213. handlePayment() {
  214. wx.navigateTo({
  215. // url: '/pages/payment/payment'
  216. })
  217. },
  218. // 功能按钮点击事件
  219. handleFunctionClick(e) {
  220. const index = e.currentTarget.dataset.index
  221. const item = this.data.functionList[index]
  222. if (item && item.url) {
  223. wx.navigateTo({
  224. url: item.url,
  225. fail: function(err) {
  226. console.error('页面跳转失败:', err)
  227. wx.showToast({
  228. title: '功能开发中',
  229. icon: 'none',
  230. duration: 2000
  231. })
  232. }
  233. })
  234. } else {
  235. wx.showToast({
  236. title: '功能开发中',
  237. icon: 'none',
  238. duration: 2000
  239. })
  240. }
  241. },
  242. // 查看账单列表
  243. handleViewBill() {
  244. wx.navigateTo({
  245. // url: '/pages/billList/billList'
  246. })
  247. },
  248. // 查看更多活动
  249. handleMoreActivity() {
  250. wx.navigateTo({
  251. // url: '/pages/activityList/activityList'
  252. })
  253. },
  254. // 添加或修改立即缴费按钮点击事件
  255. goToPayment: function(e) {
  256. // 获取当前账单信息
  257. const billInfo = {
  258. amount: this.data.billInfo.totalAmount, // 应缴金额
  259. amountDue:this.data.billInfo.totalAmount, // 总计应缴
  260. balance: this.data.billInfo.balance // 账户余额
  261. };
  262. // 编码账单信息并传递到缴费页面
  263. const billInfoStr = encodeURIComponent(JSON.stringify(billInfo));
  264. wx.navigateTo({
  265. url: `/pages/lijijiaofei/lijijiaofei?billInfo=${billInfoStr}`
  266. });
  267. },
  268. // 跳转到通知公告列表页面
  269. goToNoticeList: function() {
  270. wx.navigateTo({
  271. url: '/pages/tongzhiList/tongzhiList'
  272. })
  273. },
  274. // 跳转到通知详情页面
  275. goToNoticeDetail: function() {
  276. const currentNotice = this.data.noticeList[this.data.currentIndex];
  277. wx.navigateTo({
  278. url: `/pages/tzxq/tzxq?id=${currentNotice.id}`
  279. });
  280. },
  281. onReady: function() {
  282. this.startNoticeScroll();
  283. },
  284. startNoticeScroll: function() {
  285. if(this.data.noticeList.length==0){
  286. return;
  287. }
  288. let data= (this.data.noticeList||[]).filter(data=>{
  289. return data.homepageslider=='1';
  290. });
  291. const content = data[this.data.currentIndex].scrollingcontent;
  292. const title = data[this.data.currentIndex].noticetitle;
  293. const duration = (title.length + content.length) * 0.5;
  294. this.setData({
  295. scrollDuration: duration,
  296. isScrolling: true,
  297. });
  298. },
  299. onScrollComplete: function() {
  300. this.setData({
  301. isScrolling: false
  302. });
  303. // 等待2秒后切换到下一条
  304. setTimeout(() => {
  305. const nextIndex = (this.data.currentIndex + 1) % this.data.noticeList.length;
  306. this.setData({
  307. currentIndex: nextIndex
  308. }, () => {
  309. this.startNoticeScroll();
  310. });
  311. }, 1000);
  312. },
  313. // 加载账户和账单信息
  314. loadAccountInfo: function() {
  315. const accountInfo = app.globalData.currentAccountInfo || {};
  316. // 检查是否是首次加载
  317. const isFirstLoad = wx.getStorageSync('isFirstLoad') !== 'false';
  318. if (isFirstLoad) {
  319. // 首次加载,查找并设置默认账户
  320. this.loadDefaultAccount();
  321. // 设置标记,表示已不是首次加载
  322. wx.setStorageSync('isFirstLoad', 'false');
  323. } else {
  324. this.setData({
  325. currentAccountInfo: accountInfo
  326. });
  327. }
  328. },
  329. // 加载默认账户
  330. loadDefaultAccount: function() {
  331. if (!app.globalData.userWxInfo.token) {
  332. return;
  333. }
  334. const _this = this;
  335. wx.showLoading({
  336. title: '加载中...',
  337. });
  338. wx.request({
  339. url: app.globalData.interfaceUrls.accountList,
  340. method: 'GET',
  341. header: {
  342. 'content-type': 'application/json',
  343. 'token': app.globalData.userWxInfo.token,
  344. 'source': "wc"
  345. },
  346. success(res) {
  347. wx.hideLoading();
  348. if (res.data && res.data.data) {
  349. // 查找 defaultAccount 为 0 的账户
  350. const defaultAccount = res.data.data.find(account => account.defaultAccount === "0");
  351. if (defaultAccount) {
  352. // 设置为当前账户
  353. app.globalData.currentAccountInfo = defaultAccount;
  354. _this.setData({
  355. currentAccountInfo: defaultAccount
  356. });
  357. // 加载该默认账户的详细信息
  358. _this.loadPageData();
  359. } else {
  360. // 没有找到默认账户,使用当前账户
  361. _this.setData({
  362. currentAccountInfo: app.globalData.currentAccountInfo || {}
  363. });
  364. }
  365. }
  366. },
  367. fail(error) {
  368. wx.hideLoading();
  369. wx.showToast({
  370. title: '加载默认账户失败',
  371. icon: 'none',
  372. duration: 2000
  373. });
  374. // 使用当前账户作为备选
  375. _this.setData({
  376. currentAccountInfo: app.globalData.currentAccountInfo || {}
  377. });
  378. }
  379. });
  380. },
  381. onPullDownRefresh: function() {
  382. // 刷新数据
  383. this.refreshData();
  384. },
  385. // 修改刷新数据的函数
  386. refreshData: function() {
  387. console.log('正在刷新数据...');
  388. this.loadPageData();
  389. // 刷新完成后停止下拉刷新动画
  390. wx.stopPullDownRefresh({
  391. success: (res) => {
  392. wx.showToast({
  393. title: '刷新成功',
  394. icon: 'success',
  395. duration: 1000
  396. });
  397. }
  398. });
  399. },
  400. // 获取未读消息数量的方法
  401. getUnreadNoticeCount: function() {
  402. if (app.globalData.userNoticesList) {
  403. // 假设 userNoticesList 中有一个 isRead 属性表示消息是否已读
  404. const unreadCount = app.globalData.userNoticesList.filter(notice => notice.readstate === '2').length;
  405. this.setData({
  406. unreadCount: unreadCount
  407. });
  408. } else {
  409. this.setData({
  410. unreadCount: 0
  411. });
  412. }
  413. },
  414. // 新增方法:加载主页全部数据(包括消息通知)
  415. loadHomepageData: function() {
  416. const _this = this;
  417. wx.showLoading({
  418. title: '加载中...',
  419. });
  420. if(!app.globalData.userWxInfo.token){
  421. wx.hideLoading();
  422. return;
  423. }
  424. wx.request({
  425. url: app.globalData.interfaceUrls.homePageInfo + app.globalData.currentAccountInfo.usernumber,
  426. method: 'GET',
  427. header: {
  428. 'content-type': 'application/json',
  429. 'token': app.globalData.userWxInfo.token,
  430. 'source': "wc",
  431. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  432. },
  433. success(res) {
  434. wx.hideLoading();
  435. if (res.data && res.data.data) {
  436. let apiReturnData = res.data;
  437. // 更新消息通知数据
  438. apiReturnData.data.swNotificationAnnouncementDos.forEach(data => {
  439. data.publishtime = data.publishtime.slice(0,10);
  440. });
  441. // 更新全局数据
  442. app.globalData.notices = apiReturnData.data.swNotificationAnnouncementDos;
  443. app.globalData.userNoticesList = apiReturnData.data.sysNotifyAnnounceUserDos;
  444. // 需要轮播的公告
  445. let data = apiReturnData.data.swNotificationAnnouncementDos.filter(data => {
  446. return data.homepageslider == '1';
  447. });
  448. // 更新未读消息数
  449. _this.getUnreadNoticeCount();
  450. // 更新页面数据
  451. _this.setData({
  452. billInfo: {
  453. totalAmount: apiReturnData.data.duFees ?? 0,
  454. waterUsage: apiReturnData.data.waterConsumption ?? 0,
  455. balance: apiReturnData.data.accountbalance ?? 0
  456. },
  457. userInfo: {
  458. name: apiReturnData.data.username,
  459. accountNumber: apiReturnData.data.usernumber,
  460. address: apiReturnData.data.address
  461. },
  462. noticeList: data,
  463. isScrolling: true
  464. });
  465. // 如果有轮播公告,重新开始轮播
  466. if(data && data.length > 0) {
  467. _this.startNoticeScroll();
  468. }
  469. }
  470. },
  471. fail(error) {
  472. wx.hideLoading();
  473. wx.showToast({
  474. title: '数据加载失败',
  475. icon: 'none',
  476. duration: 2000
  477. });
  478. }
  479. });
  480. }
  481. })