const app = getApp() var innerAudioContext = wx.createInnerAudioContext() Page({ data: { actions: [{ className: 'dialog-content' }], dataList: [], isShowCommentInput: false, //是否显示评论等 collectSwitch: '1', //收藏的开关默认关闭 likeSwitch: '1', //点赞的开关 commentSwitch: '1', //评论的开关 shareSwitch: '1', //转发的开关 speedStep: 5, //快进快退秒数 waitFlag: false, avatarUrl: app.globalData.userInfo.avatarUrl, imgUrl: app.urls.baseImgUrl, uId: '', columnId: '', publishTime: '', isShowComment: false, //是否显示评论输入框 maxVideo: 1, //评论最大可以上传的视频数量 maxPhoto: 1, //评论最大可以上传的图片数量 commentValue: '', //评论输入的内容 mediaBean: null, //评论图片或视频bean {id:'',type:'video or image',coverPath:'图片路径'} uploadCount: 0, //上传图片的次数 uploadVideoCount: 0, //上传视频的次数 maxDuration: 60, //上传视频时长限制 dataId: '', //当前评论Id commentList: [], //评论列表 commentCount: 0, currentPage: 1, //当前评论页数 curUserId: app.globalData.currentUserId, isShowRestore: false, restoreList: [], currentCommentItem: undefined, //当前评论条目 currentType: 2, //当前评论类型 restorePage: 1, //当前回复的页数 restoreCount: 0, likeStatus: false, //点赞 collectStatus: false, //收藏, restoreLikeStatus: false, //评论点赞 contentHeight: app.globalData.windowHeight }, onLoad(options) { var _self = this this.setData({ uId: options.uId, columnId: options.cId, dataId: options.uId, collectSwitch: options.collectSwitch, //收藏的开关 likeSwitch: options.likeSwitch, //点赞的开关 commentSwitch: options.commentSwitch, //评论的开关 shareSwitch: options.shareSwitch, //转发的开关 }) var isShow = options.collectSwitch == '3' || options.collectSwitch == '4' || options.likeSwitch == '3' && options.likeSwitch == '4' || options.shareSwitch == '3' || options.shareSwitch == '4' || options.commentSwitch == '3' || options.commentSwitch == '4' if (isShow) { _self.setData({ isShowCommentInput: true }) } else { _self.setData({ isShowCommentInput: false }) } _self.getMainMoment() _self.getCommentList() _self.getColumnContentHandle(this.data.uId, 2) }, // 获取栏目内容 getMainMoment() { wx.showLoading({ title: '加载中...', }) var _self = this app.http.get(app.urls.getMomentsContent.format({ configColumnId: _self.data.columnId, uid: _self.data.uId }), { header: { token: app.globalData.token } }) .then(res => { wx.hideLoading({}) //构建数据 _self.buildDetail(res.data) }) .catch(err => { wx.hideLoading({}) console.log(err) }) }, //获取栏目内容点赞 收藏 评论 getColumnContentHandle(ids, type) { var _self = this wx.showLoading({ title: '加载中...', }) app.http.get(app.urls.getColumnContentHandle.format({ projectId: 'column', businessIds: ids }), { header: { token: app.globalData.token } }) .then(res => { wx.hideLoading({}) if (res.data.length > 0) { if (type == 2) { //栏目 _self.setData({ likeStatus: res.data[0].likeStatus, collectStatus: res.data[0].collectStatus }) } else { if (_self.data.currentType == 2) { //评论 _self.data.commentList.forEach(it => { res.data.forEach(item => { if (it.contentCommentId == item.businessId) { it.likeStatus = item.likeStatus it.contentCommentLike = item.likeCount it.contentCommentSubCount = item.commentCount } }) }) _self.setData({ commentList: _self.data.commentList }) } else { //回复 _self.data.restoreList.forEach(it => { res.data.forEach(item => { if (it.contentCommentId == item.businessId) { it.likeStatus = item.likeStatus it.contentCommentLike = item.likeCount it.contentCommentSubCount = item.commentCount } }) }) _self.setData({ restoreList: _self.data.restoreList }) } } } }) .catch(err => { }) }, //构建展示数据 buildDetail(data) { var _self = this _self.setData({ publishTime: data.gmtCreate }) data.list.forEach(it => { switch (it.dataType) { case '2': //图片 var photoIds = it.value.split('@') if (photoIds.length > 1) { //展示方式 it.dataMode = photoIds[1] } else { it.dataMode = '1' } var items = photoIds[0].split(',') var tempList = [] items.forEach(item => { if (item.length > 0) { var tempItem = { id: item, path: app.urls.baseImgUrl + item, } tempList.push(tempItem) } }) it.valueList = tempList break case '4': //视频 var items = it.value.split(',') var tempList = [] for (var i = 0; i < items.length; i++) { if (items[i].length > 0) { var ss = items[i].split('&') var videoUrl = app.urls.baseImgUrl + ss[0] var imgUrl = app.urls.baseImgUrl + ss[1] var tempItem = { path: videoUrl, imgPath: imgUrl } tempList.push(tempItem) } } it.valueList = tempList break case '3': //音频 var items = it.value.split(',') var tempList = [] items.forEach(item => { if (item.length > 0) { var ss = item.split('&') var totalStr = '00:00' if (ss.length > 1) { var dur = ss[1] var tM = parseInt(dur / 60) //分钟 var tS = parseInt(dur % 60) //秒 var totalMStr = tM > 9 ? tM : '0' + tM var totalSStr = +tS > 9 ? tS : '0' + tS totalStr = totalMStr + ':' + totalSStr } var tempItem = { id: ss[0], //id path: app.urls.baseImgUrl + ss[0], //地址 isPlay: false, //是否播放中 duration: 100, //时长 curDuation: 0, curDurationStr: '00:00', totalDurationStr: totalStr } tempList.push(tempItem) } }) it.valueList = tempList break case '6': //定位 var items = it.value.split('-') var tempList = [] items.forEach(item => { if (item.length > 0) { var latlng = item.split(',') var tempItem = { name: '', address: '', latitude: latlng[0], longitude: latlng[1], } tempList.push(tempItem) } }) it.valueList = tempList break case '8': //单选 it.dictionariesList.forEach(ss => { if (it.value.indexOf(ss.dataId) != -1) { it.selValue = ss.dataName } }) break case '9': var selValue = '' it.dictionariesList.forEach(ss => { if (it.value.indexOf(ss.dataId) != -1) { selValue += ss.dataName + ',' ss.isSel = true } else { ss.isSel = false } }) selValue = selValue.substr(0, selValue.length - 1) it.selValue = selValue break } }) _self.setData({ dataList: data.list }) }, //获取评论列表 getCommentList() { var _self = this app.http.get(app.urls.getCommentList.format({ projectId: 'column', businessId: _self.data.dataId }), { header: { token: app.globalData.token }, data: { page: _self.data.currentPage } }) .then(res => { wx.hideLoading({}) var ids = '' res.data.rows.forEach(it => { it.likeStatus = false ids += it.contentCommentId + ',' }) _self.data.commentList = _self.data.commentList.concat(res.data.rows) _self.setData({ commentList: _self.data.commentList, commentCount: res.data.total }) if (ids.length > 0) { _self.getColumnContentHandle(ids, 3) } }) .catch(err => { console.log(res) }) }, //获取评论的回复 getRestoreList() { var _self = this app.http.get(app.urls.getCommentList.format({ projectId: 'column', businessId: _self.data.dataId }), { header: { token: app.globalData.token }, data: { page: _self.data.restorePage } }) .then(res => { wx.hideLoading({}) var ids = '' res.data.rows.forEach(it => { it.likeStatus = false ids += it.contentCommentId + ',' }) _self.data.restoreList = _self.data.restoreList.concat(res.data.rows) _self.setData({ restoreList: _self.data.restoreList, restoreCount: res.data.total, isShowRestore: true }) if (ids.length > 0) { _self.getColumnContentHandle(ids, 3) } }) .catch(err => { console.log(res) }) }, //预览图片 viewImg(e) { wx.previewImage({ urls: [e.currentTarget.dataset.url], }) }, //预览视频 viewVideo(e) { var url = e.currentTarget.dataset.url wx.previewMedia({ sources: [{ url: url, type: 'video' }], }) }, play(e) { //进行播放 const _self = this const index = e.currentTarget.dataset.index const ix = e.currentTarget.dataset.i const a = e.currentTarget.dataset.a var audio = e.currentTarget.dataset.item //先需要停止其他录音文件的播放 _self.data.dataList.forEach(it => { it.list.forEach(item => { if (item.dataType == '3') { if (item.valueList) { item.valueList.forEach(ii => { if (ii.isPlay) { innerAudioContext.stop() } ii.isPlay = false ii.curDuration = 0 ii.curDuationStr = '00:00' }) } } }) }) if (!audio.isPlay) { innerAudioContext = wx.createInnerAudioContext() innerAudioContext.src = _self.data.dataList[index].list[ix].valueList[a].path + '.mp3' innerAudioContext.autoplay = true console.log(_self.data.dataList[index].list[ix].valueList[a].path) } else { innerAudioContext.stop() } innerAudioContext.onPlay(() => { console.log('onPlay') _self.data.dataList[index].list[ix].valueList[a]['isPlay'] = true _self.setData({ dataList: _self.data.dataList }) }); innerAudioContext.onCanplay((res) => { // if (_self.data.waitFlag) { // innerAudioContext.play() // _self.setData({ // waitFlag: false // }) // } }) innerAudioContext.onStop((res) => { console.log('停止播放') _self.data.dataList[index].list[ix].valueList[a].isPlay = false _self.data.dataList[index].list[ix].valueList[a].curDuration = 0 _self.data.dataList[index].list[ix].valueList[a].curDurationStr = '00:00' _self.setData({ dataList: _self.data.dataList, }) innerAudioContext.destroy() }) innerAudioContext.onEnded((res) => { console.log(res) console.log('播放完毕') _self.data.dataList[index].list[ix].valueList[a].isPlay = false _self.data.dataList[index].list[ix].valueList[a].curDuration = 0 _self.data.dataList[index].list[ix].valueList[a].curDurationStr = '00:00' _self.setData({ dataList: _self.data.dataList, }) innerAudioContext.destroy() }) innerAudioContext.onError((res) => { console.log('播放错误') console.log(res) _self.data.dataList[index].list[ix].valueList[a].isPlay = false _self.data.dataList[index].list[ix].valueList[a].curDuration = 0 _self.data.dataList[index].list[ix].valueList[a].curDurationStr = '00:00' _self.setData({ dataList: _self.data.dataList, }) innerAudioContext.destroy() }) innerAudioContext.onSeeking(() => { // console.log(innerAudioContext.currentTime) }) innerAudioContext.onWaiting(() => { // console.log('onWaiting') // innerAudioContext.pause() // _self.setData({ // waitFlag: true // }) }) innerAudioContext.onTimeUpdate(() => { if (innerAudioContext.duration != Infinity) { _self.data.dataList[index].list[ix].valueList[a].curDuration = parseInt(innerAudioContext.currentTime) _self.data.dataList[index].list[ix].valueList[a].duration = parseInt(innerAudioContext.duration) var curM = parseInt(innerAudioContext.currentTime / 60) //分钟 var curS = parseInt(innerAudioContext.currentTime % 60) //秒 var tM = parseInt(innerAudioContext.duration / 60) //分钟 var tS = parseInt(innerAudioContext.duration % 60) //秒 var curMStr = curM > 9 ? curM : '0' + curM var curSStr = curS > 9 ? curS : '0' + curS var totalMStr = tM > 9 ? tM : '0' + tM var totalSStr = +tS > 9 ? tS : '0' + tS _self.data.dataList[index].list[ix].valueList[a].curDurationStr = curMStr + ':' + curSStr _self.data.dataList[index].list[ix].valueList[a].totalDurationStr = totalMStr + ':' + totalSStr _self.setData({ dataList: _self.data.dataList }) } }) }, //倒退5秒 rewind(e) { var _self = this var index = e.currentTarget.dataset.index var item = e.currentTarget.dataset.item var idx = e.currentTarget.dataset.idx var a = e.currentTarget.dataset.a var ix = e.currentTarget.dataset.i if (item.isPlay) { _self.data.dataList[index].list[ix].valueList[a].curDuration = _self.data.dataList[index].list[ix].valueList[a].curDuration - _self.data.speedStep innerAudioContext.seek(_self.data.dataList[index].list[ix].valueList[a].curDuration) _self.setData({ dataList: _self.data.dataList }) } }, //快进5秒 speed(e) { var _self = this var index = e.currentTarget.dataset.index var item = e.currentTarget.dataset.item var idx = e.currentTarget.dataset.idx var a = e.currentTarget.dataset.a var ix = e.currentTarget.dataset.i if (item.isPlay) { _self.data.dataList[index].list[ix].valueList[a].curDuration = _self.data.dataList[index].list[ix].valueList[a].curDuration + _self.data.speedStep innerAudioContext.seek(_self.data.dataList[index].list[ix].valueList[a].curDuration) _self.setData({ dataList: _self.data.dataList }) } }, slider4change(e) { var index = e.currentTarget.dataset.index var item = e.currentTarget.dataset.item if (item.isPlay) { innerAudioContext.seek(e.detail.value) } }, // 转发记录 shareRecord: function (cardTemplateDispatchId) { var self = this app.http.post(app.urls.shareRecord, { header: { token: app.globalData.token }, data: { cardTemplateUseId: self.data.cardInfo.cardTemplateUseId, cardTemplateDispatchParentId: '', cardTemplateDispatchId: cardTemplateDispatchId } }).then(res => { console.log(res.data) self.setData({ shareRecordId: res.data.data }) }) }, /** * 用户点击右上角分享 */ // onShareAppMessage: function (res) { // console.log(res) // var self = this // wx.showShareMenu({ // withShareTicket: true, // success: function (res) { // console.log(res) // } // }) // if (res.from == 'button') { // var postId = self.buildId() // self.shareRecord(postId) // var param = '/pages/sharePage/sharePage?cardTemplateDispatchId=' + postId; // return { // title: '名片分享名片分享名片分享名片分享名片分享名片分享名片分享名片分享名片分享名片分享名片分享名片分享名片分享', // path: param, // imageUrl: self.data.shareImgUrl // } // // return返回 title 转发标题 path 路径 imageUrl 自定义图片,可以本地路径 代码包文件路径 支持png jpg 显示长宽比5:4 // } // }, inputWatch(e) { this.setData({ commentValue: e.detail.value }) }, delMedia() { var _self = this wx.showModal({ title: '警告', content: '确定要删除该数据吗?', success(res) { if (res.confirm) { _self.setData({ mediaBean: null }) } } }) }, //删除评论 delComment(e) { var _self = this var id = e.currentTarget.dataset.id var index = e.currentTarget.dataset.idx wx.showModal({ title: '提示', content: '确定要删除该评论吗?', success(res) { if (res.confirm) { //删除评论,刷新列表 _self.doDelComment(id, index) } } }) }, //删除评论或回复 doDelComment(id, index) { var _self = this wx.showLoading({ title: '删除中...', }) app.http.delete(app.urls.doDelComment.format({ ids: id }), { header: { token: app.globalData.token } }) .then(res => { wx.hideLoading({}) wx.showToast({ title: '删除成功', }) //删除成功 刷新列表 if (_self.data.currentType == 2) { //评论 _self.data.commentList.splice(index, 1) _self.setData({ commentList: _self.data.commentList }) } else { //回复 _self.data.restoreList.splice(index, 1) _self.data.commentList.forEach(it => { if (it.contentCommentId == _self.data.dataId) { it.contentCommentSubCount-- } }) _self.setData({ restoreList: _self.data.restoreList, commentList: _self.data.commentList }) } }) .catch(err => { }) }, //评论的评论 doCommentToComment(e) { var _self = this var id = e.currentTarget.dataset.id var item = e.currentTarget.dataset.item _self.setData({ dataId: id, currentPage: 1, restoreList: [], currentCommentItem: item, currentType: 3 }) _self.getRestoreList() }, //显示评论输入框 showComment() { this.setData({ isShowComment: true }) }, //关闭评论输入框 onCloseDialog(e) { this.setData({ isShowComment: false }) }, onCloseRestore(e) { var _self = this _self.setData({ isShowRestore: false, currentType: 2, dataId: _self.data.uId, restoreList: [], currentCommentItem: undefined }) }, //选择图片 chooseImg() { var _self = this wx.chooseImage({ count: _self.data.maxPhoto, sourceType: ['album', 'camera'], success: (res) => { if (res.tempFilePaths.length > 0) { _self.doUploadImg(res.tempFilePaths) } }, fail: (err) => { } }) }, doUploadImg(paths) { wx.showLoading({ title: '上传中...', }) let _self = this app.http.upload(app.urls.doUploadImg, { path: paths[0], name: 'image', header: { token: app.globalData.token } }) .then(res => { wx.hideLoading({}) //插入到集合中 var id = JSON.parse(res).data var pathStr = app.urls.baseImgUrl + id var item = { id: id, type: 'image', coverPath: pathStr, path: pathStr } _self.setData({ mediaBean: item }) }) .catch(err => { wx.hideLoading({}) _self.setData({ mediaBean: null }) }) }, //选择视频 chooseVideo() { var _self = this wx.chooseMedia({ count: _self.data.maxVideo, mediaType: ['video'], sourceType: ['camera', 'album'], maxDuration: 60, success: (res) => { console.log(res) if (res.tempFiles.length > 0) { if (res.tempFiles[0].duration <= _self.data.maxDuration) { _self.doUploadVideo(res.tempFiles) } else { wx.showToast({ title: '视频最长60秒', icon: 'none' }) } } }, fail: (err) => {} }) }, //上传视频 doUploadVideo(paths) { wx.showLoading({ title: '上传中...', }) let _self = this app.http.upload(app.urls.doUploadVideo, { path: paths[0].tempFilePath, name: 'video', header: { token: app.globalData.token } }) .then(res => { wx.hideLoading({}) var id = JSON.parse(res).data var pathStr = app.urls.baseImgUrl + id var item = { id: id, type: 'video', coverPath: paths[0].thumbTempFilePath, path: pathStr } _self.setData({ mediaBean: item }) }) .catch(err => { wx.hideLoading({}) _self.setData({ mediaBean: null }) }) }, //校验评论参数 doSubmitComment(e) { var _self = this console.log(e) var id = e.currentTarget.dataset.id if (_self.data.commentValue.length > 0) { _self.doSaveComment(id) } else { wx.showToast({ title: '请输入要发布的内容', icon: 'none' }) } }, //保存评论 doSaveComment(id) { var _self = this wx.showLoading({ title: '保存中...', }) app.http.post(app.urls.doSaveComment, { header: { token: app.globalData.token }, data: { businessId: id, projectId: 'column', contentCommentContent: _self.data.commentValue } }) .then(res => { wx.hideLoading({}) wx.showToast({ title: '评论成功', }) if (_self.data.currentType == 2) { _self.setData({ commentValue: '', isShowComment: false, currentPage: 1, //重置评论列表 commentList: [], commentCount: 0 }) //刷新列表 _self.getCommentList() } else { _self.data.commentList.forEach(it => { if (it.contentCommentId == id) { it.contentCommentSubCount++ } }) _self.setData({ commentValue: '', isShowComment: false, restorePage: 1, restoreList: [], restoreCount: 0, commentList: _self.data.commentList }) _self.getRestoreList() } }) .catch(err => { wx.hideLoading({}) _self.setData({ isShowComment: false }) }) }, //点赞 doLike(e) { var _self = this var type = e.currentTarget.dataset.type //用来判断是栏目内容或评论与回复 var id = type == '2' ? _self.data.uId : e.currentTarget.dataset.id var index = -1 if (type == '4') { _self.data.commentList.forEach((item, idx) => { if (item.contentCommentId == id) { index = idx } }) } else { index = e.currentTarget.dataset.idx //索引 } var favor = type == '2' ? _self.data.likeStatus : e.currentTarget.dataset.like //当前的状态 var msg = '' if (favor) { msg = '取消中...' } else { msg = '保存中...' } wx.showLoading({ title: msg, }) app.http.post(app.urls.doSaveLike, { header: { token: app.globalData.token }, data: { businessId: id, projectId: 'column' } }) .then(res => { wx.hideLoading({}) if (type == '2') { //栏目 _self.setData({ likeStatus: !favor }) } else { //评论与回复 if (_self.data.currentType == 2) { //评论 if (favor) { _self.data.commentList[index].contentCommentLike-- _self.data.commentList[index].likeStatus = false } else { _self.data.commentList[index].contentCommentLike++ _self.data.commentList[index].likeStatus = true } _self.setData({ commentList: _self.data.commentList }) } else { if (type == '4') { if (favor) { _self.data.commentList[index].contentCommentLike-- _self.data.commentList[index].likeStatus = false _self.data.currentCommentItem.likeStatus = false } else { _self.data.commentList[index].contentCommentLike++ _self.data.commentList[index].likeStatus = true _self.data.currentCommentItem.likeStatus = true } _self.setData({ commentList: _self.data.commentList, currentCommentItem: _self.data.currentCommentItem }) } else { //回复 if (favor) { _self.data.restoreList[index].contentCommentLike-- _self.data.restoreList[index].likeStatus = false } else { _self.data.restoreList[index].contentCommentLike++ _self.data.restoreList[index].likeStatus = true } _self.setData({ restoreList: _self.data.restoreList }) } } } }) .catch(err => {}) }, //收藏 doCollect(e) { var _self = this var favor = _self.data.collectStatus var msg = '' if (favor) { msg = '取消收藏...' } else { msg = '收藏中...' } wx.showLoading({ title: msg, }) app.http.post(app.urls.doSaveCollect, { header: { token: app.globalData.token }, data: { businessId: _self.data.uId, projectId: 'column' } }) .then(res => { wx.hideLoading({}) _self.data.collectStatus = !_self.data.collectStatus _self.setData({ collectStatus: _self.data.collectStatus }) }) .catch(err => {}) } })