UserController.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. package com.jsh.erp.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.jsh.erp.base.BaseController;
  6. import com.jsh.erp.base.TableDataInfo;
  7. import com.jsh.erp.constants.BusinessConstants;
  8. import com.jsh.erp.constants.ExceptionConstants;
  9. import com.jsh.erp.datasource.entities.Tenant;
  10. import com.jsh.erp.datasource.entities.User;
  11. import com.jsh.erp.datasource.entities.UserEx;
  12. import com.jsh.erp.datasource.vo.TreeNodeEx;
  13. import com.jsh.erp.exception.BusinessParamCheckingException;
  14. import com.jsh.erp.exception.BusinessRunTimeException;
  15. import com.jsh.erp.service.RedisService;
  16. import com.jsh.erp.service.RoleService;
  17. import com.jsh.erp.service.TenantService;
  18. import com.jsh.erp.service.UserService;
  19. import com.jsh.erp.utils.*;
  20. import io.swagger.annotations.Api;
  21. import io.swagger.annotations.ApiOperation;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import org.springframework.beans.factory.annotation.Value;
  25. import org.springframework.web.bind.annotation.*;
  26. import javax.annotation.Resource;
  27. import javax.servlet.http.HttpServletRequest;
  28. import javax.servlet.http.HttpServletResponse;
  29. import java.util.*;
  30. import static com.jsh.erp.utils.ResponseJsonUtil.returnJson;
  31. import static com.jsh.erp.utils.ResponseJsonUtil.returnStr;
  32. /**
  33. * @author ji_sheng_hua 富贵ERP
  34. */
  35. @RestController
  36. @RequestMapping(value = "/user")
  37. @Api(tags = {"用户管理"})
  38. public class UserController extends BaseController {
  39. private Logger logger = LoggerFactory.getLogger(UserController.class);
  40. @Value("${manage.roleId}")
  41. private Integer manageRoleId;
  42. @Resource
  43. private UserService userService;
  44. @Resource
  45. private RoleService roleService;
  46. @Resource
  47. private TenantService tenantService;
  48. @Resource
  49. private RedisService redisService;
  50. private static String SUCCESS = "操作成功";
  51. private static String ERROR = "操作失败";
  52. @GetMapping(value = "/info")
  53. @ApiOperation(value = "根据id获取信息")
  54. public String getList(@RequestParam("id") Long id,
  55. HttpServletRequest request) throws Exception {
  56. User user = userService.getUser(id);
  57. Map<String, Object> objectMap = new HashMap<>();
  58. if(user != null) {
  59. objectMap.put("info", user);
  60. return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code);
  61. } else {
  62. return returnJson(objectMap, ErpInfo.ERROR.name, ErpInfo.ERROR.code);
  63. }
  64. }
  65. @GetMapping(value = "/list")
  66. @ApiOperation(value = "获取信息列表")
  67. public TableDataInfo getList(@RequestParam(value = Constants.SEARCH, required = false) String search,
  68. HttpServletRequest request)throws Exception {
  69. String userName = StringUtil.getInfo(search, "userName");
  70. String loginName = StringUtil.getInfo(search, "loginName");
  71. List<UserEx> list = userService.select(userName, loginName);
  72. return getDataTable(list);
  73. }
  74. @PostMapping(value = "/add")
  75. @ApiOperation(value = "新增")
  76. public String addResource(@RequestBody JSONObject obj, HttpServletRequest request)throws Exception {
  77. Map<String, Object> objectMap = new HashMap<>();
  78. int insert = userService.insertUser(obj, request);
  79. return returnStr(objectMap, insert);
  80. }
  81. @PutMapping(value = "/update")
  82. @ApiOperation(value = "修改")
  83. public String updateResource(@RequestBody JSONObject obj, HttpServletRequest request)throws Exception {
  84. Map<String, Object> objectMap = new HashMap<>();
  85. int update = userService.updateUser(obj, request);
  86. return returnStr(objectMap, update);
  87. }
  88. @DeleteMapping(value = "/delete")
  89. @ApiOperation(value = "删除")
  90. public String deleteResource(@RequestParam("id") Long id, HttpServletRequest request)throws Exception {
  91. Map<String, Object> objectMap = new HashMap<>();
  92. int delete = userService.deleteUser(id, request);
  93. return returnStr(objectMap, delete);
  94. }
  95. @DeleteMapping(value = "/deleteBatch")
  96. @ApiOperation(value = "批量删除")
  97. public String batchDeleteResource(@RequestParam("ids") String ids, HttpServletRequest request)throws Exception {
  98. Map<String, Object> objectMap = new HashMap<>();
  99. int delete = userService.batchDeleteUser(ids, request);
  100. return returnStr(objectMap, delete);
  101. }
  102. @GetMapping(value = "/checkIsNameExist")
  103. @ApiOperation(value = "检查名称是否存在")
  104. public String checkIsNameExist(@RequestParam Long id, @RequestParam(value ="name", required = false) String name,
  105. HttpServletRequest request)throws Exception {
  106. Map<String, Object> objectMap = new HashMap<>();
  107. int exist = userService.checkIsNameExist(id, name);
  108. if(exist > 0) {
  109. objectMap.put("status", true);
  110. } else {
  111. objectMap.put("status", false);
  112. }
  113. return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code);
  114. }
  115. @PostMapping(value = "/login")
  116. @ApiOperation(value = "登录")
  117. public BaseResponseInfo login(@RequestBody UserEx userParam, HttpServletRequest request)throws Exception {
  118. BaseResponseInfo res = new BaseResponseInfo();
  119. try {
  120. userService.validateCaptcha(userParam.getCode(), userParam.getUuid());
  121. Map<String, Object> data = userService.login(userParam.getLoginName().trim(), userParam.getPassword().trim(), request);
  122. res.code = 200;
  123. res.data = data;
  124. } catch (BusinessRunTimeException e) {
  125. throw new BusinessRunTimeException(e.getCode(), e.getMessage());
  126. } catch(Exception e){
  127. logger.error(e.getMessage(), e);
  128. res.code = 500;
  129. res.data = "用户登录失败";
  130. }
  131. return res;
  132. }
  133. @PostMapping(value = "/pdaLogin")
  134. @ApiOperation(value = "PDA登录")
  135. public BaseResponseInfo pdaLogin(@RequestBody UserEx userParam, HttpServletRequest request) throws Exception {
  136. BaseResponseInfo res = new BaseResponseInfo();
  137. try {
  138. Map<String, Object> data = userService.login(userParam.getLoginName().trim(), userParam.getPassword().trim(), request);
  139. res.code = 200;
  140. res.data = data;
  141. } catch (BusinessRunTimeException e) {
  142. throw new BusinessRunTimeException(e.getCode(), e.getMessage());
  143. } catch(Exception e){
  144. logger.error(e.getMessage(), e);
  145. res.code = 500;
  146. res.data = "用户登录失败";
  147. }
  148. return res;
  149. }
  150. @PostMapping(value = "/weixinLogin")
  151. @ApiOperation(value = "微信登录")
  152. public BaseResponseInfo weixinLogin(@RequestBody JSONObject jsonObject,
  153. HttpServletRequest request)throws Exception {
  154. BaseResponseInfo res = new BaseResponseInfo();
  155. try {
  156. String weixinCode = jsonObject.getString("weixinCode");
  157. User user = userService.getUserByWeixinCode(weixinCode);
  158. if(user == null) {
  159. res.code = 501;
  160. res.data = "微信未绑定";
  161. } else {
  162. logger.info("微信登录:" + user.getLoginName());
  163. Map<String, Object> data = userService.login(user.getLoginName().trim(), user.getPassword().trim(), request);
  164. res.code = 200;
  165. res.data = data;
  166. }
  167. } catch(Exception e){
  168. logger.error(e.getMessage(), e);
  169. res.code = 500;
  170. res.data = "用户登录失败";
  171. }
  172. return res;
  173. }
  174. @PostMapping(value = "/weixinBind")
  175. @ApiOperation(value = "绑定微信")
  176. public String weixinBind(@RequestBody JSONObject jsonObject,
  177. HttpServletRequest request)throws Exception {
  178. Map<String, Object> objectMap = new HashMap<>();
  179. String loginName = jsonObject.getString("loginName");
  180. String password = jsonObject.getString("password");
  181. String weixinCode = jsonObject.getString("weixinCode");
  182. int res = userService.weixinBind(loginName, password, weixinCode);
  183. if(res > 0) {
  184. return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code);
  185. } else {
  186. return returnJson(objectMap, ErpInfo.ERROR.name, ErpInfo.ERROR.code);
  187. }
  188. }
  189. @GetMapping(value = "/getUserSession")
  190. @ApiOperation(value = "获取用户信息")
  191. public BaseResponseInfo getSessionUser(HttpServletRequest request)throws Exception {
  192. BaseResponseInfo res = new BaseResponseInfo();
  193. try {
  194. Map<String, Object> data = new HashMap<>();
  195. Long userId = Long.parseLong(redisService.getObjectFromSessionByKey(request,"userId").toString());
  196. User user = userService.getUser(userId);
  197. user.setPassword(null);
  198. data.put("user", user);
  199. res.code = 200;
  200. res.data = data;
  201. } catch(Exception e){
  202. logger.error(e.getMessage(), e);
  203. res.code = 500;
  204. res.data = "获取session失败";
  205. }
  206. return res;
  207. }
  208. @GetMapping(value = "/logout")
  209. @ApiOperation(value = "退出")
  210. public BaseResponseInfo logout(HttpServletRequest request, HttpServletResponse response)throws Exception {
  211. BaseResponseInfo res = new BaseResponseInfo();
  212. try {
  213. redisService.deleteObjectBySession(request,"userId");
  214. redisService.deleteObjectBySession(request,"clientIp");
  215. } catch(Exception e){
  216. logger.error(e.getMessage(), e);
  217. res.code = 500;
  218. res.data = "退出失败";
  219. }
  220. return res;
  221. }
  222. @PostMapping(value = "/resetPwd")
  223. @ApiOperation(value = "重置密码")
  224. public String resetPwd(@RequestBody JSONObject jsonObject,
  225. HttpServletRequest request) throws Exception {
  226. Map<String, Object> objectMap = new HashMap<>();
  227. Long id = jsonObject.getLong("id");
  228. String password = "123456";
  229. String md5Pwd = Tools.md5Encryp(password);
  230. int update = userService.resetPwd(md5Pwd, id);
  231. if(update > 0) {
  232. return returnJson(objectMap, SUCCESS, ErpInfo.OK.code);
  233. } else {
  234. return returnJson(objectMap, ERROR, ErpInfo.ERROR.code);
  235. }
  236. }
  237. @PutMapping(value = "/updatePwd")
  238. @ApiOperation(value = "更新密码")
  239. public String updatePwd(@RequestBody JSONObject jsonObject, HttpServletRequest request)throws Exception {
  240. Integer flag = 0;
  241. Map<String, Object> objectMap = new HashMap<String, Object>();
  242. try {
  243. String info = "";
  244. Long userId = jsonObject.getLong("userId");
  245. String oldpwd = jsonObject.getString("oldpassword");
  246. String password = jsonObject.getString("password");
  247. User user = userService.getUser(userId);
  248. //必须和原始密码一致才可以更新密码
  249. if (oldpwd.equalsIgnoreCase(user.getPassword())) {
  250. user.setPassword(password);
  251. flag = userService.updateUserByObj(user); //1-成功
  252. info = "修改成功";
  253. } else {
  254. flag = 2; //原始密码输入错误
  255. info = "原始密码输入错误";
  256. }
  257. objectMap.put("status", flag);
  258. if(flag > 0) {
  259. return returnJson(objectMap, info, ErpInfo.OK.code);
  260. } else {
  261. return returnJson(objectMap, ERROR, ErpInfo.ERROR.code);
  262. }
  263. } catch (Exception e) {
  264. logger.error(">>>>>>>>>>>>>修改用户ID为 : " + jsonObject.getLong("userId") + "密码信息失败", e);
  265. flag = 3;
  266. objectMap.put("status", flag);
  267. return returnJson(objectMap, ERROR, ErpInfo.ERROR.code);
  268. }
  269. }
  270. /**
  271. * 用户列表,用于用户下拉框
  272. * @param request
  273. * @return
  274. * @throws Exception
  275. */
  276. @GetMapping(value = "/getUserList")
  277. @ApiOperation(value = "用户列表")
  278. public JSONArray getUserList(HttpServletRequest request)throws Exception {
  279. JSONArray dataArray = new JSONArray();
  280. try {
  281. List<User> dataList = userService.getUser(request);
  282. if (null != dataList) {
  283. for (User user : dataList) {
  284. JSONObject item = new JSONObject();
  285. item.put("id", user.getId());
  286. item.put("userName", user.getUsername());
  287. dataArray.add(item);
  288. }
  289. }
  290. } catch(Exception e){
  291. logger.error(e.getMessage(), e);
  292. }
  293. return dataArray;
  294. }
  295. /**
  296. * create by: cjl
  297. * description:
  298. * 新增用户及机构和用户关系
  299. * create time: 2019/3/8 16:06
  300. * @Param: beanJson
  301. * @return java.lang.Object
  302. */
  303. @PostMapping("/addUser")
  304. @ApiOperation(value = "新增用户")
  305. @ResponseBody
  306. public Object addUser(@RequestBody JSONObject obj, HttpServletRequest request)throws Exception{
  307. JSONObject result = ExceptionConstants.standardSuccess();
  308. User userInfo = userService.getCurrentUser();
  309. Tenant tenant = tenantService.getTenantByTenantId(userInfo.getTenantId());
  310. Long count = userService.countUser(null,null);
  311. if(tenant!=null) {
  312. if(count>= tenant.getUserNumLimit()) {
  313. throw new BusinessParamCheckingException(ExceptionConstants.USER_OVER_LIMIT_FAILED_CODE,
  314. ExceptionConstants.USER_OVER_LIMIT_FAILED_MSG);
  315. } else {
  316. UserEx ue= JSONObject.parseObject(obj.toJSONString(), UserEx.class);
  317. userService.addUserAndOrgUserRel(ue, request);
  318. }
  319. }
  320. return result;
  321. }
  322. /**
  323. * create by: cjl
  324. * description:
  325. * 修改用户及机构和用户关系
  326. * create time: 2019/3/8 16:06
  327. * @Param: beanJson
  328. * @return java.lang.Object
  329. */
  330. @PutMapping("/updateUser")
  331. @ApiOperation(value = "修改用户")
  332. @ResponseBody
  333. public Object updateUser(@RequestBody JSONObject obj, HttpServletRequest request)throws Exception{
  334. JSONObject result = ExceptionConstants.standardSuccess();
  335. UserEx ue= JSONObject.parseObject(obj.toJSONString(), UserEx.class);
  336. userService.updateUserAndOrgUserRel(ue, request);
  337. return result;
  338. }
  339. /**
  340. * 注册用户
  341. * @param ue
  342. * @return
  343. * @throws Exception
  344. */
  345. @PostMapping(value = "/registerUser")
  346. @ApiOperation(value = "注册用户")
  347. public Object registerUser(@RequestBody UserEx ue,
  348. HttpServletRequest request)throws Exception{
  349. JSONObject result = ExceptionConstants.standardSuccess();
  350. ue.setUsername(ue.getLoginName());
  351. userService.validateCaptcha(ue.getCode(), ue.getUuid());
  352. userService.checkLoginName(ue); //检查登录名
  353. userService.registerUser(ue,manageRoleId,request);
  354. return result;
  355. }
  356. /**
  357. * 获取机构用户树
  358. * @return
  359. * @throws Exception
  360. */
  361. @RequestMapping("/getOrganizationUserTree")
  362. @ApiOperation(value = "获取机构用户树")
  363. public JSONArray getOrganizationUserTree()throws Exception{
  364. JSONArray arr=new JSONArray();
  365. List<TreeNodeEx> organizationUserTree= userService.getOrganizationUserTree();
  366. if(organizationUserTree!=null&&organizationUserTree.size()>0){
  367. for(TreeNodeEx node:organizationUserTree){
  368. String str=JSON.toJSONString(node);
  369. JSONObject obj=JSON.parseObject(str);
  370. arr.add(obj) ;
  371. }
  372. }
  373. return arr;
  374. }
  375. @GetMapping(value = "/getCurrentPriceLimit")
  376. @ApiOperation(value = "查询当前用户的价格屏蔽")
  377. public BaseResponseInfo getCurrentPriceLimit(HttpServletRequest request)throws Exception {
  378. BaseResponseInfo res = new BaseResponseInfo();
  379. try {
  380. Map<String, Object> data = new HashMap<>();
  381. String priceLimit = roleService.getCurrentPriceLimit(request);
  382. data.put("priceLimit", priceLimit);
  383. res.code = 200;
  384. res.data = data;
  385. } catch(Exception e){
  386. logger.error(e.getMessage(), e);
  387. res.code = 500;
  388. res.data = "获取session失败";
  389. }
  390. return res;
  391. }
  392. /**
  393. * 获取当前用户的角色类型
  394. * @param request
  395. * @return
  396. */
  397. @GetMapping("/getRoleTypeByCurrentUser")
  398. @ApiOperation(value = "获取当前用户的角色类型")
  399. public BaseResponseInfo getRoleTypeByCurrentUser(HttpServletRequest request) {
  400. BaseResponseInfo res = new BaseResponseInfo();
  401. try {
  402. Map<String, Object> data = new HashMap<String, Object>();
  403. Long userId = userService.getUserId(request);
  404. String roleType = userService.getRoleTypeByUserId(userId).getType(); //角色类型
  405. data.put("roleType", roleType);
  406. res.code = 200;
  407. res.data = data;
  408. } catch(Exception e){
  409. logger.error(e.getMessage(), e);
  410. res.code = 500;
  411. res.data = "获取失败";
  412. }
  413. return res;
  414. }
  415. /**
  416. * 获取当前用户的按钮权限
  417. * @param request
  418. * @return
  419. */
  420. @GetMapping("/getUserBtnByCurrentUser")
  421. @ApiOperation(value = "获取当前用户的按钮权限")
  422. public BaseResponseInfo getUserBtnByCurrentUser(HttpServletRequest request) {
  423. BaseResponseInfo res = new BaseResponseInfo();
  424. try {
  425. Map<String, Object> data = new HashMap<>();
  426. Long userId = userService.getUserId(request);
  427. String loginName = userService.getUser(userId).getLoginName();
  428. JSONArray btnStrArr = userService.getBtnStrArrById(userId);
  429. if(!"admin".equals(loginName)) {
  430. data.put("userBtn", btnStrArr);
  431. }
  432. res.code = 200;
  433. res.data = data;
  434. } catch(Exception e){
  435. logger.error(e.getMessage(), e);
  436. res.code = 500;
  437. res.data = "获取失败";
  438. }
  439. return res;
  440. }
  441. /**
  442. * 获取随机校验码
  443. * @param response
  444. * @return
  445. */
  446. @GetMapping(value = "/randomImage")
  447. @ApiOperation(value = "获取随机校验码")
  448. public BaseResponseInfo randomImage(HttpServletResponse response){
  449. BaseResponseInfo res = new BaseResponseInfo();
  450. try {
  451. Map<String, Object> data = new HashMap<>();
  452. String uuid = UUID.randomUUID().toString().replaceAll("-", "") + "";
  453. String verifyKey = BusinessConstants.CAPTCHA_CODE_KEY + uuid;
  454. String codeNum = Tools.getCharAndNum(4);
  455. redisService.storageCaptchaObject(verifyKey, codeNum);
  456. String base64 = RandImageUtil.generate(codeNum);
  457. data.put("uuid", uuid);
  458. data.put("base64", base64);
  459. res.code = 200;
  460. res.data = data;
  461. } catch (Exception e) {
  462. logger.error(e.getMessage(), e);
  463. res.code = 500;
  464. res.data = "获取失败";
  465. }
  466. return res;
  467. }
  468. /**
  469. * 批量设置状态-启用或者禁用
  470. * @param jsonObject
  471. * @param request
  472. * @return
  473. */
  474. @PostMapping(value = "/batchSetStatus")
  475. @ApiOperation(value = "批量设置状态")
  476. public String batchSetStatus(@RequestBody JSONObject jsonObject,
  477. HttpServletRequest request)throws Exception {
  478. Byte status = jsonObject.getByte("status");
  479. String ids = jsonObject.getString("ids");
  480. Map<String, Object> objectMap = new HashMap<>();
  481. int res = userService.batchSetStatus(status, ids, request);
  482. if(res > 0) {
  483. return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code);
  484. } else {
  485. return returnJson(objectMap, ErpInfo.ERROR.name, ErpInfo.ERROR.code);
  486. }
  487. }
  488. /**
  489. * 获取当前用户的用户数量和租户信息
  490. * @param request
  491. * @return
  492. */
  493. @GetMapping(value = "/infoWithTenant")
  494. @ApiOperation(value = "获取当前用户的用户数量和租户信息")
  495. public BaseResponseInfo infoWithTenant(HttpServletRequest request){
  496. BaseResponseInfo res = new BaseResponseInfo();
  497. try {
  498. Map<String, Object> data = new HashMap<>();
  499. Long userId = Long.parseLong(redisService.getObjectFromSessionByKey(request,"userId").toString());
  500. User user = userService.getUser(userId);
  501. //获取当前用户数
  502. int userCurrentNum = userService.getUser(request).size();
  503. Tenant tenant = tenantService.getTenantByTenantId(user.getTenantId());
  504. if(tenant.getExpireTime()!=null && tenant.getExpireTime().getTime()<System.currentTimeMillis()){
  505. //租户已经过期,移除token
  506. redisService.deleteObjectBySession(request,"userId");
  507. redisService.deleteObjectBySession(request,"clientIp");
  508. }
  509. data.put("type", tenant.getType()); //租户类型,0免费租户,1付费租户
  510. data.put("expireTime", Tools.parseDateToStr(tenant.getExpireTime()));
  511. data.put("userCurrentNum", userCurrentNum);
  512. data.put("userNumLimit", tenant.getUserNumLimit());
  513. data.put("tenantId", tenant.getTenantId());
  514. res.code = 200;
  515. res.data = data;
  516. } catch (Exception e) {
  517. logger.error(e.getMessage(), e);
  518. res.code = 500;
  519. res.data = "获取失败";
  520. }
  521. return res;
  522. }
  523. }