/
homeDetail.js
149 lines (136 loc) · 2.83 KB
/
homeDetail.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var that
const db = wx.cloud.database();
Page({
/**
* 页面的初始数据
*/
data: {
topic: {},
id: '',
openid: '',
isLike: false,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
that = this;
that.data.id = options.id;
that.data.openid = options.openid;
// 获取话题信息
db.collection('topic').doc(that.data.id).get({
success: function(res) {
that.topic = res.data;
that.setData({
topic: that.topic,
})
}
})
// 获取收藏情况
db.collection('collect')
.where({
_openid: that.data.openid,
_id: that.data.id
})
.get({
success: function(res) {
if (res.data.length > 0) {
that.refreshLikeIcon(true)
} else {
that.refreshLikeIcon(false)
}
},
fail: console.error
})
},
onShow: function() {
// 获取回复列表
that.getReplay()
},
getReplay: function() {
// 获取回复列表
db.collection('replay')
.where({
t_id: that.data.id
})
.get({
success: function(res) {
// res.data 包含该记录的数据
console.log(res)
that.setData({
replays: res.data
})
},
fail: console.error
})
},
/**
* 刷新点赞icon
*/
refreshLikeIcon(isLike) {
that.data.isLike = isLike
that.setData({
isLike: isLike,
})
},
// 预览图片
previewImg: function(e) {
//获取当前图片的下标
var index = e.currentTarget.dataset.index;
wx.previewImage({
//当前显示图片
current: this.data.topic.images[index],
//所有图片
urls: this.data.topic.images
})
},
/**
* 喜欢点击
*/
onLikeClick: function(event) {
console.log(that.data.isLike);
if (that.data.isLike) {
// 需要判断是否存在
that.removeFromCollectServer();
} else {
that.saveToCollectServer();
}
},
/**
* 添加到收藏集合中
*/
saveToCollectServer: function(event) {
db.collection('collect').add({
// data 字段表示需新增的 JSON 数据
data: {
_id: that.data.id,
date: new Date(),
},
success: function(res) {
that.refreshLikeIcon(true)
console.log(res)
},
})
},
/**
* 从收藏集合中移除
*/
removeFromCollectServer: function(event) {
db.collection('collect').doc(that.data.id).remove({
success: that.refreshLikeIcon(false),
});
},
/**
* 跳转回复页面
*/
onReplayClick() {
wx.navigateTo({
url: "../replay/replay?id=" + that.data.id + "&openid=" + that.data.openid
})
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function() {
}
})