Java tutorial
/** * ??UserControl.java</br> * ?? </br> * ? </br> * May 12, 2014 */ package com.cndatacom.user.control; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.ResourceBundle; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.security.core.context.SecurityContextHolder; 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.ResponseBody; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.view.InternalResourceView; import com.cndatacom.app.bean.AppInfo; import com.cndatacom.app.service.IAppService; import com.cndatacom.button.bean.ButtonInfo; import com.cndatacom.button.service.IButtonInfoService; import com.cndatacom.content.bean.ContentInnerInfo; import com.cndatacom.content.bean.ContentTemplete; import com.cndatacom.content.service.IContentTempleteService; import com.cndatacom.core.util.Configure; import com.cndatacom.core.util.DateUtil; import com.cndatacom.core.util.HttpJsonUtils; import com.cndatacom.core.util.SpringContextHolder; import com.cndatacom.core.util.TwoDimensionCode; import com.cndatacom.security.bean.SysUser; import com.cndatacom.text.bean.TextInfo; import com.cndatacom.text.service.ITextService; import com.cndatacom.user.service.IUserService; import com.cndatacom.util.Constants; import com.cndatacom.util.cache.CacheProxy; import com.cndatacom.work.bean.WorkOrder; import com.cndatacom.work.service.IWorkOrderService; import com.google.gson.Gson; /** * ??: UserControl</br> * ??com.cndatacom.user.control </br> * ??: </br> * ??</br> * ? </br> * May 12, 2014 */ @Controller @RequestMapping("/user") public class UserControl { Logger logger = LoggerFactory.getLogger(getClass()); @Resource(name = "userService") private IUserService userService; @Resource(name = "buttonInfoService") private IButtonInfoService buttonInfoService; @Resource(name = "textService") private ITextService textService; @Resource(name = "workOrderService") private IWorkOrderService workOrderService; @Resource(name = "contentTempleteService") private IContentTempleteService contentTempleteService; @Resource(name = "cacheProxy") private CacheProxy cacheProxy; /** * * ??: </br> * : </br>??? ajax * ?</br> * Mar 26, 2014</br> */ @RequestMapping(value = "/getCaptcha", method = RequestMethod.POST) public void getCaptcha(HttpServletRequest request, HttpServletResponse response) { logger.info("getCaptcha "); String phoneNumber = request.getParameter("phoneNumber"); logger.info("????" + phoneNumber); try { String status = "0"; if (phoneNumber != null && !"".equals(phoneNumber)) { boolean isSend = userService.sendCaptcha(phoneNumber); if (isSend) status = "1"; } Map<String, Object> map = new HashMap<String, Object>(); map.put("status", status); map.put("description", ""); String data = JSONObject.fromObject(map).toString(); logger.info("??: " + data); response.getWriter().print(data); response.getWriter().flush(); response.getWriter().close(); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } } @RequestMapping(value = "/init", method = { RequestMethod.POST, RequestMethod.GET }) @ResponseBody public Object init(HttpServletRequest request, HttpServletResponse response) { logger.info("init "); List<String> paramsName = new ArrayList<String>(); Map<String, Object> resultMap = new HashMap<String, Object>(); response.setContentType("application/json"); Map<String, Object> map = new HashMap<String, Object>(); try { userService.initAppIdOfAllUser(); map.put("status", "1"); map.put("decription", "??"); } catch (Exception e) { e.printStackTrace(); logger.error("??"); } return HttpJsonUtils.getBase64Encode(map); } /** * * ??: </br> * : </br>??? ajax * ?</br> * Mar 26, 2014</br> */ @RequestMapping(value = "/getUserId", method = RequestMethod.POST) public void getUserId(HttpServletRequest request, HttpServletResponse response) { logger.info("getUserId "); String userName = SecurityContextHolder.getContext().getAuthentication().getName(); try { String status = "0"; Map<String, Object> map = new HashMap<String, Object>(); //SysUser sysUser = userService.loadUserByUsername(userName); //SysUser sysUser = (SysUser) JSONObject.toBean(JSONObject.fromObject(cacheProxy.get(userName)),SysUser.class); SysUser sysUser = new Gson().fromJson(cacheProxy.get(userName), SysUser.class); if (sysUser != null) { map.put("userId", sysUser.getUserid()); status = "1"; } map.put("status", status); map.put("description", ""); String data = JSONObject.fromObject(map).toString(); logger.info("??: " + data); response.getWriter().print(data); response.getWriter().flush(); response.getWriter().close(); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } } /** * * ??: </br> * : </br>??? * ?</br> * May 12, 2014</br> * @param request * @param response */ @RequestMapping(value = "/checkUserName", method = RequestMethod.POST) public void checkUserName(HttpServletRequest request, HttpServletResponse response) { logger.info("checkUserName "); String userName = request.getParameter("userName"); logger.info("??" + userName); try { String status = "0"; //SysUser sysUser = userService.loadUserByUsername(userName); SysUser sysUser = new Gson().fromJson(cacheProxy.get(userName), SysUser.class); if (sysUser == null) status = "1"; Map<String, Object> map = new HashMap<String, Object>(); map.put("status", status); map.put("description", ""); String data = JSONObject.fromObject(map).toString(); logger.info("??: " + data); response.getWriter().print(data); response.getWriter().flush(); response.getWriter().close(); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } } /** * * ??: </br> * : </br> * ?</br> * May 12, 2014</br> * @param request * @param response */ @RequestMapping(value = "/register", method = RequestMethod.POST) public void register(HttpServletRequest request, HttpServletResponse response) { logger.info("checkUserName "); try { String status = "0"; String name = request.getParameter("name"); logger.info("name: " + name); String address = request.getParameter("address"); logger.info("address: " + address); String linkMan = request.getParameter("linkMan"); logger.info("linkMan: " + linkMan); String sex = request.getParameter("sex"); logger.info("sex: " + sex); String contactPhone = request.getParameter("contactPhone"); logger.info("contactPhone: " + contactPhone); String email = request.getParameter("email"); logger.info("email: " + email); String userName = request.getParameter("userName"); logger.info("userName: " + userName); String password = request.getParameter("user_password"); logger.info("password: " + password); String phoneNumber = request.getParameter("phoneNumber"); logger.info("phoneNumber: " + phoneNumber); String smsNumber = request.getParameter("smsNumber"); logger.info("smsNumber: " + smsNumber); String clerkPath = request.getParameter("clerkPath"); logger.info("clerkPath: " + clerkPath); String orgNumber = request.getParameter("orgNumber"); logger.info("orgNumber: " + orgNumber); String employPath = request.getParameter("employPath"); logger.info("employPath: " + employPath); String authorizePath = request.getParameter("authorizePath"); logger.info("authorizePath: " + authorizePath); String commodityPath = request.getParameter("commodityPath"); logger.info("commodityPath: " + commodityPath); String tradIntroduce = request.getParameter("tradIntroduce"); logger.info("tradIntroduce:" + tradIntroduce); String description = "?"; int checkStatus = userService.checkCaptcha(phoneNumber, smsNumber); if (checkStatus == 1) { boolean flag = userService.saveUser(userName, password, phoneNumber, name, address, linkMan, sex, contactPhone, email, clerkPath, orgNumber, employPath, authorizePath, commodityPath, tradIntroduce, "", "1", "1", "", 1); if (!flag) { description = "???"; } else { status = "1"; } } else if (checkStatus == 0) { description = "??"; status = "0"; } else if (checkStatus == 2) { description = "??"; status = "0"; } Map<String, Object> map = new HashMap<String, Object>(); map.put("status", status); map.put("description", description); String data = JSONObject.fromObject(map).toString(); logger.info("??: " + data); response.getWriter().print(data); response.getWriter().flush(); response.getWriter().close(); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } } /** * * ??: </br> * : </br>??? * ?</br> * May 12, 2014</br> * @param request * @param response */ @RequestMapping(value = "/getUserInfo", method = RequestMethod.POST) public void getUserInfo(HttpServletRequest request, HttpServletResponse response) { logger.info("getUserInfo "); System.out.println("?" + request.getRemoteAddr()); String userName = SecurityContextHolder.getContext().getAuthentication().getName(); logger.info("??" + userName); try { String status = "0"; String name = ""; //SysUser sysUser = userService.loadUserByUsername(userName); //SysUser sysUser = (SysUser) JSONObject.toBean(JSONObject.fromObject(cacheProxy.get(userName)),SysUser.class); SysUser sysUser = new Gson().fromJson(cacheProxy.get(userName), SysUser.class); if (sysUser != null) { status = "1"; name = sysUser.getName(); } Map<String, Object> map = new HashMap<String, Object>(); map.put("status", status); map.put("description", ""); map.put("userName", userName); map.put("name", name); String data = JSONObject.fromObject(map).toString(); logger.info("??: " + data); response.setCharacterEncoding("UTF-8"); response.getWriter().print(data); response.getWriter().flush(); response.getWriter().close(); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } } /** * * ??:getUserNewInfo</br> * : ???</br> * ?zhanghs</br> * 2014-5-17</br> * @param request * @param response * @return */ @RequestMapping(value = "/getUserNewInfo", method = RequestMethod.POST) @ResponseBody public Object getUserNewInfo(HttpServletRequest request, HttpServletResponse response) { logger.info("getUserNewInfo "); String userName = SecurityContextHolder.getContext().getAuthentication().getName(); Map<String, Object> resultMap = new HashMap<String, Object>(); //String path = request.getContextPath(); //String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path; String filePath = Configure.values("server_eappfile") + "/" + Configure.values("server_filepath"); logger.info("??" + userName); try { String status = "0"; //SysUser sysUser = userService.loadUserByUsername(userName); SysUser sysUser = new Gson().fromJson(cacheProxy.get(userName), SysUser.class); Map<String, Object> map = new HashMap<String, Object>(); if (sysUser != null) { status = "1"; map.put("status", status); map.put("appInfo", sysUser.getName()); map.put("address", sysUser.getAddress()); map.put("userName", userName); map.put("name", sysUser.getName()); map.put("linkName", sysUser.getLinkMan()); map.put("linkPhone", sysUser.getContactPhone()); map.put("email", sysUser.getEmail()); map.put("userId", sysUser.getUserid()); if (!StringUtils.isBlank(sysUser.getBusinessLicense())) { map.put("url", filePath + sysUser.getBusinessLicense()); } else { map.put("url", ""); } WorkOrder wo = this.workOrderService.getWorkOrderByOperId(sysUser.getWorkNum()); if (null != wo) { map.put("operStatus", wo.getIsResut() + ""); } else { map.put("operStatus", "1"); } List<ButtonInfo> longList = this.buttonInfoService.getButtonInfo(sysUser.getUserid(), "1"); List<ButtonInfo> shortList = this.buttonInfoService.getButtonInfo(sysUser.getUserid(), "2"); List<TextInfo> textList = this.textService.getTextInfo(sysUser.getUserid()); if (!longList.isEmpty()) { map.put("longTitle", longList.get(0).getMenuName()); } else { map.put("longTitle", ""); } if (!shortList.isEmpty()) { map.put("shortTitle", shortList.get(0).getMenuName()); } else { map.put("shortTitle", ""); } if (!textList.isEmpty()) { map.put("textTitle", textList.get(0).getMenuName()); } else { map.put("textTitle", ""); } resultMap.put("status", status); resultMap.put("data", map); } else { resultMap.put("status", "2"); } logger.info("??: " + resultMap); } catch (Exception ex) { logger.error(ex.getMessage(), ex); resultMap.put("status", "0"); } return resultMap; } /** * * ??:saveUserInfo</br> * : ???</br> * ?zhanghs</br> * 2014-5-17</br> * @param request * @param response */ @RequestMapping(value = "/saveUserInfo", method = RequestMethod.POST) @ResponseBody public Object saveUserInfo(HttpServletRequest request, HttpServletResponse response) { Map<String, Object> resultMap = new HashMap<String, Object>(); logger.info("checkUserName "); try { String status = "0"; String userId = request.getParameter("userId"); logger.info("userId: " + userId); String name = request.getParameter("name"); logger.info("name: " + name); String address = request.getParameter("address"); logger.info("address: " + address); String linkMan = request.getParameter("linkMan"); logger.info("linkMan: " + linkMan); String contactPhone = request.getParameter("contactPhone"); logger.info("contactPhone: " + contactPhone); String email = request.getParameter("email"); logger.info("email: " + email); if (!StringUtils.isBlank(userId)) { //SysUser user = this.userService.loadUserByUserId(Long.parseLong(userId)); String userName = SecurityContextHolder.getContext().getAuthentication().getName(); SysUser user = new Gson().fromJson(cacheProxy.get(userName), SysUser.class); if (null != user) { user.setAddress(address); user.setName(name); user.setEmail(email); user.setLinkMan(linkMan); user.setContactPhone(contactPhone); this.userService.saveUserInfo(user); cacheProxy.setex(user.getUsername(), 1800, new Gson().toJson(user)); cacheProxy.setex(String.valueOf(user.getUserid()), 1800, new Gson().toJson(user)); status = "1"; } } resultMap.put("status", status); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } return resultMap; } /** * * ??:CheckUserRole</br> * : ?</br> * ?lbw</br> * 2014-8-19</br> * @param request * @param response */ @RequestMapping(value = "/checkUserRole", method = RequestMethod.POST) @ResponseBody public Object checkUserRole(HttpServletRequest request, HttpServletResponse response) { Map<String, Object> resultMap = new HashMap<String, Object>(); try { /*String userName = SecurityContextHolder.getContext().getAuthentication().getName(); SysUser sysUser = userService.loadUserByUsername(userName);*/ String userName = SecurityContextHolder.getContext().getAuthentication().getName(); //SysUser sysUser = (SysUser) JSONObject.toBean(JSONObject.fromObject(cacheProxy.get(userName)),SysUser.class); SysUser sysUser = new Gson().fromJson(cacheProxy.get(userName), SysUser.class); resultMap.put("status", "1"); resultMap.put("userType", sysUser.getUserType() == null ? "1" : sysUser.getUserType() + ""); } catch (Exception ex) { logger.error(ex.getMessage(), ex); resultMap.put("status", "0"); } return resultMap; } /** * * ??:modifyPackages</br> * : ?ID?</br> * ?wangm</br> * 2014-9-19</br> * @param request * @param response */ @RequestMapping(value = "/modifyPackages", method = RequestMethod.POST) @ResponseBody public Object modifyPackages(HttpServletRequest request, HttpServletResponse response) { Map<String, Object> resultMap = new HashMap<String, Object>(); String userId = request.getParameter("userId"); String userType = request.getParameter("userType"); String status = "0"; try { if (!StringUtils.isBlank(userId) && StringUtils.isNotBlank(userType)) { SysUser user = this.userService.loadUserByUserId(Long.parseLong(userId)); if (null != user) { user.setUserType(Integer.valueOf(userType)); this.userService.saveUserInfo(user); logger.info("??" + user.getUsername()); status = "1"; } } resultMap.put("status", status); } catch (Exception ex) { logger.error(ex.getMessage(), ex); resultMap.put("status", "0"); } return resultMap; } /** * * ??:getUserType</br> * : ??</br> * ?zhanghs</br> * 2014-9-26</br> * @param request * @param response * @return */ @RequestMapping(value = "/getUserType", method = RequestMethod.POST) @ResponseBody public Object getUserType(HttpServletRequest request, HttpServletResponse response) { Map<String, Object> resultMap = new HashMap<String, Object>(); String userId = request.getParameter("userId"); String status = "0"; try { if (!StringUtils.isBlank(userId)) { SysUser user = this.userService.loadUserByUserId(Long.parseLong(userId)); if (null != user) { status = "1"; resultMap.put("userType", user.getUserType() == null ? "1" : user.getUserType() + ""); } else { status = "2"; resultMap.put("userType", "1"); } } resultMap.put("status", status); } catch (Exception ex) { logger.error(ex.getMessage(), ex); resultMap.put("status", "0"); } return resultMap; } /** * * ??: index</br> * : ?</br> * ?lis</br> * 2014-11-24</br> * @param request * @param response * @return */ @RequestMapping(value = "/index", method = { RequestMethod.POST, RequestMethod.GET }) public ModelAndView link(HttpServletRequest request, HttpServletResponse response) { ModelAndView mv = null; try { String userName = SecurityContextHolder.getContext().getAuthentication().getName(); //SysUser sysUser = (SysUser) JSONObject.toBean(JSONObject.fromObject(cacheProxy.get(userName)),SysUser.class); SysUser sysUser = new Gson().fromJson(cacheProxy.get(userName), SysUser.class); if (sysUser != null) { if ("1".equalsIgnoreCase(sysUser.getRole())) { mv = new ModelAndView(new InternalResourceView("/admin/app/app.jsp")); } else { mv = new ModelAndView(new InternalResourceView("/account/app_manager/apply.jsp")); if ("admin".equals(sysUser.getUsername())) { mv.addObject("apply", "1"); } else if ("gadmin".equals(sysUser.getUsername())) { mv.addObject("apply", "2"); } } //2015-5-6#wap?? String downUrl = ResourceBundle.getBundle("config").getString("server_ip") + "/" + ResourceBundle.getBundle("config").getString("server_name") + "/action/html5/index/" + sysUser.getUserid(); String codeImagePath = ResourceBundle.getBundle("config").getString("filePath") + "/" + userName + "/image/wapCode.png"; TwoDimensionCode handler = new TwoDimensionCode(); handler.encoderQRCode(downUrl, codeImagePath, "png"); } } catch (Exception e) { e.printStackTrace(); } return mv; } }