Parcourir la source

天气、宜忌接口验证通过

詹子聪 il y a 5 ans
Parent
commit
fa495f8bff

+ 0 - 98
src/main/java/com/miekir/shibei/bean/PasswordBean.java

@@ -1,98 +0,0 @@
-package com.miekir.shibei.bean;
-
-import javax.persistence.*;
-
-/**
- * https://segmentfault.com/q/1010000007793692?_ea=1455968
- * 外键约束主要是在数据库层面上保证数据的一致性,对性能提升没什么帮助,因为插入和更新数据需要检查外键,理论上性能会有所下降,在oralce中外键会增加主从表中主表锁定的竞争,对性能是负面的影响。
- * 实际的项目,不建议使用外键,一方面是降低开发的复杂度(有外键的话主从表类的操作必须先操作主表),另外是有外键在处理数据的时候非常麻烦。
- */
-
-@Entity
-@Table(name = "t_password", schema = "shibei", catalog = "")
-public class PasswordBean {
-    private Integer id;
-    private String platform;
-    private String url;
-    private String account;
-    private String password;
-    private String description;
-    private String userId;
-    private Long updateTime;
-
-    // Id自增
-    @Id
-    @GeneratedValue(strategy= GenerationType.AUTO)
-    @Column(name = "id", nullable = false, insertable = true, updatable = false)
-    public Integer getId() {
-        return id;
-    }
-
-    public void setId(Integer id) {
-        this.id = id;
-    }
-
-    @Column(name = "platform", nullable = true, insertable = true, updatable = true)
-    public String getPlatform() {
-        return platform;
-    }
-
-    public void setPlatform(String platform) {
-        this.platform = platform;
-    }
-
-    @Column(name = "url", nullable = true, insertable = true, updatable = true)
-    public String getUrl() {
-        return url;
-    }
-
-    public void setUrl(String url) {
-        this.url = url;
-    }
-
-    @Column(name = "account", nullable = true, insertable = true, updatable = true)
-    public String getAccount() {
-        return account;
-    }
-
-    public void setAccount(String account) {
-        this.account = account;
-    }
-
-    @Column(name = "password", nullable = true, insertable = true, updatable = true)
-    public String getPassword() {
-        return password;
-    }
-
-    public void setPassword(String password) {
-        this.password = password;
-    }
-
-    @Column(name = "description", nullable = true, insertable = true, updatable = true)
-    public String getDescription() {
-        return description;
-    }
-
-    public void setDescription(String description) {
-        this.description = description;
-    }
-
-    @Column(name = "userId", nullable = true, insertable = true, updatable = true)
-    public String getUserId() {
-        return userId;
-    }
-
-    public void setUserId(String userId) {
-        this.userId = userId;
-    }
-
-    @Basic
-    @Column(name = "updateTime", nullable = true, insertable = true, updatable = true)
-    public Long getUpdateTime() {
-        return updateTime;
-    }
-
-    public void setUpdateTime(Long updateTime) {
-        this.updateTime = updateTime;
-    }
-}

+ 2 - 0
src/main/java/com/miekir/shibei/bean/WeatherBean.java

@@ -8,6 +8,8 @@ package com.miekir.shibei.bean;
  * Description: 天气实体
  */
 public class WeatherBean {
+    public int code = -1;
+    public String msg;
     public String temperatureRange;
     public String temperatureNow;
     public String wind;

+ 2 - 0
src/main/java/com/miekir/shibei/bean/YijiBean.java

@@ -8,6 +8,8 @@ package com.miekir.shibei.bean;
  * Description: 宜忌实体
  */
 public class YijiBean {
+    public int code = -1;
+    public String msg;
     public String newDate;
     public String oldDate;
     public String hsDate;

+ 2 - 2
src/main/java/com/miekir/shibei/controller/api/CouponController.java

@@ -113,7 +113,7 @@ public class CouponController {
     @RequestMapping(value = "/api/getCouponList", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
     @ResponseBody
     public String getCouponList(int pageNum, int pageSize) {
-        ResponseResult<String> responseResult = new ResponseResult<String>();
+        ResponseResult<List<CouponBean>> responseResult = new ResponseResult<List<CouponBean>>();
         responseResult.setMessage("获取失败");
 
         // 自带的分页查询
@@ -127,7 +127,7 @@ public class CouponController {
         }
 
         try {
-            responseResult.setContent(JSON.toJSONString(couponBeanList));
+            responseResult.setContent(couponBeanList);
         } catch (Exception e) {
             e.printStackTrace();
             return JSON.toJSONString(responseResult);

+ 2 - 2
src/main/java/com/miekir/shibei/controller/api/GoodsController.java

@@ -112,7 +112,7 @@ public class GoodsController {
     @RequestMapping(value = "/api/getGoodsList", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
     @ResponseBody
     public String getGoodsList(int pageNum, int pageSize) {
-        ResponseResult<String> responseResult = new ResponseResult<String>();
+        ResponseResult<List<GoodsBean>> responseResult = new ResponseResult<List<GoodsBean>>();
         responseResult.setMessage("获取失败");
 
         // 自带的分页查询
@@ -126,7 +126,7 @@ public class GoodsController {
         }
 
         try {
-            responseResult.setContent(JSON.toJSONString(goodsBeanList));
+            responseResult.setContent(goodsBeanList);
         } catch (Exception e) {
             e.printStackTrace();
             return JSON.toJSONString(responseResult);

+ 40 - 31
src/main/java/com/miekir/shibei/controller/api/JsonController.java

@@ -34,67 +34,76 @@ public class JsonController {
     /**
      * 获取天气信息
      */
-    @RequestMapping(value = "/api/getWeather", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
+    @RequestMapping(value = "/api/getWeather", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
     @ResponseBody
-    public String getWeather(@RequestHeader HttpHeaders header, @RequestBody JsonBean jsonBean) {
-        String email = header.getFirst("email");
-        String token = header.getFirst("token");
-        // 根据email查找用户,查询用户的token是否相同
+    public String getWeather(String token) {
+        WeatherBean weatherBean = new WeatherBean();
+
+        // 根据token查找用户
         User dbUserBean;
         try {
-            dbUserBean = userRepository.findUserByEmail(email);
+            List<User> dbUserList = userRepository.findUserByToken(token);
+            if (dbUserList != null && dbUserList.size() == 1) {
+                dbUserBean = dbUserList.get(0);
+            } else {
+                weatherBean.msg = "请重新登录";
+                return JSON.toJSONString(weatherBean);
+            }
         } catch (Exception e) {
             e.printStackTrace();
-            return "请求无效";
+            weatherBean.msg = "请求无效";
+            return JSON.toJSONString(weatherBean);
         }
 
-        // 用户token不一致
-        if (dbUserBean == null || !TextUtils.equals(token, dbUserBean.getToken())) {
-            return "请求无效";
-        }
-
-        // todo 以后收费 请更新客户端并开通服务后常识
+        // todo 以后收费
+        /*if (!dbUserBean.isVip()) {
+            return "请更新客户端并开通服务后常识";
+        }*/
         String currentDate = DateTool.getCurrentDate();
         if (!TextUtils.equals(mLastWeatherDate, currentDate) || TextUtils.isEmpty(mWeatherBean.temperatureRange)) {
-            mWeatherBean = WebTool.getWeatherInfo();
+            mWeatherBean = WebTool.getWeatherInfo(weatherBean);
             mLastWeatherDate = currentDate;
         }
         return JSON.toJSONString(mWeatherBean);
     }
 
     /**
-     * 获取天气信息
+     * 获取黄历信息
      */
-    @RequestMapping(value = "/api/getYiji", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
+    @RequestMapping(value = "/api/getYiji", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
     @ResponseBody
-    public String getYiji(@RequestHeader HttpHeaders header, @RequestBody JsonBean jsonBean) {
-        String email = header.getFirst("email");
-        String token = header.getFirst("token");
-        // 根据email查找用户,查询用户的token是否相同
+    public String getYiji(String token) {
+        YijiBean yijiBean = new YijiBean();
+
+        // 根据token查找用户
         User dbUserBean;
         try {
-            dbUserBean = userRepository.findUserByEmail(email);
+            List<User> dbUserList = userRepository.findUserByToken(token);
+            if (dbUserList != null && dbUserList.size() == 1) {
+                dbUserBean = dbUserList.get(0);
+            } else {
+                yijiBean.msg = "请重新登录";
+                return JSON.toJSONString(yijiBean);
+            }
         } catch (Exception e) {
-            e.printStackTrace();
-            return "请求无效";
-        }
-
-        // 用户token不一致
-        if (dbUserBean == null || !TextUtils.equals(token, dbUserBean.getToken())) {
-            return "请求无效";
+            yijiBean.msg = "请求无效";
+            return JSON.toJSONString(yijiBean);
         }
 
-        // todo 以后收费 请更新客户端并开通服务后常识
+        // todo 以后收费
+        /*if (!dbUserBean.isVip()) {
+            return "请更新客户端并开通服务后常识";
+        }*/
         String currentDate = DateTool.getCurrentDate();
         if (!TextUtils.equals(mLastYijiDate, currentDate) || TextUtils.isEmpty(mYijiBean.newDate)) {
-            mYijiBean = WebTool.getYijiInfo();
+            mYijiBean = WebTool.getYijiInfo(yijiBean);
             mLastYijiDate = currentDate;
         }
         return JSON.toJSONString(mYijiBean);
     }
 
     /**
-     * 新增和更新优惠券
+     * 新增和更新JSON
      */
     @RequestMapping(value = "/api/saveJson", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
     @ResponseBody

+ 7 - 4
src/main/java/com/miekir/shibei/repository/UserRepository.java

@@ -8,6 +8,8 @@ import org.springframework.data.repository.query.Param;
 import org.springframework.stereotype.Repository;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.List;
+
 /**
  * Created by sjj on 2015/10/24 0024.
  */
@@ -34,12 +36,13 @@ public interface UserRepository extends JpaRepository<User, String> {
     @Query(value="select user from User user where user.email=:email")
     public User findUserByEmail(@Param("email") String email);
 
-    // 说明该方法是修改操作
+
+    @Transactional
+    @Query(value="select user from User user where user.token=:token")
+    public List<User> findUserByToken(@Param("token") String token);
+
     @Modifying
-    // 说明该方法是事务性操作
     @Transactional
-    // 定义查询
-    // @Param注解用于提取参数
     @Query("update User us set us.themeMode=:themeMode where us.email=:email")
     public void updateUser(@Param("themeMode") Integer themeMode, @Param("email") String email);
 

+ 4 - 4
src/main/java/com/miekir/shibei/tool/WebTool.java

@@ -113,8 +113,7 @@ public class WebTool {
         return content;
     }
 
-    public static WeatherBean getWeatherInfo() {
-        WeatherBean weatherBean = new WeatherBean();
+    public static WeatherBean getWeatherInfo(WeatherBean weatherBean) {
         Document document;
         //百度搜索结果每页大小为10,pn参数代表的不是页数,而是返回结果的开始数
         //如获取第一页则pn=0,第二页则pn=10,第三页则pn=20,以此类推,抽象出模式:(page-1)*pageSize
@@ -136,6 +135,7 @@ public class WebTool {
             if (currentTempElementList != null && currentTempElementList.size() > 0) {
                 Element element = currentTempElementList.get(0);
                 weatherBean.temperatureNow = element.text();
+                weatherBean.code = 0;
             }
 
             // 温度范围
@@ -175,8 +175,7 @@ public class WebTool {
         return weatherBean;
     }
 
-    public static YijiBean getYijiInfo() {
-        YijiBean yijiBean = new YijiBean();
+    public static YijiBean getYijiInfo(YijiBean yijiBean) {
         Document document;
         String url = "https://mnongli.911cha.com/";
         try {
@@ -192,6 +191,7 @@ public class WebTool {
             if (newDateElementList != null && newDateElementList.size() > 0) {
                 Element element = newDateElementList.get(0);
                 yijiBean.newDate = element.text();
+                yijiBean.code = 0;
             }
 
             // 当前农历年月日