1.接收并转化相应的参数,包括店铺信息和图片信息
2.注册店铺
3.返回结果
//注册店铺
@RequestMapping(value="/registershop", method= RequestMethod.POST )
@ResponseBody
public Map<String, Object> registerShop(HttpServletRequest request){
Map<String, Object> modelMap = new HashMap<>();
//1.接收并转化相应的参数,包括店铺信息和图片信息
String shopStr = HttpServletRequestUtil.getString( request, "shopStr" );
ObjectMapper mapper = new ObjectMapper(); // create once, reuse
Shop shop = null;
try {
shop = mapper.readValue(shopStr, Shop.class);
} catch (IOException e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
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 user = (PersonInfo) request.getSession().getAttribute("user");
shop.setOwner( user );
ShopExecution shopExecution = shopService.addShop(shop, shopImg);
if(shopExecution.getState() == ShopStateEnum.CHECK.getState()){
modelMap.put( "success", true );
}else{
modelMap.put( "success", false );
modelMap.put( "errMsg", shopExecution.getStateInfo());
}
}else{
modelMap.put("success", false);
modelMap.put("errMsg", "请输入店铺信息");
}
//3.返回结果
return modelMap;
}