You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
5.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// miniprogram/pages/detail/detail.js
var util = require('../../utils/util.js')
const app = getApp();
const db=wx.cloud.database().collection('user')
const dd=wx.cloud.database()
const db2=wx.cloud.database().collection('community')
Page({
/**
* 页面的初始数据
*/
data: {
main_top:'',
openid:'',
currentdate:'',
canIUse: wx.canIUse('button.open-type.getUserInfo'),
userInfo: {},
hasUserInfo: false,
canIUseGetUserProfile: false,
imgList: [],
head_cut:'',
authorname:'',
content:[],
comment:[],
commentinput:'',
showtop:false
},
formSubmit(e) {
const that= this
var input_content=e.detail.value.com_content
var _time = util.formatTime(new Date())
if(!input_content){
wx.showToast({
title: '请输入评论内容!',
icon:'none'
})
return
}
const _=dd.command
dd.collection('comment').add({
data: {
commentid: that.data.content._id,
username:that.data.userInfo.nickName,
img:that.data.userInfo.avatarUrl,
content:input_content,
date:_time
},
success: function(res) {
// res 是一个对象,其中有 _id 字段标记刚创建的记录的 id
console.log(res)
wx.showToast({
title: '评论成功!',
})
var array=[{
commentid: that.data.content._id,
username:that.data.userInfo.nickName,
img:that.data.userInfo.avatarUrl,
content:input_content,
date:_time
}]
that.setData({
comment:array.concat(that.data.comment),
commentinput:''
})
db2.doc(that.data.content._id).update({
data:{
comment:_.inc(1)
}
}).then()
}
})
},
preview(event) {
let currentUrl = event.currentTarget.dataset.src
const that=this
wx.cloud.getTempFileURL({
fileList:[currentUrl],
success(res){
console.log(res)
const a = `imgList[${0}]`
that.setData({
[a]:res.fileList[0].tempFileURL
})
console.log(that.data.imgList)
wx.previewImage({
current: currentUrl, // 当前显示图片的http链接
urls: that.data.imgList // 需要预览的图片http链接列表
})
}
})
},
getUserProfile(e) {
// 推荐使用wx.getUserProfile获取用户信息开发者每次通过该接口获取用户个人信息均需用户确认
// 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
const that= this
if(that.data.hasUserInfo){
console.log(that.data.userInfo)
}else{
wx.getUserProfile({
desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (res) => {
that.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
wx.setStorageSync('userInfo', that.data.userInfo)
db.add({
data:{
_id:that.data.openid,
nickName:that.data.userInfo.nickName,
avatarUrl:that.data.userInfo.avatarUrl,
city:that.data.userInfo.city,
collect:[],
like:[]
}
})
}
})
}
},
onShow:function(){
const that=this
var _time = util.formatTime(new Date())
that.setData({
currentdate:_time
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
const that= this
if (wx.getUserProfile) {
this.setData({
canIUseGetUserProfile: true
})
}
var openid=wx.getStorageSync('openid')
that.setData({
content:JSON.parse(options.content),
comment:[],
openid:openid
})
db.doc(that.data.content._openid).get({
success:function(res){
console.log(res.data)
that.setData({
head_cut:res.data.avatarUrl,
authorname:res.data.nickName
})
}
})
dd.collection('comment').orderBy("date","desc").where({
commentid:that.data.content._id
}).get({
success: function(res) {
// res.data 是包含以上定义的两条记录的数组
wx.showLoading({
title: '加载中',
mask:'true'
})
that.setData({
comment:res.data,
}, () => {
wx.hideLoading()
})
}
})
var arr=wx.getStorageSync('userInfo')
console.log(arr)
that.setData({
userInfo:arr
})
},
lower(e){
const that=this
var x=that.data.comment.length
let old_data = that.data.comment
wx.showLoading({
title: '加载中...',
mask:'true'
})
var _=dd.command
dd.collection('comment').where({
commentid:that.data.content._id,
date:_.lt(that.data.currentdate)
}).orderBy('date','desc').skip(x) // 限制返回数量为 20 条
.get()
.then(res => {
console.log(res.data)
// 利用concat函数连接新数据与旧数据
if(res.data.length==0){
wx.hideLoading()
wx.showToast({
title: '已经到底了!',
icon:'none'
})
return
}
this.setData({
comment: old_data.concat(res.data),
})
wx.hideLoading({
success: (res) => {},
})
})
.catch(err => {
console.error(err)
})
}
})