3636
3737import apijson .demo .server .model .BaseModel ;
3838import apijson .demo .server .model .Comment ;
39- import apijson .demo .server .model .Login ;
4039import apijson .demo .server .model .Moment ;
41- import apijson .demo .server .model .Password ;
42- import apijson .demo .server .model .User ;
4340import apijson .demo .server .model .Privacy ;
41+ import apijson .demo .server .model .User ;
4442import apijson .demo .server .model .Verify ;
45- import apijson .demo .server .model .Wallet ;
4643import zuo .biao .apijson .JSON ;
4744import zuo .biao .apijson .JSONResponse ;
48- import zuo .biao .apijson .Log ;
4945import zuo .biao .apijson .RequestMethod ;
5046import zuo .biao .apijson .StringUtil ;
5147import zuo .biao .apijson .server .JSONRequest ;
@@ -160,19 +156,15 @@ public String delete(@RequestBody String request, HttpSession session) {
160156
161157
162158 public static final String USER_ ;
159+ public static final String PRIVACY_ ;
163160 public static final String MOMENT_ ;
164161 public static final String COMMENT_ ;
165- public static final String WALLET_ ;
166- public static final String PASSWORD_ ;
167- public static final String USER_PRIVACY_ ;
168- public static final String VERIFY_ ;
162+ public static final String VERIFY_ ; //加下划线后缀是为了避免 Verify 和 verify 都叫VERIFY,分不清
169163 static {
170164 USER_ = User .class .getSimpleName ();
165+ PRIVACY_ = Privacy .class .getSimpleName ();
171166 MOMENT_ = Moment .class .getSimpleName ();
172167 COMMENT_ = Comment .class .getSimpleName ();
173- WALLET_ = Wallet .class .getSimpleName ();
174- PASSWORD_ = Password .class .getSimpleName ();
175- USER_PRIVACY_ = Privacy .class .getSimpleName ();
176168 VERIFY_ = Verify .class .getSimpleName ();
177169 }
178170
@@ -187,8 +179,8 @@ public String delete(@RequestBody String request, HttpSession session) {
187179 public static final String NAME = "name" ;
188180 public static final String PHONE = "phone" ;
189181 public static final String PASSWORD = "password" ;
190- public static final String LOGIN_PASSWORD = "loginPassword " ;
191- public static final String PAY_PASSWORD = "payPassword " ;
182+ public static final String _PASSWORD = "_password " ;
183+ public static final String _PAY_PASSWORD = "_payPassword " ;
192184 public static final String OLD_PASSWORD = "oldPassword" ;
193185 public static final String VERIFY = "verify" ;
194186
@@ -339,7 +331,8 @@ private JSONObject newVerifyRequest(String phone, String verify) {
339331 }
340332
341333
342-
334+ public static final int LOGIN_TYPE_PASSWORD = 0 ;//密码登录
335+ public static final int LOGIN_TYPE_VERIFY = 1 ;//验证码登录
343336 /**用户登录
344337 * @param request 只用String,避免encode后未decode
345338 * @return
@@ -379,7 +372,7 @@ public JSONObject login(@RequestBody String request, HttpSession session) {
379372 new Privacy ().setPhone (phone )
380373 )
381374 );
382- JSONResponse response = new JSONResponse (phoneResponse ).getJSONResponse (USER_PRIVACY_ );
375+ JSONResponse response = new JSONResponse (phoneResponse ).getJSONResponse (PRIVACY_ );
383376 if (JSONResponse .isSucceed (response ) == false ) {
384377 return response ;
385378 }
@@ -403,7 +396,7 @@ public JSONObject login(@RequestBody String request, HttpSession session) {
403396
404397 //校验凭证
405398 int type = Integer .valueOf (0 + StringUtil .getNumber (typeString ));
406- if (type == Login . TYPE_PASSWORD ) {//password密码登录
399+ if (type == LOGIN_TYPE_PASSWORD ) {//password密码登录
407400 response = new JSONResponse (
408401 new Parser (POST_HEAD , true ).parseResponse (
409402 new JSONRequest (new Privacy (userId ).setPassword (password ))
@@ -415,7 +408,7 @@ public JSONObject login(@RequestBody String request, HttpSession session) {
415408 if (JSONResponse .isSucceed (response ) == false ) {
416409 return response ;
417410 }
418- response = response .getJSONResponse (type == Login . TYPE_PASSWORD ? USER_PRIVACY_ : VERIFY_ );
411+ response = response .getJSONResponse (type == LOGIN_TYPE_PASSWORD ? PRIVACY_ : VERIFY_ );
419412 if (JSONResponse .isExist (response ) == false ) {
420413 return Parser .newErrorResult (new ConditionErrorException ("账号或密码错误" ));
421414 }
@@ -434,7 +427,7 @@ public JSONObject login(@RequestBody String request, HttpSession session) {
434427 session .setAttribute (USER_ID , userId );//用户id
435428 session .setAttribute (TYPE , type );//登录方式
436429 session .setAttribute (USER_ , user );//用户
437- session .setAttribute (USER_PRIVACY_ , privacy );//用户隐私信息
430+ session .setAttribute (PRIVACY_ , privacy );//用户隐私信息
438431 // session.setMaxInactiveInterval(1*60);//设置session过期时间
439432
440433 return response ;
@@ -508,7 +501,7 @@ public JSONObject register(@RequestBody String request) {
508501 new Privacy ().setPhone (phone )
509502 )
510503 );
511- JSONObject checkUser = check == null ? null : check .getJSONObject (USER_PRIVACY_ );
504+ JSONObject checkUser = check == null ? null : check .getJSONObject (PRIVACY_ );
512505 if (checkUser == null || checkUser .getIntValue (JSONResponse .KEY_COUNT ) > 0 ) {
513506 return Parser .newErrorResult (new ConflictException ("手机号" + phone + "已经注册" ));
514507 }
@@ -531,7 +524,7 @@ public JSONObject register(@RequestBody String request) {
531524 )
532525 )
533526 );
534- if (JSONResponse .isSucceed (response .getJSONResponse (USER_PRIVACY_ )) == false ) {//创建失败,删除新增的无效User和userPrivacy
527+ if (JSONResponse .isSucceed (response .getJSONResponse (PRIVACY_ )) == false ) {//创建失败,删除新增的无效User和userPrivacy
535528
536529 new Parser (DELETE , true ).parseResponse (
537530 new JSONRequest (
@@ -624,26 +617,39 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
624617 } catch (Exception e ) {
625618 return Parser .newErrorResult (e );
626619 }
620+ JSONObject privacyObj ;
621+ long userId ;
622+ String payPassword ;
623+ double change ;
624+ try {
625+ privacyObj = requestObject .getJSONObject (PRIVACY_ );
626+ if (privacyObj == null ) {
627+ throw new NullPointerException ("请设置 " + PRIVACY_ + "!" );
628+ }
629+ userId = privacyObj .getLongValue (ID );
630+ payPassword = privacyObj .getString (_PAY_PASSWORD );
631+ change = privacyObj .getDoubleValue ("balance+" );
632+
633+ if (userId <= 0 ) {
634+ throw new IllegalArgumentException (PRIVACY_ + "." + ID + ":value 中value不合法!" );
635+ }
636+ if (StringUtil .isPassword (payPassword ) == false ) {
637+ throw new IllegalArgumentException (PRIVACY_ + "." + _PAY_PASSWORD + ":value 中value不合法!" );
638+ }
639+ } catch (Exception e ) {
640+ return Parser .extendErrorResult (requestObject , e );
641+ }
627642
628643 //验证密码<<<<<<<<<<<<<<<<<<<<<<<
629644
630- JSONObject pwdObj = requestObject .getJSONObject (PASSWORD_ );
631- requestObject .remove (PASSWORD_ );
632- if (pwdObj == null ) {
633- pwdObj = new JSONRequest ();
634- }
635- if (pwdObj .getIntValue (TYPE ) != Password .TYPE_PAY ) {
636- // return Parser.extendErrorResult(requestObject, new ConditionErrorException("Password type必须是支付类型!"));
637- pwdObj .put (TYPE , Password .TYPE_PAY );
638- }
639-
645+ privacyObj .remove ("balance+" );
640646 JSONResponse response = new JSONResponse (
641647 new Parser (POST_HEAD , true ).setSession (session ).parseResponse (
642- new JSONRequest (PASSWORD_ , pwdObj )
648+ new JSONRequest (PRIVACY_ , privacyObj )
643649 )
644650 );
645- response = response .getJSONResponse (PASSWORD_ );
646- if (response == null || response .isExist () == false ) {
651+ response = response .getJSONResponse (PRIVACY_ );
652+ if (JSONResponse .isExist (response ) == false ) {
647653 return Parser .extendErrorResult (requestObject , new ConditionErrorException ("支付密码错误!" ));
648654 }
649655
@@ -652,13 +658,6 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
652658
653659 //验证金额范围<<<<<<<<<<<<<<<<<<<<<<<
654660
655- JSONObject wallet = requestObject .getJSONObject (WALLET_ );
656- long id = wallet == null ? 0 : wallet .getLongValue (ID );
657- if (id <= 0 ) {
658- return Parser .extendErrorResult (requestObject , new ConditionErrorException ("请设置Wallet及内部的id!" ));
659- }
660-
661- double change = wallet .getDoubleValue ("balance+" );
662661 if (change == 0 ) {
663662 return Parser .extendErrorResult (requestObject , new OutOfRangeException ("balance+的值不能为0!" ));
664663 }
@@ -672,20 +671,26 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
672671 response = new JSONResponse (
673672 new Parser (POST_GET , true ).parseResponse (
674673 new JSONRequest (
675- new Wallet ( id ). setUserId ( AccessVerifier . getUserId ( session ) )
674+ new Privacy ( userId )
676675 )
677676 )
678677 );
679- Wallet w = response == null ? null : response .getObject (Wallet .class );
680- if (w == null ) {
678+ Privacy privacy = response == null ? null : response .getObject (Privacy .class );
679+ long id = privacy == null ? 0 : BaseModel .value (privacy .getId ());
680+ if (id != userId ) {
681681 return Parser .extendErrorResult (requestObject , new Exception ("服务器内部错误!" ));
682682 }
683683
684- if (w . getBalance () == null || w .getBalance (). doubleValue ( ) < -change ) {
684+ if (BaseModel . value ( privacy .getBalance ()) < -change ) {
685685 return Parser .extendErrorResult (requestObject , new OutOfRangeException ("余额不足!" ));
686686 }
687687 }
688688
689+
690+ privacyObj .remove (_PAY_PASSWORD );
691+ privacyObj .put ("balance+" , change );
692+ requestObject .put (PRIVACY_ , privacyObj );
693+ requestObject .put (JSONRequest .KEY_TAG , PRIVACY_ );
689694 //不免验证,里面会验证身份
690695 return new Parser (PUT ).setSession (session ).parseResponse (requestObject );
691696 }
0 commit comments