'use strict'; /** * * @author EllisRan * @date * @version */ const crypto = require('crypto'); const maintainConst = require('../const/maintain'); module.exports = options => { return function* api2otherCheck(next) { try { // 获取系统维护信息 const maintainData = yield this.service.maintain.getDataById(1); if (maintainData.status === maintainConst.status.ongoing) { throw '系统维护中~'; } const code = this.query.projectCode || this.request.body.projectCode; const sign = this.query.sign || this.request.body.sign; const time = this.query.time || this.request.body.time; if (!code || !sign || !time) { throw '参数有误'; } if ((parseFloat(time) + 86400 * 1000) < new Date().getTime()) { throw '时间参数已过期'; } const data = yield this.service.project.getProjectByCode(code.toString().trim()); if (data === null) { throw '不存在项目数据'; } if (data.custom === 0) { throw '无法通过接口登录本系统'; } if (data.custom === 1 && data.can_api === 0) { throw '接口已关闭,无法使用'; } const encryptSign = crypto.createHash('md5').update(data.code + data.secret + time.toString()).digest('hex').toString(); if (encryptSign !== sign) { throw '参数验证失败'; } this.projectData = data; yield next; } catch (err) { console.log(err); // 重定向值标段管理 // 判断是登录请求还是接口请求 if (this.helper.isAjax(this.request)) { this.body = { err: 1, msg: err, data: '', }; return; } this.session.loginError = err; yield next; } }; };