123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- // ROUTES FOR OUR API
- // =============================================================================
- var express = require('express');
- var router = express.Router(); // get an instance of the express Router
- var bcrypt = require('bcryptjs');
- const querystring = require('querystring');
- var mysql = require('./mysql');
- const _mysqluser = ["127.0.0.1", "cocorobouser"]; //用户数据库信息
- var _api_key = '27fe2452da5fdcae2ced5ee1bbb3c5f9-b892f62e-09baaac5';
- var _DOMAIN = 'mail.cocorobo.hk';
- var _mailgun = require('mailgun-js')({ apiKey: _api_key, domain: _DOMAIN });
- //统一处理区域
- router.use(function (req, res, next) {
- next(); // make sure we go to the next routes and don't stop here
- });
- //创建edu课程
- router.route('/register').post(function (req, res, next) {
- //mode的处理
- if (req.body.mode) {
- // 0 用户名 1 用户密码 2用户身份 3 第三方id(可有可无)
- p = req.body.mode.split(","); //获取用户密码
- //没有第三方id的处理
- if (p.length < 4) {
- p.push("")
- }
- p[1] = bcrypt.hashSync(p[1], bcrypt.genSaltSync(10));
- //密码加密
- // bcrypt.hash(p[1], 10, function (err, hash) {
- // if (err) {
- // return next(err);
- // }
- // p[1] = hash;
- // });
- //参数拼接,注册
- p.unshift(_mysqledu[0], _mysqledu[1], "add_register");
- //返回注册用户数据
- mysql.usselect(p, function (ret) {
- res.end(ret);
- });
- }
- });
- //创建edu课程
- router.route('/login').post(function (req, res, next) {
- //mode的处理
- if (req.body.mode) {
- // 0 用户名 1 用户密码
- p = req.body.mode.split(","); //获取用户密码
- p[1] = bcrypt.hashSync(p[1], bcrypt.genSaltSync(10));
- //密码加密
- // bcrypt.hash(p[1], 10, function (err, hash) {
- // if (err) {
- // return next(err);
- // }
- // p[1] = hash;
- // });
- //参数拼接,注册
- p.unshift(_mysqledu[0], _mysqledu[1], "select_login");
- //执行存储过程
- mysql.usselect(p, function (ret) {
- //res.end(ret.toString());
- });
- }
- });
- //注册信息激活处理
- function sendRegisterEmail(email, activeKey) {
- let htmlForm = `
- <html>
- <h4>感謝註冊, 請點擊以下連結完成註冊。</h4>
- <br>
- <a href='${config.local.server}/activate/${email}/${activeKey}'>
- <button>點擊進行激活</button>
- </a>
- <p>Best regards,</p>
- <p>Tony Xin</p>
- <p>CEO</p>
- <p>CocoRobo Limited</p>
- </html>`;
- var emailData = {
- from: 'activation@cocorobo.cc',
- to: email,
- subject: '註冊帳號激活',
- html: htmlForm,
- };
- //发送激活邮箱
- mailgun.messages().send(emailData, function (error, body) {
- if (error) {
- console.log(error)
- } else {
- console.log(body);
- }
- });
- }
- //post存储过程处理
- postmysql = function (req, res, functionname) {
- //mode的处理
- if (req.body.mode) {
- p = req.body.mode.split(",");
- p.unshift(_mysqledu[0], _mysqledu[1], functionname);
- //执行存储过程
- mysql.usselect(p, function (ret) { res.end(JSON.stringify(ret)); });
- }
- }
- //get获取数据库信息
- getmysql = function (req, res, functionname, ...p) {
- p = p || [];
- p.unshift(_mysqledu[0], _mysqledu[1], functionname);
- //执行存储过程
- mysql.usselect(p, function (ret) { res.end(JSON.stringify(ret)); }); //
- }
- module.exports = router;
|