Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
const Express = require('express');
const router = Express.Router();
// const codeState = require('../../../config/codeState');
// const result = require('../../../config/index').result;
// const token = require('../../../common/token');
const {
    config,
    SelfCookie,
    codeState,
    result,
    log,
    token,
    HttpApi,
    Client,
    DEBUGGER,
    ResultFactory
} = require('../../../namespaceMain');
const request = require('request');
const md5 = require('md5');
const client = require("../../../common/redis");
router.get('/cc', function (req, res, next) {
    debugger;
    res.send("success--fw");
});
/**
 * @api {get} /forword/[customerid]/[accode]/[timestamp]/[sign] 前端分流管理✔
 * @apiDescription 前端主入口
 * @apiVersion 1.0.0
 * @apiSuccessExample {json}
 *      Success-Response:
 *      跳转至前端活动页面……
 * 
 * @apiErrorExample {json} 
 *      Error-Response:
 *      跳转至前端Error页面……
 * 
 * @apiGroup FrontEnd
 * @apiParam {string} customerid  客户编号
 * @apiParam {string} accode 数码
 * @apiParam {string} timestamp 时间戳
 * @apiParam {string} sign 签名,加密方式`md5(customerid+accode+token+timestamp)`;token为约定的值
 */
router.get('/:customerid/:accode/:timestamp/:sign', async function (req, res, next) {
    debugger;
    const _result = '网络出了个小差……';
    console.log(`/:customerid/:accode/:timestamp/:sign ${JSON.stringify(req.params)}`);
    var _customerid = req.params.customerid,
        _accode = req.params.accode,
        __header_sign = req.params.sign,
        __timestamp = req.params.timestamp,
        __sign = md5(_customerid + _accode + config.ZhshToNodejsToken[_customerid] + __timestamp),
        _param_notnull = (_customerid && _accode && __header_sign),
        _b_tokenisok = (__sign === __header_sign),
        _code_sign = `${_customerid}_${_accode}_${__timestamp}_${__sign}`;
    log.logger.info(`/:customerid/:accode/:timestamp/:sign /${_customerid}/${_code_sign}/${__timestamp}/${__sign} SessionId: ${req.session.id}  GetParameter: \n `);
    console.log(`/forword/index - /${_customerid}/${_code_sign}/${__timestamp}/${__sign} SessionId: ${req.session.id}`);
    if (!config.EnableZhshToNodejsToken) {
        _param_notnull = (_customerid && _accode);
        _b_tokenisok = true;
    }
    const key = `forword:${_customerid}:${_accode}`;
    const key_info = `forword:${_customerid}:actinfo:${_code_sign}`;
    debugger;
    if (_param_notnull) {
        if (_b_tokenisok) {
            console.time('/forword/index');
            debugger;
            try {
                var reply = await client.get(`${key}`);
            } catch (e) {
                console.error(e);
            }
            if (reply) {
                debugger;
                console.log(`/forword/index - 已缓存 ${JSON.stringify(reply)}`);
                console.timeEnd('/forword/index');
                try {
                    res.redirect(reply);
                } catch (e) {
                    console.error(e);
                }
            } else {
                var _f = {
                    customerid: _customerid,
                    accode: _code_sign
                }
                log.logger.info('/forword/index SessionId:' + req.session.id + ' Begin:', JSON.stringify(_f) + "\n");
                request({
                    url: HttpApi.Home.Index,
                    method: "POST",
                    json: true,
                    headers: {
                        "content-type": "application/json",
                        "Authorization": req.session.Authorization
                    },
                    form: _f
                }, function (error, response, body) {
                    //log.logger.info(JSON.parse(body)+ "\n");
                    var str_body = JSON.stringify(body);
                    log.logger.info('/forword/index SessionId:' + req.session.id + ' End:', str_body + "\n");
                    console.timeEnd('/forword/index');
                    console.log(`/forword/index - Response:${str_body}`);
                    if (body.success) {
                        if (body.result && body.result.module && body.result.module.template && body.result.module.template.url && body.result.module.id) {
                            var _type = body.result.module.template.url;
                            var _moduleid = body.result.module.id;
                            var _forword = `${config.FrontendToDomain}${_type}${_customerid}/${_moduleid}/${_code_sign}`;
                            client.set(`${key}`, _forword, 'EX', (60 * 1 * 30));//30 分钟失效
                            client.set(`${key_info}`, str_body, 'EX', (60 * 1 * 30));//30 分钟失效
                            return res.redirect(_forword);
                        } else {
                            log.logger.info('/forword/index  模块未配置成功!' + "\n");
                            console.log('/forword/index  模块未配置成功!' + "\n");
                            return res.send(_result);
                        }
                    } else {
                        log.logger.info('/forword/index 返回 false' + "\n");
                        console.log('/forword/index 返回 false' + "\n");
                        return res.send(_result);
                    }
                });
            }
        }
        else {
            log.logger.info('/forword/index  签名校验不成功' + "\n");
            console.log('/forword/index  签名校验不成功' + "\n");
            return res.send(_result);
        }
    }
    else {
        log.logger.info('/forword/index 参数为空或值错误' + "\n");
        console.log('/forword/index 参数为空或值错误' + "\n");
        return res.send(_result);
    }
});
module.exports = router;
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers