zoukankan      html  css  js  c++  java
  • shop--10.商品--商品编辑(后端)--Controller层

    主要逻辑:

    • 根据前端url中productId ,调用service层的接口获取product信息,以便前端能在商品编辑页面展示对应的数据
    • 修改商品

      1 /**
      2      * 通过商品id获取商品信息
      3      * 
      4      * @param productId
      5      * @return
      6      */
      7     @RequestMapping(value="/getproductbyid",method = RequestMethod.GET)
      8     @ResponseBody
      9     private Map<String, Object> getProductById(@RequestParam Long productId){
     10         Map<String, Object> modelMap = new HashMap<String, Object>();
     11         //非空判断
     12         if(productId > -1) {
     13             //获取商品信息
     14             Product product = productService.getProductById(productId);
     15             //获取该店铺下的商品类别列表
     16             List<ProductCategory> productCategoryList = productCategoryService.getProductCategoryList(product.getShop().getShopId());
     17             modelMap.put("product", product);
     18             modelMap.put("productCategoryList", productCategoryList);
     19             modelMap.put("success", true);
     20         }else {
     21             modelMap.put("success", false);
     22             modelMap.put("errMsg", "empty productId");
     23         }
     24         return modelMap;
     25     }
     26     
     27     @RequestMapping(value="/modifyproduct",method=RequestMethod.POST)
     28     private Map<String, Object> modifyProduct(HttpServletRequest request){
     29         Map<String, Object> modelMap = new HashMap<String,Object>();
     30         //是商品编辑时候调用还是上下架操作的时候调用
     31         //若为前者则进行验证码判断,后者则跳过验证码判断
     32         boolean statusChange = HttpServletRequestUtil.getBoolean(request, "statusChange");
     33         ////步骤1:验证码校验
     34         if(!statusChange&&!CodeUtil.checkVerifyCode(request)) {
     35             modelMap.put("success", false);
     36             modelMap.put("errMsg", "输入了错误的验证码");
     37             return modelMap;
     38         }
     39         //接受前端参数的初始化,包括商品,缩略图,详情图列表实体类
     40         ObjectMapper objectMapper = new ObjectMapper();
     41         Product product = null;
     42         ImageHolder thumbnail = null;
     43         List<ImageHolder> productImgList = new ArrayList<ImageHolder>();
     44         CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
     45         //步骤2:使用FastJson提供的api,实例化Product 构造调用service层的第一个参数
     46         //接受前端参数变量的初始化,商品,缩略图,详情图列表实体类
     47         String productStr = HttpServletRequestUtil.getString(request, "productStr");
     48         try {
     49             product = objectMapper.readValue(productStr, Product.class);
     50         } catch (Exception e) {
     51             modelMap.put("success", false);
     52             modelMap.put("errMsg", e.toString());
     53             return modelMap;
     54         }
     55         //若请求中存在文件流,则取出相关的文件(包括缩略图和详情图)
     56         try {
     57             if(commonsMultipartResolver.isMultipart(request)) {
     58                 MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
     59                 //取出缩略图并构建ImageHolder对象
     60                 CommonsMultipartFile thumbnailFile = (CommonsMultipartFile) multipartHttpServletRequest.getFile("thumbnail");
     61                 if(thumbnailFile!=null) {
     62                     thumbnail = new ImageHolder(thumbnailFile.getOriginalFilename(), thumbnailFile.getInputStream());
     63                 }
     64                 //取出详情图并构建List<ImageHolder>列表对象,最多支持六张图片上传
     65                 for(int i = 0;i<IMAGEMAXCOUNT;i++) {
     66                     CommonsMultipartFile productImgFile = (CommonsMultipartFile) multipartHttpServletRequest.getFile("productImg" + i);
     67                     if(productImgFile != null) {
     68                         //若取出的第i个详情图片文件流不为空,则将其加入详情图列表
     69                         ImageHolder productImg = new ImageHolder(productImgFile.getOriginalFilename(), productImgFile.getInputStream());
     70                         productImgList.add(productImg);
     71                     }else {
     72                         //若取出的第i个详情图片文件流为空,则终止循环
     73                         break;
     74                     }
     75                 }
     76             }
     77         }catch (Exception e) {
     78             modelMap.put("success", false);
     79             modelMap.put("errMsg", e.toString());
     80             return modelMap;
     81         }
     82         // Step4 调用Service层
     83         if(product != null && thumbnail != null && productImgList.size() > 0) {
     84             try {
     85                 //从session中获取shop信息,不依赖前端的传递更安全
     86                 Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
     87                 product.setShop(currentShop);
     88                 //调用modifyProduct
     89                 ProductExecution pe = productService.modifyProduct(product, thumbnail, productImgList);
     90                 if(pe.getState() == ProductStateEnum.SUCCESS.getState()) {
     91                     modelMap.put("success", true);
     92                 }else {
     93                     modelMap.put("success", false);
     94                     modelMap.put("errMsg", pe.getStateInfo());
     95                 }
     96             }catch (ProductOperationException e) {
     97                 modelMap.put("success", false);
     98                 modelMap.put("errMsg", e.toString());
     99                 return modelMap;
    100             }
    101         }else {
    102             modelMap.put("success", false);
    103             modelMap.put("errMsg", "请输入商品信息");
    104         }
    105         return modelMap;
    106     }
  • 相关阅读:
    查看web访问日志各url shell
    分页公式
    编译hadoop2.6.0源码
    淘宝微信支付
    nodejs hot deployment
    nwjs
    css小技巧
    webpack babel 相关文章
    nodejs web framework
    swiper
  • 原文地址:https://www.cnblogs.com/windbag7/p/9414536.html
Copyright © 2011-2022 走看看