@@ -501,7 +501,9 @@ public String openHead(@PathVariable("request") String request, HttpSession sess
501501
502502 public static final String CURRENT_USER_ID = "currentUserId" ;
503503 public static final String NAME = "name" ;
504+ public static final String ACCOUNT = "account" ;
504505 public static final String PHONE = "phone" ;
506+ public static final String EMAIL = "email" ;
505507 public static final String PASSWORD = "password" ;
506508 public static final String _PASSWORD = "_password" ;
507509 public static final String _PAY_PASSWORD = "_payPassword" ;
@@ -535,18 +537,20 @@ public JSONObject reload(@RequestBody String request) {
535537 String type ;
536538 JSONObject value ;
537539 String phone ;
540+ String email ;
538541 String verify ;
539542 try {
540543 requestObject = DemoParser .parseRequest (request );
541544 type = requestObject .getString (TYPE );
542545 value = requestObject .getJSONObject (VALUE );
543546 phone = requestObject .getString (PHONE );
547+ email = requestObject .getString (EMAIL );
544548 verify = requestObject .getString (VERIFY );
545549 } catch (Exception e ) {
546550 return extendErrorResult (requestObject , e );
547551 }
548552
549- JSONResponse response = new JSONResponse (headVerify (Verify .TYPE_RELOAD , phone , verify ));
553+ JSONResponse response = new JSONResponse (headVerify (Verify .TYPE_RELOAD , phone , email , verify ));
550554 response = response .getJSONResponse (VERIFY_ );
551555 if (JSONResponse .isExist (response ) == false ) {
552556 return extendErrorResult (requestObject , new ConditionErrorException ("手机号或验证码错误" ));
@@ -601,30 +605,33 @@ public JSONObject postVerify(@RequestBody String request) {
601605 JSONObject requestObject = null ;
602606 int type ;
603607 String phone ;
608+ String email ;
604609 try {
605610 requestObject = DemoParser .parseRequest (request );
606611 type = requestObject .getIntValue (TYPE );
607612 phone = requestObject .getString (PHONE );
613+ email = requestObject .getString (EMAIL );
608614 } catch (Exception e ) {
609615 return extendErrorResult (requestObject , e );
610616 }
611617
612- new DemoParser (DELETE , false ).parse (newVerifyRequest (type , phone ));
618+ new DemoParser (DELETE , false ).parse (newVerifyRequest (type , phone , email ));
613619
614620 JSONObject response = new DemoParser (POST , false ).parseResponse (
615621 newVerifyRequest (type , phone , "" + (new Random ().nextInt (9999 ) + 1000 ))
616622 );
617623
618624
619625 if (JSONResponse .isSuccess (response ) == false ) {
620- new DemoParser (DELETE , false ).parseResponse (newVerifyRequest (type , phone ));
626+ new DemoParser (DELETE , false ).parseResponse (newVerifyRequest (type , phone , email ));
621627 return response ;
622628 }
623629
624630 //TODO 这里直接返回验证码,方便测试。实际上应该只返回成功信息,验证码通过短信发送
625631 JSONObject object = new JSONObject ();
626632 object .put (TYPE , type );
627633 object .put (PHONE , phone );
634+ object .put (EMAIL , email );
628635 return getVerify (JSON .toJSONString (object ));
629636 }
630637
@@ -643,14 +650,16 @@ public JSONObject getVerify(@RequestBody String request) {
643650 JSONObject requestObject = null ;
644651 int type ;
645652 String phone ;
653+ String email ;
646654 try {
647655 requestObject = DemoParser .parseRequest (request );
648656 type = requestObject .getIntValue (TYPE );
649657 phone = requestObject .getString (PHONE );
658+ email = requestObject .getString (EMAIL );
650659 } catch (Exception e ) {
651660 return extendErrorResult (requestObject , e );
652661 }
653- return new DemoParser (GETS , false ).parseResponse (newVerifyRequest (type , phone ));
662+ return new DemoParser (GETS , false ).parseResponse (newVerifyRequest (type , phone , email ));
654663 }
655664
656665 /**校验验证码
@@ -669,29 +678,32 @@ public JSONObject headVerify(@RequestBody String request) {
669678 JSONObject requestObject = null ;
670679 int type ;
671680 String phone ;
681+ String email ;
672682 String verify ;
673683 try {
674684 requestObject = DemoParser .parseRequest (request );
675685 type = requestObject .getIntValue (TYPE );
676686 phone = requestObject .getString (PHONE );
687+ email = requestObject .getString (EMAIL );
677688 verify = requestObject .getString (VERIFY );
678689 } catch (Exception e ) {
679690 return extendErrorResult (requestObject , e );
680691 }
681- return headVerify (type , phone , verify );
692+ return headVerify (type , phone , email , verify );
682693 }
683694
684695 /**校验验证码
685696 * @author Lemon
686697 * @param type
687698 * @param phone
699+ * @param email
688700 * @param code
689701 * @return
690702 */
691- public JSONObject headVerify (int type , String phone , String code ) {
703+ public JSONObject headVerify (int type , String phone , String email , String code ) {
692704 JSONResponse response = new JSONResponse (
693705 new DemoParser (GETS , false ).parseResponse (
694- newVerifyRequest (type , phone , code )
706+ newVerifyRequest (type , phone , email , code )
695707 )
696708 );
697709 Verify verify = response .getObject (Verify .class );
@@ -705,13 +717,13 @@ public JSONObject headVerify(int type, String phone, String code) {
705717 long now = System .currentTimeMillis ();
706718 if (now > 60 *1000 + time ) {
707719 new DemoParser (DELETE , false ).parseResponse (
708- newVerifyRequest (type , phone )
720+ newVerifyRequest (type , phone , email )
709721 );
710722 return newErrorResult (new TimeoutException ("验证码已过期!" ));
711723 }
712724
713725 return new DemoParser (HEADS , false ).parseResponse (
714- newVerifyRequest (type , phone , code )
726+ newVerifyRequest (type , phone , email , code )
715727 );
716728 }
717729
@@ -722,18 +734,18 @@ public JSONObject headVerify(int type, String phone, String code) {
722734 * @param phone
723735 * @return
724736 */
725- public static JSONObject newVerifyRequest (int type , String phone ) {
726- return newVerifyRequest (type , phone , null );
737+ public static JSONObject newVerifyRequest (int type , String phone , String email ) {
738+ return newVerifyRequest (type , phone , email , null );
727739 }
728740 /**新建一个验证码请求
729741 * @param type
730742 * @param phone
731743 * @param verify
732744 * @return
733745 */
734- public static JSONObject newVerifyRequest (int type , String phone , String verify ) {
746+ public static JSONObject newVerifyRequest (int type , String phone , String email , String verify ) {
735747 return new JSONRequest (
736- new Verify (type , phone ).setVerify (verify )
748+ new Verify (type , phone , email ).setVerify (verify )
737749 ).setTag (VERIFY_ ).setFormat (true );
738750 }
739751
@@ -762,7 +774,9 @@ public static JSONObject newVerifyRequest(int type, String phone, String verify)
762774 public JSONObject login (@ RequestBody String request , HttpSession session ) {
763775 JSONObject requestObject = null ;
764776 boolean isPassword ;
777+ String account ;
765778 String phone ;
779+ String email ;
766780 String password ;
767781 int version ;
768782 Boolean format ;
@@ -773,7 +787,9 @@ public JSONObject login(@RequestBody String request, HttpSession session) {
773787 requestObject = DemoParser .parseRequest (request );
774788
775789 isPassword = requestObject .getIntValue (TYPE ) == LOGIN_TYPE_PASSWORD ;//登录方式
790+ account = requestObject .getString (ACCOUNT );//帐号
776791 phone = requestObject .getString (PHONE );//手机
792+ email = requestObject .getString (EMAIL );//邮箱
777793 password = requestObject .getString (PASSWORD );//密码
778794
779795 if (StringUtil .isPhone (phone ) == false ) {
@@ -790,6 +806,18 @@ public JSONObject login(@RequestBody String request, HttpSession session) {
790806 }
791807 }
792808
809+ if (StringUtil .isEmpty (phone ) && StringUtil .isPhone (account )) {
810+ phone = account ;
811+ } else if (StringUtil .isNotEmpty (phone ) && ! StringUtil .isPhone (phone )) {
812+ throw new IllegalArgumentException (PRIVACY_ + "/" + PHONE + ":value 中 value 不合法!必须符合手机号格式!" );
813+ }
814+ 815+ if (StringUtil .isEmpty (email ) && StringUtil .isEmail (account )) {
816+ email = account ;
817+ } else if (StringUtil .isNotEmpty (email ) && ! StringUtil .isEmail (email )) {
818+ throw new IllegalArgumentException (PRIVACY_ + "/" + EMAIL + ":value 中 value 不合法!必须符合邮箱格式!" );
819+ }
820+ 793821 version = requestObject .getIntValue (VERSION );
794822 format = requestObject .getBoolean (FORMAT );
795823 remember = requestObject .getBooleanValue (REMEMBER );
@@ -806,25 +834,25 @@ public JSONObject login(@RequestBody String request, HttpSession session) {
806834
807835 //手机号是否已注册
808836 JSONObject phoneResponse = new DemoParser (HEADS , false ).parseResponse (
809- new JSONRequest (new Privacy ().setPhone (phone ))
837+ new JSONRequest (StringUtil . isNotEmpty ( phone ) ? new Privacy ().setPhone (phone ) : new Privacy (). setEmail ( email ))
810838 );
811839 if (JSONResponse .isSuccess (phoneResponse ) == false ) {
812840 return newResult (phoneResponse .getIntValue (JSONResponse .KEY_CODE ), getString (phoneResponse , JSONResponse .KEY_MSG ));
813841 }
814842 JSONResponse response = new JSONResponse (phoneResponse ).getJSONResponse (PRIVACY_ );
815843 if (JSONResponse .isExist (response ) == false ) {
816- return newErrorResult (new NotExistException ("手机号未注册" ));
844+ return newErrorResult (new NotExistException (StringUtil . isNotEmpty ( phone ) ? "手机号未注册!" : "邮箱未注册! " ));
817845 }
818846
819847 //根据phone获取User
820848 JSONObject privacyResponse = new DemoParser (GETS , false ).parseResponse (
821849 new JSONRequest (
822- new Privacy ().setPhone (phone )
850+ new Privacy ().setPhone (phone ). setEmail ( email )
823851 ).setFormat (true )
824852 );
825853 response = new JSONResponse (privacyResponse );
826854
827- Privacy privacy = response == null ? null : response .getObject (Privacy .class );
855+ Privacy privacy = response .getObject (Privacy .class );
828856 long userId = privacy == null ? 0 : BaseModel .value (privacy .getId ());
829857 if (userId <= 0 ) {
830858 return privacyResponse ;
@@ -837,8 +865,8 @@ public JSONObject login(@RequestBody String request, HttpSession session) {
837865 new JSONRequest (new Privacy (userId ).setPassword (password ))
838866 )
839867 );
840- } else {//verify手机验证码登录
841- response = new JSONResponse (headVerify (Verify .TYPE_LOGIN , phone , password ));
868+ } else {//verify手机/邮箱验证码登录
869+ response = new JSONResponse (headVerify (Verify .TYPE_LOGIN , phone , email , password ));
842870 }
843871 if (JSONResponse .isSuccess (response ) == false ) {
844872 return response .toObject (JSONObject .class );
@@ -859,7 +887,7 @@ public JSONObject login(@RequestBody String request, HttpSession session) {
859887 );
860888 User user = response .getObject (User .class );
861889 if (user == null || BaseModel .value (user .getId ()) != userId ) {
862- return newErrorResult (new NullPointerException ("服务器内部错误" ));
890+ return newErrorResult (new NullPointerException ("服务器内部错误,未查到用户信息! " ));
863891 }
864892
865893 //登录状态保存至session
@@ -915,11 +943,12 @@ public JSONObject logout(HttpSession session) {
915943 * <pre>
916944 {
917945 "Privacy": {
918- "phone": "13000082222",
919- "_password": "123456"
946+ "email": "test@qq.com", // 手机号和邮箱至少传一个
947+ "phone": "13000082222",
948+ "_password": "123456"
920949 },
921950 "User": {
922- "name": "APIJSONUser"
951+ "name": "APIJSONUser"
923952 },
924953 "verify": "1234"
925954 }
@@ -932,19 +961,28 @@ public JSONObject register(@RequestBody String request) {
932961 JSONObject privacyObj ;
933962
934963 String phone ;
964+ String email ;
935965 String verify ;
936966 String password ;
937967 try {
938968 requestObject = DemoParser .parseRequest (request );
939969 privacyObj = requestObject .getJSONObject (PRIVACY_ );
940970
941971 phone = StringUtil .get (privacyObj .getString (PHONE ));
972+ email = StringUtil .get (privacyObj .getString (EMAIL ));
942973 verify = requestObject .getString (VERIFY );
943974 password = privacyObj .getString (_PASSWORD );
944975
945- if (StringUtil .isPhone (phone ) == false ) {
976+ if (StringUtil .isEmpty (phone ) && StringUtil .isEmpty (email )) {
977+ return newIllegalArgumentResult (requestObject , PRIVACY_ + " 中 " + PHONE + " 手机, " + EMAIL + " 邮箱 至少传参一个!" );
978+ }
979+ 980+ if (StringUtil .isNotEmpty (phone ) && StringUtil .isPhone (phone ) == false ) {
946981 return newIllegalArgumentResult (requestObject , PRIVACY_ + "/" + PHONE );
947982 }
983+ if (StringUtil .isNotEmpty (email ) && StringUtil .isEmail (email ) == false ) {
984+ return newIllegalArgumentResult (requestObject , PRIVACY_ + "/" + EMAIL );
985+ }
948986 if (StringUtil .isPassword (password ) == false ) {
949987 return newIllegalArgumentResult (requestObject , PRIVACY_ + "/" + _PASSWORD );
950988 }
@@ -955,14 +993,13 @@ public JSONObject register(@RequestBody String request) {
955993 return extendErrorResult (requestObject , e );
956994 }
957995
958- 959- JSONResponse response = new JSONResponse (headVerify (Verify .TYPE_REGISTER , phone , verify ));
996+ JSONResponse response = new JSONResponse (headVerify (Verify .TYPE_REGISTER , phone , email , verify ));
960997 if (JSONResponse .isSuccess (response ) == false ) {
961998 return response ;
962999 }
9631000 //手机号或验证码错误
9641001 if (JSONResponse .isExist (response .getJSONResponse (VERIFY_ )) == false ) {
965- return extendErrorResult (response , new ConditionErrorException ("手机号或验证码错误 !" ));
1002+ return extendErrorResult (response , new ConditionErrorException ("手机号/邮箱或验证码错误 !" ));
9661003 }
9671004
9681005 //生成User和Privacy
@@ -1050,7 +1087,9 @@ public JSONObject putPassword(@RequestBody String request){
10501087
10511088 JSONObject privacyObj ;
10521089 long userId ;
1090+ String account ;
10531091 String phone ;
1092+ String email ;
10541093 String password ;
10551094 try {
10561095 requestObject = DemoParser .parseRequest (request );
@@ -1065,7 +1104,9 @@ public JSONObject putPassword(@RequestBody String request){
10651104 throw new IllegalArgumentException (PRIVACY_ + " 不能为空!" );
10661105 }
10671106 userId = privacyObj .getLongValue (ID );
1107+ account = privacyObj .getString (ACCOUNT );
10681108 phone = privacyObj .getString (PHONE );
1109+ email = privacyObj .getString (EMAIL );
10691110 password = privacyObj .getString (_PASSWORD );
10701111
10711112 if (StringUtil .isEmpty (password , true )) { //支付密码
@@ -1079,6 +1120,18 @@ public JSONObject putPassword(@RequestBody String request){
10791120 throw new IllegalArgumentException (PRIVACY_ + "/" + _PASSWORD + ":value 中value不合法!" );
10801121 }
10811122 }
1123+ 1124+ if (StringUtil .isEmpty (phone ) && StringUtil .isPhone (account )) {
1125+ phone = account ;
1126+ } else if (StringUtil .isNotEmpty (phone ) && ! StringUtil .isPhone (phone )) {
1127+ throw new IllegalArgumentException (PRIVACY_ + "/" + PHONE + ":value 中 value 不合法!必须符合手机号格式!" );
1128+ }
1129+ 1130+ if (StringUtil .isEmpty (email ) && StringUtil .isEmail (account )) {
1131+ email = account ;
1132+ } else if (StringUtil .isNotEmpty (email ) && ! StringUtil .isEmail (email )) {
1133+ throw new IllegalArgumentException (PRIVACY_ + "/" + EMAIL + ":value 中 value 不合法!必须符合邮箱格式!" );
1134+ }
10821135 } catch (Exception e ) {
10831136 return extendErrorResult (requestObject , e );
10841137 }
@@ -1108,8 +1161,8 @@ public JSONObject putPassword(@RequestBody String request){
11081161 return extendErrorResult (requestObject , new ConditionErrorException ("账号或原密码错误,请重新输入!" ));
11091162 }
11101163 }
1111- else if (StringUtil .isPhone ( phone ) && StringUtil .isVerify ( verify )) {
1112- JSONResponse response = new JSONResponse (headVerify (type , phone , verify ));
1164+ else if (StringUtil .isVerify ( verify ) && ( StringUtil .isPhone ( phone )) || StringUtil . isEmail ( email )) {
1165+ JSONResponse response = new JSONResponse (headVerify (type , phone , email , verify ));
11131166 if (JSONResponse .isSuccess (response ) == false ) {
11141167 return response ;
11151168 }
@@ -1126,6 +1179,7 @@ else if (StringUtil.isPhone(phone) && StringUtil.isVerify(verify)) {
11261179 Privacy privacy = response .getObject (Privacy .class );
11271180 long id = privacy == null ? 0 : BaseModel .value (privacy .getId ());
11281181 privacyObj .remove (PHONE );
1182+ privacyObj .remove (ACCOUNT );
11291183 privacyObj .put (ID , id );
11301184
11311185 requestObject .put (PRIVACY_ , privacyObj );
0 commit comments