info
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
const db = uniCloud.database();
|
||||
const _ = db.command;
|
||||
|
||||
module.exports = {
|
||||
_before: function () {
|
||||
|
||||
},
|
||||
|
||||
async likeArticle(query) {
|
||||
let user_id = query.user_id;
|
||||
let article_id = query.article_id;
|
||||
let datetime = new Date().getTime();
|
||||
|
||||
const transaction = await db.startTransaction();
|
||||
try {
|
||||
const exist = await transaction.collection('cms-articles-like')
|
||||
.where({
|
||||
user_id: user_id,
|
||||
article_id: article_id,
|
||||
create_time: datetime
|
||||
}).count();
|
||||
|
||||
if (exist.total > 0) throw new Error('已点喜欢');
|
||||
|
||||
await transaction.collection('cms-articles-like').add({
|
||||
user_id: user_id,
|
||||
article_id: article_id,
|
||||
create_time: datetime
|
||||
|
||||
});
|
||||
|
||||
await transaction.collection('uni-cms-articles').doc(article_id)
|
||||
.update({
|
||||
like_count: _.inc(1)
|
||||
});
|
||||
|
||||
await transaction.commit();
|
||||
return {
|
||||
code: 200
|
||||
};
|
||||
} catch (e) {
|
||||
await transaction.rollback();
|
||||
return {
|
||||
code: 500,
|
||||
msg: e.message
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
async relikeArticle(query) {
|
||||
|
||||
try {
|
||||
let user_id = query.user_id;
|
||||
let article_id = query.article_id;
|
||||
let datetime = new Date().getTime();
|
||||
|
||||
const likeRes = await db.collection('cms-articles-like')
|
||||
.where({
|
||||
user_id: user_id,
|
||||
article_id: article_id,
|
||||
}).remove();
|
||||
|
||||
if (likeRes.deleted === 0) throw new Error('未找到点赞记录');
|
||||
|
||||
await db.collection('uni-cms-articles').doc(article_id)
|
||||
.update({
|
||||
like_count: _.inc(-1)
|
||||
});
|
||||
|
||||
return {
|
||||
code: 200
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
code: 500,
|
||||
msg: e.message
|
||||
};
|
||||
}
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user