Jelajahi Sumber

航班查询相关

kindring 3 tahun lalu
induk
melakukan
9ec09fb860
6 mengubah file dengan 118 tambahan dan 15 penghapusan
  1. 37 13
      app.js
  2. 4 0
      configs/server.json
  3. 44 0
      controller/c_area.js
  4. 8 1
      database/d_area.js
  5. 24 0
      routes/citys.js
  6. 1 1
      接口.md

+ 37 - 13
app.js

@@ -1,32 +1,53 @@
-var createError = require('http-errors');
-var express = require('express');
-var path = require('path');
-var cookieParser = require('cookie-parser');
-var logger = require('morgan');
+const createError = require('http-errors');
+const express = require('express');
+const path = require('path');
+const cookieParser = require('cookie-parser');
+const session = require('express-session');
+const ejs = require('ejs');
+const bodyParser = require('body-parser');
 
-var indexRouter = require('./routes/index');
-var usersRouter = require('./routes/users');
 
-var app = express();
+const indexRouter = require('./routes/index');
+const usersRouter = require('./routes/users');
+
+const app = express();
+
+const serverConfig = require('./configs/server.json');
 
 // view engine setup
 app.set('views', path.join(__dirname, 'views'));
 app.set('view engine', 'ejs');
 
-app.use(logger('dev'));
-app.use(express.json());
-app.use(express.urlencoded({ extended: false }));
-app.use(cookieParser());
-app.use(express.static(path.join(__dirname, 'public')));
+app.use('/js', express.static(path.join(__dirname, './public/js')));
+app.use('/js', express.static(path.join(__dirname, './public/img')));
+app.use('/', express.static(path.join(__dirname, './public')));
+
+// 配置session
+app.use(
+    session({
+      secret: 'air',
+      name: 'session', //这里的name值得是cookie的name,默认cookie的name是:connect.sid
+      cookie: { maxAge: 1800000 }, //过期时间半小时
+      keys: ['owner', 'captcha'], // 用户登陆信息,验证码字段
+      resave: true,
+      saveUninitialized: true,
+    })
+);
+
+app.use(bodyParser.urlencoded({ limit: '10mb', extended: true }));
+app.use(bodyParser.json({ limit: '10mb' }));
+
 
 app.use('/', indexRouter);
 app.use('/users', usersRouter);
 
+
 // catch 404 and forward to error handler
 app.use(function(req, res, next) {
   next(createError(404));
 });
 
+
 // error handler
 app.use(function(err, req, res, next) {
   // set locals, only providing error in development
@@ -38,4 +59,7 @@ app.use(function(err, req, res, next) {
   res.render('error');
 });
 
+app.listen(serverConfig.port, serverConfig.host, ()=>{
+  log.warn(`服务启动 server is running to @http://${serverConfig.host}:${serverConfig.port}`);
+})
 module.exports = app;

+ 4 - 0
configs/server.json

@@ -0,0 +1,4 @@
+{
+    "host": "127.0.0.1",
+    "port": "8181"
+}

+ 44 - 0
controller/c_area.js

@@ -0,0 +1,44 @@
+const handle = require("../until/handle");
+const db_area = require("../database/d_area");
+
+/**
+ * 新增城市
+ * @param cityType
+ * @param cityName
+ * @returns {Promise<*>}
+ */
+async function addCity(cityType,cityName){
+    let [err,result] = await handle(db_area.addArea(cityType,cityName));
+    if(err){throw err}
+    return result;
+}
+
+/**
+ * 查询城市
+ * @param [cityType]
+ * @returns {Promise<*>}
+ */
+async function searchCity(cityType){
+    let [err,result] = await handle(db_area.searchAreas(cityType));
+    if(err){throw err}
+    return result;
+}
+
+/**
+ * 更新城市信息
+ * @param cityId
+ * @param cityType
+ * @param cityName
+ * @returns {Promise<*>}
+ */
+async function updateCity(cityId,cityType,cityName){
+    let [err,result] = await handle(db_area.updateCity(cityId,cityType,cityName));
+    if(err){throw err}
+    return result;
+}
+
+module.exports = {
+    addCity,
+    searchCity,
+    updateCity
+}

+ 8 - 1
database/d_area.js

@@ -55,4 +55,11 @@ function updateCity(cityId,cityType,cityName){
     sql += ` where id = ?;`
     values.push(cityId);
     return mysql.pq(sql,values);
-}
+}
+
+
+module.exports = {
+    updateCity,
+    addArea,
+    searchAreas,
+}

+ 24 - 0
routes/citys.js

@@ -0,0 +1,24 @@
+const router = require('express').Router();
+const c_area = require('../controller/c_area')
+const code = require('../maps/rcodeMap')
+router.get('/api/citys',async (req,res)=>{
+    try{
+        let results = await c_area.searchCity(req.query.cityType);
+        // console.log(province);
+        res.json({
+            rcode: code.ok,
+            data: results
+        })
+    }catch (error) {
+        if (error.rcode !== code.customError) {
+            console.log(error);
+            console.log.error(`get area map error ${error.message||error.msg}`);
+        }
+        res.json({
+            rcode: error.rcode || code.serverError,
+            msg: error.msg || error.message
+        });
+    }
+})
+
+module.exports = router;

+ 1 - 1
接口.md

@@ -16,6 +16,7 @@
 ### 获取轮播
 > /api/carousel
  
+
 ## 后台接口
 > /admin/api
 ### 登录
@@ -29,4 +30,3 @@
 ### 线路管理
 > admin/api/line
 ### 机票 
-