Browse Source

New_EntityTable: DeviceToken; iOS Push Notifications services

jiangjc 2 months ago
parent
commit
f8d64ff4e1

+ 48 - 8
OASystem/OASystem.Api/Controllers/AuthController.cs

@@ -37,10 +37,12 @@ namespace OASystem.API.Controllers
         private readonly MessageRepository _messageRep;
         private readonly IQiYeWeChatApiService _qiYeWeChatApiServic;
         private readonly IHubContext<ChatHub, IChatClient> _hubContext;
+        private readonly DeviceTokenRepository _deviceTokenRepository;
 
 
-        public AuthController(IConfiguration config, LoginRepository loginRep, IMapper mapper,MessageRepository message,
+        public AuthController(IConfiguration config, LoginRepository loginRep, IMapper mapper, MessageRepository message,
             SystemMenuPermissionRepository systemMenuPermissionRepository, IQiYeWeChatApiService qiYeWeChatApiService, MessageRepository messageRep,
+            DeviceTokenRepository deviceRep,
             IHubContext<ChatHub, IChatClient> hubContext)
         {
             _config = config;
@@ -50,6 +52,7 @@ namespace OASystem.API.Controllers
             _SystemMenuPermissionRepository = systemMenuPermissionRepository;
             _qiYeWeChatApiServic = qiYeWeChatApiService;
             _messageRep = messageRep;
+            _deviceTokenRepository = deviceRep;
             _hubContext = hubContext;
         }
 
@@ -70,7 +73,7 @@ namespace OASystem.API.Controllers
 
             #region 校验用户信息 
             var userData = _loginRep.Login(dto).Result;
-            if (userData.Code != 0)  return Ok(JsonView(false, userData.Msg)); 
+            if (userData.Code != 0) return Ok(JsonView(false, userData.Msg));
 
             #endregion
             Result authData = null;
@@ -286,15 +289,18 @@ namespace OASystem.API.Controllers
 
                     var companyDetails2 = companyDetails.GroupBy(it => it.DepId).Select(it => it.First()).ToList();
                     //部门
-                    depDetailsView = companyDetails2.Where(depIt => depIt.CompanyId == it.CompanyId).Select(depIt => {
+                    depDetailsView = companyDetails2.Where(depIt => depIt.CompanyId == it.CompanyId).Select(depIt =>
+                    {
 
                         DepDetailsView depDetails = new DepDetailsView();
                         List<JobDetailsView> jobDetails = new List<JobDetailsView>();
 
                         //岗位
-                        jobDetails = companyDetails.Where(jobIt => jobIt.DepId == depIt.DepId).Select(jobIt => {
+                        jobDetails = companyDetails.Where(jobIt => jobIt.DepId == depIt.DepId).Select(jobIt =>
+                        {
 
-                            JobDetailsView jobDetail = new JobDetailsView() { 
+                            JobDetailsView jobDetail = new JobDetailsView()
+                            {
                                 JobId = jobIt.JobId,
                                 JobName = jobIt.JobName,
                             };
@@ -375,12 +381,46 @@ namespace OASystem.API.Controllers
             //Result result = new Result();
             //var httpContext = HttpContext.User.Claims.FirstOrDefault(it => it.Type == ClaimTypes.Name)?.Value;
             //Sys_Users sys_Users = _mapper.Map<Sys_Users>(dto);
-            var _view = await _loginRep.ChangePassword(dto.UserId,dto.Password);
-            if (_view.Code == 0) return Ok(JsonView(true,"操作成功!"));
+            var _view = await _loginRep.ChangePassword(dto.UserId, dto.Password);
+            if (_view.Code == 0) return Ok(JsonView(true, "操作成功!"));
 
             return Ok(JsonView(false, _view.Msg));
         }
 
+        /// <summary>
+        /// 保存deviceToken
+        /// </summary>
+        /// <param name="dto"></param>
+        /// <returns></returns>
+        [OASystemAuthentication]
+        [HttpPost("SaveDeviceToken")]
+        [ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
+        public async Task<IActionResult> SaveDeviceToken(SaveDeviceToken dto)
+        {
+            var view = await _deviceTokenRepository.SaveToken(dto);
+
+            if (view.Code == 0) return Ok(JsonView(true, "操作成功!"));
+
+            return Ok(JsonView(false, view.Msg));
+        }
+
+        /// <summary>
+        /// 获取deviceToken
+        /// </summary>
+        /// <param name="dto"></param>
+        /// <returns></returns>
+        [OASystemAuthentication]
+        [HttpPost("GetDeviceToken")]
+        [ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
+        public async Task<IActionResult> GetDeviceToken(GetDeviceToken dto)
+        {
+            var view = await _deviceTokenRepository.GetToken(dto.account);
+
+            if (view.Code == 0) return Ok(JsonView(true, "操作成功!"));
+
+            return Ok(JsonView(false, view.Msg));
+        }
+
         /// <summary>
         /// 测试auth
         /// </summary>
@@ -826,7 +866,7 @@ namespace OASystem.API.Controllers
 
                 return Ok(JsonView(false, "操作失败!", ex.Message));
             }
-            
+
         }
 
     }

+ 19 - 1
OASystem/OASystem.Domain/Dtos/UserDto/LoginDto.cs

@@ -6,7 +6,7 @@ namespace OASystem.Domain.Dtos.UserDto;
 /// 用户登录
 /// 提交DTO
 /// </summary>
-public class LoginDto:PortDtoBase
+public class LoginDto : PortDtoBase
 {
     /// <summary>
     /// 登录账号
@@ -17,3 +17,21 @@ public class LoginDto:PortDtoBase
     /// </summary>
     public string? Password { get; set; }
 }
+
+/// <summary>
+/// 移动设备Token保存
+/// </summary>
+public class SaveDeviceToken : PortDtoBase
+{
+    public string account { get; set; }
+    public string deviceToken { get; set; }
+}
+
+
+/// <summary>
+/// 移动设备Token获取
+/// </summary>
+public class GetDeviceToken : PortDtoBase
+{
+    public string account { get; set; }
+}

+ 27 - 0
OASystem/OASystem.Domain/Entities/System/Sys_DeviceToken.cs

@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace OASystem.Domain.Entities.System
+{
+    /// <summary>
+    /// DeviceToken
+    /// </summary>
+    [SugarTable("Sys_DeviceToken")]
+    public class Sys_DeviceToken : EntityBase
+    {
+        /// <summary>
+        /// 工号
+        /// </summary>
+        [SugarColumn(IsNullable = true, ColumnDataType = "varchar(50)")]
+        public string Number { get; set; }
+
+        /// <summary>
+        /// 设备编号
+        /// </summary>
+        [SugarColumn(IsNullable = true, ColumnDataType = "nvarchar(256)")]
+        public string DeviceToken { get; set; }
+    }
+}

+ 12 - 0
OASystem/OASystem.Domain/ViewModels/LoginView.cs

@@ -37,3 +37,15 @@ public class LoginView
     /// </summary>
     public int AnnouncementUnReadCount { get; set; }
 }
+
+
+public class DeviceTokenView
+{
+    public int Id { get; set; }
+    /// <summary>
+    /// 分类名称
+    /// </summary>
+    public string Number { get; set; }
+
+    public string DeviceToken { get; set; }
+}

+ 104 - 0
OASystem/OASystem.Infrastructure/Repositories/Login/DeviceTokenRepository.cs

@@ -0,0 +1,104 @@
+using OASystem.Domain.Dtos.UserDto;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using OASystem.Domain;
+using SqlSugar;
+using OASystem.Domain.Entities.System;
+using EyeSoft.Runtime.InteropServices;
+
+namespace OASystem.Infrastructure.Repositories.Login
+{
+    public class DeviceTokenRepository : BaseRepository<Sys_DeviceToken, DeviceTokenView>
+    {
+        public DeviceTokenRepository(SqlSugarClient sqlSugar) : base(sqlSugar)
+        {
+        }
+
+        public async Task<Result> GetToken(string number)
+        {
+            Result result = new Result() { Code = -2 };
+
+            string sql = string.Format("Select * From Sys_DeviceToken With(NoLock) Where Number = '{0}'", number);
+            Sys_DeviceToken deviceTokenEntity = await GetSingleInfoBySqlWithNolockAsync(sql);
+            if (deviceTokenEntity == null)
+            {
+                result.Code = -2;
+                result.Msg = "获取失败!";
+                result.Data = null;
+            }
+            else
+            {
+
+                DeviceTokenView view = new DeviceTokenView()
+                {
+                    Id = deviceTokenEntity.Id,
+                    DeviceToken = deviceTokenEntity.DeviceToken,
+                    Number = number
+                };
+
+                result.Code = 0;
+                result.Msg = "!";
+                result.Data = view;
+            }
+
+            return result;
+        }
+
+        public async Task<Result> SaveToken(SaveDeviceToken dto)
+        {
+            Result result = new Result() { Code = -2 };
+
+            string sql = string.Format("Select * From Sys_DeviceToken With(NoLock) Where Number = '{0}'", dto.account);
+            Sys_DeviceToken deviceTokenEntity = await GetSingleInfoBySqlWithNolockAsync(sql);
+
+            if (deviceTokenEntity == null)
+            {
+                Sys_DeviceToken ist = new Sys_DeviceToken()
+                {
+                    CreateTime = DateTime.Now,
+                    CreateUserId = 234,
+                    DeleteTime = "",
+                    DeleteUserId = 0,
+                    DeviceToken = dto.deviceToken,
+                    IsDel = 0,
+                    Number = dto.account,
+                    Remark = ""
+                };
+
+                var _AddId = await _sqlSugar.Insertable(ist).ExecuteReturnIdentityAsync();
+                if (_AddId < 0)
+                {
+                    result.Code = -2;
+                    result.Msg = "添加失败!";
+                }
+                else
+                {
+                    result.Code = 0;
+                    result.Msg = "操作成功!";
+                }
+            }
+            else
+            {
+                var change = await _sqlSugar.Updateable<Sys_DeviceToken>()
+                                        .SetColumns(it => it.DeviceToken == dto.deviceToken)
+                                        .Where(it => it.Id == deviceTokenEntity.Id)
+                                        .ExecuteCommandAsync();
+                if (change > 0)
+                {
+                    result.Code = 0;
+                    result.Msg = "操作成功!";
+                }
+                else
+                {
+                    result.Code = -2;
+                    result.Msg = "更新失败!";
+                }
+            }
+
+            return result;
+        }
+    }
+}