api2other_check.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. /**
  3. *
  4. * @author EllisRan
  5. * @date
  6. * @version
  7. */
  8. const crypto = require('crypto');
  9. const maintainConst = require('../const/maintain');
  10. module.exports = options => {
  11. return function* api2otherCheck(next) {
  12. try {
  13. // 获取系统维护信息
  14. const maintainData = yield this.service.maintain.getDataById(1);
  15. if (maintainData.status === maintainConst.status.ongoing) {
  16. throw '系统维护中~';
  17. }
  18. const code = this.query.projectCode || this.request.body.projectCode;
  19. const sign = this.query.sign || this.request.body.sign;
  20. const time = this.query.time || this.request.body.time;
  21. if (!code || !sign || !time) {
  22. throw '参数有误';
  23. }
  24. if ((parseFloat(time) + 86400 * 1000) < new Date().getTime()) {
  25. throw '时间参数已过期';
  26. }
  27. const data = yield this.service.project.getProjectByCode(code.toString().trim());
  28. if (data === null) {
  29. throw '不存在项目数据';
  30. }
  31. if (data.custom === 0) {
  32. throw '无法通过接口登录本系统';
  33. }
  34. if (data.custom === 1 && data.can_api === 0) {
  35. throw '接口已关闭,无法使用';
  36. }
  37. const encryptSign = crypto.createHash('md5').update(data.code + data.secret + time.toString()).digest('hex').toString();
  38. if (encryptSign !== sign) {
  39. throw '参数验证失败';
  40. }
  41. this.projectData = data;
  42. yield next;
  43. } catch (err) {
  44. console.log(err);
  45. // 重定向值标段管理
  46. // 判断是登录请求还是接口请求
  47. if (this.helper.isAjax(this.request)) {
  48. this.body = {
  49. err: 1,
  50. msg: err,
  51. data: '',
  52. };
  53. return;
  54. }
  55. this.session.loginError = err;
  56. yield next;
  57. }
  58. };
  59. };