Java tutorial
/* * Copyright (c) 2013, Helome and/or its affiliates. All rights reserved. * Helome PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * Created on 20131030 */ package controllers.user; import java.util.Date; import java.util.Map; import java.util.UUID; import models.User; import models.service.PhoneVerifyCodeService; import models.service.PhoneVerifyCodeService.PhoneVerifyCodeType; import models.service.PhoneVerifyCodeService.SendVerifyCodeResult; import org.apache.commons.lang3.StringUtils; import play.cache.Cache; import play.db.jpa.Transactional; import play.libs.Json; import play.mvc.Result; import utils.DateUtils; import utils.EmailUtil; import utils.HelomeUtil; import vo.EmailInfo; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; import common.Constants; import controllers.base.BaseApp; import controllers.base.ObjectNodeResult; import ext.config.ConfigFactory; import ext.sns.model.UserOAuth; import ext.sns.service.UserOAuthService; import ext.usercenter.UserCenterService; import ext.usercenter.UserCenterService.PasswordSecurityGrade; import ext.usercenter.UserCenterService.PhoneNumExistResult; /** * * * * @ClassName: UserSettingApp * @Description: controller * @date 2013-11-5 ?10:46:56 * @author ShenTeng * */ public class UserSettingApp extends BaseApp { /** * * * @return */ @Transactional(readOnly = true) public static Result detail() { User user = User.getFromSession(session()); PasswordSecurityGrade passwordSecurityGrade = User.getPasswordSecurityGrade(session()); return ok(views.html.usercenter.usersetting.render(user, passwordSecurityGrade)); } /** * * * @return */ @Transactional(readOnly = true) public static Result thirdaccountsetting() { return ok(views.html.usercenter.thirdaccountsetting.render()); } /** * * * @return */ @Transactional(readOnly = false) public static Result changeEmail() { JsonNode json = getJson(); // ? boolean isValidParams = json.hasNonNull("old") && json.hasNonNull("new") && json.hasNonNull("psw"); if (!isValidParams) { return illegalParameters(); } User user = User.getFromSession(session()); // email ObjectNodeResult result = User.changeEmail(user, json.get("old").asText(), json.get("new").asText(), json.get("psw").asText(), session()); return ok(result.getObjectNode()); } /** * ? * * @return */ @Transactional(readOnly = false) public static Result changePassword() { JsonNode json = getJson(); // ? boolean isValidParams = json.hasNonNull("old") && json.hasNonNull("new"); if (!isValidParams) { return illegalParameters(); } User user = User.getFromSession(session()); ObjectNodeResult result = User.changePassword(user, json.get("old").asText(), json.get("new").asText(), session()); return ok(result.getObjectNode()); } /** * * * @return */ @Transactional(readOnly = false) public static Result modifySafetyReminder() { JsonNode newCfg = getJson(); User user = User.getFromSession(session()); ObjectNodeResult result = User.modifySafetyReminder(user, newCfg, session()); return ok(result.getObjectNode()); } /** * ?? * * @return */ @Transactional(readOnly = false) public static Result modifyBookingReminder() { JsonNode newCfg = getJson(); User user = User.getFromSession(session()); ObjectNodeResult result = User.modifyBookingReminder(user, newCfg, session()); return ok(result.getObjectNode()); } /** * * * @return */ @Transactional(readOnly = false) public static Result bindMobilePhone() { JsonNode json = getJson(); ObjectNodeResult result = new ObjectNodeResult(); User user = User.getFromSession(session()); if (StringUtils.isNotBlank(user.getPhoneNumber())) { result.error("?", "500006"); } // ? boolean isValidParams = json.hasNonNull("phoneNum") && json.hasNonNull("code"); if (!isValidParams) { return illegalParameters(); } result = User.bindMobilePhone(user, json.findPath("phoneNum").asText(), json.findPath("phoneNum").asText(), json.findPath("code").asText(), session()); return ok(result.getObjectNode()); } /** * ? * * @return */ @Transactional(readOnly = false) public static Result updateMobilePhone() { JsonNode json = getJson(); // ? boolean isValidParams = json.hasNonNull("phoneNum") && json.hasNonNull("code"); if (!isValidParams) { return illegalParameters(); } User user = User.getFromSession(session()); String phoneNum = user.getMaskPhoneNumber(); ObjectNodeResult result = User.bindMobilePhone(user, phoneNum, json.get("phoneNum").asText(), json.get("code").asText(), session()); return ok(result.getObjectNode()); } /** * ?,? * * @return */ @Transactional(readOnly = false) public static Result bindNewPhone() { JsonNode json = getJson(); // ? if (!json.hasNonNull("code") || !json.hasNonNull("newPhoneNum") || !json.hasNonNull("key")) { return illegalParameters(); } User user = User.getFromSession(session()); ObjectNodeResult result = new ObjectNodeResult(); Long userId = (Long) Cache.get(Constants.CACHE_CHANGE_PHONE_BY_EMAIL_KEY_CU + json.get("key").asText()); if (null == userId) { return ok(result.error("??????") .getObjectNode()); } if (!user.id.equals(userId)) { return ok(result.error( "?????????") .getObjectNode()); } result = User.bindNewPhone(user, json.findPath("newPhoneNum").asText(), json.findPath("code").asText(), session()); if (result.isSuccess()) { //user-code key String ucKey = Constants.CACHE_CHANGE_PHONE_BY_EMAIL_KEY_UC + user.id; String oldCode = (String) Cache.get(ucKey); if (StringUtils.isNotBlank(oldCode)) { String oldCUKey = Constants.CACHE_CHANGE_PHONE_BY_EMAIL_KEY_CU + oldCode; Cache.remove(ucKey); Cache.remove(oldCUKey); } } return ok(result.getObjectNode()); } /** * ???? * * @return */ public static Result phoneNumExists() { JsonNode json = getJson(); // ? boolean isValidParams = json.hasNonNull("phoneNum"); if (!isValidParams) { return illegalParameters(); } ObjectNodeResult result = new ObjectNodeResult(); String phoneNum = json.get("phoneNum").asText(); if (HelomeUtil.trim(phoneNum).length() != 11) { result.error("?"); } else { PhoneNumExistResult validateResult = UserCenterService.validatePhoneNumExist(phoneNum); if (PhoneNumExistResult.EXIST == validateResult) { result.put("exists", true); } else if (PhoneNumExistResult.NOT_EXIST == validateResult) { result.put("exists", false); } else { result.error(""); } } return ok(result.getObjectNode()); } @Transactional public static Result queryConnectedSNS() { User user = User.getFromSession(session()); Map<String, UserOAuth> userOAuthMap = UserOAuthService.getValidByUserId(user.id); ArrayNode sns = Json.newObject().arrayNode(); for (Map.Entry<String, UserOAuth> e : userOAuthMap.entrySet()) { sns.add(e.getKey()); } ObjectNodeResult result = new ObjectNodeResult(); result.put("sns", sns); return ok(result.getObjectNode()); } /** * ???? * * @return */ public static Result sendPhoneVerificationCode() { JsonNode json = getJson(); // ? boolean isValidParams = json.hasNonNull("phoneNum"); if (!isValidParams) { return illegalParameters(); } User user = User.getFromSession(session()); ObjectNodeResult result = new ObjectNodeResult(); String phoneNum = json.get("phoneNum").asText(); if (HelomeUtil.trim(phoneNum).length() != 11) { result.error("?"); } else { SendVerifyCodeResult sendResult = PhoneVerifyCodeService .sendVerifyCode(PhoneVerifyCodeType.BIND_MOBILE_PHONE, String.valueOf(user.id), phoneNum); if (SendVerifyCodeResult.TOO_MANY == sendResult) { result.error("?????,????"); } else if (SendVerifyCodeResult.FAIL == sendResult) { result.error("????"); } } return ok(result.getObjectNode()); } /** * ????? * @param user * @return */ public static void sendUdpByEmail(User user) { String webContextUrl = ConfigFactory.getString("web.context.url"); String code = UUID.randomUUID().toString().replace("-", ""); //user-code key String ucKey = Constants.CACHE_CHANGE_PHONE_BY_EMAIL_KEY_UC + user.id; //code-user key String cuKey = Constants.CACHE_CHANGE_PHONE_BY_EMAIL_KEY_CU + code; String oldCode = (String) Cache.get(ucKey); if (StringUtils.isNotBlank(oldCode)) { String oldCUKey = Constants.CACHE_CHANGE_PHONE_BY_EMAIL_KEY_CU + oldCode; Cache.remove(ucKey); Cache.remove(oldCUKey); } Cache.set(ucKey, code, 10 * 60); Cache.set(cuKey, user.id, 10 * 60); String locatin = webContextUrl + "/user/usersetting/phonechange/" + code; String path = ConfigFactory.getString("upload.url"); String date = DateUtils.format(new Date(), DateUtils.FORMAT_DATE); String email = user.getEmail() == null ? "" : user.getEmail().trim(); StringBuffer sb = new StringBuffer(); sb.append("<div style=\"width:980px; background: url(").append(path).append( "topx/assets/misc/skin/v1.0.0/i/mail-bg.png);height: 470px; margin: 100px auto;padding-top: 28px;\"><div style=\"width:478px; height: 430px;border: 1px solid #c2dbff;margin: 0 auto;border-radius: 5px;-moz-box-shadow:0 2px 0 rgba(224, 237, 255, 0.6); -webkit-box-shadow:0 2px 0 rgba(224, 237, 255, 0.6); box-shadow:0 2px 0 rgba(224, 237, 255, 0.6);\"><div style=\"width: 458px;height: 69px;FILTER: progid:DXImageTransform.Microsoft.gradient(gradientType=0,startColorStr=#5c93e1,endColorStr=#2f69c9);background: linear-gradient(top, #5c93e1, #2f69c9);background: -moz-linear-gradient(top, #5c93e1, #2f69c9);background: -ms-linear-gradient(top, #5c93e1 0%,#2f69c9 100%);background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#5c93e1), to(#2f69c9));border-radius: 3px 3px 0 0;-webkit-border-radius: 3px 3px 0 0;padding-left: 20px;\"><ul style=\"margin:0; padding: 0;list-style-type: none;width: 458px;height: 69px;line-height: 79px;overflow: hidden;\">") .append("<li style=\"list-style-type:none;float:left;height:69px;padding-right:20px;font-family:Arial,Helvetica,sans-serif;font-size: 16px;color: #8fb4ec;font-weight: bold;\"><a href=\"http://www.helome.com\" target=\"_blank\"><img style=\"padding-top: 11px;border: 0;\" src=\"") .append(path) .append("topx/assets/misc/images/logo.png\" width=\"157\" height=\"47\"></a></li><li style=\"list-style-type: none;float: left;height: 69px;padding-right: 20px;font-family: Arial, Helvetica, sans-serif;font-size: 16px;color: #8fb4ec;font-weight: bold;\">|</li><li style=\"list-style-type: none;float:left;height: 69px;padding-right: 20px;font-family: Arial, Helvetica, sans-serif;font-size: 16px;color: #8fb4ec;font-weight: bold;\">?</li></ul>") .append("</div><div style=\"width:458px;padding: 20px 0 0 20px;\"><div style=\"font-size: 16px;color: #40526a;font-weight: bold;\">") .append("").append(email) .append(":</div><div style=\"margin: 27px 0 0 25px;line-height: 20px;color: #40526a;font-family: Arial, Helvetica, sans-serif;font-size: 12px;\">") .append("?<br /><br />") .append("?????<br />") .append("<a style=\"width:458px;font-weight: bold;color: #3a75d7;text-decoration: none;word-break: break-all;\" href=\"") .append(locatin).append("\">").append(locatin).append("</a><br /><br />") .append("?<br />") .append("???????<br /><br />") .append("<br />").append("<br />").append(date).append("<br /><br />") .append("<span style=\"color: #a9b2bf;\">??????</span></div></div></div></div>"); EmailInfo emailInfo = new EmailInfo(); emailInfo.setSubject(""); emailInfo.setBody(sb.toString(), "text/html;charset=utf-8"); emailInfo.setTo(email); EmailUtil.pushEmail(emailInfo); } /** * ????? * * @return */ public static Result sendEmail() { ObjectNodeResult result = new ObjectNodeResult(); sendUdpByEmail(User.getFromSession(session())); return ok(result.getObjectNode()); } /** * ? * * @return */ @Transactional public static Result completeUserInfo() { JsonNode json = getJson(); if (!json.hasNonNull("email") || !json.hasNonNull("pwd")) { return illegalParameters(); } String email = json.get("email").asText(); String pwd = json.get("pwd").asText(); ObjectNodeResult result = User.completeUserInfo(session(), email, pwd); return ok(result.getObjectNode()); } /** * ? * * @return */ @Transactional(readOnly = true) public static Result phoneChange(String code) { User user = User.getFromSession(session()); Long userId = (Long) Cache.get(Constants.CACHE_CHANGE_PHONE_BY_EMAIL_KEY_CU + code); if (null == userId) { return errorInfo("??????", "????"); } if (!user.id.equals(userId)) { return errorInfo( "?????????", "???"); } return ok(views.html.usercenter.phonechange.render(code)); } /** * ?????? * * @return */ public static Result sendVerificationCodeByPhone() { User user = User.getFromSession(session()); String phoneNum = user.getMaskPhoneNumber(); ObjectNodeResult result = new ObjectNodeResult(); SendVerifyCodeResult sendResult = PhoneVerifyCodeService .sendVerifyCode(PhoneVerifyCodeType.BIND_MOBILE_PHONE, String.valueOf(user.id), phoneNum); if (SendVerifyCodeResult.TOO_MANY == sendResult) { result.error("?????,????"); } else if (SendVerifyCodeResult.FAIL == sendResult) { result.error("????"); } else { result.put("phoneNum", user.getMaskPhoneNum()); } return ok(result.getObjectNode()); } /** * ?????? * * @return */ public static Result sendVerificationCodeByNewPhone() { JsonNode json = getJson(); User user = User.getFromSession(session()); if (!json.hasNonNull("newPhoneNum")) { return illegalParameters(); } String newPhoneNum = json.findPath("newPhoneNum").asText(); ObjectNodeResult result = new ObjectNodeResult(); SendVerifyCodeResult sendResult = PhoneVerifyCodeService .sendVerifyCode(PhoneVerifyCodeType.BIND_MOBILE_PHONE, String.valueOf(user.id), newPhoneNum); if (SendVerifyCodeResult.TOO_MANY == sendResult) { result.error("?????,????"); } else if (SendVerifyCodeResult.FAIL == sendResult) { result.error("????"); } return ok(result.getObjectNode()); } /** * ???? * * @return */ @Transactional(readOnly = true) public static Result phoneMailSuccess() { return ok(views.html.usercenter.phonemailsuccess.render()); } /** * ???? * * @return */ @Transactional(readOnly = true) public static Result phoneResetSuccess() { return ok(views.html.usercenter.phoneresetsuccess.render()); } }