Jelajahi Sumber

推荐接口修改

kindring 2 tahun lalu
induk
melakukan
90f99162d2
17 mengubah file dengan 243 tambahan dan 4 penghapusan
  1. 4 2
      app.js
  2. 83 0
      controller/c_recommend.js
  3. 59 2
      database/d_recommend.js
  4. TEMPAT SAMPAH
      public/upload/bg_weekend.jpg
  5. TEMPAT SAMPAH
      public/upload/default.jpg
  6. TEMPAT SAMPAH
      public/upload/丽江.jpg
  7. TEMPAT SAMPAH
      public/upload/北海.jpg
  8. TEMPAT SAMPAH
      public/upload/厦门.jpg
  9. TEMPAT SAMPAH
      public/upload/岳阳.jpg
  10. TEMPAT SAMPAH
      public/upload/日照.jpg
  11. TEMPAT SAMPAH
      public/upload/济南.jpg
  12. TEMPAT SAMPAH
      public/upload/海边浪一浪.jpg
  13. TEMPAT SAMPAH
      public/upload/福州.jpg
  14. TEMPAT SAMPAH
      public/upload/腾冲.jpg
  15. TEMPAT SAMPAH
      public/upload/贵阳.jpg
  16. TEMPAT SAMPAH
      public/upload/黄山.jpg
  17. 97 0
      routes/recommend_api.js

+ 4 - 2
app.js

@@ -11,6 +11,7 @@ const citysRouter = require('./routes/citys');
 const adminRouter = require('./routes/admin');
 const captcha = require('./routes/captcha');
 const flight = require('./routes/flight_api');
+const recommend = require('./routes/recommend_api');
 
 const app = express();
 
@@ -21,9 +22,9 @@ app.set('views', path.join(__dirname, 'views'));
 app.set('view engine', 'ejs');
 
 app.use('/js', express.static(path.join(__dirname, './public/js')));
-app.use('/js', express.static(path.join(__dirname, './public/img')));
+app.use('/img', express.static(path.join(__dirname, './public/img')));
 app.use('/', express.static(path.join(__dirname, './public')));
-
+app.use('/public', express.static(path.join(__dirname, './public')));
 // 配置session
 app.use(
     session({
@@ -46,6 +47,7 @@ app.use('/admin', adminRouter);
 app.use('/api/city/',citysRouter);
 app.use('/api/captcha', captcha);
 app.use('/api/flight', flight);
+app.use('/api/recommend', recommend);
 
 
 

+ 83 - 0
controller/c_recommend.js

@@ -0,0 +1,83 @@
+const db_air = require('../database/d_air')
+const db_area = require('../database/d_area')
+const db_recommend = require('../database/d_recommend')
+const handle = require('../until/handle')
+const field = require('../maps/field')
+const codeMap = require('../maps/rcodeMap')
+
+
+async function list(){
+    let [err,result] = await handle(db_recommend.recommendList());
+    if(err){throw err}
+    return result;
+}
+
+async function addRecommend(name,descript,zIndex){
+    let [err,result] = await handle(db_recommend.addRecommend(name,descript,zIndex));
+    if(err){throw err}
+    return result;
+}
+
+async function addFlights(recommendId,flights){
+    let [err,result] = await handle(db_recommend.addFlights(recommendId,flights));
+    if(err){throw err}
+    return result;
+}
+
+async function changeRecommend(recommendId,params){
+    let [err,result] = await handle(db_recommend.updateRecommend(recommendId, {
+        recommendName:params.recommendName,
+        recommendName:params.descript,
+        recommendName:params.zIndex,
+    }));
+    if(err){throw err}
+    return result;
+}
+
+/**
+ * 显示指定推荐的信息,以及他的所有航班信息
+ * @returns {Promise<void>}
+ */
+async function recommendInfo(recommendId){
+    let err,result,recommend;
+    [err,result] = await handle(db_recommend.find(recommendId));
+    if(err){throw err}
+    if(result.length < 1){throw {rcode: codeMap.notFound,msg:'无法找到对应推荐'}}
+    recommend = result[0];
+    [err,result] = await handle(db_recommend.loadFlights(recommendId,true));
+    if(err){throw err}
+    recommend.flights = result;
+    return recommend;
+}
+/**
+ * 加载主要界面展示用的推荐,显示5个活动,每个活动显示前5个航班
+ * @returns {Promise<void>}
+ */
+async function homeRecommends(){
+    let err,recommends,flights;
+    [err,recommends] = await handle(list());
+    if(err){throw err}
+    if(recommends.length){
+        recommends = recommends.splice(0,5);
+    }
+    for (const recommend of recommends) {
+        // 获取对应的航班信息
+        [err,flights] = await handle(recommendInfo(recommend.id));
+        if(err){throw err}
+        flights=flights.flights;
+        if(flights.length>=5){
+            flights = flights.splice(0,5);
+        }
+        recommend.flights = flights;
+    }
+    console.log(recommends);
+    return recommends;
+}
+
+module.exports = {
+    list,
+    addRecommend,
+    addFlights,
+    changeRecommend,
+    homeRecommends
+}

+ 59 - 2
database/d_recommend.js

@@ -19,6 +19,31 @@ function addRecommend(recommendName,discript,zIndex = 1){
     return mysql.pq(sql,values);
 }
 
+/**
+ * 活动列表
+ * @returns {Promise | Promise<unknown>}
+ */
+function recommendList(){
+    let sql=``,values=[];
+    sql+=`select * from recommendDir`
+    sql += ' order by zIndex desc;'
+    return mysql.pq(sql,values);
+}
+
+
+/**
+ * 获取活动
+ * @param recommendId 活动id
+ * @returns {Promise | Promise<unknown>}
+ */
+function find(recommendId){
+    let sql=``,values=[];
+    sql+=`select * from recommendDir where id = ?`
+    sql += ';'
+    values.push(recommendId)
+    return mysql.pq(sql,values);
+}
+
 /**
  * 搜索活动
  * @param key
@@ -52,7 +77,13 @@ function searchRecommend(key,state){
  */
 function loadFlights(recommendId,isHave){
     let sql=``,values=[];
-    sql+=`select f.*,r.recommendIndex from flight as f , recommendFlight as r  where`
+    sql+=`select r.*,f.flightName,f.currentPrice,f.originalPrice,f.sailingTime,f.langdinTime ,air.airCode,dep.cityname as departureCityName,tar.cityname as targetCityName
+            from
+            recommendFlight as r,
+            flight as f
+            LEFT JOIN (select * from air ) as air on air.id = f.airId
+            LEFT JOIN (select id,cityName from area ) as dep on dep.id = f.departureCity
+            LEFT JOIN (select id,cityName from area ) as tar on tar.id = f.targetCity where`
     if(isHave){
         sql += ` f.id = r.flightId`;
     }else {
@@ -60,7 +91,7 @@ function loadFlights(recommendId,isHave){
     }
     sql += ` and r.recommendId = ?`;
     values.push(recommendId);
-    sql += ';'
+    sql += ' order by r.zIndex desc;'
     return mysql.pq(sql,values);
 }
 
@@ -97,10 +128,36 @@ function deleteFlight(recommendId,flightId){
     return mysql.pq(sql,values);
 }
 
+
+
+
+function updateRecommend(recommendId,params){
+    let sql=`update recommendDir set`,values=[];
+    let fields = Object.keys(params);
+    fields = fields.filter(field=>params[field])
+    if(fields.length<1){
+        throw {rcode:code.notParam}
+    }
+    for(let field of fields) {
+        if (!params[field]) {
+            continue;
+        }
+        if(values.length>0){sql+=','}
+        sql+=` \`${field}\` = ?`
+        values.push(params[field])
+    }
+    sql+=` where id = ?`;
+    values.push(recommendId);
+    return mysql.pq(sql,values);
+}
+
 module.exports = {
+    recommendList,
     addRecommend,
     searchRecommend,
     loadFlights,
     addFlights,
     deleteFlight,
+    updateRecommend,
+    find
 }

TEMPAT SAMPAH
public/upload/bg_weekend.jpg


TEMPAT SAMPAH
public/upload/default.jpg


TEMPAT SAMPAH
public/upload/丽江.jpg


TEMPAT SAMPAH
public/upload/北海.jpg


TEMPAT SAMPAH
public/upload/厦门.jpg


TEMPAT SAMPAH
public/upload/岳阳.jpg


TEMPAT SAMPAH
public/upload/日照.jpg


TEMPAT SAMPAH
public/upload/济南.jpg


TEMPAT SAMPAH
public/upload/海边浪一浪.jpg


TEMPAT SAMPAH
public/upload/福州.jpg


TEMPAT SAMPAH
public/upload/腾冲.jpg


TEMPAT SAMPAH
public/upload/贵阳.jpg


TEMPAT SAMPAH
public/upload/黄山.jpg


+ 97 - 0
routes/recommend_api.js

@@ -0,0 +1,97 @@
+const router = require('express').Router();
+const paramsCheck = require('../middleware/paramsCheck');
+const checkLogin = require('../middleware/checkLogin');
+const c_recommend = require("../controller/c_recommend");
+const field = require('../maps/field');
+const code = require('../maps/rcodeMap');
+const progress = require('../maps/progress')
+router.get('/recommends',async (req,res)=>{
+    try{
+        let results = await c_recommend.list();
+        res.json({
+            rcode: code.ok,
+            data: results
+        })
+    }catch (error) {
+        if (error.rcode !== code.customError) {
+            console.log(error);
+        }
+        res.json({
+            rcode: error.rcode || code.serverError,
+            msg: error.msg || error.message
+        });
+    }
+});
+router.get('/homer',async (req,res)=>{
+    try{
+        let results = await c_recommend.homeRecommends();
+        res.json({
+            rcode: code.ok,
+            data: results
+        })
+    }catch (error) {
+        if (error.rcode !== code.customError) {
+            console.log(error);
+        }
+        res.json({
+            rcode: error.rcode || code.serverError,
+            msg: error.msg || error.message
+        });
+    }
+});
+
+
+router.post('/recommend/add',
+    checkLogin(field.adminType),
+    paramsCheck({
+        post:{
+            recommendName:{required:true},
+            descript:{required:true},
+        }
+    }),
+    async (req,res)=>{
+    try{
+        let results = await c_recommend.addRecommend(req.body.recommendName,req.body.descript ,req.body.zIndex);
+        res.json({
+            rcode: code.ok,
+            data: results
+        })
+    }catch (error) {
+        if (error.rcode !== code.customError) {
+            console.log(error);
+        }
+        res.json({
+            rcode: error.rcode || code.serverError,
+            msg: error.msg || error.message
+        });
+    }
+});
+
+router.post('/recommend/flights',
+    checkLogin(field.adminType),
+    paramsCheck({
+        post:{
+            recommendId:{required:true},
+            flights:{required:true},
+        }
+    }),
+    async (req,res)=>{
+        try{
+            let results = await c_recommend.addFlights(req.body.recommendId,req.body.flights);
+            res.json({
+                rcode: code.ok,
+                data: results
+            })
+        }catch (error) {
+            if (error.rcode !== code.customError) {
+                console.log(error);
+            }
+            res.json({
+                rcode: error.rcode || code.serverError,
+                msg: error.msg || error.message
+            });
+        }
+    });
+
+
+module.exports = router;