Files
t/uniCloud-alipay/cloudfunctions/uni-cms-articles/index.obj.js
T
2025-06-13 20:46:10 +08:00

235 lines
4.0 KiB
JavaScript

const db = uniCloud.database()
const collection = db.collection('uni-cms-articles')
module.exports = {
_before: function () { // 通用预处理器
},
async del_cms_articles(query) {
try {
console.log(query)
let {
id
} = query;
await collection.doc(id).remove();
return {
code: 200,
}
} catch(e) {
return {
code: 500,
msg: e.message
}
}
},
async get_cms_articles_for_userId(query) {
try {
console.log(query)
let {
user_id
} = query;
const res = await collection.where({
user_id: user_id,
"edit_type": "mobile"
}).get();
console.log(res)
return {
code: 200,
data:res.data
}
} catch(e) {
console.error(e.message)
return {
code: 500,
msg: e.message
}
}
},
async update_cms_articles(query) {
try{
let {
id,
title,
user_id,
title_html,
title_delta,
thumbnail,
p_type,
category_id,
temp_id,
edit_type,
excerpt,
view_count,
cmsLst,
last_modify_ip,
article_status
} = query;
let last_modify_date = new Date();
let publish_date = new Date();
const r = await collection.doc(id).update({
title,
user_id,
title_html,
title_delta,
thumbnail,
p_type,
category_id,
temp_id,
edit_type,
excerpt,
view_count,
cmsLst,
publish_date,
last_modify_date,
last_modify_ip,
article_status
});
console.log(r.id)
let res = await collection.doc(r.id)
.get();
return {
code: 200,
res: {
data: res.data[0],
_id: r.id,
user_id: user_id
}
}
} catch(e) {
return {
code:500,
msg: e.message
}
}
},
async add_cms_articles(query) {
try {
let {
title,
user_id,
title_html,
title_delta,
thumbnail,
p_type,
category_id,
temp_id,
edit_type,
excerpt,
view_count,
cmsLst,
last_modify_ip,
article_status
} = query;
let last_modify_date = new Date();
let publish_date = new Date();
const r = await collection.add({
title,
user_id,
title_html,
title_delta,
thumbnail,
p_type,
category_id,
temp_id,
edit_type,
excerpt,
view_count,
cmsLst,
publish_date,
last_modify_date,
last_modify_ip,
article_status
});
console.log(r.id)
let res = await collection.doc(r.id)
.get();
return {
code: 200,
res: {
data: res.data[0],
_id: r.id,
user_id: user_id
}
}
} catch(e) {
return {
code:500,
msg: e.message
}
}
},
async getTemp(query) {
try{
const {
temp_id
} = query;
const res = await db.collection("cms-temp").doc(temp_id).get();
console.log(res)
return {
code: 200,
data: res.data[0]
}
}catch(e) {
return {
code: 500,
msg: e.message
}
}
},
async getTotal() {
try {
const res = await collection.count()
return {
code: 200,
message: 'success',
total: res.total
}
} catch (err) {
return {
code: 500,
message: err.message
}
}
},
// 文章访问日志服务
async detailLog(query) {
let datetime = new Date();
if (!query.userId) {
throw new Error('未登录用户无法记录访问日志');
}
try {
// 查询是否存在该用户对同一文章的访问记录
console.log(query.articleId);
const { data: existingLog } = await db.collection('cms-articles-log')
.where({
article_id: query.articleId
})
.get();
// 存在则更新,否则新增
if (existingLog.length > 0) {
await db.collection('cms-articles-log')
.doc(existingLog[0]._id)
.update({
last_view_time: datetime // 使用服务器时间
});
} else {
await db.collection('cms-articles-log')
.add({
user_id: query.userId,
article_id: query.articleId,
first_view_time: datetime,
last_view_time: datetime
});
}
return { code: 0, msg: '日志记录成功' };
} catch (err) {
console.error('日志记录失败:', err);
return { code: -1, msg: '系统异常,请重试' };
}
}
}