login.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // ROUTES FOR OUR API
  2. // =============================================================================
  3. var express = require('express');
  4. var router = express.Router(); // get an instance of the express Router
  5. var bcrypt = require('bcryptjs');
  6. const querystring = require('querystring');
  7. var mysql = require('./mysql');
  8. const _mysqluser = ["127.0.0.1", "cocorobouser"]; //用户数据库信息
  9. var _api_key = '27fe2452da5fdcae2ced5ee1bbb3c5f9-b892f62e-09baaac5';
  10. var _DOMAIN = 'mail.cocorobo.hk';
  11. var _mailgun = require('mailgun-js')({ apiKey: _api_key, domain: _DOMAIN });
  12. //统一处理区域
  13. router.use(function (req, res, next) {
  14. next(); // make sure we go to the next routes and don't stop here
  15. });
  16. //创建edu课程
  17. router.route('/register').post(function (req, res, next) {
  18. //mode的处理
  19. if (req.body.mode) {
  20. // 0 用户名 1 用户密码 2用户身份 3 第三方id(可有可无)
  21. p = req.body.mode.split(","); //获取用户密码
  22. //没有第三方id的处理
  23. if (p.length < 4) {
  24. p.push("")
  25. }
  26. p[1] = bcrypt.hashSync(p[1], bcrypt.genSaltSync(10));
  27. //密码加密
  28. // bcrypt.hash(p[1], 10, function (err, hash) {
  29. // if (err) {
  30. // return next(err);
  31. // }
  32. // p[1] = hash;
  33. // });
  34. //参数拼接,注册
  35. p.unshift(_mysqledu[0], _mysqledu[1], "add_register");
  36. //返回注册用户数据
  37. mysql.usselect(p, function (ret) {
  38. res.end(ret);
  39. });
  40. }
  41. });
  42. //创建edu课程
  43. router.route('/login').post(function (req, res, next) {
  44. //mode的处理
  45. if (req.body.mode) {
  46. // 0 用户名 1 用户密码
  47. p = req.body.mode.split(","); //获取用户密码
  48. p[1] = bcrypt.hashSync(p[1], bcrypt.genSaltSync(10));
  49. //密码加密
  50. // bcrypt.hash(p[1], 10, function (err, hash) {
  51. // if (err) {
  52. // return next(err);
  53. // }
  54. // p[1] = hash;
  55. // });
  56. //参数拼接,注册
  57. p.unshift(_mysqledu[0], _mysqledu[1], "select_login");
  58. //执行存储过程
  59. mysql.usselect(p, function (ret) {
  60. //res.end(ret.toString());
  61. });
  62. }
  63. });
  64. //注册信息激活处理
  65. function sendRegisterEmail(email, activeKey) {
  66. let htmlForm = `
  67. <html>
  68. <h4>感謝註冊, 請點擊以下連結完成註冊。</h4>
  69. <br>
  70. <a href='${config.local.server}/activate/${email}/${activeKey}'>
  71. <button>點擊進行激活</button>
  72. </a>
  73. <p>Best regards,</p>
  74. <p>Tony Xin</p>
  75. <p>CEO</p>
  76. <p>CocoRobo Limited</p>
  77. </html>`;
  78. var emailData = {
  79. from: 'activation@cocorobo.cc',
  80. to: email,
  81. subject: '註冊帳號激活',
  82. html: htmlForm,
  83. };
  84. //发送激活邮箱
  85. mailgun.messages().send(emailData, function (error, body) {
  86. if (error) {
  87. console.log(error)
  88. } else {
  89. console.log(body);
  90. }
  91. });
  92. }
  93. //post存储过程处理
  94. postmysql = function (req, res, functionname) {
  95. //mode的处理
  96. if (req.body.mode) {
  97. p = req.body.mode.split(",");
  98. p.unshift(_mysqledu[0], _mysqledu[1], functionname);
  99. //执行存储过程
  100. mysql.usselect(p, function (ret) { res.end(JSON.stringify(ret)); });
  101. }
  102. }
  103. //get获取数据库信息
  104. getmysql = function (req, res, functionname, ...p) {
  105. p = p || [];
  106. p.unshift(_mysqledu[0], _mysqledu[1], functionname);
  107. //执行存储过程
  108. mysql.usselect(p, function (ret) { res.end(JSON.stringify(ret)); }); //
  109. }
  110. module.exports = router;