// index.js // 获取应用实例 const app = getApp() const $api = require('../../utils/api.js').API; const $push = require('../../utils/api.js').PUSH; Page({ data: { loading:0, notices:[], tabs:['今日留言','我的关注'], tabs1:['收益榜','盈利榜','亏损榜','跌幅榜'], cur:0, cur1:0, date:'2022-04-27', explist:[], //留言 total1:0, page1:1, followList:[], //关注 total2:-1, page2:1, banners:[], winLost:{}, baikeList:[], articleList1:[], articleList2:[], articleList3:[], articleList4:[], groups:{}, role:0, match_day:0, }, onLoad() { wx.showNavigationBarLoading() wx.getStorage({ key: 'user', complete: res => { this.setData({ role: res.data.role, }) } }) $api.getDate().then(res=>{ this.setData({ date:res.data.data }) $api.getNotoice().then(res=>{ this.setData({ notices:res.data.data }) }) }) this.getFollow() this.getIndexData() this.getExp() }, fresh: function () { }, // 获取首页数据 getIndexData:function(){ $api.getChampionlList({isbanner:1}).then(res=>{ this.setData({ banners:res.data.data.list }) }) $api.getWinList({new:1}).then(res=>{ let day = res.data.data[0].valid_dates.indexOf(res.data.data[0].date) this.setData({ winLost:res.data.data[0], match_day:day, }) $api.getAvg({match_id:res.data.data[0].match_id}).then(res=>{ var days=res.data.data.list,weeks=[] days=days.reverse() for(let i=0;i<55;i++){ weeks.push({ name:'', data:[], total:0 }) } days.forEach(item=>{ var w=this.getWeekNumber(item.stock_date) weeks[w-1].data.push(item) }) weeks=weeks.filter(item=>item.data.length>0) var w_text=['一','二','三','四','五','六','七','八','九','十','十一','十二','十三','十四','十五'] weeks.forEach((item,index)=>{ if(item.data.length>0){ item.name='第'+w_text[index]+'周' var total=0 var money = 10000.0 item.data.forEach(i=>{ var incom=Number(i.today_income.replace('%','')) money += money*incom/100.0 i.date=(new Date(i.stock_date).getMonth()+1)+'/'+(new Date(i.stock_date).getDate()) }) total = (money-10000)*100.0/10000.0 item.total=total.toFixed(2) } }) this.setData({ days:weeks.reverse() }) }) $api.getWinLose({match_id:res.data.data[0].match_id}).then(res1=>{ this.setData({ groups:res1.data.data }) }) }) $api.getBaikeRandom().then(res=>{ this.setData({ baikeList:res.data.data }) }) $api.getChampionlList({category:'每日点评',page:1}).then(res=>{ this.setData({ articleList1:res.data.data.list }) }) $api.getChampionlList({category:'冠军交割',page:1}).then(res=>{ this.setData({ articleList2:res.data.data.list }) }) $api.getChampionlList({category:'牛人专场',page:1}).then(res=>{ this.setData({ articleList3:res.data.data.list }) }) $api.getChampionlList({category:'妖股列传',page:1}).then(res=>{ this.setData({ articleList4:res.data.data.list }) }) }, //计算周数 getWeekNumber:function(dateString) { const date = new Date(dateString); const year = date.getFullYear(); // 将年份的第一个周一作为第一周的开始 const firstWeekStart = new Date(year, 0, 1); firstWeekStart.setDate(firstWeekStart.getDate() - firstWeekStart.getDay() + 1); // 计算给定日期是一年中的第几周 const currentDate = new Date(date); currentDate.setDate(currentDate.getDate() - currentDate.getDay() + 1); let milliseconds = currentDate.getTime() - firstWeekStart.getTime(); const weeks = Math.round(milliseconds / (7 * 24 * 60 * 60 * 1000)); return weeks + 1; // 因为getWeek返回的是年初至今的周数,所以加1得到完整的周数 }, //获取今日留言 getExp:function(){ var list = this.data.explist $api.getExperience({page:this.data.page1,page_size:20}).then(res=>{ res.data.data.list.forEach(item => { list.push(item) }) this.setData({ explist: list, total1: res.data.data.total }) }) }, //留言分页 next1: function () { if (this.data.explist.length < this.data.total1) { const page = this.data.page1 + 1 this.setData({ page1: page }) this.getExp() } }, //我的关注 getFollow(){ var followList = this.data.followList if(this.data.loading){ followList=[] } $api.getMyFollowV3({ page: this.data.page2, page_size: 20 }).then(res => { wx.hideNavigationBarLoading() res.data.data.list.forEach(item => { followList.push(item) }) this.setData({ followList: followList, total2:res.data.data.total, error:0, loading: 0 }) app.globalData.follow = 0 }) .catch(err=>{ this.setData({ error:1 }) }) }, //关注分页 next2: function () { if (this.data.followList.length < this.data.total2) { const page = this.data.page2 + 1 this.setData({ page2: page }) this.getFollow() } }, tabChange(e){ this.setData({ cur:e.target.dataset.id }) this.renew() }, tabChange1(e){ this.setData({ cur1:e.target.dataset.id }) }, tapGroup(e){ wx.navigateTo({ url: '../avg/avg?index='+e.currentTarget.dataset.index + '&list=' + encodeURIComponent(JSON.stringify(this.data.groups.group_win_lose_list)), }) }, tapAvg(e){ wx.navigateTo({ url: '../avg/avg?index=0' + '&list=' + encodeURIComponent(JSON.stringify(this.data.groups.group_win_lose_list)), }) }, //预览 preview(e){ wx.previewImage({ urls: e.target.dataset.urls, current: e.target.dataset.src }) }, curChange(e){ if (e.detail.source == "touch"){ this.setData({ cur: e.detail.current }) this.renew() } }, curChange1(e){ if (e.detail.source == "touch"){ this.setData({ cur1: e.detail.current }) } }, renew(){ this.setData({ followList:[], explist:[], page1: 1, total1: 0, page2: 1, total2: -1 }) switch(this.data.cur){ case 0: this.data.page1 = 1 this.getExp() break; case 1: this.data.page2 = 1 this.getFollow() break; } }, pushToday(e){ $push.pushToday(e.currentTarget.dataset) }, onShow: function (e) { $api.initUser() $api.getsignup().then(res => { this.setData({ info: res.data.data }) }) if(this.data.cur == 0 && app.globalData.follow == 1){ this.data.loading = 1 this.data.page2 = 1 this.data.total2 = -1, this.getFollow() } }, })