feat: 添加访问统计模块

This commit is contained in:
陈淳
2023-09-15 15:05:14 +08:00
parent c2ca0b1f29
commit c988ed7988
10 changed files with 164 additions and 12 deletions

View File

@@ -0,0 +1,17 @@
import request from '@/utils/request'
// 触发访问
export function access() {
return request({
url: '/access-log',
method: 'post'
})
}
// 获取本周数据
export function getWeek() {
return request({
url: '/access-log/week',
method: 'get'
})
}

View File

@@ -8,30 +8,34 @@ import { LineChart } from 'echarts/charts';
import { UniversalTransition } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';
import { ref ,onMounted} from 'vue';
import { getWeek } from '@/apis/accessApi.js'
echarts.use([GridComponent, LineChart, CanvasRenderer, UniversalTransition]);
const VisitsLineChart=ref(null);
onMounted(()=>{
onMounted(async()=>{
var myChart = echarts.init(VisitsLineChart.value, null, {
width: 320,
height: 230
});
var option;
const response=await getWeek();
var numberData=response.data.map(x=>x.number);
option = {
xAxis: {
type: 'category',
boundaryGap: false,
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
},
yAxis: {
type: 'value'
},
series: [
{
data: [82, 93, 90, 93, 129, 133, 132],
data: numberData,
type: 'line',
areaStyle: {}
}

View File

@@ -103,6 +103,7 @@ import AvatarInfo from '@/components/AvatarInfo.vue'
import BottomInfo from '@/components/BottomInfo.vue'
import VisitsLineChart from '@/components/echars/VisitsLineChart.vue'
import { access } from '@/apis/accessApi.js'
import { getList } from '@/apis/plateApi.js'
import { getList as bannerGetList } from '@/apis/bannerApi.js'
import { getList as discussGetList } from '@/apis/discussApi.js'
@@ -121,6 +122,7 @@ const query = reactive({
//初始化
onMounted(async () => {
await access();
plateList.value = (await getList()).data.items;
discussList.value = (await discussGetList(query)).data.items;
bannerList.value = (await bannerGetList()).data.items

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Furion.Application.Bbs.Services
{
public interface IAccessLogService
{
}
}

View File

@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mapster;
using Yi.Framework.Infrastructure.Ddd.Repositories;
using Yi.Furion.Core.Bbs.Dtos.AccessLog;
using Yi.Furion.Core.Bbs.Entities;
namespace Yi.Furion.Application.Bbs.Services.Impl
{
public class AccessLogService : IAccessLogService,IDynamicApiController
{
private readonly IRepository<AccessLogEntity> _repository;
public AccessLogService(IRepository<AccessLogEntity> repository) { _repository = repository; }
/// <summary>
/// 触发
/// </summary>
/// <returns></returns>
[HttpPost("")]
public async Task AccessAsync()
{
//可判断http重复防止同一ip多次访问
var last = await _repository._DbQueryable.OrderByDescending(x => x.CreationTime).FirstAsync();
if (last is null || last.CreationTime.Date != DateTime.Today)
{
await _repository.InsertAsync(new AccessLogEntity());
}
else
{
await _repository._Db.Updateable<AccessLogEntity>().SetColumns(it => it.Number == it.Number + 1).Where(it => it.Id == last.Id).ExecuteCommandAsync();
}
}
/// <summary>
/// 获取当前周数据
/// </summary>
/// <returns></returns>
public async Task<AccessLogDto[]> GetWeekAsync()
{
var lastSeven = await _repository._DbQueryable.OrderByDescending(x => x.CreationTime).ToPageListAsync(1, 7);
return WeekTimeHandler(lastSeven.ToArray());
}
private AccessLogDto[] WeekTimeHandler(AccessLogEntity[] data)
{
data = data.Where(x => x.CreationTime.DayOfWeek == DateTime.Now.DayOfWeek).ToArray();
Dictionary<DayOfWeek, AccessLogDto> processedData = new Dictionary<DayOfWeek, AccessLogDto>();
// 初始化字典将每天的数据都设为0
foreach (DayOfWeek dayOfWeek in Enum.GetValues(typeof(DayOfWeek)))
{
processedData.Add(dayOfWeek, new AccessLogDto());
}
// 处理原始数据
foreach (var item in data)
{
DayOfWeek dayOfWeek = item.CreationTime.DayOfWeek;
// 如果当天有数据则更新字典中的值为对应的Number
var sss= data.Adapt<AccessLogDto>();
processedData[dayOfWeek] = item.Adapt<AccessLogDto>();
}
return processedData.Values.ToArray();
}
}
}

View File

@@ -34,6 +34,18 @@
Label服务抽象
</summary>
</member>
<member name="M:Yi.Furion.Application.Bbs.Services.Impl.AccessLogService.AccessAsync">
<summary>
触发
</summary>
<returns></returns>
</member>
<member name="M:Yi.Furion.Application.Bbs.Services.Impl.AccessLogService.GetWeekAsync">
<summary>
获取当前周数据
</summary>
<returns></returns>
</member>
<member name="T:Yi.Furion.Application.Bbs.Services.Impl.AgreeService">
<summary>
点赞功能

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Helper;
namespace Yi.Furion.Core.Bbs.Dtos.AccessLog
{
public class AccessLogDto
{
public long Id { get; set; }
public long Number { get; set; }
public DateTime? LastModificationTime { get; set; }
public DateTime CreationTime { get; set; }
}
}

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using Yi.Framework.Infrastructure.Data.Auditing;
using Yi.Framework.Infrastructure.Ddd.Entities;
using Yi.Framework.Infrastructure.Helper;
namespace Yi.Furion.Core.Bbs.Entities
{
[SugarTable("AccessLog")]
public class AccessLogEntity : IEntity<long>, IHasModificationTime, IHasCreationTime
{
[SugarColumn(IsPrimaryKey = true)]
public long Id { get; set; } = SnowflakeHelper.NextId;
public long Number { get; set; }
public DateTime? LastModificationTime { get; set; }
public DateTime CreationTime { get; set; }
}
}

View File

@@ -28,10 +28,4 @@
<ItemGroup>
<Folder Include="Bbs\Dtos\" />
</ItemGroup>
</Project>

View File

@@ -21,7 +21,7 @@
"Url": "DataSource=yi-sqlsugar-dev.db",
"DbType": "Sqlite",
"EnabledReadWrite": false,
"EnabledCodeFirst": true,
"EnabledCodeFirst": false,
"ReadUrl": [
"DataSource=[xxxx]", //Sqlite
"server=[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]", //Mysql
@@ -29,7 +29,7 @@
]
},
"EnabledDataSeed": true,
"EnabledDataSeed": false,
"JWTSettings": {
"ValidateIssuerSigningKey": true, // 是否验证密钥bool 类型默认true