Browse Source

修改接口返回方式

yaobo 7 years ago
parent
commit
3f87ddfe10
1 changed files with 16 additions and 33 deletions
  1. 16 33
      src/main/java/cn/efunbox/audio/controller/DeviceController.java

+ 16 - 33
src/main/java/cn/efunbox/audio/controller/DeviceController.java

@@ -111,44 +111,32 @@ public class DeviceController {
 
 
     @RequestMapping(value = "/token" ,method = RequestMethod.POST)
-    public Map Login(@RequestParam long id, @RequestParam String token){
-        Map map = new HashMap<>();
+    public void Login(HttpServletRequest request, HttpServletResponse response, @RequestParam long id, @RequestParam String token){
         Device device = deviceService.Login(id, token);
         if(device==null){
-            map.put("code", ApiCode.INVALID_TOKEN.getCode());
-            map.put("msg", ApiCode.INVALID_TOKEN.getMessage());
-            return map;
+            HttpUtil.responseApiCode(request, response, ApiCode.INVALID_TOKEN);
+            return;
         }
-
-        map.put("code", ApiCode.OK.getCode());
-        Map data = new HashMap<>();
-        data.put("id", device.getId());
-        data.put("token", device.getTokenNew());
-        map.put("data", data);
-
-        return map;
+        HttpUtil.responseOkData(request, response, device);
+        return;
     }
 
     @RequestMapping(value = "/register" ,method = RequestMethod.POST)
-    public Map Register(HttpServletRequest request, HttpServletResponse response){
-        Map map = new HashMap<>();
+    public void Register(HttpServletRequest request, HttpServletResponse response){
         String idChannel = request.getParameter("idChannel");
         String idDevice = request.getParameter("idDevice");
         if(idChannel==null || idDevice==null){
-            map.put("code", ApiCode.PARAMETER_ERROR.getCode());
-            map.put("msg", ApiCode.PARAMETER_ERROR.getMessage());
-            return map;
+            HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
+            return;
         }
         if(deviceService.CheckRegister(Long.valueOf(idChannel), Long.valueOf(idDevice))) {
-            map.put("code", ApiCode.RECORD_EXIST.getCode());
-            map.put("msg", ApiCode.RECORD_EXIST.getMessage());
-            return map;
+            HttpUtil.responseApiCode(request, response, ApiCode.RECORD_EXIST);
+            return;
         }
         List<Channel> channelList = channelService.SearchById(Long.valueOf(idChannel));
         if(channelList==null || channelList.size()<1){
-            map.put("code", ApiCode.PARAMETER_ERROR.getCode());
-            map.put("msg", ApiCode.PARAMETER_ERROR.getMessage());
-            return map;
+            HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
+            return;
         }
         //向云平台注册设备
         String ip = HttpUtil.getIpAddress(request);
@@ -159,9 +147,8 @@ public class DeviceController {
         Map mapRes = JSONObject.parseObject(res);
         System.out.println(mapRes.toString());
         if(Integer.valueOf(mapRes.get("code").toString())!=200){
-            map.put("code", ApiCode.OPERATION_FAIL.getCode());
-            map.put("msg", ApiCode.OPERATION_FAIL.getMessage());
-            return map;
+            HttpUtil.responseApiCode(request, response, ApiCode.OPERATION_FAIL);
+            return;
         }
         mapRes = JSONObject.parseObject(mapRes.get("data").toString());
 
@@ -176,14 +163,10 @@ public class DeviceController {
             device.setIaas_expiration(new Timestamp(Long.valueOf(mapRes.get("expiration").toString())));
             device = deviceService.Update(device);
 
-            map.put("code", ApiCode.OK.getCode());
-            Map data = new HashMap<>();
-            data.put("id", device.getId());
-            data.put("token", device.getTokenNew());
-            map.put("data", data);
+            HttpUtil.responseOkData(request, response, device);
         }
 
-        return map;
+        return;
     }
 
 //    @RequestMapping(value = "/register" ,method = RequestMethod.POST)