shop--7.店铺信息编辑--Controller层

Posted windbag7

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shop--7.店铺信息编辑--Controller层相关的知识,希望对你有一定的参考价值。

在Controller层需要两个方法,一个就是获取店铺信息

getShopById

 

 1   /**
 2     * 根据店铺信息获取店铺
 3     * @param request
 4     * @return
 5     */
 6    @RequestMapping(value="/getshopbyid", method=RequestMethod.GET)
 7    @ResponseBody
 8    private Map<String, Object> getShopById(HttpServletRequest request){
 9        Map<String, Object> modelMap = new HashMap<>();
10        Long shopId = HttpServletRequestUtil.getLong( request, "shopId" );
11        if(shopId > -1){//大于-1就证明前端传过来了
12            try{
13                Shop shop = shopService.getShopByShopId( shopId );
14                List<Area> areaList = areaService.getAreaList();//因为区域信息是可以更改的,所以需要获取
15                modelMap.put("shop", shop);
16                modelMap.put( "areaList", areaList );
17                modelMap.put( "success", true );
18            } catch(Exception e){
19                modelMap.put("success", false);
20                modelMap.put("errMsg", e.toString());
21            }
22  
23        }else{
24            modelMap.put("success", false);
25            modelMap.put( "errMsg", "empty shopId" );
26        }
27        return modelMap;
28    }

 


更改店铺信息(和注册店铺相似)

  1.先看传递过来的验证码

  2.接受从前台传过来的shopStr的字符串

  3.将shopStr转成Shop实体对象

  4.前台传过来的文件流解析成CommonsMultipartFile里面去

  5.最后将Shop对象和文件流使用Service层方法添加

 

  

 1 @RequestMapping(value = "/modifyshop", method = RequestMethod.POST)
 2     @ResponseBody
 3     private Map<String, Object> modifyShop(HttpServletRequest request) {
 4         Map<String, Object> modelMap = new HashMap<String, Object>();
 5         if (!CodeUtil.checkVerifyCode(request)) {
 6             modelMap.put("success", false);
 7             modelMap.put("error", "输入了错误的验证码");
 8             return modelMap;
 9         }
10         // 1.接受并转化相应的参数,包括店铺信息以及图片信息
11         String shopStr = HttpServletRequestUtil.getString(request, "shopStr");
12         ObjectMapper mapper = new ObjectMapper();
13         Shop shop = null;
14         try {
15             shop = mapper.readValue(shopStr, Shop.class);
16         } catch (Exception e) {
17             modelMap.put("success", false);
18             modelMap.put("errMsg", e.getMessage());
19             return modelMap;
20         }
21         CommonsMultipartFile shopImg = null;
22         CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(
23                 request.getSession().getServletContext());
24         // 如果文件上传有值的话
25         if (commonsMultipartResolver.isMultipart(request)) {
26             MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
27             shopImg = (CommonsMultipartFile) multipartHttpServletRequest.getFile("shopImg");
28         }//图片可上传可不上传的 非空判断可以去掉
29         // 2.修改店铺信息
30         if (shop != null && shop.getShopId() != null) {//确保shopId不为空
31             ShopExecution se;
32             try {
33                 // 当文件为空时 直接传入空值
34                 if (shopImg == null) {
35                     se = shopService.modifyShop(shop, null, null);
36                 } else {
37                     se = shopService.modifyShop(shop, shopImg.getInputStream(), shopImg.getOriginalFilename());
38                 }
39                 // se = shopService.modifyShop(shop,
40                 // shopImg.getInputStream(),shopImg.getOriginalFilename()); 换成了上面这个if else 判断
41                 if (se.getState() == ShopStateEnum.SUCCESS.getState()) {//CHECK->SUCCESS
42                     modelMap.put("success", true);
43                 } else {
44                     modelMap.put("errMsg", se.getStateInfo());
45                 }
46             } catch (ShopOperationException e) {
47                 modelMap.put("success", false);
48                 modelMap.put("errMsg", e.getMessage());
49             } catch (IOException e) {
50                 modelMap.put("success", false);
51                 modelMap.put("errMsg", e.getMessage());
52             }
53 
54             return modelMap;
55         } else {
56             modelMap.put("success", false);
57             modelMap.put("errMsg", "请输入店铺ID");
58             return modelMap;
59         }
60         // 3.返回结果
61     }

 

顺便在注册方法中Session

   通过Session获取登录后的user信息

            PersonInfo owner = (PersonInfo) request.getSession().getAttribute("user");
            shop.setOwner(owner);

  获取user信息后设置user可操作的店铺列表

1           @SuppressWarnings("unchecked")
2           List<Shop> shopList = (List<Shop>) request.getSession().getAttribute("shopList");
3           if (shopList == null || shopList.size() == 0) {
4           shopList = new ArrayList<Shop>();
5           }
6           shopList.add(se.getShop());
7           request.getSession().setAttribute("shopList", shopList);

 


以上是关于shop--7.店铺信息编辑--Controller层的主要内容,如果未能解决你的问题,请参考以下文章

shop--7.店铺信息编辑--Service层(包括更新店铺,对照片的处理)

校园商铺-6店铺编辑列表和列表功能-2店铺信息编辑之Controller层的实现

医美小程序实战教程-店铺信息编辑

医美小程序实战教程店铺信息编辑

校园商铺-6店铺编辑列表和列表功能-2店铺信息编辑之Service层的实现

医美小程序实战教程-店铺信息编辑后提交