Swagger-JS 如何增加MD5校验,和项目依赖

swagger-bundle 打包

首先进入swagger-js目录

npm install
npm run build

如何修改

src/execute.js文件中增加代码:

import md5 from 'md5';

export function buildRequest({方法类mergeInQueryOrForm(req)语句前增加代码:

  //calcSn for server
  var req = request
  console.log(req.body);
  if (req.body) {
    let bodyObj = JSON.parse(req.body);
    bodyObj['header']['token'] = req['headers']['token'];
    bodyObj['header']['snTime'] = (new Date()).valueOf();

    let allData = Object.assign(JSON.parse(JSON.stringify(bodyObj['body'])), {
      token: bodyObj['header']['token'],
      snTime: bodyObj['header']['snTime']
    });
    bodyObj['header']['sn'] = calcSn(allData);
    req.body = JSON.stringify(bodyObj);
    req.headers['X-Sn-Verify'] = md5(req.body);
  }
  console.log(req.body);

  function calcSn(allData) {
    let queryString = "";
    Object.keys(allData)
      .sort()
      .forEach(function (key, i) {
        let value = allData[key];
        queryString += key + "=" + value + "&";
      });
    if (queryString.length > 0) {
      queryString = queryString.substring(0, queryString.length - 1)
    }
    return md5(queryString);
  }

package.json需要增加跨平台编译的支持