Skip to content

Commit c1546d8

Browse files
committed
feat(消息通知): ✨ 完善站内信和消息通知功能
1 parent 21ecc33 commit c1546d8

File tree

94 files changed

+5467
-2236
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+5467
-2236
lines changed

api/SimpleAdmin/SimpleAdmin.Core/SimpleAdmin.Core.csproj

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
2+
33

44
<ItemGroup>
5-
<PackageReference Include="Lazy.Captcha.Core" Version="2.0.7" />
5+
<PackageReference Include="Lazy.Captcha.Core" Version="2.0.7"/>
66
<PackageReference Include="Minio" Version="5.0.0"/>
7-
<PackageReference Include="MoYu.Extras.Authentication.JwtBearer" Version="1.0.6" />
8-
<PackageReference Include="MoYu.Extras.ObjectMapper.Mapster" Version="1.0.6" />
9-
<PackageReference Include="MoYu.Pure" Version="1.0.6" />
10-
<PackageReference Include="NewLife.Core" Version="10.10.2024.601" />
7+
<PackageReference Include="MoYu.Extras.Authentication.JwtBearer" Version="1.0.6"/>
8+
<PackageReference Include="MoYu.Extras.ObjectMapper.Mapster" Version="1.0.6"/>
9+
<PackageReference Include="MoYu.Pure" Version="1.0.6"/>
10+
<PackageReference Include="NewLife.Core" Version="10.10.2024.601"/>
1111
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0"/>
12-
<PackageReference Include="SimpleTool" Version="1.0.6"/>
13-
<PackageReference Include="System.Drawing.Common" Version="8.0.6" />
12+
<PackageReference Include="SimpleTool" Version="1.0.7"/>
13+
<PackageReference Include="System.Drawing.Common" Version="8.0.6"/>
1414
<PackageReference Include="Yitter.IdGenerator" Version="1.0.14"/>
15-
<PackageReference Include="Masuit.Tools.Core" Version="2024.3.4" />
15+
<PackageReference Include="Masuit.Tools.Core" Version="2024.3.4"/>
1616
<PackageReference Include="SharpZipLib" Version="1.4.2"/>
1717

1818
</ItemGroup>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace SimpleAdmin.Plugin.Mqtt;
2+
3+
/// <summary>
4+
/// mqtt常量
5+
/// </summary>
6+
public class MqttConst
7+
{
8+
/// <summary>
9+
/// mqtt认证登录信息key
10+
/// </summary>
11+
public const string CACHE_MQTT_CLIENT_USER = CacheConst.CACHE_PREFIX_WEB + "MqttClientUser:";
12+
13+
/// <summary>
14+
/// mqtt主题前缀
15+
/// </summary>
16+
public const string MQTT_TOPIC_PREFIX = "SimpleAdmin/";
17+
18+
/// <summary>
19+
/// 登出
20+
/// </summary>
21+
public const string MQTT_MESSAGE_LOGIN_OUT = "LoginOut";
22+
23+
/// <summary>
24+
/// 新消息
25+
/// </summary>
26+
public const string MQTT_MESSAGE_NEW = "NewMessage";
27+
28+
/// <summary>
29+
/// 修改密码
30+
/// </summary>
31+
public const string MQTT_MESSAGE_UPDATE_PASSWORD = "UpdatePassword";
32+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace SimpleAdmin.Plugin.Mqtt;
2+
3+
/// <summary>
4+
/// mqtt服务控制器
5+
/// </summary>
6+
[ApiDescriptionSettings(Tag = "mqtt服务")]
7+
[Route("mqtt")]
8+
public class MqttController : IDynamicApiController
9+
{
10+
private readonly IMqttService _mqttService;
11+
12+
public MqttController(IMqttService mqttService)
13+
{
14+
_mqttService = mqttService;
15+
}
16+
17+
/// <summary>
18+
/// 获取mqtt登录参数
19+
/// </summary>
20+
/// <returns></returns>
21+
[HttpGet("getParameter")]
22+
public async Task<dynamic> GetParameter()
23+
{
24+
return await _mqttService.GetWebLoginParameter();
25+
}
26+
27+
/// <summary>
28+
/// mqtt认证
29+
/// </summary>
30+
/// <param name="input"></param>
31+
/// <returns></returns>
32+
[HttpPost("auth")]
33+
[AllowAnonymous]
34+
[NonUnify]
35+
public async Task<dynamic> Auth([FromBody] MqttAuthInput input)
36+
{
37+
return await _mqttService.Auth(input);
38+
}
39+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
global using MoYu;
2+
global using MoYu.DependencyInjection;
3+
global using Microsoft.AspNetCore.Builder;
4+
global using Microsoft.AspNetCore.Hosting;
5+
global using Microsoft.Extensions.DependencyInjection;
6+
global using SimpleAdmin.Core;
7+
global using SimpleAdmin.Cache;
8+
global using SimpleAdmin.SqlSugar;
9+
global using SimpleMQTT;
10+
global using Microsoft.AspNetCore.Authorization;
11+
global using Microsoft.AspNetCore.Mvc;
12+
global using SimpleAdmin.System;
13+
global using MoYu.DynamicApiController;
14+
global using MoYu.DataEncryption;
15+
global using Microsoft.AspNetCore.Http;
16+
global using SimpleTool;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
//mqtt设置
3+
"MqttSettings": {
4+
"Host": "127.0.0.1",
5+
"Port": 1883,
6+
"UserName": "admin",
7+
"SecretKey": "admin",
8+
"ClientId": "SimpleAdminWeb"
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
//mqtt设置
3+
"MqttSettings": {
4+
"Host": "127.0.0.1",
5+
"Port": 1883,
6+
"UserName": "admin",
7+
"SecretKey": "admin",
8+
"ClientId": "SimpleAdminWeb"
9+
}
10+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace SimpleAdmin.Plugin.Mqtt;
2+
3+
/// <summary>
4+
/// mqtt组件
5+
/// </summary>
6+
public sealed class MqttComponent : IServiceComponent
7+
{
8+
/// <summary>
9+
/// ConfigureServices中不能解析服务,比如App.GetService(),尤其是不能在ConfigureServices中获取诸如缓存等数据进行初始化,应该在Configure中进行
10+
/// 服务都还没初始化完成,会导致内存中存在多份 IOC 容器!!
11+
/// 正确应该在 Configure 中,这个时候服务(IServiceCollection 已经完成 BuildServiceProvider() 操作了
12+
/// </summary>
13+
/// <param name="services"></param>
14+
/// <param name="componentContext"></param>
15+
public void Load(IServiceCollection services, ComponentContext componentContext)
16+
{
17+
Console.WriteLine("注册Mqtt插件");
18+
services.AddMqttClientManager();
19+
}
20+
}
21+
22+
/// <summary>
23+
/// mqtt组件
24+
/// 模拟 Configure
25+
/// </summary>
26+
public sealed class MqttApplicationComponent : IApplicationComponent
27+
{
28+
public void Load(IApplicationBuilder app, IWebHostEnvironment env, ComponentContext componentContext)
29+
{
30+
App.GetService<IMqttClientManager>();//获取mqtt服务判断配置是否有问题
31+
}
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace SimpleAdmin.Plugin.Mqtt;
2+
3+
/// <summary>
4+
/// mqtt认证参数
5+
/// </summary>
6+
public class MqttAuthInput
7+
{
8+
/// <summary>
9+
/// 用户ID
10+
/// </summary>
11+
public string UserId { get; set; }
12+
13+
/// <summary>
14+
/// 账号
15+
/// </summary>
16+
public string Username { get; set; }
17+
18+
/// <summary>
19+
/// 密码
20+
/// </summary>
21+
public string Password { get; set; }
22+
23+
/// <summary>
24+
/// 客户端ID
25+
/// </summary>
26+
public string ClientId { get; set; }
27+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace SimpleAdmin.Plugin.Mqtt;
2+
3+
/// <summary>
4+
/// mqtt登录参数输出
5+
/// </summary>
6+
public class MqttParameterOutput
7+
{
8+
/// <summary>
9+
/// 地址
10+
/// </summary>
11+
public string Url { get; set; }
12+
13+
/// <summary>
14+
/// 用户名
15+
/// </summary>
16+
public string UserName { get; set; }
17+
18+
/// <summary>
19+
/// 密码
20+
/// </summary>
21+
22+
public string Password { get; set; }
23+
24+
/// <summary>
25+
/// 客户端ID
26+
/// </summary>
27+
public string ClientId { get; set; }
28+
29+
/// <summary>
30+
/// 主题列表
31+
/// </summary>
32+
public List<string> Topics { get; set; }
33+
}
34+
35+
/// <summary>
36+
/// mqtt认证输出
37+
/// </summary>
38+
public class MqttAuthOutput
39+
{
40+
/// <summary>
41+
/// 结果 "allow" | "deny" | "ignore", // Default `"ignore"`
42+
/// </summary>
43+
public string Result { get; set; } = "deny";
44+
45+
/// <summary>
46+
/// 是否超级管理员
47+
/// </summary>
48+
public bool Is_superuser { get; set; } = false;
49+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace SimpleAdmin.Plugin.Mqtt;
2+
3+
/// <summary>
4+
///Mqtt服务
5+
/// </summary>
6+
public interface IMqttService : ITransient
7+
{
8+
/// <summary>
9+
/// mqtt登录http认证
10+
/// </summary>
11+
/// <param name="input">认证参数</param>
12+
/// <param name="userId">用户Id</param>
13+
/// <returns>认证结果</returns>
14+
Task<MqttAuthOutput> Auth(MqttAuthInput input);
15+
16+
/// <summary>
17+
/// 获取mqtt登录web端参数
18+
/// </summary>
19+
/// <returns>登录参数</returns>
20+
Task<MqttParameterOutput> GetWebLoginParameter();
21+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
namespace SimpleAdmin.Plugin.Mqtt;
2+
3+
/// <summary>
4+
/// <inheritdoc cref="IMqttService"/>
5+
/// </summary>
6+
public class MqttService : IMqttService
7+
{
8+
private readonly ISimpleCacheService _simpleCacheService;
9+
private readonly ISysUserService _sysUserService;
10+
private readonly IConfigService _configService;
11+
12+
public MqttService(ISimpleCacheService simpleCacheService, ISysUserService sysUserService, IConfigService configService)
13+
{
14+
_simpleCacheService = simpleCacheService;
15+
_sysUserService = sysUserService;
16+
_configService = configService;
17+
}
18+
19+
/// <inheritdoc/>
20+
public async Task<MqttParameterOutput> GetWebLoginParameter()
21+
{
22+
var user = await _sysUserService.GetUserById(UserManager.UserId);//获取用户信息
23+
var token = JWTEncryption.GetJwtBearerToken((DefaultHttpContext)App.HttpContext);// 获取当前token
24+
//获取mqtt配置
25+
var mqttconfig = await _configService.GetConfigsByCategory(CateGoryConst.CONFIG_MQTT_BASE);
26+
//地址
27+
var url = mqttconfig.Where(it => it.ConfigKey == SysConfigConst.MQTT_PARAM_URL).Select(it => it.ConfigValue).FirstOrDefault();
28+
//用户名
29+
var userName = mqttconfig.Where(it => it.ConfigKey == SysConfigConst.MQTT_PARAM_USERNAME).Select(it => it.ConfigValue).FirstOrDefault();
30+
//密码
31+
var password = mqttconfig.Where(it => it.ConfigKey == SysConfigConst.MQTT_PARAM_PASSWORD).Select(it => it.ConfigValue).FirstOrDefault();
32+
33+
#region 用户名特殊处理
34+
35+
if (userName.ToLower() == "$username")
36+
userName = user.Account;
37+
else if (userName.ToLower() == "$userid")
38+
userName = user.Id.ToString();
39+
40+
#endregion 用户名特殊处理
41+
42+
#region 密码特殊处理
43+
44+
if (password.ToLower() == "$username")
45+
password = token;// 当前token作为mqtt密码
46+
47+
#endregion 密码特殊处理
48+
49+
var clientId = $"{user.Id}_{RandomHelper.CreateLetterAndNumber(5)}";//客户端ID
50+
_simpleCacheService.Set(MqttConst.CACHE_MQTT_CLIENT_USER + clientId, token,
51+
TimeSpan.FromMinutes(1));//将该客户端ID对应的token插入redis后面可以根据这个判断是哪个token登录的
52+
return new MqttParameterOutput
53+
{
54+
ClientId = clientId,
55+
Password = password,
56+
Url = url,
57+
UserName = userName,
58+
Topics = new List<string> { MqttConst.MQTT_TOPIC_PREFIX + user.Id }//默认监听自己
59+
};
60+
}
61+
62+
/// <inheritdoc/>
63+
public async Task<MqttAuthOutput> Auth(MqttAuthInput input)
64+
{
65+
var user = await _sysUserService.GetUserByAccount(input.Username);
66+
var mqttAuthOutput = new MqttAuthOutput { Is_superuser = false, Result = "deny" };
67+
//获取用户token
68+
var tokens = _simpleCacheService.HashGetOne<List<TokenInfo>>(CacheConst.CACHE_USER_TOKEN, user.Id.ToString());
69+
if (tokens != null)
70+
{
71+
if (tokens.Any(it => it.Token == input.Password))//判断是否有token
72+
mqttAuthOutput.Result = "allow";//允许登录
73+
}
74+
return mqttAuthOutput;
75+
}
76+
}

0 commit comments

Comments
 (0)