81 lines
1.6 KiB
JavaScript
81 lines
1.6 KiB
JavaScript
const db = uniCloud.database();
|
|
const _ = db.command;
|
|
|
|
module.exports = {
|
|
_before: function () { // 通用预处理器
|
|
|
|
},
|
|
|
|
async collectArticle(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-collect')
|
|
.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-collect').add({
|
|
user_id: user_id,
|
|
article_id: article_id,
|
|
create_time: datetime
|
|
|
|
});
|
|
|
|
await transaction.collection('uni-cms-articles').doc(article_id)
|
|
.update({
|
|
collect_count: _.inc(1)
|
|
});
|
|
|
|
await transaction.commit();
|
|
return {
|
|
code: 200
|
|
};
|
|
} catch (e) {
|
|
await transaction.rollback();
|
|
return {
|
|
code: 500,
|
|
msg: e.message
|
|
};
|
|
}
|
|
},
|
|
|
|
async recollectArticle(query) {
|
|
|
|
try {
|
|
let user_id = query.user_id;
|
|
let article_id = query.article_id;
|
|
|
|
const reclollectRes = await db.collection('cms-articles-collect')
|
|
.where({
|
|
user_id: user_id,
|
|
article_id: article_id
|
|
}).remove();
|
|
|
|
console.log(reclollectRes);
|
|
|
|
if (reclollectRes.deleted === 0) throw new Error('未找到收藏记录');
|
|
|
|
await db.collection('uni-cms-articles').doc(article_id)
|
|
.update({
|
|
collect_count: _.inc(-1)
|
|
});
|
|
|
|
return {
|
|
code: 200
|
|
};
|
|
} catch (e) {
|
|
return {
|
|
code: 500,
|
|
msg: e.message
|
|
};
|
|
}
|
|
},
|
|
}
|