|
@@ -0,0 +1,119 @@
|
|
|
|
|
+package com.miekir.shibei.controller.api;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.miekir.shibei.bean.*;
|
|
|
|
|
+import com.miekir.shibei.repository.JsonRepository;
|
|
|
|
|
+import com.miekir.shibei.repository.UserRepository;
|
|
|
|
|
+import com.miekir.shibei.tool.RequestTool;
|
|
|
|
|
+import com.miekir.shibei.tool.TextUtils;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.data.domain.PageRequest;
|
|
|
|
|
+import org.springframework.data.domain.Sort;
|
|
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+@Controller
|
|
|
|
|
+public class JsonController {
|
|
|
|
|
+ // 自动装配
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private JsonRepository jsonRepository;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private UserRepository userRepository;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 新增和更新优惠券
|
|
|
|
|
+ */
|
|
|
|
|
+ @RequestMapping(value = "/api/saveJson", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
|
|
|
|
|
+ @ResponseBody
|
|
|
|
|
+ public String saveJson(@RequestHeader HttpHeaders header, @RequestBody JsonBean jsonBean) {
|
|
|
|
|
+ ResponseResult<String> responseResult = new ResponseResult<String>();
|
|
|
|
|
+ responseResult.setMessage("操作失败");
|
|
|
|
|
+
|
|
|
|
|
+ String email = header.getFirst("email");
|
|
|
|
|
+ String token = header.getFirst("token");
|
|
|
|
|
+ // 根据email查找用户,查询用户的token是否相同
|
|
|
|
|
+ User dbUserBean;
|
|
|
|
|
+ try {
|
|
|
|
|
+ dbUserBean = userRepository.findUserByEmail(email);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 用户token不一致
|
|
|
|
|
+ if (dbUserBean == null || !TextUtils.equals(token, dbUserBean.getToken())) {
|
|
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ jsonRepository.save(jsonBean);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ responseResult.setContent("操作成功");
|
|
|
|
|
+ responseResult.setCode(ResultCode.SUCCESS);
|
|
|
|
|
+ responseResult.setMessage("操作成功");
|
|
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取保存的JSON Bean
|
|
|
|
|
+ */
|
|
|
|
|
+ @RequestMapping(value = "/api/getJsonBean", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
|
|
|
|
|
+ @ResponseBody
|
|
|
|
|
+ public String getJsonBean(@RequestHeader HttpHeaders header) {
|
|
|
|
|
+ ResponseResult<JsonBean> responseResult = new ResponseResult<JsonBean>();
|
|
|
|
|
+ responseResult.setMessage("获取失败");
|
|
|
|
|
+
|
|
|
|
|
+ String email = header.getFirst("email");
|
|
|
|
|
+ String token = header.getFirst("token");
|
|
|
|
|
+ // 根据email查找用户,查询用户的token是否相同
|
|
|
|
|
+ User dbUserBean;
|
|
|
|
|
+ try {
|
|
|
|
|
+ dbUserBean = userRepository.findUserByEmail(email);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 用户token不一致
|
|
|
|
|
+ if (dbUserBean == null || !TextUtils.equals(token, dbUserBean.getToken())) {
|
|
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ JsonBean jsonBean = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ jsonBean = jsonRepository.findCouponByEmail(email);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ responseResult.setContent(jsonBean);
|
|
|
|
|
+
|
|
|
|
|
+ responseResult.setCode(ResultCode.SUCCESS);
|
|
|
|
|
+ responseResult.setMessage("获取成功");
|
|
|
|
|
+ return JSON.toJSONString(responseResult);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 通过Get方法获取保存的JSON字符串
|
|
|
|
|
+ */
|
|
|
|
|
+ @RequestMapping(value = "/api/getJson", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
|
|
|
|
|
+ @ResponseBody
|
|
|
|
|
+ public String getJson(String email) {
|
|
|
|
|
+ JsonBean jsonBean = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ jsonBean = jsonRepository.findCouponByEmail(email);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+ return jsonBean.json;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|