1414
1515package zuo .biao .apijson .server ;
1616
17+ import java .util .Random ;
18+
1719import org .springframework .web .bind .annotation .PathVariable ;
1820import org .springframework .web .bind .annotation .RequestBody ;
1921import org .springframework .web .bind .annotation .RequestMapping ;
2022import org .springframework .web .bind .annotation .RequestMethod ;
2123import org .springframework .web .bind .annotation .RestController ;
2224
25+ import com .alibaba .fastjson .JSONObject ;
26+
27+ import zuo .biao .apijson .JSON ;
28+ import zuo .biao .apijson .StringUtil ;
29+
2330/**request receiver and controller
2431 * @author Lemon
2532 */
@@ -31,7 +38,7 @@ public class Controller {
3138 public String get (@ PathVariable String request ) {
3239 return new RequestParser (zuo .biao .apijson .RequestMethod .GET ).parse (request );
3340 }
34-
41+
3542 /**用POST方法GET数据,request和response都非明文,浏览器看不到,用于对安全性要求高的GET请求
3643 * @param request
3744 * @return
@@ -40,23 +47,78 @@ public String get(@PathVariable String request) {
4047 public String post_get (@ RequestBody String request ) {
4148 return new RequestParser (zuo .biao .apijson .RequestMethod .POST_GET ).parse (request );
4249 }
43-
50+
4451 @ RequestMapping (value ="post" , method = RequestMethod .POST )
4552 public String post (@ RequestBody String request ) {
4653 return new RequestParser (zuo .biao .apijson .RequestMethod .POST ).parse (request );
4754 }
48-
55+
4956 /**以下接口继续用POST接口是为了客户端方便,只需要做get,post请求。也可以改用实际对应的方法。
5057 * post,put方法名可以改为add,update等更客户端容易懂的名称
51- */
58+ */
5259 @ RequestMapping (value ="put" , method = RequestMethod .POST )
5360 public String put (@ RequestBody String request ) {
5461 return new RequestParser (zuo .biao .apijson .RequestMethod .PUT ).parse (request );
5562 }
56-
63+
5764 @ RequestMapping (value ="delete" , method = RequestMethod .POST )
5865 public String delete (@ RequestBody String request ) {
5966 return new RequestParser (zuo .biao .apijson .RequestMethod .DELETE ).parse (request );
6067 }
6168
69+
70+ @ RequestMapping ("post/authCode/{phone}" )
71+ public String postAuthCode (@ PathVariable String phone ) {
72+ new RequestParser (zuo .biao .apijson .RequestMethod .DELETE ).parse (newVerifyRequest (newVerify (phone , 0 )));
73+
74+ JSONObject response = new RequestParser (zuo .biao .apijson .RequestMethod .POST ).parseResponse (
75+ newVerifyRequest (newVerify (phone , new Random ().nextInt (9999 ) + 1000 )));
76+
77+ JSONObject verify = null ;
78+ try {
79+ verify = response .getJSONObject ("Verify" );
80+ } catch (Exception e ) {
81+ // TODO: handle exception
82+ }
83+ if (verify == null || verify .getIntValue ("status" ) != 200 ) {
84+ return JSON .toJSONString (response );
85+ }
86+
87+ return getAuthCode (phone );
88+ }
89+
90+ @ RequestMapping (value ="post_get/authCode/{phone}" , method = RequestMethod .POST )
91+ public String getAuthCode (@ PathVariable String phone ) {
92+ return new RequestParser (zuo .biao .apijson .RequestMethod .POST_GET ).parse (newVerifyRequest (newVerify (phone , 0 )));
93+ }
94+
95+ @ RequestMapping ("check/authCode/{phone}/{code}" )
96+ public String checkAuthCode (@ PathVariable String phone , @ PathVariable String code ) {
97+ if (StringUtil .isNumer (code ) == false ) {
98+ code = "-1" ;
99+ }
100+ return new RequestParser (zuo .biao .apijson .RequestMethod .POST_GET ).parse (
101+ newVerifyRequest (newVerify (phone , Integer .parseInt (0 + StringUtil .getNumber (code )))));
102+ }
103+
104+ private JSONObject newVerify (String phone , int code ) {
105+ JSONObject verify = new JSONObject (true );
106+ verify .put ("id" , phone );
107+ if (code > 0 ) {
108+ verify .put ("code" , code );
109+ }
110+ return verify ;
111+ }
112+ private JSONObject newVerifyRequest (JSONObject verify ) {
113+ return newRequest (verify , "Verify" , true );
114+ }
115+ private JSONObject newRequest (JSONObject object , String name , boolean needTag ) {
116+ JSONObject request = new JSONObject (true );
117+ request .put (name , object );
118+ if (needTag ) {
119+ request .put ("tag" , name );
120+ }
121+ return request ;
122+ }
123+
62124}
0 commit comments