import { getIssueList, getProjectList} from '../../utils/api' import { formatTimestamp } from '../../utils/common' Page({ data: { issueList: [], loadMoreVisible: false, loadMoreType: 'none', preloadVisible: false, timestamp: '', pageNo: 1, pageSize: 10, projectList: [], type: 'project', searchInputValue: '', searchTimer: '', selectedIssueId: '', selectedProjectId: '' }, onLoad (options) { if (options.type) { this.setData({ type: options.type }) } }, onShow () { this.backAndRefreshIssueList() this.backAndRefreshProjectList() }, onReady () { this.pullDownRefresh = this.selectComponent('#pulldown-refresh') }, onPageScroll (e) { this.pullDownRefresh.onPageScroll(e) }, onReachBottom () { this.setData({ loadMoreVisible: true }) if (this.data.loadMoreType === 'loading') { this.setData({ pageNo: this.data.pageNo + 1 }) if (this.data.type === 'issue') { this.pullDownGetIssueList() } else if (this.data.type === 'project') { this.pulldownGetProjectList() } } }, // 下拉刷新 获取议题列表 getIssueList () { this.setData({ timestamp: formatTimestamp(), preloadVisible: true, issueList: [], pageNo: 1 }) const para = { pageIndex: this.data.pageNo, pageSize: this.data.pageSize, timestamp: this.data.timestamp, searchContent: this.data.searchInputValue, orderType: '0', firstCategoryCode: '' } getIssueList(para).then(res => { console.log('获取议题列表', res) this.setData({ issueList: [...res.data], preloadVisible: false, loadMoreType: res.data.length === 10 ? 'loading' : 'none' }) }).catch(err => { this.setData({ issueList: [], preloadVisible: false, loadMoreType: 'none', loadMoreVisible: false }) console.log(err) }).finally(() => { this.pullDownRefresh.stopRefresh() }) }, // 上拉加载更多 pullDownGetIssueList () { const para = { pageIndex: this.data.pageNo, pageSize: this.data.pageSize, timestamp: this.data.timestamp, searchContent: this.data.searchInputValue, orderType: '0', firstCategoryCode: '' } getIssueList(para).then(res => { console.log('获取议题列表', res) this.setData({ issueList: [...this.data.issueList,...res.data], preloadVisible: false, loadMoreType: res.data.length === 10 ? 'loading' : 'none' }) }).catch(err => { this.setData({ issueList: [], preloadVisible: false, loadMoreType: 'none', loadMoreVisible: false }) console.log(err) }) }, // 详情回列表,更新状态 backAndRefreshIssueList () { if (this.data.selectedIssueId) { const para = { pageIndex: 1, pageSize: 10, timestamp: this.data.timestamp, searchContent: '', orderType: '0', firstCategoryCode: '', issueId: this.data.selectedIssueId } getIssueList(para).then(res => { console.log('详情回列表,筛选刷新', res) if (res.data.length === 1) { this.data.issueList.forEach((item,index) => { if (item.id === this.data.selectedIssueId) { this.data.issueList.splice(index, 1, res.data[0]) } }) } else { const index = this.data.issueList.findIndex(item => item.id === this.data.selectedIssueId) this.data.issueList.splice(index, 1) } this.setData({ issueList: this.data.issueList }) }).finally(() => { this.data.selectedIssueId = '' }) } }, // 跳转详情 navigateToIssueDetail (e) { this.data.selectedIssueId = e.detail.issueId wx.navigateTo({ url: `/subpages/discussion/pages/discussionDetail/discussionDetail?type=issue&detailId=${e.detail.issueId}` }) }, // 下拉刷新 获取 项目列表接口 getProjectList () { this.setData({ timestamp: formatTimestamp(), pageNo: 1, preloadVisible: true, loadMoreVisible: false, projectList: [], }) const para = { pageIndex: this.data.pageNo, pageSize: this.data.pageSize, timestamp: this.data.timestamp, searchContent: this.data.searchInputValue, categoryCode: '' } getProjectList(para).then(res => { console.log('获取项目列表', res) this.setData({ projectList: [...res.data], preloadVisible: false, loadMoreType: res.data.length === 10 ? 'loading' : 'none' }) }).catch(err => { this.setData({ projectList: [], preloadVisible: false, loadMoreType: 'none' }) console.log(err) }).finally(() => { this.pullDownRefresh.stopRefresh() }) }, // 上拉加载 项目列表 pulldownGetProjectList () { const para = { pageIndex: this.data.pageNo, pageSize: this.data.pageSize, timestamp: this.data.timestamp, searchContent: this.data.searchInputValue, categoryCode: '' } 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}` }) }, // 下拉刷新 searchPullDownRefresh () { if (this.data.searchInputValue) { if (this.data.type === 'issue') { this.getIssueList() } else if (this.data.type === 'project') { this.getProjectList() } } else { this.pullDownRefresh.stopRefresh() this.setData({ issueList: [], projectList: [] }) } }, // 返回刷新项目列表 backAndRefreshProjectList () { if (this.data.selectedProjectId) { const para = { pageIndex: 1, pageSize: 10, timestamp: this.data.timestamp, searchContent: '', firstCategoryCode: '', itemId: this.data.selectedProjectId } 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 = '' }) } }, // input 框 双向绑定 bindSearchInput (e) { this.setData({ searchInputValue: e.detail.value.replace(/[`~!@#$%^&*()_+=<>?:"{}|·~!@#¥%……&*()——+={}|《》?:“”【】、;‘’,。、]/g, '') }) }, debounceSearch () { clearTimeout(this.data.searchTimer) this.data.searchTimer = setTimeout(() => { this.search() }, 500) }, search () { if (this.data.searchInputValue) { if (this.data.type === 'issue') { this.getIssueList() } else if (this.data.type === 'project') { this.getProjectList() } } else { this.setData({ issueList: [], projectList: [] }) } } })