// 云对象教程: 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 if (event.type == "跑步") { res = await collection.doc(event.userId).update({ total_running_distance: cur }) } else { 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: { } } } }