手机移动端接口

孤街浪徒 提交于 2020-03-23 09:08:30

在项目开发中,可能移动端需要请求查询数据或者向数据表中插入数据,这时需要编写接口给移动端调用。

//查询
@RequestMapping("/query")
//通过request传递请求参数
    public String query(Page page, HttpServletRequest request) {
        Map<String, Object> keyMap = new HashMap<String, Object>();    
        List<Map<String, Object>> rtnList = new ArrayList<Map<String, Object>>();
        try {
                        获取查询条件
            keyMap.put("carNumber", request.getParameter("CarsNo"));
            List<Map<String, Object>> vehicleRegulationList = vehicleRegulationService.findAll(page, keyMap);
            for(Map<String, Object> vehicleRegulation : vehicleRegulationList) {
            Map<String, Object> map = new HashMap<>();
            map.put("CarsNo", vehicleRegulation.get("carNumber"));
            map.put("PhoneNo", vehicleRegulation.get("phone"));
            map.put("CarsType", vehicleRegulation.get("carType"));
            map.put("ViolationPlace", vehicleRegulation.get("siteVio"));
            map.put("ViolationType", vehicleRegulation.get("typeVio"));
            map.put("datetimeVio", vehicleRegulation.get("datetimeVio"));
            rtnList.add(map);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
               //返回json格式数据
        return AjaxUtilNew.getJson("200", "查询成功", rtnList);
    }

}                    
//手机端上传图片
@RequestMapping("/upload")
    public String upload(HttpServletRequest request) {
        String url = "";
        try {
                        //接受图片流
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
                          //将图片流转成MultipartFile,有可能上传多张图片
            Map<String, MultipartFile> files = multiRequest.getFileMap();
            //将图片上传到图片服务器并获取图片路径
                        for (MultipartFile multipartFile : files.values()) {
                if (multipartFile.isEmpty()) {
                    continue;
                }
                String fileName = multipartFile.getOriginalFilename();
                if (fileName == null || fileName.trim().equals("")) {
                    continue;
                }
                Integer index = fileName.lastIndexOf("\\");
                String newStr = "";
                if (index > -1) {
                    newStr = fileName.substring(index + 1);
                } else {
                    newStr = fileName;
                }
                if (!newStr.equals("")) {
                    fileName = newStr;
                }
                String preFixName = fileName.substring(0, fileName.lastIndexOf("."));
                String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1);
                String prefileName = preFixName + "_" + DateTools.getFullNowDateTimeErp() + "." + suffixName;
          
                String filePath = new File(System.getProperty("user.dir")).getParent() + "/webapps/fileApp";
                String realPath = filePath + ("/uploadFiles/vehicleRegulation/"
                        + "mobileUpload" + "/" + UUID.randomUUID());
                File upload = new File(realPath);
                log.info(realPath);
                if (!upload.exists()) {
                    upload.mkdirs();
                }
                File dest = new File(upload.getAbsolutePath() + "/" + prefileName);
                 multipartFile.transferTo(dest);
                url = url + "," + upload.getAbsolutePath() + "/" + prefileName;
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
                //返回图片url,多个图片就将多个url拼接返回给调用端
        return url;
    }
 1 //手机端接受数据,插入数据库
 2 @RequestMapping("/receive")
 3     public void receive(HttpServletRequest request) {
 4         VehicleRegulationEntity vehicleRegulation = new VehicleRegulationEntity();
 5         User user = new User();
 6                 //通过request获取参数
 7         user.setStrName(request.getParameter("UploadPerNmae"));
 8         user.setStrAccount(request.getParameter("UploadPerNo"));
 9         Company company = new Company();
10         company.setId("1");
11         String id = UUIDUtil.getUUID();
12         vehicleRegulation.setId(id);
13         vehicleRegulation.setCarNumber(request.getParameter("CarsNo"));
14         vehicleRegulation.setPhone(request.getParameter("PhoneNo"));
15         vehicleRegulation.setCarType(request.getParameter("CarsType"));
16         vehicleRegulation.setSiteVio(request.getParameter("ViolationPlace"));
17         vehicleRegulation.setTypeVio(request.getParameter("ViolationType"));
18         vehicleRegulation.setRemark(request.getParameter("Remark"));
19         vehicleRegulation.setCreateDateTime(DateTools.getFullNowDateTime());
20         vehicleRegulation.setCreateUser(user);
21         vehicleRegulation.setCompany(company);
22         try {
23             vehicleRegulationService.createMobile(vehicleRegulation);    
24         } catch (Exception e) {
25             log.error(e.getMessage(), e);
26         }
27         NjAttach attach = new NjAttach();
28         String[] url = request.getParameter("url").split(",");
29         for(int i = 0; i < url.length; i++) {
30             if(url[i].isEmpty()) {
31                 continue;
32             }
33             String idString = UUIDUtil.getUUID();
34             attach.setId(idString);
35             attach.setStrPath(url[i]);
36             attach.setRelativedId(id);
37             attach.setCompany(company);
38             attach.setCreateUser(user);
39             int j = url[i].lastIndexOf('/');
40             String strName = url[i].substring(j+1);
41             attach.setStrName(strName);
42             attach.setCreateDateTime(DateTools.getFullNowDateTime());
43             try {
44                 attachService.createMobile(attach);
45             } catch (Exception e) {
46                 log.error(e.getMessage(), e);
47             }
48         }
49     }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!