正在回答 回答被采纳积分+1
@Controller
@RequestMapping("/shopadmin")
public class ProductManagementController {
@Autowired
private ProductService productService;
@Autowired
private ProductCategoryService productCategoryService;
// 支持上传商品详情图的最大数量
private static final int IMAGEMAXCOUNT = 6;
/**
* 通过店铺id获取该店铺下的商品列表
*
* @param request
* @return
*/
@RequestMapping(value = "/getproductlistbyshop", method = RequestMethod.GET)
@ResponseBody
private Map<String, Object> getProductListByShop(HttpServletRequest request) {
Map<String, Object> modelMap = new HashMap<String, Object>();
// 获取前台传过来的页码
int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
// 获取前台传过来的每页要求返回的商品数上限
int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
// 从当前session中获取店铺信息,主要是获取shopId
Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
// 空值判断
if ((pageIndex > -1) && (pageSize > -1) && (currentShop != null) && (currentShop.getShopId() != null)) {
// 获取传入的需要检索的条件,包括是否需要从某个商品类别以及模糊查找商品名去筛选某个店铺下的商品列表
// 筛选的条件可以进行排列组合
long productCategoryId = HttpServletRequestUtil.getLong(request, "productCategoryId");
String productName = HttpServletRequestUtil.getString(request, "productName");
Product productCondition = compactProductCondition(currentShop.getShopId(), productCategoryId, productName);
// 传入查询条件以及分页信息进行查询,返回相应商品列表以及总数
ProductExecution pe = productService.getProductList(productCondition, pageIndex, pageSize);
modelMap.put("productList", pe.getProductList());
modelMap.put("count", pe.getCount());
modelMap.put("success", true);
} else {
modelMap.put("success", false);
modelMap.put("errMsg", "empty pageSize or pageIndex or shopId");
}
return modelMap;
}
/**
* 通过商品id获取商品信息
*
* @param productId
* @return
*/
@RequestMapping(value = "/getproductbyid", method = RequestMethod.GET)
@ResponseBody
private Map<String, Object> getProductById(@RequestParam Long productId) {
Map<String, Object> modelMap = new HashMap<String, Object>();
// 非空判断
if (productId > -1) {
// 获取商品信息
Product product = productService.getProductById(productId);
// 获取该店铺下的商品类别列表
List<ProductCategory> productCategoryList = productCategoryService
.getProductCategoryList(product.getShop().getShopId());
modelMap.put("product", product);
modelMap.put("productCategoryList", productCategoryList);
modelMap.put("success", true);
} else {
modelMap.put("success", false);
modelMap.put("errMsg", "empty productId");
}
return modelMap;
}
@RequestMapping(value = "/addproduct", method = RequestMethod.POST)
@ResponseBody
private Map<String, Object> addProduct(HttpServletRequest request) {
Map<String, Object> modelMap = new HashMap<String, Object>();
// 验证码校验
if (!CodeUtil.checkVerifyCode(request)) {
modelMap.put("success", false);
modelMap.put("errMsg", "输入了错误的验证码");
return modelMap;
}
// 接收前端参数的变量的初始化,包括商品,缩略图,详情图列表实体类
ObjectMapper mapper = new ObjectMapper();
Product product = null;
ImageHolder thumbnail = null;
List<ImageHolder> productImgList = new ArrayList<ImageHolder>();
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
try {
// 若请求中存在文件流,则取出相关的文件(包括缩略图和详情图)
if (multipartResolver.isMultipart(request)) {
thumbnail = handleImage(request, thumbnail, productImgList);
} else {
modelMap.put("success", false);
modelMap.put("errMsg", "上传图片不能为空");
return modelMap;
}
} catch (Exception e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.toString());
return modelMap;
}
try {
String productStr = HttpServletRequestUtil.getString(request, "productStr");
// 尝试获取前端传过来的表单string流并将其转换成Product实体类
product = mapper.readValue(productStr, Product.class);
} catch (Exception e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.toString());
return modelMap;
}
// 若Product信息,缩略图以及详情图列表为非空,则开始进行商品添加操作
if (product != null && thumbnail != null && productImgList.size() > 0) {
try {
// 从session中获取当前店铺的Id并赋值给product,减少对前端数据的依赖
Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
product.setShop(currentShop);
// 执行添加操作
ProductExecution pe = productService.addProduct(product, thumbnail, productImgList);
if (pe.getState() == ProductStateEnum.SUCCESS.getState()) {
modelMap.put("success", true);
} else {
modelMap.put("success", false);
modelMap.put("errMsg", pe.getStateInfo());
}
} catch (ProductOperationException e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.toString());
return modelMap;
}
} else {
modelMap.put("success", false);
modelMap.put("errMsg", "请输入商品信息");
}
return modelMap;
}
private ImageHolder handleImage(HttpServletRequest request, ImageHolder thumbnail, List<ImageHolder> productImgList)
throws IOException {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 取出缩略图并构建ImageHolder对象
CommonsMultipartFile thumbnailFile = (CommonsMultipartFile) multipartRequest.getFile("thumbnail");
if (thumbnailFile != null) {
thumbnail = new ImageHolder(thumbnailFile.getOriginalFilename(), thumbnailFile.getInputStream());
}
// 取出详情图列表并构建List<ImageHolder>列表对象,最多支持六张图片上传
for (int i = 0; i < IMAGEMAXCOUNT; i++) {
CommonsMultipartFile productImgFile = (CommonsMultipartFile) multipartRequest.getFile("productImg" + i);
if (productImgFile != null) {
// 若取出的第i个详情图片文件流不为空,则将其加入详情图列表
ImageHolder productImg = new ImageHolder(productImgFile.getOriginalFilename(),
productImgFile.getInputStream());
productImgList.add(productImg);
} else {
// 若取出的第i个详情图片文件流为空,则终止循环
break;
}
}
return thumbnail;
}
/**
* 商品编辑
*
* @param request
* @return
*/
@RequestMapping(value = "/modifyproduct", method = RequestMethod.POST)
@ResponseBody
private Map<String, Object> modifyProduct(HttpServletRequest request) {
Map<String, Object> modelMap = new HashMap<String, Object>();
// 是商品编辑时候调用还是上下架操作的时候调用
// 若为前者则进行验证码判断,后者则跳过验证码判断
boolean statusChange = HttpServletRequestUtil.getBoolean(request, "statusChange");
// 验证码判断
if (!statusChange && !CodeUtil.checkVerifyCode(request)) {
modelMap.put("success", false);
modelMap.put("errMsg", "输入了错误的验证码");
return modelMap;
}
// 接收前端参数的变量的初始化,包括商品,缩略图,详情图列表实体类
ObjectMapper mapper = new ObjectMapper();
Product product = null;
ImageHolder thumbnail = null;
List<ImageHolder> productImgList = new ArrayList<ImageHolder>();
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
// 若请求中存在文件流,则取出相关的文件(包括缩略图和详情图)
try {
if (multipartResolver.isMultipart(request)) {
thumbnail = handleImage(request, thumbnail, productImgList);
}
} catch (Exception e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.toString());
return modelMap;
}
try {
String productStr = HttpServletRequestUtil.getString(request, "productStr");
// 尝试获取前端传过来的表单string流并将其转换成Product实体类
product = mapper.readValue(productStr, Product.class);
} catch (Exception e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.toString());
return modelMap;
}
// 非空判断
if (product != null) {
try {
// 从session中获取当前店铺的Id并赋值给product,减少对前端数据的依赖
Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
product.setShop(currentShop);
// 开始进行商品信息变更操作
ProductExecution pe = productService.modifyProduct(product, thumbnail, productImgList);
if (pe.getState() == ProductStateEnum.SUCCESS.getState()) {
modelMap.put("success", true);
} else {
modelMap.put("success", false);
modelMap.put("errMsg", pe.getStateInfo());
}
} catch (RuntimeException e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.toString());
return modelMap;
}
} else {
modelMap.put("success", false);
modelMap.put("errMsg", "请输入商品信息");
}
return modelMap;
}
$(function() {
// 获取此店铺下的商品列表的URL
var listUrl = '/o2o/shopadmin/getproductlistbyshop?pageIndex=1&pageSize=999';
// 商品下架URL
var statusUrl = '/o2o/shopadmin/modifyproduct';
getList();
/**
* 获取此店铺下的商品列表
*
* @returns
*/
function getList() {
// 从后台获取此店铺的商品列表
$.getJSON(listUrl, function(data) {
if (data.success) {
var productList = data.productList;
var tempHtml = '';
// 遍历每条商品信息,拼接成一行显示,列信息包括:
// 商品名称,优先级,上架\下架(含productId),编辑按钮(含productId)
// 预览(含productId)
productList.map(function(item, index) {
var textOp = "下架";
var contraryStatus = 0;
if (item.enableStatus == 0) {
// 若状态值为0,表明是已下架的商品,操作变为上架(即点击上架按钮上架相关商品)
textOp = "上架";
contraryStatus = 1;
} else {
contraryStatus = 0;
}
// 拼接每件商品的行信息
tempHtml += '' + '<div class="row row-product">'
+ '<div class="col-33">'
+ item.productName
+ '</div>'
+ '<div class="col-20">'
+ item.point
+ '</div>'
+ '<div class="col-40">'
+ '<a href="#" class="edit" data-id="'
+ item.productId
+ '" data-status="'
+ item.enableStatus
+ '">编辑</a>'
+ '<a href="#" class="status" data-id="'
+ item.productId
+ '" data-status="'
+ contraryStatus
+ '">'
+ textOp
+ '</a>'
+ '<a href="#" class="preview" data-id="'
+ item.productId
+ '" data-status="'
+ item.enableStatus
+ '">预览</a>'
+ '</div>'
+ '</div>';
});
// 将拼接好的信息赋值进html控件中
$('.product-wrap').html(tempHtml);
}
});
}
// 将class为product-wrap里面的a标签绑定上点击的事件
$('.product-wrap')
.on(
'click',
'a',
function(e) {
var target = $(e.currentTarget);
if (target.hasClass('edit')) {
// 如果有class edit则点击就进入店铺信息编辑页面,并带有productId参数
window.location.href = '/o2o/shopadmin/productoperation?productId='
+ e.currentTarget.dataset.id;
} else if (target.hasClass('status')) {
// 如果有class status则调用后台功能上/下架相关商品,并带有productId参数
changeItemStatus(e.currentTarget.dataset.id,
e.currentTarget.dataset.status);
} else if (target.hasClass('preview')) {
// 如果有class preview则去前台展示系统该商品详情页预览商品情况
window.location.href = '/o2o/frontend/productdetail?productId='
+ e.currentTarget.dataset.id;
}
});
function changeItemStatus(id, enableStatus) {
// 定义product json对象并添加productId以及状态(上架/下架)
var product = {};
product.productId = id;
product.enableStatus = enableStatus;
$.confirm('确定么?', function() {
// 上下架相关商品
$.ajax({
url : statusUrl,
type : 'POST',
data : {
productStr : JSON.stringify(product),
statusChange : true
},
dataType : 'json',
success : function(data) {
if (data.success) {
$.toast('操作成功!');
getList();
} else {
$.toast('操作失败!');
}
}
});
});
}
});
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星