const api = require('../../../../utils/api') import { getTimestamp } from '../../../../utils/common' const app = getApp() Component({ data: { projectList: [], pageNo: 1, pageSize: 10, timestamp: '', preloadVisible: true, loadMoreType: 'loading', loadMoreVisible: false, selectedProjectId: '' }, lifetimes: { attached () { this.getProjectList() this.pulldownRefresh = this.selectComponent('#pulldown-refresh') this.triggerEvent('getProjectComponent') } }, pageLifetimes: { show () { if (!app.globalData.previewImage) { this.backAndRefreshProjectList() } app.globalData.previewImage = false } }, methods: { onPageScroll (e) { this.pulldownRefresh.onPageScroll(e) }, onReachBottom () { this.setData({ loadMoreVisible: true }) if (this.data.loadMoreType === 'loading') { this.data.pageNo += 1 this.pulldownGetProjectList() } }, // 下拉刷新 获取 项目列表接口 getProjectList () { this.setData({ timestamp: getTimestamp(), pageNo: 1, preloadVisible: true, loadMoreVisible: false, projectList: [], }) const para = { pageIndex: this.data.pageNo, pageSize: this.data.pageSize, timestamp: this.data.timestamp, searchContent: '', firstCategoryCode: '' } api.getProjectList(para).then(res => { this.pulldownRefresh.stopRefresh() console.log('获取项目列表', res) this.setData({ projectList: [...res.data], preloadVisible: false, loadMoreType: res.data.length === 10 ? 'loading' : 'none' }) }).catch(err => { this.pulldownRefresh.stopRefresh() this.setData({ projectList: [], preloadVisible: false, loadMoreType: 'none' }) console.log(err) }) }, // 上拉加载 项目列表 pulldownGetProjectList () { const para = { pageIndex: this.data.pageNo, pageSize: this.data.pageSize, timestamp: this.data.timestamp, searchContent: '', firstCategoryCode: '' } api.getProjectList(para).then(res => { console.log('获取项目列表', res) this.setData({ projectList: [...this.data.projectList,...res.data], preloadVisible: false, loadMoreType: res.data.length === 10 ? 'loading' : 'none' }) }).catch(err => { this.setData({ projectList: [], preloadVisible: false, loadMoreType: 'none' }) console.log(err) }) }, // 跳转到项目详情 navigateToProjectDetail (e) { this.data.selectedProjectId = e.detail.projectId wx.navigateTo({ url: `/subpages/discussion/pages/discussionDetail/discussionDetail?type=project&detailId=${e.detail.projectId}&showClassify=show` }) }, // 跳转搜索页面 navigateToSearchDiscussion () { wx.navigateTo({ url: `/subpages/discussion/pages/searchDiscussion/searchDiscussion?type=project` }) }, // 返回刷新项目列表 backAndRefreshProjectList () { if (this.data.selectedProjectId) { const para = { pageIndex: 1, pageSize: 10, timestamp: this.data.timestamp, searchContent: '', firstCategoryCode: '', itemId: this.data.selectedProjectId } api.getProjectList(para).then(res => { console.log('详情回列表,刷新列表', res) if (res.data.length === 1) { this.data.projectList.forEach((item, index) => { if (item.id === this.data.selectedProjectId) { this.data.projectList.splice(index, 1, res.data[0]) } }) } else { const index = this.data.projectList.findIndex(item => item.id === this.data.selectedProjectId) this.data.projectList.splice(index, 1) } this.setData({ projectList: this.data.projectList }) }).finally(() => { this.data.selectedProjectId = '' }) } } } })