|
@@ -7,7 +7,7 @@
|
|
|
* @date 2020/8/31
|
|
|
* @version
|
|
|
*/
|
|
|
-const UAParser = require('ua-parser-js');
|
|
|
+// const UAParser = require('ua-parser-js');
|
|
|
|
|
|
module.exports = app => {
|
|
|
class LoginLogging extends app.BaseService {
|
|
@@ -39,75 +39,16 @@ module.exports = app => {
|
|
|
*/
|
|
|
async addLoginLog(type, status) {
|
|
|
const { ctx } = this;
|
|
|
- const ip = ctx.request.ip ? ctx.request.ip : '';
|
|
|
- const ipInfo = await this.getIpInfoFromApi(ip);
|
|
|
- const parser = new UAParser(ctx.header['user-agent']);
|
|
|
- const osInfo = parser.getOS();
|
|
|
- const cpuInfo = parser.getCPU();
|
|
|
- const browserInfo = parser.getBrowser();
|
|
|
+ const ipMsg = await ctx.helper.getUserIPMsg();
|
|
|
const payload = {
|
|
|
- os: `${osInfo.name} ${osInfo.version} ${cpuInfo.architecture}`,
|
|
|
- browser: `${browserInfo.name} ${browserInfo.version}`,
|
|
|
- ip,
|
|
|
- address: ipInfo,
|
|
|
uid: ctx.session.sessionUser.accountId,
|
|
|
pid: ctx.session.sessionProject.id,
|
|
|
type,
|
|
|
show: status,
|
|
|
};
|
|
|
+ this._.assign(payload, ipMsg);
|
|
|
return await this.createLog(payload);
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据ip请求获取详细地址
|
|
|
- * @param {String} a_ip - ip地址
|
|
|
- * @return {String} 详细地址
|
|
|
- */
|
|
|
- async getIpInfoFromApi(a_ip = '') {
|
|
|
- if (!a_ip) return '';
|
|
|
- if (a_ip === '127.0.0.1' || a_ip === '::1' || a_ip.indexOf('192.168') !== -1) return '服务器本机访问';
|
|
|
- const { ip = '', region = '', city = '', isp = '' } = await this.sendRequest(a_ip);
|
|
|
- let address = '';
|
|
|
- region && (address += region + '省');
|
|
|
- city && (address += city + '市 ');
|
|
|
- isp && (address += isp + ' ');
|
|
|
- ip && (address += `(${ip})`);
|
|
|
- return address;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 发送请求获取详细地址
|
|
|
- * @param {String} ip - ip地址
|
|
|
- * @return {Object} the result of request
|
|
|
- * @private
|
|
|
- */
|
|
|
- async sendRequest(ip) {
|
|
|
- return new Promise(resolve => {
|
|
|
- this.ctx.curl(`https://api01.aliyun.venuscn.com/ip?ip=${ip}`, {
|
|
|
- dateType: 'json',
|
|
|
- encoding: 'utf8',
|
|
|
- timeout: 2000,
|
|
|
- headers: {
|
|
|
- Authorization: 'APPCODE 85c64bffe70445c4af9df7ae31c7bfcc',
|
|
|
- },
|
|
|
- }).then(({ status, data }) => {
|
|
|
- if (status === 200) {
|
|
|
- const result = JSON.parse(data.toString()).data;
|
|
|
- if (!result.ip) {
|
|
|
- resolve({});
|
|
|
- } else {
|
|
|
- resolve(result);
|
|
|
- }
|
|
|
- } else {
|
|
|
- resolve({});
|
|
|
- }
|
|
|
- }).catch(() => {
|
|
|
- resolve({});
|
|
|
- });
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 获取登录日志
|
|
|
* @param {Number} pid - 项目id
|