liuxiaolong
2019-05-06 f99bc8c6a1d10610373738edd7d0aa0181c81d99
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package com.cloud.user.controller;
 
//import com.cloud.common.utils.AppUserUtil;
import com.cloud.model.common.Page;
import com.cloud.model.common.Result;
import com.cloud.model.log.LogAnnotation;
import com.cloud.model.log.constants.AbstractLogModule;
import com.cloud.model.sys.AppUser;
import com.cloud.model.sys.LoginAppUser;
import com.cloud.model.sys.SysRole;
import com.cloud.model.sys.constants.CredentialType;
//import com.cloud.user.feign.SmsClient;
import com.cloud.user.model.OrgNameAppUser;
import com.cloud.user.service.AppUserService;
import com.cloud.user.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
//import org.springframework.security.oauth2.common.util.OAuth2Utils;
import org.springframework.web.bind.annotation.*;
 
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
 
@Slf4j
@RestController
@RequestMapping("data/api-u")
@Api(value = "UserController", description = "用户控制层")
public class UserController {
 
    @Autowired
    private AppUserService appUserService;
 
    @Autowired
    private TokenService tokenService;
 
 
    /**
     * 当前登录用户 LoginAppUser
     */
    @GetMapping("/users/current")
    @ApiOperation(value = "获取当前登录用户", notes = "", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public LoginAppUser getLoginAppUser() {
        return tokenService.currentUser();
    }
 
    /**
     * 根据用户名查找登录对象信息
     */
    @GetMapping(value = "/users-anon/internal", params = "username")
    @ApiOperation(value = "查找用户", notes = "根据用户名字查找用户信息集合", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String", paramType = "query")
    })
    public LoginAppUser findByUsername(String username) {
        return appUserService.findByUsername(username);
    }
 
    /**
     * 用户查询
     */
    @GetMapping("/users")
    @PreAuthorize("hasAuthority('sys:user:query')")
    @ApiOperation(value = "用户查询", notes = "有orgId分库查询,没有orgId全局查询", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "start", value = "起始页", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "length", value = "条数", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "分库:orgId", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "组织id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "isAll", value = "是否查询全部", required = true, dataType = "String", paramType = "query")
    })
    public Page<OrgNameAppUser> findUsers(@RequestParam Map<String, Object> params){
        return appUserService.findUsers(params);
    }
 
 
    /**
     * 根据id查询用户
     */
    @GetMapping("/users/findUserById")
    @PreAuthorize("hasAuthority('sys:user:query')")
    @ApiOperation(value = "根据id查询用户", notes = "参数id,orgId", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params",
                    value = "{id:用户id ," +
                            " orgId:组织机构Id" +
                            "}",
                    required = true, dataType = "string", paramType = "query"),
    })
    public AppUser findUserById(@RequestParam Map<String,Object> params) {
        return appUserService.findById(params);
    }
 
    /**
     * 添加用户,根据用户名注册
     */
    @PostMapping("/users-anon/register")
    @ApiOperation(value = "添加用户", notes = "添加用户,根据用户名注册", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "分库orgId", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "officeId", value = "部门id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "username", value = "分库orgId", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "用户名", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "nickname", value = "昵称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sex", value = "性别", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "no", value = "编号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "eMail", value = "电子邮件", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "telPhone", value = "电话", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "headImgUrl", value = "头像", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "qrCode", value = "二维码路径", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "idCard", value = "身份证号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userType", value = "用户类型", required = false, dataType = "String", paramType = "query")
    })
    public AppUser register(@RequestBody AppUser appUser) {
 
        appUserService.addAppUser(appUser);
        return appUser;
    }
 
    /**
     * 逻辑删除系统用户
     * @param params
     * @return
     */
    @ApiOperation(value = "逻辑删除系统用户", notes = "逻辑删除系统用户", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping("/users-anon/delete")
    public Map<String,Object> deleteAppUser(@RequestParam Map<String,Object> params){
 
        return appUserService.deleteAppUser(params);
    }
 
    /**
     * 修改自己的个人信息
     */
    @ApiOperation(value = "修改用户", notes = "修改用户保存模块", httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LogAnnotation(module = AbstractLogModule.UPDATE_ME)
    @PutMapping("/users/me")
    public AppUser updateMe(@RequestBody AppUser appUser) {
        AppUser user = tokenService.currentUser();
        appUser.setId(user.getId());
        appUser.setEnabled(user.isEnabled());
 
        appUserService.updateAppUser(appUser);
 
        return appUser;
    }
 
    /**
     * 修改密码
     */
    @PutMapping(value = "/users/password", params = { "oldPassword", "newPassword" })
    @LogAnnotation(module = AbstractLogModule.UPDATE_PASSWORD)
    @ApiOperation(value = "修改密码", notes = "用户修改自己密码模块", httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "oldPassword", value = "旧密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "组织ID", required = true, dataType = "String", paramType = "query")
    })
    public Result updatePassword(@RequestParam  Map<String,Object> params) {
        String oldPassword = params.get("oldPassword").toString();
        String newPassword = params.get("newPassword").toString();
        if (StringUtils.isBlank(oldPassword)) {
            throw new IllegalArgumentException("旧密码不能为空");
        }
        if (StringUtils.isBlank(newPassword)) {
            throw new IllegalArgumentException("新密码不能为空");
        }
        //AppUser user = AppUserUtil.getLoginAppUser();
        appUserService.updatePassword(params);
        return Result.ok();
    }
 
    /**
     * 管理后台,给用户重置密码
     */
    @PostMapping(value = "/users/resetPassword")
    //@PreAuthorize("hasAuthority('back:user:password')")
    @ApiOperation(value = "重置密码", notes = "管理后台,给用户重置密码", httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "组织orgId", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", required = true, dataType = "String", paramType = "query")
    })
    @LogAnnotation(module = AbstractLogModule.RESET_PASSWORD)
    public Result resetPassword(@RequestParam Map<String,Object> params) {
 
        appUserService.updatePassword(params);
        return Result.ok();
    }
 
    /**
     * 用户重置密码123456
     */
    @GetMapping(value = "/users/setPassWord")
    @ApiOperation(value = "用户重置密码", notes = "系统管理用户重置密码", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "组织orgId", required = true, dataType = "Long", paramType = "query")
    })
    @LogAnnotation(module = AbstractLogModule.RESET_PASSWORD)
    public Result setPassWord(@RequestParam Map<String,Object> params) {
        AppUser appUser = new AppUser();
        appUser.setId(Long.parseLong(params.get("id").toString()));
        appUser.setOrgId(Long.parseLong(params.get("orgId").toString()));
        appUserService.setPassWord(appUser);
        return Result.ok();
    }
 
    /**
     * 管理后台修改用户
     */
    @ApiOperation(value = "管理后台修改用户", notes = "管理后台修改用户", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "分库orgId", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "officeId", value = "部门id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "username", value = "分库orgId", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "用户名", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "nickname", value = "昵称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sex", value = "性别", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "no", value = "编号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "eMail", value = "电子邮件", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "telPhone", value = "电话", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "headImgUrl", value = "头像", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "qrCode", value = "二维码路径", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "idCard", value = "身份证号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userType", value = "用户类型", required = false, dataType = "String", paramType = "query")
    })
    @LogAnnotation(module = AbstractLogModule.UPDATE_USER)
    //@PreAuthorize("hasAuthority('back:user:update')")
    @PostMapping("/users-anon/update")
    public Result updateAppUser(@RequestBody AppUser appUser) {
        appUserService.updateAppUser(appUser);
        return Result.ok();
    }
 
    /**
     * 管理后台给用户分配角色
     * id,orgId,set<Long> roleIds
     */
    @PostMapping("/users/setRoleToUser")
    @LogAnnotation(module = AbstractLogModule.SET_ROLE)
    @ApiOperation(value = "管理后台给用户分配角色", notes = "管理后台给用户分配角色", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "orgId", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "roleIds", value = "角色ids,字符串','分割", required = true, dataType = "query", paramType = "query")
    })
    //@PreAuthorize("hasAuthority('back:user:role:set')")
    public Map<String,Object> setRoleToUser(@RequestBody Map<String,Object> params) {
        Map<String,Object> map = new HashMap<>();
        int status = appUserService.setRoleToUser(params);
        if(status > 0){
            map.put("code",0);
            map.put("message","更新成功!");
        }else {
            map.put("code",1);
            map.put("message","更新失败!");
        }
        return map;
    }
 
    /**
     * 获取用户的角色列表
     * orgId,userId
     */
    @GetMapping("/users/findRolesByUserId")
    //@PreAuthorize("hasAnyAuthority('back:user:role:set','user:role:byuid')")
    @ApiOperation(value = "获取用户的角色列表", notes = "获取用户的角色列表", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "orgId", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "query")
    })
    public Set<SysRole> findRolesByUserId(@RequestParam Map<String,Object> params) {
        //TODO lllppp 先临时查出orgId,以后由前台传入
        AppUser user = appUserService.findById(params);
        if(user != null){
            return appUserService.findRolesByUserId(user.getId(),user.getOrgId());
        }
        return null;
    }
 
    /**
     * 获取用户的角色回显列表
     * id,orgId
     */
    @GetMapping("/users/findRolesIdsByUserId")
    @ApiOperation(value = "获取用户的角色回显的角色Id", notes = "获取用户的角色回显的角色Id", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "orgId", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "query")
    })
    public Map<String,Object> findRolesIdsByUserId(@RequestParam Map<String,Object> params){
 
        return appUserService.findRolesIdsByUserId(params);
    }
 
//    @Autowired
//    private SmsClient smsClient;
 
    @PostMapping(value = "/users/binding-phone")
    public void bindingPhone(String phone, String key, String code) {
        if (StringUtils.isBlank(phone)) {
            throw new IllegalArgumentException("手机号不能为空");
        }
 
        if (StringUtils.isBlank(key)) {
            throw new IllegalArgumentException("key不能为空");
        }
 
        if (StringUtils.isBlank(code)) {
            throw new IllegalArgumentException("code不能为空");
        }
 
        LoginAppUser loginAppUser =tokenService.currentUser();
        log.info("绑定手机号,key:{},code:{},username:{}", key, code, loginAppUser.getUsername());
 
        String value = "";//smsClient.matcheCodeAndGetPhone(key, code, false, 30);
        if (value == null) {
            throw new IllegalArgumentException("验证码错误");
        }
 
        if (phone.equals(value)) {
            appUserService.bindingPhone(loginAppUser.getId(), phone,loginAppUser.getOrgId());
        } else {
            throw new IllegalArgumentException("手机号不一致");
        }
    }
 
    /**
     * 用户组织机构变更
     * @param params
     * @return
     */
    @ApiOperation(value = "用户组织机构变更", notes = "用户组织机构变更", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "分库orgId", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userIds", value = "人员ids", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "officeId", value = "部门id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "updateOfficeId", value = "修改后的部门id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "updateOrgId", value = "修改后的组织机构id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping("/users/updateOfficeId")
    public Result updateOfficeId(@RequestParam Map<String, Object> params){
        try{
            return Result.ok("操作成功", appUserService.updateOfficeId(params));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
}