// pages/topics/index.js import dayjs from '../../utils/dayjs/index.js' import relativeTime from '../../utils/dayjs/relativeTime.js' import { TopicModel } from '../../models/topic.js' import { store } from '../../utils/store.js' import { UserModel } from '../../models/user.js' let userModel = new UserModel() dayjs.extend(relativeTime) let topicModel = new TopicModel() const app = getApp() Page({ /** * 页面的初始数据 */ data: { segmentIndex: 0, //留言互动的列表 topicList: [], goodIdeaList: [], //赋能中心的列表 centerList: [], typeList:[], page:1, next:true, tab:"5", active:"5", loan:{}, apartment:[] }, /** * 生命周期函数--监听页面加载 */ onLoad: function () { }, //进入页面判断是否绑定微信号,如果绑定手机号根据segmentIndex的数值fetch留言互动和赋能中心的资源,否则退回注册页面或者首页 onShow: function () { if (store.hasBindUserInfo()) { console.log('已经绑定微信') if (store.hasPhone()) { console.log('已经绑定手机号码') this.setData({ topicList: [], goodIdeaList: [], centerList: [], page:1, next:true, // tab:"5", // active:"5", }) return this.getData(this.data.tab); } else { console.log('未绑定手机号码') if(app.globalData.navigate.mobile){ var params = { phone:app.globalData.navigate.mobile, nickName:app.globalData.navigate.nickname, avatarUrl:app.globalData.navigate.faceImg } userModel.navigateUser(params,res=>{ store.saveUserInfo({ nickName: params.nickName, avatarUrl: params.avatarUrl, phone: params.phone }) wx.reLaunch({ url: '/pages/topics/index', }) }) }else{ wx.showModal({ title: '温馨提示', content: '是否前往验证手机号码?', success(res) { if (res.confirm) { wx.redirectTo({ url: '/pages/weChatAuth/index?type=2', }) } else if (res.cancel) { wx.switchTab({ url: '/pages/home/index', }) } } }) } } } else { console.log('未绑定微信') if(app.globalData.navigate.mobile){ var params = { phone:app.globalData.navigate.mobile, nickName:app.globalData.navigate.nickname, avatarUrl:app.globalData.navigate.faceImg } userModel.navigateUser(params,res=>{ store.saveUserInfo({ nickName: params.nickName, avatarUrl: params.avatarUrl, phone: params.phone }) wx.reLaunch({ url: '/pages/topics/index', }) }) }else{ wx.showModal({ title: '温馨提示', content: '是否前往绑定微信?', success(res) { if (res.confirm) { wx.redirectTo({ url: '/pages/weChatAuth/index', }) } else if (res.cancel) { wx.switchTab({ url: '/pages/home/index', }) } } }) } } }, changeTabs:function(e){ var tab = e.detail.name; this.setData({ page:1, next:true, tab:tab, topicList:[], centerList:[] }) this.getData(tab); }, getData:function(tab){ switch (tab) { case "1": return this.fetchCenterList(); case "3": return this.fetchTopicList(); case "4": return this.fetchLoan(); case "5": return this.fetchRent(); default: return; } }, //留言互动 //fetch留言互动的资源 fetchTopicList () { var th = this; var page = th.data.page; var list = th.data.topicList; topicModel.getTopicList(page, res => { console.log('议题列表') console.log(res) if (res.result.list.length == 0 && page == 1) { wx.showToast({ title: '暂无数据', icon: 'none' }) } const datas = res.result.list let tempDatas = [] datas.forEach((item,index) => { tempDatas.push({ topicId: item.id, userIcon: item.groupAvator || '', title: item.title, // userIcon: item.groupAvator, userName: item.author, commentNum: item.commentNum, topicImg: item.image, time: item.createTime, dataIndex:index+((page-1)*10), isTouchMove:false, }) }) if(tempDatas.length > 0){ list = list.concat(tempDatas); th.setData({ topicList: list, }) }else{ th.setData({ next: false, page: page-1 }) } }) }, //赋能中心 fetchCenterList(){ // let page = this.data.currPage; var th = this; var page = th.data.page; var list = th.data.centerList; if(th.data.typeList.length == 0){ topicModel.getResourceTypeList(res=>{ this.setData({ typeList:res.result }) }) } topicModel.getResourceList('',page,res => { if(res.result.records.length > 0){ list = list.concat(res.result.records); th.setData({ centerList: list, }) }else{ th.setData({ next: false, page: page-1 }) } }) }, fetchLoan:function(){ topicModel.getResourceDetail('aba954f98c489bddf137f6f9f83d6c89',res=>{ this.setData({ loan:res.result, }) }) }, fetchRent:function(){ topicModel.getApartment(res=>{ this.setData({ apartment:res.result }) }) }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { var next = this.data.next; var tab = this.data.tab; var page = this.data.page; if(!next){ return; } this.setData({ page: page + 1 }) switch (tab) { case "1": return this.fetchCenterList(); case "3": return this.fetchTopicList(); default: return; } }, deleteTopic(e) { // console.log(e.detail.id) let that = this topicModel.deleteGroup(e.detail.id, res => { console.log(res) if (res.code === 200) { wx.showToast({ title: '删除成功', icon: 'none', success() { that.setData({ currPage: 1, }) switch (that.data.segmentIndex) { case 0: return that.fetchTopicList() case 1: // return that.fetchGoodIdeaList() //GoodIdea修改为center return that.fetchCenterList() } } }) } }) }, deleteTopicComment(e){ let that = this topicModel.deleteComment(e.detail.id,res=>{ if(res.code === 200){ wx.showToast({ title: '删除成功', icon: 'none', success() { that.setData({ currPage: 1, }) switch (that.data.segmentIndex) { case 0: return that.fetchTopicList() case 1: return that.fetchCenterList() } } }) } }) }, tapGoodIdeaPraise(e){ this.fetchGoodIdeaPraise(e.detail.commentId) }, fetchGoodIdeaPraise(id) { wx.showLoading() topicModel.topicCommentPraise(id, res => { if (res.code === 200) { wx.hideLoading() this.onPullDownRefresh() } }) }, })