Skip to content

Commit cae36a5

Browse files
committed
step api
1 parent 0b79d9f commit cae36a5

5 files changed

Lines changed: 282 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.example.demo.sport;
2+
3+
import java.text.ParseException;
4+
import java.text.SimpleDateFormat;
5+
import java.util.Date;
6+
import java.util.Map;
7+
import java.util.HashMap;
8+
import java.util.regex.Matcher;
9+
import java.util.regex.Pattern;
10+
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RequestMethod;
13+
import org.springframework.web.bind.annotation.RestController;
14+
15+
import com.example.demo.util.*;
16+
17+
18+
@RestController
19+
public class SportController {
20+
21+
private DateUtils dateUtil = new DateUtils();
22+
23+
@RequestMapping(value = "/getstep", method = RequestMethod.GET)
24+
public String getStepData(long uid,String date) {
25+
try {
26+
//检测参数是否合法
27+
if (date != null && !VerifyStartTime(date)) {
28+
return ErrorResponseBuilder.InvalidParamsHadSend();
29+
}
30+
//将String类型的时间转换成Date类型
31+
SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");
32+
java.util.Date sDate =(java.util.Date) sdf.parse(date);
33+
Date sqlsDate = dateUtil.getSqlDate(sDate);
34+
//根据 uid 和 date 查询数据,一般是去查询数据库;这里暂时使用假数据;
35+
Map m = getStepDataFromDB(uid,sqlsDate) ;
36+
//构造json返回值
37+
Return2Client response = new Return2Client(ErrorResponseBuilder.SUCCESS);
38+
response.setContent(m);
39+
return response.toJson();
40+
41+
} catch (ParseException e) {
42+
return ErrorResponseBuilder.InsertOrUpdateTryCatch();
43+
}
44+
}
45+
46+
47+
/************************************************* Private ***********************************************************/
48+
private boolean VerifyStartTime(String st){
49+
Pattern p = Pattern.compile("^\\d{8}$");
50+
Matcher m = p.matcher(st);
51+
52+
if (!m.matches()) {
53+
return false;
54+
}
55+
return true;
56+
}
57+
58+
private Map<String,String> getStepDataFromDB(long uid,Date date){
59+
Map<String,String> m1 = new HashMap<String,String>();
60+
m1.put("step", "8888");
61+
m1.put("calorie", "88");
62+
return m1;
63+
}
64+
65+
66+
67+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.example.demo.util;
2+
3+
import java.sql.Date;
4+
import java.text.ParseException;
5+
import java.text.SimpleDateFormat;
6+
import java.util.Calendar;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
9+
10+
public class DateUtils {
11+
12+
public SimpleDateFormat df_ymd = new SimpleDateFormat("yyyyMMdd");
13+
14+
15+
public boolean VerifyStartTime(String st){
16+
Pattern p = Pattern.compile("^\\d{8}$");
17+
Matcher m = p.matcher(st);
18+
19+
if (!m.matches()) {
20+
return false;
21+
}
22+
return true;
23+
}
24+
25+
26+
/**
27+
* util 转 sql date
28+
* @param date
29+
* @return
30+
*/
31+
public java.sql.Date getSqlDate(java.util.Date date){
32+
Date sqlDate = new java.sql.Date(date.getTime());
33+
return sqlDate;
34+
}
35+
36+
37+
//numberDate like yyyyMMdd
38+
public java.util.Date numberToDate(int numberDate){
39+
try {
40+
java.util.Date dt = df_ymd.parse(String.valueOf(numberDate));
41+
return dt;
42+
} catch (ParseException e) {
43+
// TODO Auto-generated catch block
44+
e.printStackTrace();
45+
return null;
46+
}
47+
}
48+
49+
50+
public int getYear() {
51+
Calendar c = Calendar.getInstance();
52+
return c.get(Calendar.YEAR);
53+
}
54+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.example.demo.util;
2+
3+
public class ErrorResponseBuilder {
4+
/**
5+
* success
6+
*/
7+
public static int SUCCESS=0;
8+
9+
10+
/*
11+
* user service error prefix 500
12+
*/
13+
public static int InvalidLoginAccountType = 50001;
14+
15+
public static String InvalidLoginAccountType_Message()
16+
{
17+
return String.format("{retCode:%d,uid:null}",InvalidLoginAccountType);
18+
}
19+
20+
public static int InvalidPhoneNumberformat = 50002;
21+
22+
public static String InvalidPhoneNumberformat_Message()
23+
{
24+
return String.format("{retCode:%d,uid:null}", InvalidPhoneNumberformat);
25+
}
26+
27+
public static int AccountTypeNotProvided = 50003;
28+
29+
public static String AccountTypeNotProvided_Message()
30+
{
31+
return String.format("{retCode:%d,uid:null}", AccountTypeNotProvided);
32+
}
33+
34+
public static int AccountNotProvided = 50004;
35+
36+
public static String AccountNotProvided_Message()
37+
{
38+
return String.format("{retCode:%d,uid:null}", AccountNotProvided);
39+
}
40+
41+
public static int PasswordNotProvided = 50005;
42+
43+
public static String PasswordNotProvided_Message()
44+
{
45+
return String.format("{retCode:%d,uid:null}", PasswordNotProvided);
46+
}
47+
48+
public static int PasswordOrUserNameNotMatch = 50006;
49+
50+
public static int DBError = 50007;
51+
52+
public static int CacheLimitReached = 50008;
53+
54+
public static int UserAlreadyExist = 50009;
55+
56+
/**
57+
* health service error prefix 600
58+
*/
59+
public static int ParameterNotProvided = 60001;
60+
61+
/**
62+
* sport data error prefix 600
63+
*/
64+
public static int InvalidUidOrTokenType = 60001;
65+
66+
public static String InvalidUidOrTokenType_message() {
67+
return String.format("{retcode:%d,msg:invalid token}", InvalidUidOrTokenType);
68+
}
69+
70+
public static int UploadDatasIsEmpty = 60002;
71+
72+
public static String UploadDatasIsEmpty_message() {
73+
return String.format("retcode:%d,msg:upload empty data", UploadDatasIsEmpty);
74+
}
75+
76+
public static int InvalidParams = 60003;
77+
78+
public static String InvalidParamsHadSend() {
79+
return String.format("{retcode:%d,msg:InvalidParamsHadSend}", InvalidParams);
80+
}
81+
82+
public static int InsertOrUpdateError = 60004;
83+
public static int NoData = 60404;
84+
public static String InsertOrUpdateTryCatch() {
85+
return String.format("retcode:%d,msg:InsertOrUpdateTryCatch}", InsertOrUpdateError);
86+
}
87+
88+
89+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.example.demo.util;
2+
import com.google.gson.Gson;
3+
import com.google.gson.GsonBuilder;
4+
/**
5+
*
6+
* @author hzy
7+
*
8+
*/
9+
public class Message {
10+
private int retCode;
11+
private String msg;
12+
13+
public Message(int retCode) {
14+
super();
15+
this.retCode = retCode;
16+
}
17+
18+
public int getRetCode() {
19+
return retCode;
20+
}
21+
22+
23+
24+
public void setRetCode(int retCode) {
25+
this.retCode = retCode;
26+
}
27+
28+
29+
30+
31+
public String getMsg() {
32+
return msg;
33+
}
34+
35+
public void setMsg(String msg) {
36+
this.msg = msg;
37+
}
38+
39+
/**
40+
* 返回json字符串
41+
* @return
42+
*/
43+
public String toJson() {
44+
// Gson gson = new Gson();
45+
Gson gson = new GsonBuilder()
46+
.setDateFormat("yyyy-MM-dd HH:mm:ss").create();
47+
return gson.toJson(this);
48+
}
49+
50+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.example.demo.util;
2+
3+
public class Return2Client extends Message{
4+
5+
public Return2Client(int retCode) {
6+
super(retCode);
7+
}
8+
9+
private Object content;
10+
11+
12+
13+
public Object getContent() {
14+
return content;
15+
}
16+
17+
public void setContent(Object content) {
18+
this.content = content;
19+
}
20+
21+
22+
}

0 commit comments

Comments
 (0)