提交 13825a23 编写于 作者: M MicroMilo

云对象重构

上级 55a14365
// 本文件中的json内容将在云函数【运行】时作为参数传给云函数。
// 配置教程参考:https://uniapp.dcloud.net.cn/uniCloud/rundebug.html#runparam
{
"type": "跑步",
"id": "645b8a67e1a35c371b015038",
"feeling": "good"
}
\ No newline at end of file
'use strict';
exports.main = async (event, context) => {
//event为客户端上传的参数
console.log('event : ', event)
const db = uniCloud.database();
let table;
if (event.type == '跑步')
table = 'mustgo-running-record';
else
table = 'mustgo-walking-record';
const collection = db.collection(table);
let res = await collection.doc(event.id).update({
feelings: event.feeling
})
if (res.updated > 0) {
return {
code: 200,
message: "保存成功",
data: {
}
}
}
//返回数据给客户端
return {
code: 500,
message: "服务器错误",
data: {
}
}
};
{
"name": "fe-sport-feelings",
"dependencies": {},
"extensions": {
"uni-cloud-jql": {}
}
}
\ No newline at end of file
'use strict';
exports.main = async (event, context) => {
//event为客户端上传的参数
console.log('event : ', event)
const db = uniCloud.database();
let fieldName;
if (event.type == "跑步") {
fieldName = "total_running_distance";
} else {
fieldName = "total_walking_distance";
}
const collection = db.collection('mustgo-user');
let res = await collection.orderBy(fieldName, "desc").get()
console.log(res)
for (let i = 0; i < res.data.length; i ++) {
if (res.data[i]["_id"] == event.userId) {
return {
code: 200,
message: "查询成功",
data :{
totalRunningDistance: res.data[i][fieldName],
totalRunningRank: i + 1
}
}
}
}
//返回数据给客户端
return {
code: 500,
message: "服务器错误",
data: {
}
}
};
{
"name": "fe-sport-run-index",
"dependencies": {},
"extensions": {
"uni-cloud-jql": {}
}
}
\ No newline at end of file
feelings({
type: "跑步",
id: "645cddc5f5cf3a2df4bdb9a3",
feeling: "normal"
})
index({
type: "跑步",
userId: "645c972809e298919852519f"
});
// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程:https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
_before: function() { // 通用预处理器
},
async index(event) {
// 参数校验,如无参数则不需要
if (!event.type) {
return {
code: 400,
message: "客户端错误",
data: {}
}
}
// 业务逻辑
const db = uniCloud.database();
let fieldName;
if (event.type == "跑步") {
fieldName = "total_running_distance";
} else {
fieldName = "total_walking_distance";
}
const collection = db.collection('mustgo-user');
let res = await collection.orderBy(fieldName, "desc").get()
for (let i = 0; i < res.data.length; i++) {
if (res.data[i]["_id"] == event.userId) {
return {
code: 200,
message: "查询成功",
data: {
totalRunningDistance: res.data[i][fieldName],
totalRunningRank: i + 1
}
}
}
}
// 返回结果
return {
code: 500,
message: "服务器错误",
data: {}
}
},
async rank(event) {
const db = uniCloud.database();
const collection = db.collection('mustgo-user');
let fieldName;
if (event.type == "跑步") {
fieldName = "total_running_distance";
} else {
fieldName = "total_walking_distance";
}
let res = await collection.orderBy(fieldName, "desc").get()
console.log(res)
let userlist = new Array;
for (let i = 0; i < res.data.length; i++) {
let user = {
"rank": i + 1,
"icon": res.data[i]["icon"],
"username": res.data[i]["name"],
"distance":res.data[i][fieldName]
}
userlist.push(user)
}
//返回数据给客户端
return {
"code": 200,
"message": "成功返回累计排名",
"data": {
"userList": userlist
}
}
},
async save(event) {
//event为客户端上传的参数
console.log('event : ', event)
const db = uniCloud.database();
let table, fieldName;
if (event.type == "跑步") {
table = "mustgo-running-record";
fieldName = "total_running_distance";
} else {
table = "mustgo-walking-record";
fieldName = "total_walking_distance";
}
let collection = db.collection(table);
if (event.distance < 0.01) {
return {
code: 400,
message: "距离过短,不予保存",
data: {
}
}
}
let res = await collection.add({
start_date: event.startTime,
duration: event.duration,
feelings: "",
owner_id: event.userId,
distance: event.distance,
pace: event.pace,
start_point: event.startPoint,
end_point: event.endPoint,
path_line: event.pathLine
})
console.log(res)
let sportId = res.id;
// userID
collection = db.collection('mustgo-user');
res = await collection.where({
_id: event.userId
}).get()
// console.log(res)
let cur = res.data[0][fieldName];
cur += event.distance
res = await collection.doc(event.userId).update({
total_walking_distance: cur,
})
if (res.updated > 0)
return {
code: 200,
message: "保存成功",
data: {
id: sportId
}
}
//返回数据给客户端
return {
code: 500,
message: "服务器错误",
data: {
}
}
},
async feelings(event) {
const db = uniCloud.database();
let table;
if (event.type == '跑步')
table = 'mustgo-running-record';
else
table = 'mustgo-walking-record';
const collection = db.collection(table);
let res = await collection.doc(event.id).update({
feelings: event.feeling
})
if (res.updated > 0) {
return {
code: 200,
message: "保存成功",
data: {
}
}
}
//返回数据给客户端
return {
code: 500,
message: "服务器错误",
data: {
}
}
}
}
\ No newline at end of file
{
"name": "fe-sport-run-save",
"name": "fe-sport-object",
"dependencies": {},
"extensions": {
"uni-cloud-jql": {}
......
'use strict';
exports.main = async (event, context) => {
//event为客户端上传的参数
console.log('event : ', event)
const db = uniCloud.database();
const collection = db.collection('mustgo-user');
let res = await collection.orderBy("total_running_distance", "desc").get()
console.log(res)
let userlist = new Array;
for (let i = 0; i < res.data.length; i++) {
let user = {
"rank": i + 1,
"icon": res.data[i]["icon"],
"username": res.data[i]["name"],
"distance":res.data[i]["total_running_distance"]
}
userlist.push(user)
}
//返回数据给客户端
return {
"code": 200,
"message": "成功返回跑步累计排名",
"data": {
"userList": userlist
}
}
};
\ No newline at end of file
{
"name": "fe-sport-run-rank",
"dependencies": {},
"extensions": {
"uni-cloud-jql": {}
}
}
\ No newline at end of file
'use strict';
exports.main = async (event, context) => {
//event为客户端上传的参数
console.log('event : ', event)
const db = uniCloud.database();
let collection = db.collection('mustgo-running-record');
if (event.distance < 0.01) {
return {
code: 400,
message: "距离过短,不予保存",
data: {
}
}
}
let res = await collection.add({
start_date: event.startTime,
duration: event.duration,
feelings: "",
owner_id: event.userId,
distance: event.distance,
pace: event.pace,
start_point: event.startPoint,
end_point: event.endPoint,
path_line: event.pathLine
})
console.log(res)
let sportId = res.id;
// userID
collection = db.collection('mustgo-user');
res = await collection.where({
_id: event.userId
}).get()
console.log(res)
let cur = res.data[0]["total_running_distance"];
cur += event.distance
res = await collection.doc(event.userId).update({
total_running_distance: cur,
})
if (res.updated > 0)
return {
code: 200,
message: "保存成功",
data: {
id: sportId
}
}
//返回数据给客户端
return {
code: 500,
message: "服务器错误",
data: {
}
}
};
'use strict';
exports.main = async (event, context) => {
//event为客户端上传的参数
console.log('event : ', event)
const db = uniCloud.database();
const collection = db.collection('mustgo-user');
let res = await collection.orderBy("total_walking_distance", "desc").get()
console.log(res)
let userlist = new Array;
for (let i = 0; i < res.data.length; i++) {
let user = {
"rank": i + 1,
"icon": res.data[i]["icon"],
"username": res.data[i]["name"],
"distance":res.data[i]["total_walking_distance"]
}
userlist.push(user)
}
//返回数据给客户端
return {
"code": 200,
"message": "成功返回健走累计排名",
"data": {
"userList": userlist
}
}
};
\ No newline at end of file
{
"name": "fe-sport-walk-rank",
"dependencies": {},
"extensions": {
"uni-cloud-jql": {}
}
}
\ No newline at end of file
'use strict';
exports.main = async (event, context) => {
//event为客户端上传的参数
console.log('event : ', event)
const db = uniCloud.database();
let collection = db.collection('mustgo-walking-record');
if (event.distance < 0.01) {
return {
code: 400,
message: "距离过短,不予保存",
data: {
}
}
}
let res = await collection.add({
start_date: event.startTime,
duration: event.duration,
feelings: "",
owner_id: event.userId,
distance: event.distance,
pace: event.pace,
start_point: event.startPoint,
end_point: event.endPoint,
path_line: event.pathLine
})
console.log(res)
let sportId = res.id;
// userID
collection = db.collection('mustgo-user');
res = await collection.where({
_id: event.userId
}).get()
// console.log(res)
let cur = res.data[0]["total_walking_distance"];
cur += event.distance
res = await collection.doc(event.userId).update({
total_walking_distance: cur,
})
if (res.updated > 0)
return {
code: 200,
message: "保存成功",
data: {
id: sportId
}
}
//返回数据给客户端
return {
code: 500,
message: "服务器错误",
data: {
}
}
};
{
"name": "fe-sport-walk-save",
"dependencies": {},
"extensions": {
"uni-cloud-jql": {}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册