Java tutorial
package com.clt.systemmanger.controller; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.encoding.Md5PasswordEncoder; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.clt.common.UserSession; import com.clt.sub.model.TSubsuppliers; import com.clt.sub.model.TUserCode; import com.clt.sub.service.ISubsuppliersService; import com.clt.sub.service.IUserCodeService; import com.clt.systemmanger.model.TArchiveType; import com.clt.systemmanger.model.TStores; import com.clt.systemmanger.model.TTokenUser; import com.clt.systemmanger.model.TUser; import com.clt.systemmanger.model.TUserGps; import com.clt.systemmanger.service.IArchiveTypeService; import com.clt.systemmanger.service.IStaticService; import com.clt.systemmanger.service.IStoresService; import com.clt.systemmanger.service.ITokenUserService; import com.clt.systemmanger.service.IUserGpsService; import com.clt.systemmanger.service.IUserRoleService; import com.clt.systemmanger.service.IUserService; import com.clt.util.AjaxUtil; import com.clt.util.DateUtil; import com.clt.util.HqlHelper; import com.clt.util.Page; import com.clt.util.ServiceUtil; import com.clt.util.SystemConstants; import com.clt.util.TokenUtil; import com.mangofactory.swagger.annotations.ApiIgnore; import com.wordnik.swagger.annotations.Api; import com.wordnik.swagger.annotations.ApiOperation; import com.wordnik.swagger.annotations.ApiParam; /** * @Package com.clt.systemmanger.controller * @Description: ?Action * @author hjx * @date 2014717 ?3:50:50 * @version V1.0 */ @Controller @RequestMapping("/userAction") @Api(value = "user-api", description = "?", position = 5) public class UserAction { @Autowired private IUserService userService; @Autowired private IUserRoleService urService; @Autowired private IArchiveTypeService archiveTypeService; @Autowired private ITokenUserService tokenUserService; @Autowired private IUserGpsService gpsService; @Autowired private ISubsuppliersService iSubsuppliersService; @Autowired private IStoresService iStoresService; @Autowired private IUserCodeService userCodeService; @Autowired private IStaticService staticService; /** * @Description: ???session * @param session * @return String ?? * @author hjx * @create_date 2014717 ?4:47:29 */ @RequestMapping("/login") @ApiIgnore public String login(HttpSession session) { User userDt = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); TUser user = userService.getByAccount(userDt.getUsername()); session.setAttribute("user", user); return "index";// } /** * @Description: * @param vcAccount * @param vcPassword * @param utypeID * @return String ?? * @author hjx * @create_date 2015413 ?9:50:35 */ @ApiOperation(value = "?", notes = "?", position = 5) @RequestMapping(value = "/loginByApp", method = RequestMethod.POST) public void loginByApp( @ApiParam(value = "??", required = true) @RequestParam("vcAccount") String vcAccount, @ApiParam(value = "?", required = true) @RequestParam("vcPassword") String vcPassword, @ApiParam(value = "??????activity", required = true) @RequestParam(value = "utypeID") Integer utypeID, HttpServletResponse response, HttpServletRequest request) { TUser user = userService.getByAccount(vcAccount); if (null == user) { // ????? user = userService.getByPhone(vcAccount); } try { if (null != user) { // String temp = AndroidAes.decrypt( "unlcn" , vcPassword ); org.springframework.security.authentication.encoding.Md5PasswordEncoder t = new Md5PasswordEncoder(); String tt = t.encodePassword(vcPassword, user.getVcAccount()); if (tt.equals(user.getVcPassword())) { // ?? int type = user.getIArchiveType(); String msg = null; switch (utypeID) { // case SystemConstants.SYS_TARCHIVE_SUB: if (type != utypeID) { AjaxUtil.rendJson(response, false, "??"); return; } break; // ? case SystemConstants.SYS_TARCHIVE_DRIVER: if (type != utypeID) { AjaxUtil.rendJson(response, false, "??"); return; } break; // ?? case SystemConstants.SYS_TARCHIVE_FINANCE: if (type != utypeID) { AjaxUtil.rendJson(response, false, "???"); return; } break; // 4s case SystemConstants.SYS_TARCHIVE_STORES: if (type != utypeID) { AjaxUtil.rendJson(response, false, "?4s"); return; } break; } List<TTokenUser> tokens = tokenUserService.getTokenListByUserId(user.getId()); if (CollectionUtils.isNotEmpty(tokens)) { for (TTokenUser oldToken : tokens) { tokenUserService.delToken(oldToken); } } String token = ""; token = TokenUtil.getToken(); // token? TTokenUser tokenUser = new TTokenUser(); tokenUser.setVcToken(token); tokenUser.setIUser(user.getId()); tokenUser.setDtLasttime(new Date()); tokenUserService.saveTokenUser(tokenUser); response.addHeader("token", token); response.addHeader("userId", String.valueOf(user.getId())); AjaxUtil.rendJson(response, true, "??"); return; } else { AjaxUtil.rendJson(response, false, "??"); return; } } else { AjaxUtil.rendJson(response, false, "???"); return; } } catch (Exception e) { e.printStackTrace(); AjaxUtil.rendJson(response, false, "?" + e.getMessage()); return; } } /** * @Description: ? * @return String ?? * @author hjx * @create_date 201482 ?2:18:13 */ @RequestMapping("/openUserList") @ApiIgnore public String openUserList() { return "back/system/userList"; } /** * @Description: ? * @return String ?? * @author hjx * @create_date 2014719 ?2:16:10 */ @RequestMapping("/giveRol") @ApiIgnore public String giveRol(HttpServletRequest request) { String userId = request.getParameter("userId"); String roleTree = urService.getAllAndUserHaveRole(userId); System.out.println(roleTree); request.setAttribute("roleTree", roleTree); return "back/system/roleTree";// ? } /** * @Description: ? * @param userId * @param roleids * @param response * void ?? * @author hjx * @create_date 201484 ?3:29:00 */ @RequestMapping("/saveUserRole") @ApiIgnore public void saveUserRole(@RequestParam("userId") String userId, @RequestParam("roleids") String roleids, HttpServletResponse response) { try { if (StringUtils.isNotBlank(roleids)) { String[] ids = roleids.split(","); List<String> idList = Arrays.asList(ids); urService.updateUserRoles(userId, idList); } else { urService.updateUserRoles(userId, null); } AjaxUtil.rendJson(response, true, "???"); } catch (Exception e) { AjaxUtil.rendJson(response, true, "?" + e.getMessage()); } } /** * @Description: * @return String ?? * @author hjx * @create_date 2014721 ?10:39:27 */ @RequestMapping("/index") @ApiIgnore public String testaction() { return "index"; } /** * @Description: * @return String ?? * @author hjx * @create_date 2014721 ?11:04:21 */ @RequestMapping(value = "/register", method = RequestMethod.POST) @ApiOperation(value = "", notes = "", position = 5) @ResponseBody public Map<String, Object> register( @ApiParam(value = "??", required = true) @RequestParam("vcAccount") String vcAccount, @ApiParam(value = "?", required = true) @RequestParam("vcPassword") String vcPassword, @ApiParam(value = "", required = true) @RequestParam("usertypeID") int utypeID, @ApiParam(value = "", required = true) @RequestParam("phone") String phone, @ApiParam(value = "", required = true) @RequestParam(value = "email") String email, HttpServletResponse resp) { TUser user = new TUser(); user.setIArchiveType(utypeID); user.setDtAddtime(new Date()); user.setVcAccount(vcAccount); user.setVcEmail(email); org.springframework.security.authentication.encoding.Md5PasswordEncoder t = new Md5PasswordEncoder(); String tt = t.encodePassword(vcPassword, user.getVcAccount()); user.setVcPassword(tt); try { TUser userAccount = userService.getByAccount(vcAccount); if (null == userAccount) { userService.saveRegister(user, phone); String token = TokenUtil.getToken(); // token? TTokenUser tokenUser = new TTokenUser(); tokenUser.setVcToken(token); tokenUser.setIUser(user.getId()); tokenUser.setDtLasttime(new Date()); tokenUserService.saveTokenUser(tokenUser); resp.addHeader("token", token); return AjaxUtil.getMap(true, "?!"); } else { return AjaxUtil.getMap(false, "?????"); } /*request.setAttribute( "account" , uname ); request.setAttribute( "password" , upass ); request.getRequestDispatcher( "j_spring_security_check" ).forward( request , resp );*/ } catch (Exception e) { e.printStackTrace(); return AjaxUtil.getMapByException(e); } } /** * @Description: * @return String ?? * @author hjx * @create_date 2014721 ?11:04:21 */ @RequestMapping(value = "/registerUser", method = RequestMethod.POST) @ApiOperation(value = "", notes = "", position = 5) @ResponseBody public Map<String, Object> registerUser( @ApiParam(value = "??", required = true) @RequestParam("vcAccount") String vcAccount, @ApiParam(value = "?", required = true) @RequestParam("vcPassword") String vcPassword, @ApiParam(value = "", required = true) @RequestParam("usertypeID") int utypeID, @ApiParam(value = "", required = true) @RequestParam("phone") String phone, @ApiParam(value = "", required = true) @RequestParam(value = "email") String email, @ApiParam(value = "??", required = true) @RequestParam(value = "code") String code, HttpServletResponse resp) { // int utypeID = Integer.parseInt( usertypeID ); // ???????? String codeStr = userCodeService.getCodeByTel(phone); if (StringUtils.isEmpty(codeStr)) { return AjaxUtil.getMap(false, "??????"); } // ????? if (!StringUtils.trim(code).equals(codeStr)) { return AjaxUtil.getMap(false, "???"); } TUser user = new TUser(); user.setIArchiveType(utypeID); user.setDtAddtime(new Date()); user.setVcAccount(vcAccount); user.setVcEmail(email); org.springframework.security.authentication.encoding.Md5PasswordEncoder t = new Md5PasswordEncoder(); String tt = t.encodePassword(vcPassword, user.getVcAccount()); user.setVcPassword(tt); try { TUser userAccount = userService.getByAccount(vcAccount); if (null == userAccount) { userService.saveRegister(user, phone); String token = TokenUtil.getToken(); // token? TTokenUser tokenUser = new TTokenUser(); tokenUser.setVcToken(token); tokenUser.setIUser(user.getId()); tokenUser.setDtLasttime(new Date()); tokenUserService.saveTokenUser(tokenUser); resp.addHeader("token", token); return AjaxUtil.getMap(true, "?!"); } else { return AjaxUtil.getMap(false, "?????"); } /*request.setAttribute( "account" , uname ); request.setAttribute( "password" , upass ); request.getRequestDispatcher( "j_spring_security_check" ).forward( request , resp );*/ } catch (Exception e) { e.printStackTrace(); return AjaxUtil.getMapByException(e); } } /** * @Description: ???? * @return String ?? * @author hjx * @create_date 2014721 ?11:04:21 */ @ApiOperation(value = ",????", notes = "??????", position = 5) @ResponseBody @RequestMapping(value = "/checkRegisterUname", method = RequestMethod.POST, produces = "application/json; charset=utf-8") public void checkRegisterUname( @ApiParam(value = "??", required = true) @RequestParam("vcAccount") String vcAccount, HttpServletResponse resp) { TUser user = userService.getByAccount(vcAccount); if (user != null) { AjaxUtil.rendJson(resp, false, vcAccount + " ,? "); } else { AjaxUtil.rendJson(resp, true, "????"); } } /** * @Description: ? * @return String ?? * @author hjx * @create_date 2014721 ?11:04:21 */ @RequestMapping("/registerUserBefore") @ApiIgnore public String registerUserBefore(HttpServletRequest request) { List<TArchiveType> typelist = archiveTypeService.loadAllByEnable(); request.setAttribute("typelist", typelist); return "back/reg"; } /** * @Description: * @param request * @return Map<String,Object> ?? * @author hjx * @create_date 201484 ?3:38:39 */ @RequestMapping("/pageList") @ResponseBody @ApiIgnore public Map<String, Object> searchPrepayPage(HttpServletRequest request) { Page p = ServiceUtil.getcurrPage(request); HqlHelper helper = new HqlHelper(TUser.class); helper.setQueryPage(p); helper.addEqual("NEnable", SystemConstants.SYS_ENABLE);// ? String userName = request.getParameter("userName"); if (StringUtils.isNotBlank(userName)) { helper.addLike("vcUsername", userName); } String vcAccount = request.getParameter("vcAccount"); if (StringUtils.isNotBlank(vcAccount)) { helper.addLikeIgnoreCase("vcAccount", vcAccount); } Map<String, Object> map = userService.findByHelper(helper); return map; } /** * @Description: ? * @param userIds * @param response * void ?? * @author hjx * @create_date 201486 ?11:29:09 */ @RequestMapping("/disableUser") @ApiIgnore public void disableUser(@RequestParam("userIds") String userIds, HttpServletResponse response) { if (StringUtils.isNotBlank(userIds)) { try { String[] arr = StringUtils.split(userIds, ","); for (String id : arr) { TUser user = userService.getByid(id); if (null != user) { user.setNEnable(1); userService.updateUser(user); } } AjaxUtil.rendJson(response, true, "???"); } catch (Exception e) { e.printStackTrace(); AjaxUtil.rendJson(response, false, "?" + e.getMessage()); } } else { AjaxUtil.rendJson(response, true, "???"); } } /** * @Description: ? * @return String ?? * @author hjx * @create_date 201486 ?11:46:42 */ @RequestMapping("/openAddUser") @ApiIgnore public String openAddUser(HttpServletRequest request) { String paramType = "add"; String userId = request.getParameter("userId"); if (StringUtils.isNotBlank(userId)) { paramType = "update"; TUser tuser = userService.getByid(userId); request.setAttribute("tuser", tuser); } List<TArchiveType> archiveTypes = archiveTypeService.loadAllByEnable(); request.setAttribute("archiveTypes", archiveTypes); request.setAttribute("paramType", paramType); return "back/system/userForm"; } /** * * @Description: TODO(?) * @param request * @param response * @param tUser * void ?? * @author liuwu * @create_date 2015-6-15 ?3:18:38 */ @RequestMapping(value = "/saveUser", method = RequestMethod.POST) @ApiIgnore public void saveUser(HttpServletRequest request, String paramType, HttpServletResponse response, TUser tUser) { try { if (tUser.getVcPassword() == null || tUser.getVcPassword() == "") { tUser.setVcPassword("123456"); } org.springframework.security.authentication.encoding.Md5PasswordEncoder t = new Md5PasswordEncoder(); String tt = t.encodePassword("123456", tUser.getVcAccount()); tUser.setVcPassword(tt); userService.saveUpdateUser(tUser); if (paramType.equalsIgnoreCase("add"))// { if (tUser.getIArchiveType().equals(SystemConstants.SYS_TARCHIVE_SUB)) { // TSubsuppliers tSubsuppliers = new TSubsuppliers(); iSubsuppliersService.save(tSubsuppliers); tUser.setiArchive(tSubsuppliers.getId()); userService.updateUser(tUser); } if (tUser.getIArchiveType().equals(SystemConstants.SYS_TARCHIVE_STORES)) { // 4S TStores tStores = new TStores(); tStores.setiUserId(tUser.getId()); iStoresService.save(tStores); tUser.setiArchive(tStores.getId()); userService.updateUser(tUser); } } AjaxUtil.rendJson(response, true, "??"); } catch (Exception e) { AjaxUtil.rendJson(response, false, "?" + e.getMessage()); } } /** * @Description: ?? ?? * @return String ?? * @author hjx * @create_date 2014721 ?11:04:21 */ @RequestMapping("/resetUserApplyResource") @ApiIgnore @ResponseBody public Map<String, Object> resetUserApplyResource(HttpServletRequest request, HttpServletResponse resp) { String usid = request.getParameter("usid"); try { userService.updateUserApplyResource(usid); request.getSession().setAttribute("user", userService.getByid(usid)); return AjaxUtil.getMap(true, "???"); } catch (Exception e) { e.printStackTrace(); return AjaxUtil.getMapByException(e); } } /** * @Description: ?? * @param resp * void ?? * @author hjx * @create_date 2015410 ?3:33:44 */ @ApiOperation(value = "??", notes = "????????????? ??", position = 5) @RequestMapping(value = "/passwordReset", method = RequestMethod.POST) @ResponseBody public Map<String, Object> passwordReset( @ApiParam(value = "??", required = true) @RequestParam("userId") String userId, @ApiParam(value = "??", required = true) @RequestParam("phoneNo") String phoneNo, @ApiParam(value = "?", required = true) @RequestParam("passwrod") String passwrod, @ApiParam(value = "??", required = true) @RequestParam("code") String code, HttpServletResponse resp) { // TUser user = userService.getByid( userId ); TUser user; try { user = userService.getByAccount(userId); if (null == user) { return AjaxUtil.getMap(false, "????"); } } catch (Exception e1) { e1.printStackTrace(); return AjaxUtil.getMapByException(e1); } String phone = user.getVcPhone(); // ???? if (phoneNo.equals(phone)) { try { // ??? // ???????? String codeStr = userCodeService.getCodeByTel(phone); if (StringUtils.isEmpty(codeStr)) { return AjaxUtil.getMap(false, "??????"); } // ????? if (!StringUtils.trim(code).equals(codeStr)) { return AjaxUtil.getMap(false, "???"); } // ?? org.springframework.security.authentication.encoding.Md5PasswordEncoder t = new Md5PasswordEncoder(); String tt = t.encodePassword(passwrod, user.getVcAccount()); user.setVcPassword(tt); userService.updateUser(user); return AjaxUtil.getMap(true, "???"); } catch (Exception e) { e.printStackTrace(); return AjaxUtil.getMap(false, "??"); } } else { return AjaxUtil.getMap(false, "??????"); } } /** * @Description: apptoken * @param request * @return Map<String,Object> ?? * @author hjx * @create_date 2015417 ?5:29:29 */ @RequestMapping(value = "/heartbeat", method = RequestMethod.POST) @ApiOperation(value = "app", notes = "apptoken,??app, token?", position = 5) @ResponseBody public Map<String, Object> heartbeat(HttpServletRequest request) { System.out.println("***********" + DateUtil.formatTime(new Date())); Map<String, Object> result; try { String token = request.getHeader("token"); result = tokenUserService.updateLast(token); TUser user = (TUser) UserSession.get("user"); user.setNIntegral(user.getNIntegral() + SystemConstants.SYS_ADD_INTEGRAL); userService.updateUser(user); } catch (Exception e) { e.printStackTrace(); return AjaxUtil.getMapByException(e); } return result; } @RequestMapping(value = "/logOutByApp", method = RequestMethod.POST) @ApiOperation(value = "app", notes = "app,?app, token?", position = 5) public Map<String, Object> logOutByApp(HttpServletRequest request) { try { String token = request.getHeader("token"); tokenUserService.delToken(token); return AjaxUtil.getMapByNotException(true, null); } catch (Exception e) { e.printStackTrace(); return AjaxUtil.getMapByException(e); } } /** * ?gps? * * @param gps * @param request * @param response */ @RequestMapping(value = "/saveGps", method = RequestMethod.POST) @ApiOperation(value = "app??", notes = "??", position = 5) @ResponseBody public Map<String, Object> saveGps(@ApiParam(value = "?") @RequestParam("log") String log, @ApiParam(value = "") @RequestParam("lat") String lat, @ApiParam(value = "app?") HttpSession session, HttpServletResponse response) { try { System.out.println("?" + DateUtil.formatTime(new Date())); TUserGps gps = new TUserGps(); TUser user = (TUser) session.getAttribute("user"); gps.setIUser(user.getId()); gps.setVcUser(user.getVcUsername()); gps.setVcLat(lat); gps.setVcLong(log); gpsService.save(gps); Map<String, String> map = new HashMap<String, String>(); map.put("nextTime", "300"); return AjaxUtil.getMapByNotException(true, map); } catch (Exception e) { e.printStackTrace(); return AjaxUtil.getMapByException(e); } } /** * * @Description: TODO(????) * @param request * @param response * void ?? * @author liuwu * @create_date 2015-6-17 ?5:30:27 */ @SuppressWarnings("deprecation") @ApiOperation(value = "??", notes = "??") @RequestMapping(value = "/bindDeviceTokens", method = RequestMethod.POST) public void bindDeviceTokens(HttpServletRequest request, HttpServletResponse response, @ApiParam(value = "?vcDeviceTokens", required = true) @RequestParam(value = "vcDeviceTokens", required = true) String vcDeviceTokens) { HttpSession session = request.getSession(); TUser user = (TUser) session.getAttribute("user"); if (null == user) { user = (TUser) UserSession.get("user"); } user.setVcDeviceTokens(vcDeviceTokens); try { userService.updateUser(user); AjaxUtil.rendJson(response, true, "??"); } catch (Exception e) { AjaxUtil.rendJson(response, false, "?" + e.getMessage()); } } /** * * @Description: TODO(??) * @param request * @param response * @return String ?? * @author liuwu * @create_date 2015-7-27 ?4:22:08 */ @ApiOperation(value = "???", notes = "???") @RequestMapping(value = "/registerLicense") public String registerLicense(HttpServletRequest request, HttpServletResponse response) { return "sub/subuser/registerLicense"; } @ApiOperation(value = "????", notes = "????:?json" + "<br/>json? dispaly<?>message<??>" + "<br/>dispalytrue???dispalyfalse?") @RequestMapping(value = "/getVerificationCode", method = RequestMethod.POST) @ResponseBody public Map<String, Object> getVerificationCode( @ApiParam(value = "??", required = true) @RequestParam(value = "tel", required = true) String tel) { // ??? int i = (int) ((Math.random() * 9 + 1) * 100000); String messageCode = String.valueOf(i); // ?? TUserCode userCode = new TUserCode(); userCode.setVcPhone(tel); userCode.setVcCode(messageCode); userCodeService.save(userCode); // ?? Map<String, Object> map = new HashMap<String, Object>(); // map.put("isSuccess", true); map.put("dispaly", true); map.put("verification", messageCode); map.put("message", "?????"); return map; } /** * ? * * @return */ @ApiOperation(value = "?", notes = "?content ") @RequestMapping(value = "", method = RequestMethod.POST) @ResponseBody public Map<String, Object> getShareText() { Map<String, Object> result = null; try { String shareText = staticService.getStringByParame("shareText"); String imgUrl = staticService.getStringByParame("imgUrl"); String appurl = staticService.getStringByParame("appUrl"); if (StringUtils.isNotBlank(shareText)) { result = AjaxUtil.getMap(true, "???"); result.put("content", shareText); result.put("imgUrl", imgUrl); result.put("url", appurl); } } catch (Exception e) { e.printStackTrace(); return AjaxUtil.getMapByException(e); } return result; } /** * @Description: * @param session * @return String ?? * @author hjx * @create_date 2015910 ?3:05:04 */ @RequestMapping("/layoutByPC") @ResponseBody public Map<String, Object> layoutByPC(HttpSession session) { TUser user = (TUser) session.getAttribute("user"); if (null != user) { UserSession.set("user", null); session.setAttribute("user", null); session.setMaxInactiveInterval(0); } return AjaxUtil.getMap(true, "??"); } }