Files
ClassContent/项目合集/电商App/shop_backend/routes/index.js
2025-04-15 23:46:14 +08:00

273 lines
6.6 KiB
JavaScript
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 { log } = require("debug");
const _ = require("../import");
/**
* @api {post} /home/index index
* @apiDescription 获取首页商品列表默认返回20条数据不需要参数传空对象 {} 即可
* @apiName index
* @apiGroup Home
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "请求成功"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 404,
* "message" : "请求失败"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:9090/home/index
* @apiVersion 1.0.0
*/
_.router.post('/index', async function (req, res, next) {
var data = await _.db.query({
sql: _.config.sql.getIndex,
data: []
})
data.forEach(v => {
v.wares_masterimg = JSON.parse(v.wares_masterimg);
v.wares_infoimg = JSON.parse(v.wares_infoimg);
v.wares_conf = JSON.parse(v.wares_conf);
})
res.json({
status: 200,
message: '请求成功',
result: data
});
});
/**
* @api {post} /home/banner banner
* @apiDescription 获取首页的banner数据
* @apiName banner
* @apiGroup Home
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "请求成功"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 404,
* "message" : "请求失败"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:9090/home/banner
* @apiVersion 1.0.0
*/
_.router.post('/banner', async function (req, res, next) {
var data = await _.db.query({
sql: _.config.sql.getIndex,
data: []
});
var banner = []
data.forEach((v, i) => {
if (i < 3) {
banner.push(v);
}
});
banner.forEach((v, i) => {
switch (i) {
case 0:
v.banner = "https://img.pconline.com.cn/images/product/6448/644851/novalite-c_m2.jpg"
break;
case 1:
v.banner = "https://2c.zol-img.com.cn/product/199_800x600/122/cet0UsShO9wl.jpg";
break;
case 2:
v.banner = "https://2e.zol-img.com.cn/product/196_800x600/88/ceHLpcITEHhtw.jpg"
break;
}
})
console.log("banner: ", banner);
res.json({
status: 200,
message: '请求成功',
result: banner
});
});
/**
* @api {post} /home/recommend recommend
* @apiDescription 获取首页推荐分类的数据
* @apiName recommend
* @apiGroup Home
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "请求成功"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 404,
* "message" : "请求失败"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:9090/home/recommend
* @apiVersion 1.0.0
*/
_.router.post('/recommend', async function (req, res, next) {
var recommend = [
{ id: 1, url: '/img/type_1.png', title: '限时白嫖' },
{ id: 2, url: '/img/type_2.png', title: '新品推荐' },
{ id: 3, url: '/img/type_3.png', title: '低价高配' },
{ id: 4, url: '/img/type_4.png', title: '超值特卖' }
];
res.json({
status: 200,
message: '请求成功',
result: recommend
});
});
/**
* @api {post} /home/info info
* @apiDescription 获取商品的详情接口
* @apiName info
* @apiGroup Home
* @apiParam {number} id 商品的id参数从 /home/index 接口中获得 wares_id 作为id参数传入即可
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "请求成功"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 404,
* "message" : "请求失败"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:9090/home/info
* @apiVersion 1.0.0
*/
_.router.post('/info', async function (req, res, next) {
var id = req.body.id;
var data = (await _.db.query({
sql: _.config.sql.getInfo,
data: [id]
}))[0]
data.wares_masterimg = JSON.parse(data.wares_masterimg);
data.wares_infoimg = JSON.parse(data.wares_infoimg);
data.wares_conf = JSON.parse(data.wares_conf);
let result = await _.db.query({
sql: 'SELECT * from wares ORDER BY wares_id LIMIT 6',
data: []
})
result.forEach(v => {
v.wares_masterimg = JSON.parse(v.wares_masterimg);
v.wares_infoimg = JSON.parse(v.wares_infoimg);
v.wares_conf = JSON.parse(v.wares_conf);
})
data.recommend_list = result
res.json({
status: 200,
message: '请求成功',
result: data
});
});
/**
* @api {post} /home/activity activity
* @apiDescription 获取首页轮播下活动分类商品的接口
* @apiName activity
* @apiGroup Home
* @apiParam {number} id 活动的id取值对应为1->限时白嫖2->新品推荐3->低价高配4->超值特卖
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "请求成功"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 404,
* "message" : "请求失败"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:9090/home/activity
* @apiVersion 1.0.0
*/
_.router.post('/activity', async function (req, res, next) {
var activity_id = req.body.id;
var data = await _.db.query({
sql: _.config.sql.getActivity,
data: [activity_id]
})
res.json({
status: 200,
message: '请求成功',
result: data
});
});
/**
* @api {post} /home/search search
* @apiDescription 搜索接口,用于首页搜索商品
* @apiName search
* @apiGroup Home
* @apiParam {string} keyword 搜索关键词,例如:小米
* @apiSuccessExample {json} 请求成功的返回:
* {
* "status" : 200,
* "message" : "请求成功"
* "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
* }
* @apiErrorExample {json} 请求失败的返回
* {
* "status": 404,
* "message" : "请求失败"
* "result": null
* }
* @apiSampleRequest http://127.0.0.1:9090/home/search
* @apiVersion 1.0.0
*/
_.router.post('/search', async function (req, res, next) {
var keyword = req.body.keyword;
console.log("keyword ", keyword)
var data = await _.db.query({
sql: `SELECT * from wares where wares_title LIKE "%${keyword}%"`,
data: []
});
res.json({
status: 200,
message: '请求成功',
result: data
});
});
module.exports = _.router;