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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
| package com.jsh.erp.controller;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.jsh.erp.constants.BusinessConstants; import com.jsh.erp.constants.ExceptionConstants; import com.jsh.erp.datasource.entities.Tenant; import com.jsh.erp.datasource.entities.User; import com.jsh.erp.datasource.entities.UserEx; import com.jsh.erp.datasource.vo.TreeNodeEx; import com.jsh.erp.exception.BusinessParamCheckingException; import com.jsh.erp.service.log.LogService; import com.jsh.erp.service.tenant.TenantService; import com.jsh.erp.service.user.UserService; import com.jsh.erp.utils.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.net.URLEncoder; import java.security.NoSuchAlgorithmException; import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;
import static com.jsh.erp.utils.ResponseJsonUtil.returnJson;
@RestController @RequestMapping(value = "/user") public class UserController { private Logger logger = LoggerFactory.getLogger(UserController.class);
@Value("${manage.roleId}") private Integer manageRoleId;
@Resource private UserService userService;
@Resource private TenantService tenantService;
@Resource private LogService logService;
private static String message = "成功"; private static final String HTTP = "http://"; private static final String CODE_OK = "200";
@PostMapping(value = "/login") public BaseResponseInfo login(@RequestParam(value = "loginName", required = false) String loginName, @RequestParam(value = "password", required = false) String password, HttpServletRequest request)throws Exception { logger.info("============用户登录 login 方法调用开始=============="); String msgTip = ""; User user=null; BaseResponseInfo res = new BaseResponseInfo(); try { String username = loginName.trim(); password = password.trim(); Object userInfo = request.getSession().getAttribute("user"); User sessionUser = new User(); if (userInfo != null) { sessionUser = (User) userInfo; } if (sessionUser != null && username.equalsIgnoreCase(sessionUser.getLoginame())) { logger.info("====用户 " + username + "已经登录过, login 方法调用结束===="); msgTip = "user already login"; } int userStatus = -1; try { request.getSession().removeAttribute("tenantId"); userStatus = userService.validateUser(username, password); } catch (Exception e) { e.printStackTrace(); logger.error(">>>>>>>>>>>>>用户 " + username + " 登录 login 方法 访问服务层异常====", e); msgTip = "access service exception"; } switch (userStatus) { case ExceptionCodeConstants.UserExceptionCode.USER_NOT_EXIST: msgTip = "user is not exist"; break; case ExceptionCodeConstants.UserExceptionCode.USER_PASSWORD_ERROR: msgTip = "user password error"; break; case ExceptionCodeConstants.UserExceptionCode.BLACK_USER: msgTip = "user is black"; break; case ExceptionCodeConstants.UserExceptionCode.USER_ACCESS_EXCEPTION: msgTip = "access service error"; break; default: try { msgTip = "user can login"; user = userService.getUserByUserName(username); request.getSession().setAttribute("user",user); if(user.getTenantId()!=null) { Tenant tenant = tenantService.getTenantByTenantId(user.getTenantId()); if(tenant!=null) { Long tenantId = tenant.getTenantId(); Integer userNumLimit = tenant.getUserNumLimit(); Integer billsNumLimit = tenant.getBillsNumLimit(); if(tenantId!=null) { request.getSession().setAttribute("tenantId",tenantId); request.getSession().setAttribute("userNumLimit",userNumLimit); request.getSession().setAttribute("billsNumLimit",billsNumLimit); } } } logService.insertLog("用户", new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_LOGIN).append(user.getId()).toString(), ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()); } catch (Exception e) { e.printStackTrace(); logger.error(">>>>>>>>>>>>>>>查询用户名为:" + username + " ,用户信息异常", e); } break; } Map<String, Object> data = new HashMap<String, Object>(); data.put("msgTip", msgTip);
if(user!=null){ data.put("user",user); } res.code = 200; res.data = data; logger.info("===============用户登录 login 方法调用结束==============="); } catch(Exception e){ e.printStackTrace(); logger.error(e.getMessage()); res.code = 500; res.data = "用户登录失败"; } return res; }
@GetMapping(value = "/getUserSession") public BaseResponseInfo getSessionUser(HttpServletRequest request)throws Exception { BaseResponseInfo res = new BaseResponseInfo(); try { Map<String, Object> data = new HashMap<String, Object>(); Object userInfo = request.getSession().getAttribute("user"); if(userInfo!=null) { User user = (User) userInfo; user.setPassword(null); data.put("user", user); } res.code = 200; res.data = data; } catch(Exception e){ e.printStackTrace(); res.code = 500; res.data = "获取session失败"; } return res; }
@GetMapping(value = "/logout") public BaseResponseInfo logout(HttpServletRequest request, HttpServletResponse response)throws Exception { BaseResponseInfo res = new BaseResponseInfo(); try { request.getSession().removeAttribute("user"); request.getSession().removeAttribute("tenantId"); request.getSession().removeAttribute("userNumLimit"); request.getSession().removeAttribute("billsNumLimit"); response.sendRedirect("/login.html"); } catch(Exception e){ e.printStackTrace(); res.code = 500; res.data = "退出失败"; } return res; }
@PostMapping(value = "/resetPwd") public String resetPwd(@RequestParam("id") Long id, HttpServletRequest request) throws Exception { Map<String, Object> objectMap = new HashMap<String, Object>(); String password = "123456"; String md5Pwd = Tools.md5Encryp(password); int update = userService.resetPwd(md5Pwd, id); if(update > 0) { return returnJson(objectMap, message, ErpInfo.OK.code); } else { return returnJson(objectMap, message, ErpInfo.ERROR.code); } }
@PostMapping(value = "/updatePwd") public String updatePwd(@RequestParam("userId") Long userId, @RequestParam("password") String password, @RequestParam("oldpwd") String oldpwd, HttpServletRequest request)throws Exception { Integer flag = 0; Map<String, Object> objectMap = new HashMap<String, Object>(); try { User user = userService.getUser(userId); String oldPassword = Tools.md5Encryp(oldpwd); String md5Pwd = Tools.md5Encryp(password); if(user.getLoginame().equals("jsh")){ flag = 3; } else if (oldPassword.equalsIgnoreCase(user.getPassword())) { user.setPassword(md5Pwd); flag = userService.updateUserByObj(user); } else { flag = 2; } objectMap.put("status", flag); if(flag > 0) { return returnJson(objectMap, message, ErpInfo.OK.code); } else { return returnJson(objectMap, message, ErpInfo.ERROR.code); } } catch (Exception e) { logger.error(">>>>>>>>>>>>>修改用户ID为 : " + userId + "密码信息失败", e); flag = 3; objectMap.put("status", flag); return returnJson(objectMap, message, ErpInfo.ERROR.code); } }
@GetMapping(value = "/getAllList") public BaseResponseInfo getAllList(HttpServletRequest request)throws Exception { BaseResponseInfo res = new BaseResponseInfo(); try { Map<String, Object> data = new HashMap<String, Object>(); List<User> dataList = userService.getUser(); if(dataList!=null) { data.put("userList", dataList); } res.code = 200; res.data = data; } catch(Exception e){ e.printStackTrace(); res.code = 500; res.data = "获取失败"; } return res; }
@GetMapping(value = "/getUserList") public String getUserList(@RequestParam(value = Constants.PAGE_SIZE, required = false) Integer pageSize, @RequestParam(value = Constants.CURRENT_PAGE, required = false) Integer currentPage, @RequestParam(value = Constants.SEARCH, required = false) String search)throws Exception {
Map<String, Object> parameterMap = new HashMap<String, Object>(); JSONObject obj= JSON.parseObject(search); Set<String> key= obj.keySet(); for(String keyEach: key){ parameterMap.put(keyEach,obj.getString(keyEach)); } PageQueryInfo queryInfo = new PageQueryInfo(); Map<String, Object> objectMap = new HashMap<String, Object>(); if (pageSize == null || pageSize <= 0) { pageSize = BusinessConstants.DEFAULT_PAGINATION_PAGE_SIZE; } if (currentPage == null || currentPage <= 0) { currentPage = BusinessConstants.DEFAULT_PAGINATION_PAGE_NUMBER; } PageHelper.startPage(currentPage,pageSize,true); List<UserEx> list = userService.getUserList(parameterMap); PageInfo<UserEx> pageInfo = new PageInfo<>(list); objectMap.put("page", queryInfo); if (list == null) { queryInfo.setRows(new ArrayList<Object>()); queryInfo.setTotal(BusinessConstants.DEFAULT_LIST_NULL_NUMBER); return returnJson(objectMap, "查找不到数据", ErpInfo.OK.code); } queryInfo.setRows(list); queryInfo.setTotal(pageInfo.getTotal()); return returnJson(objectMap, ErpInfo.OK.name, ErpInfo.OK.code); }
@PostMapping("/addUser") @ResponseBody public Object addUser(@RequestParam("info") String beanJson, HttpServletRequest request)throws Exception{ JSONObject result = ExceptionConstants.standardSuccess(); Long userNumLimit = Long.parseLong(request.getSession().getAttribute("userNumLimit").toString()); Long count = userService.countUser(null,null); if(count>= userNumLimit) { throw new BusinessParamCheckingException(ExceptionConstants.USER_OVER_LIMIT_FAILED_CODE, ExceptionConstants.USER_OVER_LIMIT_FAILED_MSG); } else { UserEx ue= JSON.parseObject(beanJson, UserEx.class); userService.addUserAndOrgUserRel(ue); } return result; }
@PostMapping(value = "/registerUser") public Object registerUser(@RequestParam(value = "loginame", required = false) String loginame, @RequestParam(value = "password", required = false) String password, HttpServletRequest request)throws Exception{ JSONObject result = ExceptionConstants.standardSuccess(); UserEx ue= new UserEx(); ue.setUsername(loginame); ue.setLoginame(loginame); ue.setPassword(password); userService.checkUserNameAndLoginName(ue); ue = userService.registerUser(ue,manageRoleId,request); return result; }
@PostMapping("/updateUser") @ResponseBody public Object updateUser(@RequestParam("info") String beanJson,@RequestParam("id") Long id)throws Exception{ JSONObject result = ExceptionConstants.standardSuccess(); UserEx ue= JSON.parseObject(beanJson, UserEx.class); ue.setId(id); userService.updateUserAndOrgUserRel(ue); return result; } @PostMapping("/deleteUser") @ResponseBody public Object deleteUser(@RequestParam("ids") String ids)throws Exception{ JSONObject result = ExceptionConstants.standardSuccess(); userService.batDeleteUser(ids); return result; } @PostMapping("/batchDeleteUser") @ResponseBody public Object batchDeleteUser(@RequestParam("ids") String ids)throws Exception{ JSONObject result = ExceptionConstants.standardSuccess(); userService.batDeleteUser(ids); return result; } @RequestMapping("/getOrganizationUserTree") public JSONArray getOrganizationUserTree()throws Exception{ JSONArray arr=new JSONArray(); List<TreeNodeEx> organizationUserTree= userService.getOrganizationUserTree(); if(organizationUserTree!=null&&organizationUserTree.size()>0){ for(TreeNodeEx node:organizationUserTree){ String str=JSON.toJSONString(node); JSONObject obj=JSON.parseObject(str); arr.add(obj) ; } } return arr; } }
|