阿里云 云函数
'use strict';
exports.main = async (event, context) => {
const db = uniCloud.database();
const dbCmd = db.command;
const collection = db.collection('dataList');
const $ = dbCmd.aggregate
if (!event.id) {
return false
}
let dats = {};
let status = dbCmd.eq(2);
if (event.isMe) {
status = dbCmd.gte(-2);
} else {
status = dbCmd.gte(1);
}
let skip = event.skip*1;
if(!skip){
skip = 0
}
if (event.lookup) { //连表查询,添加like数据
if (event.type == 'dynamic' || event.type == 'dynamic2') {
dats = {
uid: event.id,
status: status
}
if (event.sortId) {
if (event.sortId > 0) {
dats = {
uid: event.id,
status: status,
sortId: event.sortId
}
}
}
} else {
dats = {
uid: event.id,
status: dbCmd.eq(2)
}
}
let res = await collection.aggregate()
.sort({
"_id": -1
})
.match(dats)
.skip(skip)
.limit(15)
.lookup({
from: 'user-like',
let: {
m_id: '$_id'
},
pipeline: $.pipeline()
.match(dbCmd.expr($.and([
$.eq(['$mid', '$$m_id']),
$.eq(['$uid', event.id])
])))
.project({
_id: 1,
uid: 1,
mid: 1
})
.done(),
as: 'like'
})
.end()
dats = null;
skip = null;
status=null;
return res.data;
} else {
if (event.type == 'dynamic' || event.type == 'dynamic2') {
dats = {
uid: event.id,
status: status
}
if (event.sortId) {
if (event.sortId > 0) {
dats = {
uid: event.id,
status: status,
sortId: event.sortId
}
}
}
} else {
dats = {
uid: event.id,
status: dbCmd.eq(2)
}
}
let res = await collection.where(dats)
.skip(event.skip)
.limit(15)
.orderBy("time", "desc")
.get();
return res.data
}
};