平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

85 lines
3.5 KiB

  1. using Furion.DataEncryption;
  2. using Microsoft.AspNetCore.Http;
  3. using SimpleTool;
  4. namespace SafeCampus.Plugin.Mqtt
  5. {
  6. /// <summary>
  7. /// <inheritdoc cref="IMqttService"/>
  8. /// </summary>
  9. public class MqttService : IMqttService
  10. {
  11. private readonly ISimpleCacheService _simpleCacheService;
  12. private readonly ISysUserService _sysUserService;
  13. private readonly IConfigService _configService;
  14. public MqttService(ISimpleCacheService simpleCacheService, ISysUserService sysUserService, IConfigService configService)
  15. {
  16. _simpleCacheService = simpleCacheService;
  17. _sysUserService = sysUserService;
  18. _configService = configService;
  19. }
  20. /// <inheritdoc/>
  21. public async Task<MqttParameterOutput> GetWebLoginParameter()
  22. {
  23. var user = await _sysUserService.GetUserById(UserManager.UserId);//获取用户信息
  24. var token = JWTEncryption.GetJwtBearerToken((DefaultHttpContext)App.HttpContext);// 获取当前token
  25. //获取mqtt配置
  26. var mqttconfig = await _configService.GetListByCategory(CateGoryConst.Config_MQTT_BASE);
  27. //地址
  28. var url = mqttconfig.Where(it => it.ConfigKey == DevConfigConst.MQTT_PARAM_URL).Select(it => it.ConfigValue).FirstOrDefault();
  29. //用户名
  30. var userName = mqttconfig.Where(it => it.ConfigKey == DevConfigConst.MQTT_PARAM_USERNAME).Select(it => it.ConfigValue).FirstOrDefault();
  31. //密码
  32. var password = mqttconfig.Where(it => it.ConfigKey == DevConfigConst.MQTT_PARAM_PASSWORD).Select(it => it.ConfigValue).FirstOrDefault();
  33. #region 用户名特殊处理
  34. if (userName.ToLower() == "$username")
  35. userName = user.Account;
  36. else if (userName.ToLower() == "$userid")
  37. userName = user.Id.ToString();
  38. #endregion 用户名特殊处理
  39. #region 密码特殊处理
  40. if (password.ToLower() == "$username")
  41. password = token;// 当前token作为mqtt密码
  42. #endregion 密码特殊处理
  43. var clientId = $"{user.Id}_{RandomHelper.CreateLetterAndNumber(5)}";//客户端ID
  44. _simpleCacheService.Set(MqttConst.Cache_MqttClientUser + clientId, token, TimeSpan.FromMinutes(1));//将该客户端ID对应的token插入redis后面可以根据这个判断是哪个token登录的
  45. return new MqttParameterOutput
  46. {
  47. ClientId = clientId,
  48. Password = password,
  49. Url = url,
  50. UserName = userName,
  51. Topics = new List<string> { MqttConst.Mqtt_TopicPrefix + user.Id }//默认监听自己
  52. };
  53. }
  54. /// <inheritdoc/>
  55. public async Task<MqttAuthOutput> Auth(MqttAuthInput input)
  56. {
  57. var user = await _sysUserService.GetUserByAccount(input.Username);
  58. var mqttAuthOutput = new MqttAuthOutput { Is_superuser = false, Result = "deny" };
  59. //获取用户token
  60. var tokens = _simpleCacheService.HashGetOne<List<TokenInfo>>(CacheConst.Cache_UserToken, user.Id.ToString());
  61. if (tokens != null)
  62. {
  63. if (tokens.Any(it => it.Token == input.Password))//判断是否有token
  64. mqttAuthOutput.Result = "allow";//允许登录
  65. }
  66. return mqttAuthOutput;
  67. }
  68. #region 方法
  69. #endregion 方法
  70. }
  71. }