大家好,欢迎来到IT知识分享网。
- 第三方速度快
- dsdvi
- 说得对
- 的发送到您
- 萨克斯内存的
- skdnckdnv
- 十多年才开始v
- 的富士康的你付款
- 大仓库
- 萨克斯内存的
package com.imooc.o2o.web.shopadmin; import com.fasterxml.jackson.databind.ObjectMapper; import com.imooc.o2o.dto.ShopExecution; import com.imooc.o2o.entity.Area; import com.imooc.o2o.entity.PersonInfo; import com.imooc.o2o.entity.Shop; import com.imooc.o2o.entity.ShopCategory; import com.imooc.o2o.enums.ShopStateEnum; import com.imooc.o2o.exceptions.ShopOperationException; import com.imooc.o2o.service.AreaService; import com.imooc.o2o.service.ShopCategoryService; import com.imooc.o2o.service.ShopService; import com.imooc.o2o.util.CodeUtil; import com.imooc.o2o.util.HttpServletRequestUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Controller @RequestMapping("/shopadmin") public class ShopManagementController { @Autowired private ShopService shopService; @Autowired private ShopCategoryService shopCategoryService; @Autowired private AreaService areaService; @RequestMapping(value = "/getshopbyid",method = RequestMethod.GET) @ResponseBody private Map<String,Object> getShopById(HttpServletRequest request){ Map<String,Object> modelMap = new HashMap<String,Object>(); long shopId = HttpServletRequestUtil.getLong(request, "shopId"); if (shopId > -1){ try { Shop byShopId = shopService.getByShopId(shopId); List<Area> areaList = areaService.getAreaList(); modelMap.put("shop",byShopId); modelMap.put("areaList",areaList); modelMap.put("success",true); } catch (Exception e) { modelMap.put("success",false); modelMap.put("errMsg",e.toString()); } }else{ modelMap.put("success",false); modelMap.put("errMsg","empty shopId"); } return modelMap; } @RequestMapping(value = "/getshopinitinfo",method = RequestMethod.GET) @ResponseBody private Map<String,Object> getShopInitInfo(){ Map<String,Object> modelMap = new HashMap<String,Object>(); List<ShopCategory> shopCategoryList = new ArrayList<ShopCategory>(); //店铺类别列表 List<Area> areaList = new ArrayList<Area>(); //区域列表 try { shopCategoryList = shopCategoryService.getShopCategoryList(new ShopCategory()); //获取店铺类别全部列表 areaList = areaService.getAreaList(); modelMap.put("shopCategoryList",shopCategoryList); modelMap.put("areaList",areaList); modelMap.put("success",true); } catch (Exception e) { modelMap.put("success",false); modelMap.put("errMsg",e.getMessage()); } return modelMap; } @RequestMapping(value = "/registershop",method = RequestMethod.POST) @ResponseBody private Map<String,Object> registerShop(HttpServletRequest request){ Map<String,Object> modelMap = new HashMap<String,Object>(); //验证码 if (!CodeUtil.checkVerifyCode(request)){ modelMap.put("success",false); modelMap.put("errMsg","输入错误的验证码"); return modelMap; } /** * 1.接收并转化相应的参数,包括店铺信息以及图片信息 * 获取前端传过来店铺信息,并将它转换成实体类 */ String shopStr = HttpServletRequestUtil.getString(request,"shopStr"); ObjectMapper mapper = new ObjectMapper(); //这个类是jackson提供的,主要是用来把对象转换成为一个json字符串返回到前端 Shop shop = null; try { shop = mapper.readValue(shopStr,Shop.class); } catch (Exception e) { modelMap.put("success",false); modelMap.put("errMsg",e.getMessage()); return modelMap; } /** * 获取前端传递过来的文件流,将它接收到shopImg里面去 */ CommonsMultipartFile shopImg = null; CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); //从本次会话当中的上下文去获取相关文件上传的内容 if(commonsMultipartResolver.isMultipart(request)){ MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request; //定义出这个对象,之后我们就可以提取出相对应的文件流 shopImg = (CommonsMultipartFile) multipartHttpServletRequest.getFile("shopImg"); //获取上传图片文件流 }else{ modelMap.put("success",false); //这些数据是放置在视图模型里面 modelMap.put("errMsg","上传图片不能为空"); return modelMap; } //2.注册店铺 if(shop!=null && shopImg!=null){ PersonInfo owner = new PersonInfo(); owner.setUserId(1L); // L标识长整型数值. shop.setOwner(owner); ShopExecution se; try { se = shopService.addShop(shop,shopImg.getInputStream(),shopImg.getOriginalFilename()); //getOriginalFilename() :可以获取到原本文件的名字 //因为se是需要初始化的,就把它放在try这里面来 if(se.getState() == ShopStateEnum.CHECK.getState()){ //如果这状态码和自己原先定义的相同,注册成功 modelMap.put("success",true); }else { modelMap.put("success",false); modelMap.put("errMsg",se.getStateInfo()); } }catch (ShopOperationException e) { modelMap.put("success",false); modelMap.put("errMsg",e.getMessage()); }catch (IOException e) { modelMap.put("success",false); modelMap.put("errMsg",e.getMessage()); } return modelMap; }else{ modelMap.put("success",false); modelMap.put("errMsg","请输入店铺信息"); return modelMap; } } }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/29779.html