zhengyuxuan
2025-04-07 6ae97d768948e197a89492239441feca04c1dbb9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using LifePayment.Application.Contracts;
using LifePayment.Domain.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Nest;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Identity;
using ZeroD.Util;
 
namespace LifePayment.HttpApi
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class UserRoleController : AbpController
    {
        private readonly IAccountService _accountService;
        private readonly IUserRoleService _userRoleService;
        private readonly IIdentityUserAppService _identityUserService;
        private readonly IIdentityRoleAppService _identityRoleService;
 
        public UserRoleController(
               IAccountService accountService,
               IUserRoleService userRoleService,
               IIdentityUserAppService identityUserService,
               IIdentityRoleAppService identityRoleService)
        {
            _accountService = accountService;
            _userRoleService = userRoleService;
            _identityUserService = identityUserService;
            _identityRoleService = identityRoleService;
        }
 
        /// <summary>
        /// 新增后台管理账户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Guid> CreateBackClientUser(CreateBackClientUserInput input)
        {
 
 
            return await _accountService.CreateAccount(ObjectMapper.Map<CreateBackClientUserInput, CreateAccountInput>(input), isAdminCreate: true);
        }
 
        /// <summary>
        /// 后台管理账户列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<UserDto>> GetBackClientUsers(GetBackClientUsersInput input)
        {
            return await _userRoleService.GetBackClientUsers(input);
        }
 
        /// <summary>
        /// 后台管理账户编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateBackClientUser(UpdateBackClientUserInput input)
        {
 
            var identityUser = await _identityUserService.GetAsync(input.Id);
 
            CheckExtensions.IfTrueThrowUserFriendlyException(identityUser == null,
                                                             "用户不存在");
            await _identityUserService.UpdateAsync(input.Id, new IdentityUserUpdateDto
            {
                Name = input.Name,
                PhoneNumber = input.PhoneNumber,
                UserName = input.UserName,
                RoleNames = input.RoleNames,
            });
            return await _userRoleService.UpdateBackClientUser(input);
        }
 
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public virtual async Task<int> ResetUserPassword(ResetPasswordBaseInput input)
        {
            await _userRoleService.ResetPassword(input);
 
            return Constant.SUCCESS;
        }
 
        /// <summary>
        /// 删除后台管理账户
        /// </summary>
        /// <param name="id">用户Id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<int> DeleteBackClientUser(Guid id)
        {
            await _identityUserService.DeleteAsync(id);
            return await _userRoleService.DeleteBackClientUser(id);
        }
 
        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Guid> CreateRole(CreateBaseRoleInput input)
        {
            if (input.DataRange == LifePaymentConstant.DataRange.PowerPerson)
            {
                input.Name = input.Name.Replace(LifePaymentConstant.DataRangePower.PowerPerson, string.Empty) + LifePaymentConstant.DataRangePower.PowerPerson;
            }
            else
            {
                input.Name = input.Name.Replace(LifePaymentConstant.DataRangePower.PowerAll, string.Empty) + LifePaymentConstant.DataRangePower.PowerAll;
            }
 
            var res = await _identityRoleService.CreateAsync(new IdentityRoleCreateDto
            {
                Name = input.Name,
                Sequence = input.Sequence,
                Note = input.Remark,
            });
            CreateOrUpdateRoleInput newInput = new CreateOrUpdateRoleInput()
            {
                Id = res.Id,
                Name = input.Name,
                Sequence = input.Sequence,
                DepartmentId = input.DepartmentId,
                DataRange = input.DataRange,
                Remark = input.Remark,
            };
            await _userRoleService.CreateRole(newInput);
            return res.Id;
        }
 
        /// <summary>
        /// 角色列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<RoleInfo>> GetRoles(GetRolesInput input)
        {
            return await _userRoleService.GetRoles(input);
        }
 
        /// <summary>
        /// 角色编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateRole(CreateOrUpdateRoleInput input)
        {
            if (input.DataRange == LifePaymentConstant.DataRange.PowerPerson)
            {
                input.Name = input.Name.Replace(LifePaymentConstant.DataRangePower.PowerPerson, string.Empty) + LifePaymentConstant.DataRangePower.PowerPerson;
            }
            else
            {
                input.Name = input.Name.Replace(LifePaymentConstant.DataRangePower.PowerAll, string.Empty) + LifePaymentConstant.DataRangePower.PowerAll;
            }
 
 
            await _identityRoleService.UpdateAsync(input.Id, new IdentityRoleUpdateDto
            {
                Name = input.Name,
                Sequence = input.Sequence,
                Note = input.Remark,
            });
            return await _userRoleService.UpdateRole(input);
        }
 
        /// <summary>
        /// 角色启用/禁用
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> RoleEnableOrForbid(RoleEnableOrForbidInput input)
        {
            await _identityRoleService.UpdateAsync(input.Id, new IdentityRoleUpdateDto
            {
                IsSetEnable = true,
                IsEnable = input.IsEnable,
                Name = input.Name,
            });
            return await _userRoleService.RoleEnableOrForbid(input.Id, input.IsEnable);
        }
 
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id">角色Id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<int> DeleteRole(Guid id)
        {
            await _identityRoleService.DeleteAsync(id);
            return await _userRoleService.DeleteRole(id);
        }
 
        /// <summary>
        /// 获取用户账号详情
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<BackClientUserInfoOutput> GetBackClientUserInfo()
        {
            return await _accountService.GetBackClientUserInfo();
        }
    }
}