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.
git/scr/lt2/pages/answer/answer.js

297 lines
7.1 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.

//answer.js
let dianzan=false
var app = getApp()
Page({
data: {
motto: '知乎--微信小程序版',
userInfo: {},
feed: [],
//修改
pinglun:[], //评论数组
content:'',
detail: '',
dianzanUrl: "../../images/heart2.png",
comment_num:0
},
//事件处理函数
// toQuestion: function() {
// wx.navigateTo({
// url: '../question/question'
// })
// },
onLoad: function (options) {
var aid=options.aid
console.log(options);
console.log(options.aid);
console.log('onLoad')
wx.setStorageSync('aid', aid)
//修改
wx.cloud.database().collection("tiezi")
.doc(aid)
.get()
.then(res => {
console.log("详情页成功", res);
//将收藏添加到数据库
//shoucang = res.data.shoucang
//dianzan = res.data.dianzan
//console.log(shoucang, dianzan);
//再次显示数据
this.setData({
detail: res.data,
pinglun:res.data.pinglun
})
})
.catch(err => {
console.log("详情页失败", err);
})
var getId = options.aid;
// 让接收到的id值传递到data:{}里面
this.setData({
currentId: getId
});
// 读取所有的文章列表点赞缓存状态
var cache = wx.getStorageSync('cache_key');
// 如果缓存状态存在
if (cache) {
// 拿到所有缓存状态中的1个
var currentCache = cache[getId];
// 把拿到的缓存状态中的1个赋值给data中的collection如果当前文章没有缓存状态currentCache 的值就是 false如果当前文章的缓存存在那么 currentCache 就是有值的,有值的说明 currentCache 的值是 true
this.setData({
collection: currentCache
})
} else {
// 如果所有的缓存状态都不存在 就让不存在的缓存存在
var cache = {};
// 既然所有的缓存都不存在,那么当前这个文章点赞的缓存也不存在,我们可以把当前这个文章点赞的缓存值设置为 false
cache[getId] = false;
// 把设置的当前文章点赞放在整体的缓存中
wx.setStorageSync('cache_key',cache);
}
},
// 点击图片的点赞事件 这里使用的是同步的方式
toCollect: function(options) {
let aid= wx.getStorageSync ("aid")
console.log(aid);
// 获取所有的缓存
var cache = wx.getStorageSync('cache_key');
// 获取当前文章是否被点赞的缓存
var currentCache = cache[this.data.currentId];
// 取反,点赞的变成未点赞 未点赞的变成点赞
currentCache = !currentCache;
// 更新cache中的对应的1个的缓存值使其等于当前取反的缓存值
cache[this.data.currentId] = currentCache;
// 重新设置缓存
wx.setStorageSync('cache_key',cache);
// 更新数据绑定,从而切换图片
this.setData({
// collection 默认的是 false
collection: currentCache
});
// 交互反馈
wx.showToast({
title: currentCache?'点赞':'取消',
icon: 'success',
duration: 2000
});
if(currentCache==true){
wx.cloud.database().collection("tiezi")
.doc(aid)
.update({
data: {
// 前面为数据库字段,后面为修改之后的值
good_num: wx.cloud.database().command.inc(1),
}
})
.then(res => {
console.log("添加评论成功", res);
return res
})
.catch(err => {
console.log("添加评论失败", err);
return err
})
}
else{
wx.cloud.database().collection("tiezi")
.doc(aid)
.update({
data: {
// 前面为数据库字段,后面为修改之后的值
good_num: wx.cloud.database().command.inc(-1),
}
})
.then(res => {
console.log("成功", res);
return res
})
.catch(err => {
console.log("失败", err);
return err
})
}
},
/*
onLoad: function (options) {
},*/
tapName: function(event){
console.log(event)
},
//获取输入的值
getContent(event){
this.setData({
content:event.detail.value
})
},
//发表评论
fabiao(options){
let content=this.data.content
if(content.length<4){
wx.showLoading({
icon:"none",
title: '评论太短了',
})
return
}
let pinglunItem={}
pinglunItem.nickName=wx.getStorageSync ("nickName")
pinglunItem.avatarUrl=wx.getStorageSync ("avatarUrl")
pinglunItem.content=content
let pinglunArr=this.data.pinglun
pinglunArr.push(pinglunItem)
let l= pinglunArr.length
console.log("评论数",l)
console.log("添加后的评论数组",pinglunArr)
wx.showLoading({
title: '发表中',
})
wx.cloud.init({
env:"cloud1-8g5wmepxce8a3b8a",
})
let aid= wx.getStorageSync ("aid")
console.log(aid);
//异步操作
wx.cloud.database().collection("tiezi")
.doc(aid)
.update({
data: {
// 前面为数据库字段,后面为修改之后的值
pinglun: pinglunArr,
comment_num:pinglunArr.length
}
})
.then(res => {
console.log("添加评论成功", res);
return res
})
.catch(err => {
console.log("添加评论失败", err);
return err
})
let that = this;
wx.hideLoading({
success: (res) => {
that.setData({
pinglun:pinglunArr,
content:''
})
wx.showToast({
title: '发布成功',
})}})
},
//点赞切换
clickDianzan() {
this.setData({
dianzanUrl: dianzan ? "../../images/heart2.png" : "../../images/heart1.png"
})
dianzan = !dianzan
wx.setStorageSync ("dianzan",dianzan)
console.log("改变点赞状态", dianzan)
},
amend: function(e) {
let aid= wx.getStorageSync ("aid")
console.log(aid);
wx.showModal({
title: '编辑',
content: '确定要修改已发布的信息吗?',
success(res) {
if(res.confirm) {
wx.redirectTo({
url: '../change/change?aid='+aid,
})
console.log('用户点击了确定')
}else if(res.cancel) {
console.log('用户点击了取消')
}
}
})
},
delete(event){
let aid= wx.getStorageSync ("aid")
console.log(aid);
wx.cloud.database().collection('tiezi')
.doc(aid)
.remove()
.then(res => {
console.log(res);
}).catch(res => {
console.log(res);
})
wx.switchTab({
url: '../index/index'
})
},
delete_p: function(event) {
/*wx.navigateTo({
url: '../answer/answer'
})*/
let aid= wx.getStorageSync ("aid")
console.log(aid);
var aidc=event.currentTarget.dataset.aidc;
console.log(aidc)
//console.log("1")
wx.cloud.database().collection('tiezi')
.where({
'_id':aid,
'pinglun.content':aidc
})
.update({
data:{
pinglun:wx.cloud.database().command.pull({
content:aidc
}),
comment_num:wx.cloud.database().command.inc(-1)
}
})
.then(res => {
console.log(res);
console.log(res);
wx.showLoading({
title: '删除成功...',
})
wx.redirectTo({
url: '../answer/answer?aid='+aid,
})
wx.hideLoading()
}).catch(res => {
console.log(res);
})
}
})