index.js 1.2 KB
Newer Older
M
MicroMilo 已提交
1 2 3 4 5 6 7 8
'use strict';
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	
	const db = uniCloud.database();
	let collection = db.collection('mustgo-walking-record');
	
M
MicroMilo 已提交
9 10 11 12 13 14 15 16 17
	if (event.distance < 0.01) { 	
			return {
				code: 400,
				message: "距离过短,不予保存",
				data: {
				}
			}
		}

M
MicroMilo 已提交
18 19 20
	let res = await collection.add({
		start_date: event.startTime,
		duration: event.duration,
M
MicroMilo 已提交
21
		feelings: "",
M
MicroMilo 已提交
22 23 24 25 26 27 28 29 30
		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)
	
M
MicroMilo 已提交
31 32
	let sportId = res.id;
	
M
MicroMilo 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	// 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: {
M
MicroMilo 已提交
53
				id: sportId
M
MicroMilo 已提交
54 55 56 57 58 59 60 61 62 63 64
			}
		}
	
	//返回数据给客户端
	return {
			code: 500,
			message: "服务器错误",
			data: {
			}
		}
};