server.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. let express = require('express');
  2. let config = require("./config/config.js");
  3. let fileUtils = require("./modules/common/fileUtils");
  4. let dbm = require("./config/db/db_manager");
  5. //config.setToLocalDb();
  6. config.setToQaDb();
  7. //config.setupCache();
  8. let cfgCacheUtil = require("./config/cacheCfg");
  9. cfgCacheUtil.setupDftCache();
  10. let path = require('path');
  11. let session = require('express-session');
  12. let DBStore = require('connect-mongo')(session);
  13. let fs = require('fs');
  14. let URL = require('url');
  15. dbm.connect();
  16. //这里现在只引入了定额工料机里的models,当其它模块的models修改后使用:./modules/**/models/*.js引入所有的模块
  17. fileUtils.getGlobbedFiles('./modules/ration_glj/models/*.js').forEach(function(modelPath) {
  18. require(path.resolve(modelPath));
  19. })
  20. let app = express();
  21. let _rootDir = __dirname;
  22. app.use(express.static(_rootDir));
  23. app.set('views', path.join(__dirname, 'web'));
  24. app.engine('.html', require('ejs').__express);
  25. app.set('view engine', 'html');
  26. let bodyParser = require('body-parser');
  27. app.use(bodyParser.urlencoded({extended: false}));
  28. app.use(bodyParser.json());
  29. app.use(session({
  30. name: 'usersSession',
  31. secret: 'session users secret',
  32. cookie: {maxAge: 1000*60*30},
  33. resave: false,
  34. saveUninitialized: true
  35. //*
  36. ,store: new DBStore({
  37. url: 'mongodb://' + config.current.server + ':' + config.current.port + '/usersManages'
  38. })
  39. //*/
  40. }));
  41. // 登录状态全局判断
  42. app.use(function (req, res, next) {
  43. let url = req.originalUrl;
  44. // @todo 上一个页面跳转
  45. let referer = '';
  46. if (/^\/login/.test(url)) {
  47. // 如果是登录页面则忽略判断数据
  48. next();
  49. } else {
  50. try {
  51. // 判断session
  52. let sessionUser = req.session.sessionUser;
  53. if (!sessionUser) {
  54. throw 'session error';
  55. }
  56. res.locals.sessionUser = sessionUser;
  57. } catch (error) {
  58. return res.redirect('/login');
  59. }
  60. next();
  61. }
  62. });
  63. // 用户相关路由区域
  64. //app.use('/', require("./modules/users/routes/login_route"));
  65. require("./modules/users/routes/login_route")(app);
  66. require("./modules/users/routes/boot_route")(app);
  67. require("./modules/users/routes/user_route")(app);
  68. // 项目工料机相关
  69. app.use('/glj', require("./modules/glj/routes/glj_router"));
  70. app.use('/feeRates', require('./modules/fee_rates/routes/fee_rates_route'));
  71. app.get('/template/bills', function (req, res) {
  72. let checkAdmin = function (userAccount) {
  73. return true;
  74. }
  75. if (checkAdmin(req.session.userAccount)) {
  76. res.render('maintain/templates/html/bills.html',
  77. {userAccount: req.session.userAccount,
  78. userID: req.session.userID});
  79. } else {
  80. res.redirect('/pm');
  81. }
  82. });
  83. app.use('/template/bills/api', function (req, res, next) {
  84. let checkAdmin = function (userAccount) {
  85. return true;
  86. }
  87. if (checkAdmin(req.session.sessionUser)) {
  88. next();
  89. } else {
  90. res.json({error: 1, message: '对不起,您无权限操作清单模板。', data: null});
  91. }
  92. });
  93. let billsTemplateRouter = require('./modules/templates/routes/bills_template_router');
  94. app.use('/template/bills/api', billsTemplateRouter);
  95. app.get('/pm', function(req, res){
  96. res.render('building_saas/pm/html/project-management.html',
  97. {userAccount: req.session.userAccount,
  98. userID: req.session.sessionUser.ssoId});
  99. });
  100. let pmRouter = require('./modules/pm/routes/pm_route');
  101. app.use('/pm/api', pmRouter);
  102. app.get('/main', function(req, res) {
  103. let pm = require('./modules/pm/controllers/pm_controller');
  104. pm.checkProjectRight(req.session.sessionUser.ssoId, req.query.project, function (hasRight) {
  105. if (hasRight) {
  106. res.render('building_saas/main/html/main.html',
  107. {
  108. userAccount: req.session.userAccount,
  109. userID: req.session.sessionUser.ssoId
  110. });
  111. } else {
  112. res.redirect('/pm');
  113. }
  114. });
  115. });
  116. let project_Router = require('./modules/main/routes/project_route');
  117. let bills_Router = require('./modules/main/routes/bills_route');
  118. let ration_Router = require('./modules/main/routes/ration_route');
  119. let GLJ_Router = require('./modules/main/routes/GLJ_route');
  120. app.use('/project', project_Router);
  121. app.use('/bills', bills_Router);
  122. app.use('/ration', ration_Router);
  123. app.use('/glj_bak', GLJ_Router);
  124. //app.use(express.static(_rootDir+"/web"));
  125. //app.use(express.static(_rootDir+"/lib"));
  126. //let rations_Router = require("./modules/rationLibEditor/routes/rationLibEditor_route");
  127. //app.use("/rationLibEditor",rations_Router);
  128. app.get("/stdBillsmain", function(req, res){
  129. if(!req.session.sessionUser){
  130. res.redirect('/login');
  131. }
  132. else {
  133. res.render("maintain/bills_lib/html/main.html",
  134. {userAccount: req.session.userAccount,
  135. userID: req.session.sessionUser.ssoId});
  136. }
  137. });
  138. app.get("/stdBills", function(req, res){
  139. res.render("maintain/bills_lib/html/qingdan.html");
  140. });
  141. app.get('/stdJobs', function(req, res){
  142. res.render('maintain/bills_lib/html/neirong.html');
  143. });
  144. app.get('/stdItems', function(req, res){
  145. res.render('maintain/bills_lib/html/tezheng.html');
  146. });
  147. let billsLibRouter = require("./modules/bills_lib/routes/bills_lib_routes");
  148. app.use("/stdBillsEditor", billsLibRouter);
  149. let rationRouter = require("./modules/ration_repository/routes/ration_front_end_routes");
  150. app.use("/rationRepository/api", rationRouter);
  151. let rpt_Router = require("./modules/reports/routes/report_router");
  152. app.use("/report_api", rpt_Router);
  153. let rptTpl_Router = require("./modules/reports/routes/rpt_tpl_router_fe");
  154. app.use("/report_tpl_api", rptTpl_Router);
  155. //-----------------
  156. app.use(function(req, res, next) {
  157. res.status(404).send('404 Error');
  158. });
  159. app.use(function(err, req, res, next) {
  160. console.error(err.stack);
  161. res.status(500).send('500 Error');
  162. });
  163. //设置Date Format函数
  164. fs.readFile(__dirname + '/public/web/date_util.js', 'utf8', 'r', function (err, data) {
  165. eval(data);
  166. // let dt = new Date();
  167. // console.log(dt.Format('yyyy-M-dd'));
  168. // console.log(dt.Format('yyyy 年 M 月 dd 日'));
  169. // console.log(dt.Format('yyyy 年 M 月 20 日'));
  170. });
  171. app.listen(6060, function(){
  172. console.log('server started!');
  173. });