Java tutorial
/* * Copyright 2011-2016 MSUN.com All right reserved. This software is the confidential and proprietary information of * MSUN.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into with MSUN.com. */ package com.mmj.app.web.controller.user; import java.io.IOException; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.mmj.app.biz.cons.SexEnum; import com.mmj.app.biz.domain.MemberDO; import com.mmj.app.biz.domain.MemberThinDO; import com.mmj.app.biz.query.MemberQuery; import com.mmj.app.common.checkcode.WebsiteCheckCodeManager; import com.mmj.app.common.cookie.manager.CookieManagerLocator; import com.mmj.app.common.util.PushSMSUtils; import com.mmj.app.common.util.SerialNumGenerator; import com.mmj.app.common.util.StringFormatter; import com.mmj.app.web.controller.BaseController; import com.mmj.app.web.tools.WebUserTools; import com.mmj.app.web.vo.UserInfoVO; /** * @author zxc Nov 26, 2014 11:37:12 AM */ @Controller public class UserController extends BaseController { // /link/user.do // {"result":{"code":"9998", "message":"request?jid", "data":""}} // {"result":{"code":"9999", "message":"", // "data":{"banStatus":1,"bindPhone":false,"canChat":false,"cityName":"", // "imgUrl":"http://img1.chouti.com/group9/M00/19/22/wKgCNFR3P5Wc4AjcAAAbxP2MOAo147=48x48.jpg","integration":0,"jid":"zxc337","nick":"zxc338","proveName":"","sex":true}}} @RequestMapping(value = "/link/user.do") public ModelAndView userInfo() { if (!WebUserTools.hasLogin()) { return createJsonMav("9998", "request?jid", ""); } MemberDO member = userService.getMemberById(WebUserTools.getUid()); return createJsonMav("9999", "", new UserInfoVO(member)); } // /////////////////////////////////////////////////////////////////////////////////////////////////////////////// // /// // /// // /// ?? // /// // /// // /////////////////////////////////////////////////////////////////////////////////////////////////////////////// // {"result":{"code":"24001", "message":"????", "data":{"extMst":""}}} // {"result":{"code":"9999", "message":"", "data":{"extMst":""}}} // /passport/valAccessCode.do?code=d3ad0d361be6b51e8f1513b2e75d7120 @RequestMapping(value = "/passport/valAccessCode.do") public ModelAndView valAccessCode(String code) { if (StringUtils.isEmpty(code)) { return createExtJsonMav("24001", "????", ""); } boolean check = WebsiteCheckCodeManager.INSTANCE.checkByMD5(cookieManager, code); if (!check) { return createExtJsonMav("24001", "????", ""); } return createExtJsonMav("9999", "", ""); } // /gozapIdentifyCode?t=0.583490149956273 ???(???,??,) @RequestMapping(value = "/gozapIdentifyCode") public ModelAndView gozapIdentifyCode(String t, HttpServletRequest request, HttpServletResponse response) { response.setContentType("image/png; charset=utf-8"); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "private, must-revalidate,no-store, no-cache, must-revalidate,post-check=0, pre-check=0"); response.addHeader("Content-Disposition", "attachment; filename=\"" + "gozapIdentifyCode" + "\""); response.setCharacterEncoding("UTF-8"); final byte[] bytes = WebsiteCheckCodeManager.INSTANCE.create(CookieManagerLocator.get(request, response), response); OutputStream os; try { os = response.getOutputStream(); IOUtils.write(bytes, os); response.flushBuffer(); } catch (IOException e) { e.printStackTrace(); } return null; } // /////////////////////////////////////////////////////////////////////////////////////////////////////////////// // /// // /// // /// ?? ? // /// // /// // /////////////////////////////////////////////////////////////////////////////////////////////////////////////// // /profile/user/find ?? @RequestMapping(value = "/profile/user/find") public ModelAndView userFind() { ModelAndView mav = new ModelAndView("/user/userFind"); return mav; } // /profile/user/password/lost ???? // {"result":{"code":"9999", "message":"", // "data":{"cacheValue":"42037B86BC52054FED3FA6BCA038D54B","cachekey":"403323f988ee49e18fe57a85a6dcc633","mail":"zhangxiongcai@163.com", // "mailPath":"http://mail.163.com"}}} // email=zhangxiongcai%40163.com&isReset=1 // {"result":{"code":"20002", "message":"???", "data":{"extMst":""}}} @RequestMapping(value = "/profile/user/password/lost") public ModelAndView lostUserSendEmail(String email, Integer isReset) { if (StringUtils.isEmpty(email)) { return createExtJsonMav("20002", "???", ""); } Pattern pattern = Pattern.compile(EMAIL_REG); Matcher matcher = pattern.matcher(email); if (!matcher.matches()) { return createExtJsonMav("20002", "???", ""); } Map<String, String> map = new HashMap<String, String>(); map.put("cacheValue", "42037B86BC52054FED3FA6BCA038D54B"); map.put("cachekey", "403323f988ee49e18fe57a85a6dcc633"); map.put("mail", "zhangxiongcai@163.com"); map.put("mailPath", "http://mail.163.com"); return createJsonMav("9999", "", map); } // /profile/user/password/back // cachekey=22e2e42f4d2f4d3692bf2321261616a1&cacheValue=A710681E6D8FD4E6A888A2BA8E3DD4DC @RequestMapping(value = "/profile/user/password/back") public ModelAndView lostUserBack(String cachekey, String cacheValue) { ModelAndView mav = new ModelAndView("/user/lostUserBack"); return mav; } // /profile/password/reset ? @RequestMapping(value = "/profile/password/reset") public ModelAndView passwdRest() { ModelAndView mav = new ModelAndView("/user/passwdReset"); return mav; } // /passport/lostPwdSendEmail.do ??? // jid=zxc337&code=7f6557037207a84bdace3a3949e575f3 // {"result":{"code":"9999", "message":"", // "data":{"cacheValue":"D8C09A3F6CE473951C57E39347F684D1","cachekey":"d681a04dbd3443aea69915da947985a9","mail":"****@11.com","mailPath":"http://mail.11.com"}}} @RequestMapping(value = "/passport/lostPwdSendEmail.do") public ModelAndView lostPwdSendEmail(String jid, String code) { Map<String, String> map = new HashMap<String, String>(); map.put("cacheValue", "D8C09A3F6CE473951C57E39347F684D1"); map.put("cachekey", "d681a04dbd3443aea69915da947985a9"); map.put("mail", "****@11.com"); map.put("mailPath", "http://mail.11.com"); return createJsonMav("9999", "", ""); } // /passport/lostPwdEmail.do // cachekey=9c1106ab678842da90629a9ac6dffa90&cacheValue=E2BA502ADEA562F6AD5407D68A2E7023 @RequestMapping(value = "/passport/lostPwdEmail.do") public ModelAndView lostPwdEmail(String cachekey, String cacheValue) { ModelAndView mav = new ModelAndView("/user/lostPwdEmail"); return mav; } // /////////////////////////////////////////////////////////////////////////////////////////////////////////////// // /// // /// // /// ? // /// // /// // /////////////////////////////////////////////////////////////////////////////////////////////////////////////// // /message/edit ?? @RequestMapping(value = "/message/edit") public ModelAndView msgEdit() { ModelAndView mav = new ModelAndView("/user/msgEdit"); MemberDO member = userService.getMemberById(WebUserTools.getUid()); mav.addObject("member", member); return mav; } // /message/update ??? // linksMsg:true commentsMsg:false linksIntoHotMsg:true jid:zxc337 // {"result":{"code":"9999", "message":"?", "data":""}} @RequestMapping(value = "/message/update") public ModelAndView msgUpdate(String jid, Boolean commentsMsg, Boolean linksIntoHotMsg, Boolean linksMsg) { if (linksMsg == null || commentsMsg == null || linksIntoHotMsg == null) { return createJsonMav("0000", "???", ""); } userService.update(new MemberDO(WebUserTools.getUid(), commentsMsg, linksIntoHotMsg, linksMsg)); return createJsonMav("9999", "?", ""); } // ?? @RequestMapping(value = "/profile") public ModelAndView profile() { ModelAndView mav = new ModelAndView("/user/setting"); MemberDO member = userService.getMemberById(WebUserTools.getUid()); mav.addObject("member", member); return mav; } // /profile/update ??? // {"result":{"code":"9999", "message":"??", "data":""}} // {"result":{"code":"0000", "message":"?,?", "data":""}} @RequestMapping(value = "/profile/update") public ModelAndView profileUpdate(String jid, String nick, String imgUrl, String sex, String proveName, String cityName) { if (StringUtils.isEmpty(jid)) { return createJsonMav("0000", "?,??", ""); } if (StringUtils.isEmpty(nick)) { return createJsonMav("0000", "?,", ""); } MemberDO member = new MemberDO(nick); if (StringUtils.isNotEmpty(imgUrl)) { Pattern p = Pattern.compile(IMG_URL_REG, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(WEB_APP_HOST + imgUrl); logger.error("imgUrl: " + WEB_APP_HOST + imgUrl); logger.error("imgUrl matches: " + m.matches()); if (!m.matches()) { return createJsonMav("0000", "?,?", ""); } member.setPic(imgUrl); } if (StringUtils.isNotEmpty(sex)) { SexEnum sexEnum = SexEnum.getEnum(sex); if (sexEnum == null) { return createJsonMav("0000", "?,", ""); } member.setSex(sexEnum.getValue()); } MemberDO memberDO = userService.find(new MemberQuery("", nick, "")); if (memberDO != null) { if (!StringUtils.equals(jid, memberDO.getName())) { return createJsonMav("0000", "?,?", ""); } } if (StringUtils.isNotEmpty(proveName)) { member.setProvince(proveName); } if (StringUtils.isNotEmpty(cityName)) { member.setCity(cityName); } member.setId(WebUserTools.getUid()); userService.update(member); return createJsonMav("9999", "??", ""); } // /profile/password/edit ?? @RequestMapping(value = "/profile/password/edit") public ModelAndView passwordEdit() { ModelAndView mav = new ModelAndView("/user/passwdEdit"); return mav; } // /profile/password/auth ???? // {"result":{"code":"21101", "message":"???", "data":{"extMst":""}}} @RequestMapping(value = "/profile/password/auth") public ModelAndView auth(String password) { MemberDO member = userService.getMemberById(WebUserTools.getUid()); if (!StringUtils.equals(password, member.getPassword())) { return createExtJsonMav("21101", "???", ""); } return createExtJsonMav("9999", "", ""); } // /profile/password/update ??? // {"result":{"code":"9999", "message":"", "data":{"extMst":"??"}}} // {"result":{"code":"20015", "message":"??", "data":{"extMst":""}}} // {"result":{"code":"20013", "message":"?6-16??", "data":{"extMst":""}}} // {"result":{"code":"20005", "message":"?", "data":{"extMst":""}}} // {"result":{"code":"20003", "message":"?6-16??", "data":{"extMst":""}}} @RequestMapping(value = "/profile/password/update") public ModelAndView passwdUpdate(String oldPwd, String pwd) { if (StringUtils.isEmpty(oldPwd)) { return createExtJsonMav("20015", "??", ""); } int oldSize = StringFormatter.getEnWordSize(oldPwd); if (oldSize < 6 || oldSize > 16) { return createExtJsonMav("20013", "?6-16??", ""); } if (StringUtils.isEmpty(pwd)) { return createExtJsonMav("20015", "?", ""); } int pwdSize = StringFormatter.getEnWordSize(oldPwd); if (pwdSize < 6 || pwdSize > 16) { return createExtJsonMav("20003", "?6-16??", ""); } MemberDO member = userService.getMemberById(WebUserTools.getUid()); if (!StringUtils.equals(oldPwd, member.getPassword())) { return createExtJsonMav("21101", "???", ""); } member.setPassword(pwd); userService.update(member); return createExtJsonMav("9999", "", "??"); } // /profile/email/edit ? @RequestMapping(value = "/profile/email/edit") public ModelAndView emailEdit() { ModelAndView mav = new ModelAndView("/user/emailEdit"); MemberDO member = userService.getMemberById(WebUserTools.getUid()); mav.addObject("member", member); return mav; } // /profile/email/update ?? // {"result":{"code":"9999", "message":"", "data":{"extMst":"??"}}} // {"result":{"code":"20007", "message":"?", "data":{"extMst":""}}} // {"result":{"code":"20005", "message":"?", "data":{"extMst":""}}} @RequestMapping(value = "/profile/email/update") public ModelAndView emailUpdate(String password, String email) { if (StringUtils.isEmpty(password)) { return createExtJsonMav("20005", "??", ""); } if (StringUtils.isEmpty(email)) { return createExtJsonMav("20007", "?", ""); } MemberDO member = userService.getMemberById(WebUserTools.getUid()); if (!StringUtils.equals(password, member.getPassword())) { return createExtJsonMav("21101", "???", ""); } MemberDO md = userService.find(new MemberQuery(null, email)); if (md != null) { return createExtJsonMav("21122", "", ""); } member.setEmail(email); userService.update(member); return createExtJsonMav("9999", "", "??"); } // /profile/bind ? @RequestMapping(value = "/profile/bind", method = RequestMethod.GET) public ModelAndView bind() { ModelAndView mav = new ModelAndView("/user/phoneBind"); MemberDO member = userService.getMemberById(WebUserTools.getUid()); mav.addObject("member", member); return mav; } // /passport/checkPhoneAbled phone // {"result":{"code":"9999", "message":"???", "data":""}} // {"result":{"code":"8887", "message":"????", "data":""}} // {"result":{"code":"8888", "message":"?", "data":""}} @RequestMapping(value = "/passport/checkPhoneAbled") public ModelAndView checkPhoneAbled(String phone) { if (StringUtils.isEmpty(phone)) { return createExtJsonMav("8887", "????", ""); } Pattern p = Pattern.compile(PHONE_REG); Matcher m = p.matcher(phone); if (!m.matches()) { return createJsonMav("8887", "????", ""); } MemberDO member = userService.find(new MemberQuery(null, null, null, phone)); if (member != null) { return createExtJsonMav("8888", "?", ""); } return createJsonMav("9999", "???", ""); } // /profile/sendcode ???,????? // {"result":{"code":"24001", "message":"??", "data":""}} // {"result":{"code":"8887", "message":"?????", "data":""}} // {"result":{"code":"9999", "message":"?????", "data":""}} @RequestMapping(value = "/profile/sendcode") public ModelAndView sendcode(String phone, String code) { if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)) { return createJsonMav("24001", "??", ""); } Pattern p = Pattern.compile(PHONE_REG); Matcher m = p.matcher(phone); if (!m.matches()) { return createJsonMav("8887", "?????", ""); } boolean check = WebsiteCheckCodeManager.INSTANCE.checkByMD5(cookieManager, code); if (!check) { return createJsonMav("24001", "????", ""); } code = SerialNumGenerator.RandomNum(4); PushSMSUtils.getInstance().sendCodeSMS(code, phone); userService.checkMemberCode(WebUserTools.getUid(), code); return createJsonMav("9999", "?????", ""); } // /profile/bind ???? // {"result":{"code":"8888", "message":"????", "data":""}} // {"result":{"code":"8888", "message":"????", "data":""}} // {"result":{"code":"8887", "message":"?????", "data":""}} // {"result":{"code":"9999", "message":"?", "data":""}} @RequestMapping(value = "/profile/bind", method = RequestMethod.POST) public ModelAndView phoneBind(String phone, String code) { if (StringUtils.isEmpty(phone)) { return createJsonMav("8887", "?????", ""); } Pattern p = Pattern.compile(PHONE_REG); Matcher m = p.matcher(phone); if (!m.matches()) { return createJsonMav("8887", "?????", ""); } if (StringUtils.isEmpty(code)) { return createJsonMav("8888", "????", ""); } MemberDO member = userService.find(new MemberQuery(null, null, null, phone)); if (member != null) { return createJsonMav("8888", "????", ""); } MemberThinDO memberThin = userService.fetchMemberById(WebUserTools.getUid()); if (memberThin == null || !StringUtils.equalsIgnoreCase(code, memberThin.getSmsCode())) { return createJsonMav("8888", "????", ""); } if (System.currentTimeMillis() - memberThin.getSmsSendTime() > 10 * 60 * 1000) { return createJsonMav("8888", "??,10?", ""); } userService.update(new MemberDO(WebUserTools.getUid(), phone)); return createJsonMav("9999", "?", ""); } // /profile/unbind // {"result":{"code":"9999", "message":"?", "data":""}} @RequestMapping(value = "/profile/unbind") public ModelAndView phoneUnBind() { MemberDO member = new MemberDO(WebUserTools.getUid()); member.setMobile(StringUtils.EMPTY); userService.update(member); return createJsonMav("9999", "?", ""); } }