const tabService = require("../../../utils/tab-serve"); import { getTeamList, joinTeam ,isInTeam} from '../../../utils/serve/team'; import { getGoal } from '../../../utils/serve/train'; Page({ /** * 页面的初始数据 */ data: { isMe: true, //个人||团队 teamIsMe: true, //是否自己创建的 teamName: '', goal:{ accumulatedCount: 0, completedCount: 0, completionRate: null, goalAmount: 0, lastCount: 0, },//目标 page:1,//当前那一页 teamList:[],//群组列表 hasMore:true, isLoading:false, trainList: [ { img: 'fit.png', url: 'pages/parent/train/train' },{ img: 'small.png', url: 'pages/parent/train/train' }, { img: 'mid.png', url: 'pages/parent/train/train' }, ], }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { if (tabService.tabBar.length <= 0) { tabService.updateRole(this, 'user') } this.getTeam(false) this.getGoalObj() }, //跳转目标 goGolabDeatil() { console.log('aaaa') wx.navigateTo({ url: '/subpackage/train/goal/goal', }) }, changeTab(e) { const value = e.currentTarget.id if (value == 'me' || value == 'team') { this.setData({ isMe: value == 'me' }) } else { this.setData({ teamIsMe: value == 'myCreat', hasMore:true, teamList:[], page:1 },async()=>{ await this.getTeam() }) } }, async getGoalObj(){ const that = this const res = await getGoal() res.data.completionRate = res.data.goalAmount == 0 ? 0: (res.data.completedCount/res.data.goalAmount).toFixed(2) > 1? 1 : (res.data.completedCount/res.data.goalAmount).toFixed(2) res.data.completionRateStr = (res.data.completionRate * 100).toFixed(0) console.log(res.data) that.setData({ goal:res.data }) }, newTeam() { wx.navigateTo({ url: '/subpackage/team/creat/creat', }) }, async getTeam (isRefresh) { //获取群组列表 if(!this.data.hasMore) return //没有更多了 if(isRefresh){ this.setData({ page:1, }) }else { this.setData({ isLoading:true, }) } const res = await getTeamList({ PageIndex: this.data.page, PageSize:20, IsCreated:this.data.teamIsMe }) console.log(res.data) if(res.data.datas){ this.setData({ teamList: isRefresh ? res.data.datas : [...this.data.teamList,...res.data.datas], hasMore: res.data.total / 20 > this.page, page:this.page + 1, }) } this.setData({ isLoading:false }) console.log(this.data.teamList) if(isRefresh){ wx.showToast({ title: '刷新成功', icon:'none' }) wx.stopPullDownRefresh() } }, goTeamDetail(e){ const id = e.currentTarget.id // console.log(e) wx.navigateTo({ url: '/subpackage/team/detail/detail?id='+id+'&name='+e.currentTarget.dataset.name +'&isCreat='+this.data.teamIsMe, }) }, goTongji(){ wx.navigateTo({ url: '/subpackage/train/user-report/user-report', }) }, goTrainer(e){ //去训练 console.log('aaaa') wx.navigateTo({ url: '/subpackage/train/train/train?type='+ e.currentTarget.id.replace('.png',''), }) }, // 下拉刷新 onPullDownRefresh() { console.log('下拉刷新') this.setData({ hasMore:true },async()=>{ await this.getTeam(true) this.getGoalObj() wx.stopPullDownRefresh() wx.showToast({ title: '刷新成功', icon:'none' }) }) }, //上拉加载 onReachBottom: function () { if(this.data.isMe){ }else{ //加载群组列表 } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ async onShow(e) { tabService.updateIndex(this, 0) const app = getApp() const that = this console.log(app) if (app.globalData.teamRefresh == 1) { //刷新我创建的列表 this.setData({ hasMore:true },async =>{ console.log('刷新首页数据') that.getTeam(true) that.getGoalObj() app.globalData.teamRefresh = 0 }) } const groupId = wx.getStorageSync('groupId') if(groupId != null && groupId != 0){ //判断是否在群组中 const isIn = await isInTeam(groupId) console.log('groupId===========>',groupId) if(!isIn.data){ wx.showModal({ title:'是否加入群组?', confirmColor:"#26A6F6", success:async (res)=>{ if(res.confirm){ wx.showLoading({ title: '', }) this.setData({ isMe:false, teamIsMe:false }) console.log('加入群组') const tt = await joinTeam(groupId) if(tt.success){ wx.showToast({ title: '加入成功!', icon:'none' }) wx.removeStorageSync('groupId') } } if(res.cancel){ wx.removeStorageSync('groupId') } } }) } } }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })