homepage.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. },
  69. onLoad() {
  70. let refresh= app.globalData.refresh;
  71. if(refresh==1){
  72. wx.showLoading({
  73. title: '加载中...',
  74. });
  75. if(!app.globalData.userWxInfo.token){
  76. wx.hideLoading();
  77. return;
  78. }
  79. const _this = this;
  80. wx.request({
  81. url: app.globalData.interfaceUrls.homePageInfo+app.globalData.currentAccountInfo.usernumber,
  82. method: 'GET',
  83. header: {
  84. 'content-type': 'application/json', // 默认值
  85. 'token':app.globalData.userWxInfo.token,
  86. 'source':"wc",
  87. '!SAAS_LOGIN_TOKEN_!':app.globalData.currentAccountInfo.dsKey
  88. },
  89. success (res) {
  90. wx.hideLoading();
  91. let apiReturnData=res.data;
  92. apiReturnData.data.swNotificationAnnouncementDos.forEach(data=>{
  93. data.publishtime=data.publishtime.slice(0,10);
  94. })
  95. // 所有的通知公告
  96. app.globalData.notices= apiReturnData.data.swNotificationAnnouncementDos;
  97. // 需要轮播的公告
  98. let data= apiReturnData.data.swNotificationAnnouncementDos.filter(data=>{
  99. return data.homepageslider=='1';
  100. });
  101. _this.setData({
  102. billInfo:{
  103. totalAmount:apiReturnData.data.duFees, // 总计应缴
  104. waterUsage:apiReturnData.data.waterConsumption, // 用水量
  105. balance: apiReturnData.data.accountbalance // 账户余额
  106. },
  107. userInfo:{
  108. name: apiReturnData.data.username,
  109. accountNumber: apiReturnData.data.usernumber,
  110. address: apiReturnData.data.address
  111. },
  112. noticeList :data,
  113. isScrolling: true,
  114. })
  115. },
  116. fail(error) {
  117. wx.hideLoading()
  118. utils.simleInfo('登录失败,请稍后再试')
  119. }
  120. })
  121. }
  122. // 加载账户和账单信息
  123. this.loadAccountInfo();
  124. },
  125. onShow() {
  126. // 检查是否需要刷新
  127. const needRefresh = wx.getStorageSync('needRefreshHomepage');
  128. if (needRefresh) {
  129. // 清除刷新标记
  130. wx.removeStorageSync('needRefreshHomepage');
  131. this.loadPageData();
  132. }
  133. },
  134. loadPageData: function() {
  135. const _this = this;
  136. wx.showLoading({
  137. title: '加载中...',
  138. });
  139. // 检查token是否存在
  140. if(!app.globalData.userWxInfo.token){
  141. wx.hideLoading();
  142. return;
  143. }
  144. wx.request({
  145. url: app.globalData.interfaceUrls.homePageInfo + app.globalData.currentAccountInfo.usernumber,
  146. method: 'GET',
  147. header: {
  148. 'content-type': 'application/json',
  149. 'token': app.globalData.userWxInfo.token,
  150. 'source': "wc",
  151. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  152. },
  153. success(res) {
  154. wx.hideLoading();
  155. let apiReturnData = res.data;
  156. // 更新页面数据
  157. _this.setData({
  158. billInfo: {
  159. totalAmount: apiReturnData.data.duFees, // 总计应缴
  160. waterUsage: apiReturnData.data.waterConsumption, // 用水量
  161. balance: apiReturnData.data.accountbalance // 账户余额
  162. },
  163. userInfo: {
  164. name: apiReturnData.data.username,
  165. accountNumber: apiReturnData.data.usernumber,
  166. address: apiReturnData.data.address
  167. },
  168. });
  169. },
  170. fail(error) {
  171. wx.hideLoading();
  172. wx.showToast({
  173. title: '数据加载失败,请稍后再试',
  174. icon: 'none',
  175. duration: 2000
  176. });
  177. }
  178. });
  179. },
  180. // 切换户号
  181. handleSwitchMeter() {
  182. wx.navigateTo({
  183. url: '/pages/huhaoguanli/huhaoguanli'
  184. })
  185. },
  186. // 充值缴费
  187. handlePayment() {
  188. wx.navigateTo({
  189. // url: '/pages/payment/payment'
  190. })
  191. },
  192. // 功能按钮点击事件
  193. handleFunctionClick(e) {
  194. const index = e.currentTarget.dataset.index
  195. const item = this.data.functionList[index]
  196. if (item && item.url) {
  197. wx.navigateTo({
  198. url: item.url,
  199. fail: function(err) {
  200. console.error('页面跳转失败:', err)
  201. wx.showToast({
  202. title: '功能开发中',
  203. icon: 'none',
  204. duration: 2000
  205. })
  206. }
  207. })
  208. } else {
  209. wx.showToast({
  210. title: '功能开发中',
  211. icon: 'none',
  212. duration: 2000
  213. })
  214. }
  215. },
  216. // 查看账单列表
  217. handleViewBill() {
  218. wx.navigateTo({
  219. // url: '/pages/billList/billList'
  220. })
  221. },
  222. // 查看更多活动
  223. handleMoreActivity() {
  224. wx.navigateTo({
  225. // url: '/pages/activityList/activityList'
  226. })
  227. },
  228. // 添加或修改立即缴费按钮点击事件
  229. goToPayment: function(e) {
  230. // 获取当前账单信息
  231. const billInfo = {
  232. amount: this.data.billInfo.totalAmount, // 应缴金额
  233. amountDue:this.data.billInfo.totalAmount, // 总计应缴
  234. balance: this.data.billInfo.balance // 账户余额
  235. };
  236. // 编码账单信息并传递到缴费页面
  237. const billInfoStr = encodeURIComponent(JSON.stringify(billInfo));
  238. wx.navigateTo({
  239. url: `/pages/lijijiaofei/lijijiaofei?billInfo=${billInfoStr}`
  240. });
  241. },
  242. // 跳转到通知公告列表页面
  243. goToNoticeList: function() {
  244. wx.navigateTo({
  245. url: '/pages/tongzhiList/tongzhiList'
  246. })
  247. },
  248. // 跳转到通知详情页面
  249. goToNoticeDetail: function() {
  250. const currentNotice = this.data.noticeList[this.data.currentIndex];
  251. wx.navigateTo({
  252. url: `/pages/tzxq/tzxq?id=${currentNotice.id}`
  253. });
  254. },
  255. onReady: function() {
  256. this.startNoticeScroll();
  257. },
  258. startNoticeScroll: function() {
  259. if(this.data.noticeList.length==0){
  260. return;
  261. }
  262. let data= (this.data.noticeList||[]).filter(data=>{
  263. return data.homepageslider=='1';
  264. });
  265. const content = data[this.data.currentIndex].scrollingcontent;
  266. const title = data[this.data.currentIndex].noticetitle;
  267. const duration = (title.length + content.length) * 0.4;
  268. this.setData({
  269. scrollDuration: duration,
  270. isScrolling: true,
  271. });
  272. },
  273. onScrollComplete: function() {
  274. this.setData({
  275. isScrolling: false
  276. });
  277. // 等待1秒后切换到下一条
  278. setTimeout(() => {
  279. const nextIndex = (this.data.currentIndex + 1) % this.data.noticeList.length;
  280. this.setData({
  281. currentIndex: nextIndex
  282. }, () => {
  283. this.startNoticeScroll();
  284. });
  285. }, 1000);
  286. },
  287. // 加载账户和账单信息
  288. loadAccountInfo: function() {
  289. const accountInfo = app.globalData.currentAccountInfo || {};
  290. // 检查是否是首次加载
  291. const isFirstLoad = wx.getStorageSync('isFirstLoad') !== 'false';
  292. if (isFirstLoad) {
  293. // 首次加载,查找并设置默认账户
  294. this.loadDefaultAccount();
  295. // 设置标记,表示已不是首次加载
  296. wx.setStorageSync('isFirstLoad', 'false');
  297. } else {
  298. this.setData({
  299. currentAccountInfo: accountInfo
  300. });
  301. }
  302. },
  303. // 加载默认账户
  304. loadDefaultAccount: function() {
  305. if (!app.globalData.userWxInfo.token) {
  306. return;
  307. }
  308. const _this = this;
  309. wx.showLoading({
  310. title: '加载中...',
  311. });
  312. wx.request({
  313. url: app.globalData.interfaceUrls.accountList,
  314. method: 'GET',
  315. header: {
  316. 'content-type': 'application/json',
  317. 'token': app.globalData.userWxInfo.token,
  318. 'source': "wc"
  319. },
  320. success(res) {
  321. wx.hideLoading();
  322. if (res.data && res.data.data) {
  323. // 查找 defaultAccount 为 0 的账户
  324. const defaultAccount = res.data.data.find(account => account.defaultAccount === "0");
  325. if (defaultAccount) {
  326. // 设置为当前账户
  327. app.globalData.currentAccountInfo = defaultAccount;
  328. _this.setData({
  329. currentAccountInfo: defaultAccount
  330. });
  331. // 加载该默认账户的详细信息
  332. _this.loadPageData();
  333. } else {
  334. // 没有找到默认账户,使用当前账户
  335. _this.setData({
  336. currentAccountInfo: app.globalData.currentAccountInfo || {}
  337. });
  338. }
  339. }
  340. },
  341. fail(error) {
  342. wx.hideLoading();
  343. wx.showToast({
  344. title: '加载默认账户失败',
  345. icon: 'none',
  346. duration: 2000
  347. });
  348. // 使用当前账户作为备选
  349. _this.setData({
  350. currentAccountInfo: app.globalData.currentAccountInfo || {}
  351. });
  352. }
  353. });
  354. },
  355. onPullDownRefresh: function() {
  356. // 刷新数据
  357. this.refreshData();
  358. },
  359. // 修改刷新数据的函数
  360. refreshData: function() {
  361. console.log('正在刷新数据...');
  362. this.loadPageData();
  363. // 刷新完成后停止下拉刷新动画
  364. wx.stopPullDownRefresh({
  365. success: (res) => {
  366. wx.showToast({
  367. title: '刷新成功',
  368. icon: 'success',
  369. duration: 1000
  370. });
  371. }
  372. });
  373. },
  374. })