- follow/cms-vote/cms-articles-like/cms-articles-collect 云对象写操作强制 checkToken,uid 以服务端令牌为准,杜绝客户端伪造身份 - user-info.getIP 仅允许查询本人登录 IP(get_user_info 公开资料保持不变) - ext-storage-co 上传凭证需登录,删除文件仅限 admin 角色(原为完全裸奔) - cms-vote/cms-vote-info/user_select_vote 三表 schema 写权限全部关闭(投票读写均走云对象) - 修复 follow.reFollow 中 total<0 永假导致的逻辑错误 - 各云对象 package.json 补 uni-id-common 依赖声明 - main.js 登录过期跳转修正为实际页面路径 /pages/login/login
88 lines
2.1 KiB
JavaScript
88 lines
2.1 KiB
JavaScript
const db = uniCloud.database();
|
|
const _ = db.command;
|
|
const uniID = require('uni-id-common');
|
|
|
|
module.exports = {
|
|
_before: function () { // 通用预处理器
|
|
this.uniID = uniID.createInstance({
|
|
context: this.getClientInfo()
|
|
});
|
|
},
|
|
|
|
async collectArticle(query) {
|
|
const payload = await this.uniID.checkToken(this.getUniIdToken());
|
|
if (payload.errCode) throw new Error('登录状态失效,请重新登录');
|
|
let user_id = payload.uid; // 以服务端令牌为准,忽略客户端传入
|
|
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 {
|
|
const payload = await this.uniID.checkToken(this.getUniIdToken());
|
|
if (payload.errCode) throw new Error('登录状态失效,请重新登录');
|
|
let user_id = payload.uid; // 以服务端令牌为准,忽略客户端传入
|
|
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
|
|
};
|
|
}
|
|
},
|
|
}
|