123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using System;
- using System.IO;
- using System.Threading.Tasks;
- using Datory;
- using GxPress.Common.AppOptions;
- using GxPress.Common.Tools;
- using GxPress.Entity;
- using GxPress.Service.Interface;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Options;
- using GxPress.Service.Interface.VisitHistory;
- using GxPress.Repository.Interface.VisitHistory;
- using GxPress.EnumConst;
- namespace GxPress.Service.Implement
- {
- public class ApiLogService : IApiLogService
- {
- private readonly IConfiguration _configuration;
- private readonly Repository<UserLogin> _repository;
- private readonly Repository<User> _userRepository;
- private readonly IVisitHistoryService visitHistoryService;
- private readonly IVisitHistoryRepository visitHistoryRepository;
- public ApiLogService(IConfiguration configuration, IOptionsMonitor<DatabaseOptions> dbOptionsAccessor,
- IVisitHistoryService visitHistoryService, IVisitHistoryRepository visitHistoryRepository)
- {
- _configuration = configuration;
- var databaseType =
- StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
- //_connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
- var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
- _repository = new Repository<UserLogin>(database);
- _userRepository = new Repository<User>(database);
- this.visitHistoryService = visitHistoryService;
- this.visitHistoryRepository = visitHistoryRepository;
- }
- public async Task DataSave(HttpContext context, long responseTime, int userId)
- {
- var isLog = _configuration.GetValue<bool>("ApiLog:IsEnable");
- var requestMethod = context.Request.Method;
- var requestURL = $"{context.Request.Scheme}://{context.Request.Host}{context.Request.Path}";
- //var accessToken = context.GetTokenAsync("access_token").Result;//添加身份验证的项目可以使用此方法获取到access_toekn
- var accessToken = string.Empty;
- var requestBody = string.Empty;
- //获取用户的历史记录
- var sourceDataId = 0;
- var sourceTypeId = 0;
- GetPathValue(context.Request.Path, out sourceTypeId, out sourceDataId);
- if (sourceDataId == 0 && sourceTypeId == 0)
- return;
- if (!await visitHistoryRepository.ExistsAsync(userId, sourceDataId, sourceTypeId))
- await visitHistoryService.InsertAsync(userId, sourceDataId, sourceTypeId);
- // if (requestMethod == "POST")
- // {
- // // //接收从微信后台POST过来的数据
- // // using (var reader = new StreamReader(context.Request.Body))
- // // {
- // // requestBody = await reader.ReadToEndAsync();
- // // }
- // // context.Request.Body.Seek(0, SeekOrigin.Begin);
- // // var _reader = new StreamReader(context.Request.Body);
- // // requestBody = _reader.ReadToEnd();
- // }
- // var model = new UserLogin
- // {
- // AccessToken = accessToken,
- // AccessTime = DateTime.Now,
- // AccessAction = requestMethod,
- // AccessApiUrl = requestURL,
- // QueryString = context.Request.QueryString.ToString(),
- // Body = requestBody,
- // HttpStatus = context.Response.StatusCode,
- // ClientIP = context.Connection.RemoteIpAddress.ToString(),
- // ResponseTime = responseTime,
- // UserId = userId
- // };
- // await _repository.InsertAsync(model);
- // //
- // await _userRepository.UpdateAsync(Q.Where(nameof(User.Id), userId).Set(nameof(User.LoginTime), DateTime.Now));
- //}
- }
- public void GetPathValue(string path, out int sourceTypeId, out int sourceDataId)
- {
- if (string.IsNullOrEmpty(path))
- {
- sourceDataId = 0; sourceTypeId = 0;
- return;
- }
- if (path.Contains("article/detail/"))
- {
- sourceTypeId = AllTypeConst.Article.GetHashCode();
- sourceDataId = int.Parse(path.Split('/')[path.Split('/').Length]);
- return;
- }
- sourceDataId = 0; sourceTypeId = 0;
- }
- }
- }
|