const api = require('../../../../utils/home') var api_getResidentConfig = require('../../../../utils/api.js') import { getTimestamp } from '../../../../utils/common' import util from '../../../../utils/util' const app = getApp() Page({ data: { detailId: '', supportStatus: 'support', dispportStatus: 'dispport', noticeObj: {}, infoCompleted: 0, noticeObjContent: '', residentValue:0,//配置时间 }, onLoad(options) { this.setData({ detailId: options.id, infoCompleted: app.globalData.infoCompleted }) this.getNewsDetail(options.id) this.browseNews(options.id) // 获取默认配置 this.getResidentConfig() }, getNewsDetail(id) { let that = this api.getNewsDetail(id).then(res => { that.setData({ noticeObj: res.data, noticeObjContent: util.formatRichText(res.data.newsContent) }) if (that.data.noticeObj.likeFlag == '1') { that.setData({ supportStatus: 'supported' }) } if (that.data.noticeObj.unLikeFlag == '1') { that.setData({ dispportStatus: 'dispported' }) } }).catch(err => { console.log(err) }) }, browseNews(id) { let para = { newsId: id } api.browseNews(para).then(res => { }).catch(err => { console.log(err) }) }, // 检查 是否完善信息 verifyCompleteInfo() { if (this.data.infoCompleted == 0) { this.setData({ completeInfoDialogVisible: !this.data.completeInfoDialogVisible }) return true } else { return false } }, newsPosition(e) { // newsPosition (attitude, bySelf = false) { if (this.verifyCompleteInfo()) { return false } console.log(e) let attitude = e.currentTarget.dataset.state, bySelf = e.currentTarget.dataset.byself if (bySelf == 'true') { let attitudeState = '' if (attitude === '0') { if (this.data.dispportStatus === 'dispported') { // this.dispportStatus = 'dispport' // this.newsObj.unlikeNumber -= 1 this.setData({ dispportStatus: 'dispport', ['noticeObj.unlikeNumber']: this.data.noticeObj.unlikeNumber - 1 }) } if (this.data.supportStatus === 'support') { attitudeState = '0' // this.supportStatus = 'supported' // this.newsObj.likeNumber += 1 this.setData({ supportStatus: 'supported', ['noticeObj.likeNumber']: this.data.noticeObj.likeNumber + 1 }) } else if (this.data.supportStatus === 'supported') { attitudeState = '2' // this.supportStatus = 'support' // this.newsObj.likeNumber -= 1 this.setData({ supportStatus: 'support', ['noticeObj.likeNumber']: this.data.noticeObj.likeNumber - 1 }) } } else if (attitude === '1') { if (this.data.supportStatus === 'supported') { // this.supportStatus = 'support' // this.newsObj.likeNumber -= 1 this.setData({ supportStatus: 'support', ['noticeObj.likeNumber']: this.data.noticeObj.likeNumber - 1 }) } if (this.data.dispportStatus === 'dispport') { attitudeState = '1' // this.dispportStatus = 'dispported' // this.newsObj.unlikeNumber += 1 this.setData({ dispportStatus: 'dispported', ['noticeObj.unlikeNumber']: this.data.noticeObj.unlikeNumber + 1 }) } else if (this.data.dispportStatus === 'dispported') { attitudeState = '3' // this.dispportStatus = 'dispport' // this.newsObj.unlikeNumber -= 1 this.setData({ dispportStatus: 'dispport', ['noticeObj.unlikeNumber']: this.data.noticeObj.unlikeNumber - 1 }) } } const para = { newsId: this.data.detailId, attitude: attitudeState } console.log(attitudeState) if (attitudeState == 0) { //表态 0赞;1踩;2取消赞;3取消踩 api.newsSupport(para).then(res => { }).catch(err => { console.log(err) }) } else if (attitudeState == 1) { api.newsOpposition(para).then(res => { }).catch(err => { console.log(err) }) } else { // const res = await newsPosition(para) // console.log('新闻表态', res) api.newsPosition(para).then(res => { }).catch(err => { console.log(err) }) } // const res = await newsPosition(para) // console.log('新闻表态', res) // api.newsPosition(para).then(res => { // }).catch(err => { // console.log(err) // }) } }, // 获取默认配置 阅读时间 getResidentConfig(){ let that = this api_getResidentConfig.getResidentConfig().then(res => { if (res.code === 0 && res.msg === 'success') { res.data.forEach(item => { if (item.residentType === 'poins_config') { that.setData({ residentValue:parseInt(item.residentValue) }) } }) that.countDown() } }).catch(err => { console.log(err) }) }, // 倒计时 countDown() { this.data.timer = setInterval(() => { console.log('加积分倒计时'+this.data.residentValue) if (this.data.residentValue > 1) { this.data.residentValue-- this.setData({ residentValue: this.data.residentValue }) } else { this.browsePoints() clearInterval(this.data.timer) } }, 1000) }, // 加积分 browsePoints(){ let that = this const para = { newsId: that.data.detailId } api.browsePoints(para).then(res => { console.log('加积分成功',res) }).catch(err => { console.log(err) }) }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { clearInterval(this.data.timer) }, })