Files
ClassContent/项目合集/校园失物招领/min-backend/routes/users.js
2024-09-27 02:06:13 +08:00

276 lines
7.0 KiB
JavaScript
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const _ = require("../import");
/**
* @api {post} /user/login login
* @apiDescription 用户登录的接口,登录成功返回用户的基本信息
* @apiName login
* @apiGroup user
* @apiParam {string} phone 用户名
* @apiParam {string} pwd 用户密码
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "请求成功"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 404,
* "message" : "请求错误"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:3030/user/login
* @apiVersion 1.0.0
*/
_.router.post('/login', async function (req, res, next) {
let { phone, pwd } = req.body;
var data = await _.db.query({
sql: _.config.sql.userLogin,
data: [phone,_.utils.Md5Password(pwd)]
})
if (data.length != 0 ){
res.json({
status: 200,
message: '登录成功',
result: data[0]
});
} else {
res.json({
status: 404,
message: '账户名或密码有误',
result: null
});
}
});
/**
* @api {post} /user/add add
* @apiDescription 用户注册的接口
* @apiName add
* @apiGroup user
* @apiParam {string} phone 用户名
* @apiParam {string} pwd 用户密码
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "注册成功"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 404,
* "message" : "注册失败"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:3030/user/add
* @apiVersion 1.0.0
*/
_.router.post('/add', async function (req, res, next) {
let { phone, pwd } = req.body;
var data = await _.db.query({
sql: _.config.sql.addUser,
data: [_.utils.uuid(), phone, _.utils.Md5Password(pwd), 'https://upload.jianshu.io/users/upload_avatars/7778745/7c512474-3865-4c39-b4f2-d0a830174e62.jpg']
})
console.log(data);
if(data.hasOwnProperty('insertId')) {
res.json({
status: 200,
message: '注册成功',
result: null
});
} else {
res.json({
status: 404,
message: '注册失败',
result: null
});
}
});
/**
* @api {post} /user/history history
* @apiDescription 获取用户发布的历史记录信息
* @apiName history
* @apiGroup user
* @apiParam {number} user_id 用户id
* @apiParam {number} thing_type 1 失物招领2 寻物启事
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "注册成功"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 404,
* "message" : "注册失败"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:3030/user/history
* @apiVersion 1.0.0
*/
_.router.post('/history', async function (req, res, next) {
let { user_id, thing_type } = req.body;
var data = await _.db.query({
sql: _.config.sql.getHistory,
data: [user_id, thing_type]
})
if (data.length != 0) {
res.json({
status: 200,
message: '请求成功',
result: data
});
} else {
res.json({
status: 200,
message: '暂无数据',
result: []
});
}
});
/**
* @api {post} /user/changeStatus changeStatus
* @apiDescription 修改 失物招领 和 寻物启事 的状态
* @apiName changeStatus
* @apiGroup user
* @apiParam {number} user_id 用户id
* @apiParam {number} thing_id 用户发布的 求助信息 的事件 id
* @apiParam {number} thing_status 要修改的状态
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "注册成功"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 404,
* "message" : "注册失败"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:3030/user/changeStatus
* @apiVersion 1.0.0
*/
_.router.post('/changeStatus', async function (req, res, next) {
let { user_id, thing_id, thing_status } = req.body;
var data = await _.db.query({
sql: _.config.sql.changeStatus,
data: [thing_status, thing_id, user_id ]
})
res.json({
status: 200,
message: '发布成功',
result: '修改成功'
});
});
/**
* @api {post} /user/Statistics Statistics
* @apiDescription 我的页面获取 失物招领 和 寻物启事 的数量
* @apiName Statistics
* @apiGroup user
* @apiParam {number} user_id 用户id
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "注册成功"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 404,
* "message" : "注册失败"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:3030/user/Statistics
* @apiVersion 1.0.0
*/
_.router.post('/Statistics', async function (req, res, next) {
let { user_id } = req.body;
var data = await _.db.query({
sql: _.config.sql.getStatistics,
data: [ user_id, user_id ]
})
if (data.length != 0) {
res.json({
status: 200,
message: '请求成功',
result: data[0]
});
} else {
res.json({
status: 200,
message: '暂无数据',
result: []
});
}
});
/**
* @api {post} /user/edit edit
* @apiDescription 我的页面获取 编辑用户信息
* @apiName edit
* @apiGroup user
* @apiParam {number} user_id 用户id
* @apiParam {string} user_city 用户城市,格式: 安徽,合肥,蜀山区
* @apiParam {string} user_name 用户名
* @apiParam {string} user_portrait 头像地址
* @apiParam {string} user_school 用户学校
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "注册成功"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 404,
* "message" : "注册失败"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:3030/user/edit
* @apiVersion 1.0.0
*/
_.router.post('/edit', async function (req, res, next) {
let { user_id,user_city,user_name,user_portrait,user_school } = req.body;
var data = await _.db.query({
sql: _.config.sql.edit,
data: [ user_city,user_name,user_portrait,user_school,user_id ]
})
res.json({
status: 200,
message: '请求成功',
result: '修改成功'
});
});
module.exports = _.router;