基于javaweb+mysql的springboot水果生鲜商城系统(java+springboot+maven+mybatis+vue+mysql+redis)
运行环境
Java≥8、MySQL≥5.7、Node.js≥10
开发工具
后端:eclipse/idea/myeclipse/sts等均可配置运行
前端:WebStorm/VSCode/HBuilderX等均可
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SpringBoot水果生鲜商城系统(java+springboot+maven+mybatis+vue+mysql+redis)
一、项目简述 本系统功能包括: 商品的分类展示,用户的注册登录,购物车,订单结算,购物车加减,后台商品管理,分类管理,订单管理等等功能。
二、项目运行 环境配置:
Jdk1.8 + Tomcat8.5 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)。
项目技术:
Springboot + Maven + mybatis+ Vue 等等组成,B/S模式 + Maven管理等等。
}
@ApiOperation("商品全选或取消全选")
@PostMapping("selectAll")
public ApiRestResponse selectAll(@RequestParam Integer selected) {
List<CartVO> cartVOList = cartService.selectAll(UserFilter.currentUser.get().getId(), selected);
return ApiRestResponse.success(cartVOList);
}
}
package cn.goodboyding.mall.controller;
/**
* 描述:分类控制器
*/
@RestController
public class CategoryController {
private final CategoryService categoryService;
public CategoryController(UserService userService, CategoryService categoryService) {
this.categoryService = categoryService;
}
@ApiOperation("后台添加目录")
@PostMapping("/admin/category/add")
public ApiRestResponse<Object> addCategory(@RequestBody @Valid AddCategoryReq categoryReq) throws GobMallException {
categoryService.add(categoryReq);
return ApiRestResponse.success();
}
@ApiOperation("后台更新目录")
@PostMapping("/admin/category/update")
//保存用户信息时,不保存密码
User user = userService.login(username, password);
user.setPassword(null);
session.setAttribute(Constant.GOB_MALL_USER, user);
return ApiRestResponse.success(user);
}
/**
* 更新个性签名
*/
@ApiOperation("更新个性签名")
@PostMapping("/user/update")
public ApiRestResponse<Object> updateUserInfo(@RequestParam("signature") String signature, HttpSession session) throws GobMallException {
User currentUser = (User) session.getAttribute(Constant.GOB_MALL_USER);
if (currentUser == null) {
return ApiRestResponse.error(GobMallExceptionEnum.NEED_LOGIN);
}
User user = new User();
user.setId(currentUser.getId());
user.setPersonalizedSignature(signature);
userService.updateInformation(user);
return ApiRestResponse.success();
}
/**
* 退出登录
*/
@ApiOperation("退出登录")
@PostMapping("/user/logout")
public ApiRestResponse<Object> logout(HttpSession session) {
session.removeAttribute(Constant.GOB_MALL_USER);
return ApiRestResponse.success();
}
/**
* 管理员登录
*/
@ApiOperation("管理员登录")
@PostMapping("/admin/login")
public ApiRestResponse<User> adminLogin(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) throws GobMallException {
if (StringUtils.isEmpty(username)) {
return ApiRestResponse.error(GobMallExceptionEnum.NEED_USER_NAME);
}
if (StringUtils.isEmpty(password)) {
return ApiRestResponse.error(GobMallExceptionEnum.NEED_PASSWORD);
/**
* 购物车控制器
*/
@RestController
@RequestMapping("/cart")
public class CartController {
private final CartService cartService;
public CartController(CartService cartService) {
this.cartService = cartService;
}
@ApiOperation("购物车列表")
@GetMapping("/list")
public ApiRestResponse list() {
List<CartVO> list = cartService.list(UserFilter.currentUser.get().getId());
return ApiRestResponse.success(list);
}
@ApiOperation("购物车中选中商品")
@GetMapping("/list/selected")
public ApiRestResponse listOfSelected() {
List<CartVO> cartVOList = cartService.listOfSelected(UserFilter.currentUser.get().getId());
return ApiRestResponse.success(cartVOList);
}
@ApiOperation("添加商品到购物车")
@PostMapping("/add")
public ApiRestResponse add(@RequestParam Integer productId, @RequestParam Integer count) {
if (count < 0) {
throw new GobMallException(GobMallExceptionEnum.REQUEST_PARAM_ERROR);
}
List<CartVO> cartVOList = cartService.add(UserFilter.currentUser.get().getId(), productId, count);
return ApiRestResponse.success(cartVOList);
}
@ApiOperation("更新购物车信息")
@PostMapping("/update")
order.setPaymentType(1);
//插入到Order表
orderMapper.insertSelective(order);
//循环保存每个商品到order_item表
for (OrderItem orderItem : orderItemList) {
orderItem.setOrderNo(order.getOrderNo());
orderItemMapper.insertSelective(orderItem);
}
//返回结构
return orderCode;
}
@Override
public OrderVO detail(String orderNo) {
Order order = validOrder(orderNo);
return this.orderToOrderVO(order);
}
@Override
public PageInfo listForCustomer(Integer pageNum, Integer pageSize) {
Integer userId = UserFilter.currentUser.get().getId();
PageHelper.startPage(pageNum, pageSize);
List<Order> orderList = orderMapper.selectForCustomer(userId);
List<OrderVO> orderVOList = this.orderListToOrderVOList(orderList);
PageInfo pageInfo = new PageInfo<>(orderList);
pageInfo.setList(orderVOList);
return pageInfo;
}
@Override
public void cancel(String orderNo) {
Order order = validOrder(orderNo);
if (order.getOrderStatus().equals(Constant.OrderStatusEnum.NOT_PAID.getCode())) {
order.setOrderStatus(Constant.OrderStatusEnum.CANCELED.getCode());
order.setEndTime(new Date());
orderMapper.updateByPrimaryKeySelective(order);
} else {
throw new GobMallException(GobMallExceptionEnum.WRONG_ORDER_STATUS);
}
}
@Override
public String qrCode(String orderNo) {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
String address = ip + ":" + request.getLocalPort();
String payUrl = "http://" + address + "/pay?orderNo=" + orderNo;
try {
QRCodeGenerator.generateQRCodeImage(payUrl, 350, 350, Constant.FILE_UPLOAD_DIR + orderNo + ".png");
log.info("订单" + orderNo + "支付二维码文件:" + Constant.FILE_UPLOAD_DIR + orderNo + ".png is create");
}
return totalPrice;
}
/**
* Order转OrderVO
*/
private OrderVO orderToOrderVO(Order order) {
OrderVO orderVO = new OrderVO();
BeanUtils.copyProperties(order, orderVO);
List<OrderItemVO> orderItemVOList = new ArrayList<>();
List<OrderItem> orderItemList = orderItemMapper.selectListByOrderNO(order.getOrderNo());
for (OrderItem orderItem : orderItemList) {
OrderItemVO orderItemVO = new OrderItemVO();
System.out.println(orderItem);
BeanUtils.copyProperties(orderItem, orderItemVO);
orderItemVOList.add(orderItemVO);
}
orderVO.setOrderItemVOList(orderItemVOList);
orderVO.setOrderStatusName(Constant.OrderStatusEnum.codeOf(order.getOrderStatus()).getValue());
return orderVO;
}
/**
* OrderList转OrderVOList
*/
private List<OrderVO> orderListToOrderVOList(List<Order> orderList) {
List<OrderVO> orderVOList = new ArrayList<>();
for (Order order : orderList) {
OrderVO orderVO = this.orderToOrderVO(order);
orderVOList.add(orderVO);
}
return orderVOList;
}
/**
* 判断订单是否存在且属于当前用户
* @param orderNo 订单号
* @return 如果订单存在且属于当前用户返回订单信息,否则抛出异常
*/
private Order validOrder(String orderNo) {
Order order = orderMapper.selectByOrderNO(orderNo);
//判断订单是否存在
if (order == null) {
throw new GobMallException(GobMallExceptionEnum.ORDER_EXISTED);
}
//判断是否是该用户的订单
User currentUser = (User) session.getAttribute(Constant.GOB_MALL_USER);
if (currentUser == null) {
return ApiRestResponse.error(GobMallExceptionEnum.NEED_LOGIN);
}
User user = new User();
user.setId(currentUser.getId());
user.setPersonalizedSignature(signature);
userService.updateInformation(user);
return ApiRestResponse.success();
}
/**
* 退出登录
*/
@ApiOperation("退出登录")
@PostMapping("/user/logout")
public ApiRestResponse<Object> logout(HttpSession session) {
session.removeAttribute(Constant.GOB_MALL_USER);
return ApiRestResponse.success();
}
/**
* 管理员登录
*/
@ApiOperation("管理员登录")
@PostMapping("/admin/login")
public ApiRestResponse<User> adminLogin(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) throws GobMallException {
if (StringUtils.isEmpty(username)) {
return ApiRestResponse.error(GobMallExceptionEnum.NEED_USER_NAME);
}
if (StringUtils.isEmpty(password)) {
return ApiRestResponse.error(GobMallExceptionEnum.NEED_PASSWORD);
}
//保存用户信息时,不保存密码
User user = userService.login(username, password);
if (userService.checkAdminRole(user)) {
user.setPassword(null);
session.setAttribute(Constant.GOB_MALL_USER, user);
return ApiRestResponse.success(user);
}
return ApiRestResponse.error(GobMallExceptionEnum.NEED_ADMIN);
}
}
package cn.goodboyding.mall.controller;
}
@ApiOperation("商品详情")
@GetMapping("/detail")
public ApiRestResponse detail(@RequestParam Integer id) {
Product product = productService.detail(id);
return ApiRestResponse.success(product);
}
@ApiOperation("前台列表")
@GetMapping("/list")
public ApiRestResponse list(ProductListReq productListReq) {
PageInfo pageInfo = productService.list(productListReq);
return ApiRestResponse.success(pageInfo);
}
@ApiOperation("推荐")
@GetMapping("/recommend")
public ApiRestResponse recommend(Integer size) {
List<Product> recommend = productService.recommend(size);
return ApiRestResponse.success(recommend);
}
}
package cn.goodboyding.mall.filter;
/**
* 管理员校验过滤器 : 对需要管理员权限的接口进行过滤,过滤拦截没有权限的请求,并返回错误信息
*/
public class UserFilter implements Filter {
public static ThreadLocal<User> currentUser = new ThreadLocal<>();
@Autowired
private UserService userService;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
session.setAttribute(Constant.GOB_MALL_USER, user);
return ApiRestResponse.success(user);
}
return ApiRestResponse.error(GobMallExceptionEnum.NEED_ADMIN);
}
}
package cn.goodboyding.mall.controller;
/**
* 购物车控制器
*/
@RestController
@RequestMapping("/cart")
public class CartController {
private final CartService cartService;
public CartController(CartService cartService) {
this.cartService = cartService;
}
@ApiOperation("购物车列表")
@GetMapping("/list")
public ApiRestResponse list() {
List<CartVO> list = cartService.list(UserFilter.currentUser.get().getId());
return ApiRestResponse.success(list);
}
@ApiOperation("购物车中选中商品")
@GetMapping("/list/selected")
public ApiRestResponse listOfSelected() {
List<CartVO> cartVOList = cartService.listOfSelected(UserFilter.currentUser.get().getId());
return ApiRestResponse.success(cartVOList);
}
@ApiOperation("添加商品到购物车")
@PostMapping("/add")
public ApiRestResponse add(@RequestParam Integer productId, @RequestParam Integer count) {
if (count < 0) {
throw new GobMallException(GobMallExceptionEnum.REQUEST_PARAM_ERROR);
* 退出登录
*/
@ApiOperation("退出登录")
@PostMapping("/user/logout")
public ApiRestResponse<Object> logout(HttpSession session) {
session.removeAttribute(Constant.GOB_MALL_USER);
return ApiRestResponse.success();
}
/**
* 管理员登录
*/
@ApiOperation("管理员登录")
@PostMapping("/admin/login")
public ApiRestResponse<User> adminLogin(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) throws GobMallException {
if (StringUtils.isEmpty(username)) {
return ApiRestResponse.error(GobMallExceptionEnum.NEED_USER_NAME);
}
if (StringUtils.isEmpty(password)) {
return ApiRestResponse.error(GobMallExceptionEnum.NEED_PASSWORD);
}
//保存用户信息时,不保存密码
User user = userService.login(username, password);
if (userService.checkAdminRole(user)) {
user.setPassword(null);
session.setAttribute(Constant.GOB_MALL_USER, user);
return ApiRestResponse.success(user);
}
return ApiRestResponse.error(GobMallExceptionEnum.NEED_ADMIN);
}
}
package cn.goodboyding.mall.controller;
*/
@ApiOperation("登录")
@PostMapping("login")
public ApiRestResponse<User> login(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) throws GobMallException {
if (StringUtils.isEmpty(username)) {
return ApiRestResponse.error(GobMallExceptionEnum.NEED_USER_NAME);
}
if (StringUtils.isEmpty(password)) {
return ApiRestResponse.error(GobMallExceptionEnum.NEED_PASSWORD);
}
//保存用户信息时,不保存密码
User user = userService.login(username, password);
user.setPassword(null);
session.setAttribute(Constant.GOB_MALL_USER, user);
return ApiRestResponse.success(user);
}
/**
* 更新个性签名
*/
@ApiOperation("更新个性签名")
@PostMapping("/user/update")
public ApiRestResponse<Object> updateUserInfo(@RequestParam("signature") String signature, HttpSession session) throws GobMallException {
User currentUser = (User) session.getAttribute(Constant.GOB_MALL_USER);
if (currentUser == null) {
return ApiRestResponse.error(GobMallExceptionEnum.NEED_LOGIN);
}
User user = new User();
user.setId(currentUser.getId());
user.setPersonalizedSignature(signature);
userService.updateInformation(user);
return ApiRestResponse.success();
}
/**
* 退出登录
*/
@ApiOperation("退出登录")
@PostMapping("/user/logout")
public ApiRestResponse<Object> logout(HttpSession session) {
/**
* OrderList转OrderVOList
*/
private List<OrderVO> orderListToOrderVOList(List<Order> orderList) {
List<OrderVO> orderVOList = new ArrayList<>();
for (Order order : orderList) {
OrderVO orderVO = this.orderToOrderVO(order);
orderVOList.add(orderVO);
}
return orderVOList;
}
/**
* 判断订单是否存在且属于当前用户
* @param orderNo 订单号
* @return 如果订单存在且属于当前用户返回订单信息,否则抛出异常
*/
private Order validOrder(String orderNo) {
Order order = orderMapper.selectByOrderNO(orderNo);
//判断订单是否存在
if (order == null) {
throw new GobMallException(GobMallExceptionEnum.ORDER_EXISTED);
}
//判断是否是该用户的订单
Integer userId = UserFilter.currentUser.get().getId();
if (!order.getUserId().equals(userId)) {
throw new GobMallException(GobMallExceptionEnum.NOT_YOUR_ORDER);
}
return order;
}
/**
* 订单是否未空
* @param orderNo 订单号
* @return 不未空返回对应订单,否则抛出异常
*/
private Order isOrderNotNull(String orderNo) {
Order oldOrder = orderMapper.selectByOrderNO(orderNo);
if (oldOrder == null) {
throw new GobMallException(GobMallExceptionEnum.ORDER_EXISTED);
}
return oldOrder;
}
/**
* 创建订单
*/
//数据库事务,遇到任何异常都会回滚
@Transactional(rollbackFor = Exception.class)
@Override
/**
* 管理员校验过滤器 : 对需要管理员权限的接口进行过滤,过滤拦截没有权限的请求,并返回错误信息
*/
public class AdminFilter implements Filter {
@Autowired
private UserService userService;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
User currentUser = UserHandler.checkUserLogin(request, response);
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
if (currentUser != null) {
boolean adminRole = userService.checkAdminRole(currentUser);
if (adminRole) {
chain.doFilter(request,response);
} else {
PrintWriter out = new HttpServletResponseWrapper((HttpServletResponse) response).getWriter();
out.write("{\n" +
" \"status\": 10009,\n" +
"\"msg\": \"没有权限\",\n" +
"\"data\": null\n" +
"}");
out.flush();
out.close();
}
}
}
@Override
public void destroy() {
}
}
@Override
public void pay(String orderNo) {
Order oldOrder = this.isOrderNotNull(orderNo);
if (!oldOrder.getOrderStatus().equals(Constant.OrderStatusEnum.NOT_PAID.getCode())) {
throw new GobMallException(GobMallExceptionEnum.WRONG_ORDER_STATUS);
}
Order newOrder = new Order();
newOrder.setId(oldOrder.getId());
newOrder.setOrderStatus(Constant.OrderStatusEnum.PAID.getCode());
newOrder.setPayTime(new Date());
orderMapper.updateByPrimaryKeySelective(newOrder);
File file = new File(Constant.FILE_UPLOAD_DIR + orderNo + ".png");
if(true || file.delete()){
log.info("订单" + orderNo + "支付二维码文件:" + Constant.FILE_UPLOAD_DIR + orderNo + ".png is delete");
}else {
log.error("订单" + orderNo + "支付二维码文件:" + Constant.FILE_UPLOAD_DIR + orderNo + ".png not create");
}
}
@Override
public PageInfo listForAdmin(Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Order> orderList = orderMapper.selectAll();
List<OrderVO> orderVOList = this.orderListToOrderVOList(orderList);
PageInfo pageInfo = new PageInfo<>(orderList);
pageInfo.setList(orderVOList);
return pageInfo;
}
@Override
public void delivered(String orderNo) {
Order oldOrder = this.isOrderNotNull(orderNo);
if (!oldOrder.getOrderStatus().equals(Constant.OrderStatusEnum.PAID.getCode())) {
throw new GobMallException(GobMallExceptionEnum.WRONG_ORDER_STATUS);
}
Order newOrder = new Order();
newOrder.setId(oldOrder.getId());
newOrder.setOrderStatus(Constant.OrderStatusEnum.DELIVERED.getCode());
newOrder.setDeliveryTime(new Date());
orderMapper.updateByPrimaryKeySelective(newOrder);
}
@ApiOperation("选中商品或取消选中")
@PostMapping("/select")
public ApiRestResponse select(@RequestParam Integer productId, @RequestParam Integer selected) {
List<CartVO> cartVOList = cartService.selectOrNot(UserFilter.currentUser.get().getId(), productId, selected);
return ApiRestResponse.success(cartVOList);
}
@ApiOperation("商品全选或取消全选")
@PostMapping("selectAll")
public ApiRestResponse selectAll(@RequestParam Integer selected) {
List<CartVO> cartVOList = cartService.selectAll(UserFilter.currentUser.get().getId(), selected);
return ApiRestResponse.success(cartVOList);
}
}
package cn.goodboyding.mall.controller;
/**
* 描述:分类控制器
*/
@RestController
public class CategoryController {
private final CategoryService categoryService;
public CategoryController(UserService userService, CategoryService categoryService) {
this.categoryService = categoryService;
}
@ApiOperation("后台添加目录")
@PostMapping("/admin/category/add")
public ApiRestResponse<Object> addCategory(@RequestBody @Valid AddCategoryReq categoryReq) throws GobMallException {
categoryService.add(categoryReq);
return ApiRestResponse.success();
}
public class CategoryController {
private final CategoryService categoryService;
public CategoryController(UserService userService, CategoryService categoryService) {
this.categoryService = categoryService;
}
@ApiOperation("后台添加目录")
@PostMapping("/admin/category/add")
public ApiRestResponse<Object> addCategory(@RequestBody @Valid AddCategoryReq categoryReq) throws GobMallException {
categoryService.add(categoryReq);
return ApiRestResponse.success();
}
@ApiOperation("后台更新目录")
@PostMapping("/admin/category/update")
public ApiRestResponse<Object> updateCategory(@RequestBody @Valid UpdateCategoryReq updateCategoryReq) {
Category category = new Category();
BeanUtils.copyProperties(updateCategoryReq, category);
categoryService.update(category);
return ApiRestResponse.success();
}
@ApiOperation("后台删除目录")
@PostMapping("/admin/category/delete")
public ApiRestResponse<Object> deleteCategory(@RequestParam("id") Integer id) {
categoryService.delete(id);
return ApiRestResponse.success();
}
@ApiOperation("目录列表分页查询(管理员)")
@GetMapping("admin/category/list")
public ApiRestResponse<Object> listCategoryAdmin(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
PageInfo<Category> pageInfo = categoryService.listForAdmin(pageNum, pageSize);
return ApiRestResponse.success(pageInfo);
}
@ApiOperation("目录列表分页查询")
@GetMapping("category/list")
public ApiRestResponse<Object> listCategoryCustomer() {
List<CategoryVO> categoryVos = categoryService.listCategoryForCustomer(0);
return ApiRestResponse.success(categoryVos);
}
@RequestMapping("/product/file")
public ApiRestResponse upload(HttpServletRequest httpServletRequest, @RequestParam("file") MultipartFile file) {
String fileName = file.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
UUID uuid = UUID.randomUUID();
String newFileName = uuid.toString() + suffixName;
File fileDirectory = new File(Constant.FILE_UPLOAD_DIR);
File destFile = new File(Constant.FILE_UPLOAD_DIR + newFileName);
if (!fileDirectory.exists()) {
if (!fileDirectory.mkdir()) {
throw new GobMallException(GobMallExceptionEnum.MKDIR_FAILED);
}
}
try {
file.transferTo(destFile);
} catch (IOException e) {
e.printStackTrace();
}
try {
return ApiRestResponse.success(getHost(new URI(httpServletRequest.getRequestURL() + "")) + "/images/" + newFileName);
// return ApiRestResponse.success(newFileName);
} catch (Exception e) {
return ApiRestResponse.error(GobMallExceptionEnum.UPLOAD_FAILED);
}
}
private URI getHost(URI uri) {
URI effectiveURI;
try {
effectiveURI = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null);
} catch (URISyntaxException e) {
effectiveURI = null;
}
return effectiveURI;
}
@ApiOperation("后台更新商品")
@PostMapping("/product/update")
public ApiRestResponse updateProduct(@RequestBody @Valid UpdateProductReq updateProductReq) {
productService.update(updateProductReq);
return ApiRestResponse.success();
}
@ApiOperation("更新商品")
@PostMapping("/product/updateState")
/***
* 前台商品Controller
*/
@RestController
@RequestMapping("/product")
public class ProductController {
private final ProductService productService;
public ProductController(ProductService productService) {
this.productService = productService;
}
@ApiOperation("商品详情")
@GetMapping("/detail")
public ApiRestResponse detail(@RequestParam Integer id) {
Product product = productService.detail(id);
return ApiRestResponse.success(product);
}
@ApiOperation("前台列表")
@GetMapping("/list")
public ApiRestResponse list(ProductListReq productListReq) {
PageInfo pageInfo = productService.list(productListReq);
return ApiRestResponse.success(pageInfo);
}
@ApiOperation("推荐")
@GetMapping("/recommend")
public ApiRestResponse recommend(Integer size) {
List<Product> recommend = productService.recommend(size);
return ApiRestResponse.success(recommend);
}
}
package cn.goodboyding.mall.filter;
/**
* 描述:分类控制器
*/
@RestController
public class CategoryController {
private final CategoryService categoryService;
public CategoryController(UserService userService, CategoryService categoryService) {
this.categoryService = categoryService;
}
@ApiOperation("后台添加目录")
@PostMapping("/admin/category/add")
public ApiRestResponse<Object> addCategory(@RequestBody @Valid AddCategoryReq categoryReq) throws GobMallException {
categoryService.add(categoryReq);
return ApiRestResponse.success();
}
@ApiOperation("后台更新目录")
@PostMapping("/admin/category/update")
public ApiRestResponse<Object> updateCategory(@RequestBody @Valid UpdateCategoryReq updateCategoryReq) {
Category category = new Category();
BeanUtils.copyProperties(updateCategoryReq, category);
categoryService.update(category);
return ApiRestResponse.success();
}
@ApiOperation("后台删除目录")
@PostMapping("/admin/category/delete")
public ApiRestResponse<Object> deleteCategory(@RequestParam("id") Integer id) {
categoryService.delete(id);
return ApiRestResponse.success();
}
@ApiOperation("目录列表分页查询(管理员)")
@GetMapping("admin/category/list")
public ApiRestResponse<Object> listCategoryAdmin(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
PageInfo<Category> pageInfo = categoryService.listForAdmin(pageNum, pageSize);
return ApiRestResponse.success(pageInfo);
}
@ApiOperation("目录列表分页查询")
@GetMapping("category/list")
newOrder.setOrderStatus(Constant.OrderStatusEnum.DELIVERED.getCode());
newOrder.setDeliveryTime(new Date());
orderMapper.updateByPrimaryKeySelective(newOrder);
}
@Override
public void finish(String orderNo) {
Order oldOrder = this.isOrderNotNull(orderNo);
if (!oldOrder.getOrderStatus().equals(Constant.OrderStatusEnum.DELIVERED.getCode())) {
throw new GobMallException(GobMallExceptionEnum.WRONG_ORDER_STATUS);
}
Order newOrder = new Order();
newOrder.setId(oldOrder.getId());
newOrder.setOrderStatus(Constant.OrderStatusEnum.FINISHED.getCode());
newOrder.setEndTime(new Date());
orderMapper.updateByPrimaryKeySelective(newOrder);
}
}
package cn.goodboyding.mall.controller;
/**
* 后台商品管理Controller
*/
}
package cn.goodboyding.mall.controller;
/**
* 描述:分类控制器
*/
@RestController
public class CategoryController {
private final CategoryService categoryService;
public CategoryController(UserService userService, CategoryService categoryService) {
this.categoryService = categoryService;
}
@ApiOperation("后台添加目录")
@PostMapping("/admin/category/add")
public ApiRestResponse<Object> addCategory(@RequestBody @Valid AddCategoryReq categoryReq) throws GobMallException {
categoryService.add(categoryReq);
return ApiRestResponse.success();
}
@ApiOperation("后台更新目录")
return ApiRestResponse.success();
}
/**
* 管理员登录
*/
@ApiOperation("管理员登录")
@PostMapping("/admin/login")
public ApiRestResponse<User> adminLogin(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) throws GobMallException {
if (StringUtils.isEmpty(username)) {
return ApiRestResponse.error(GobMallExceptionEnum.NEED_USER_NAME);
}
if (StringUtils.isEmpty(password)) {
return ApiRestResponse.error(GobMallExceptionEnum.NEED_PASSWORD);
}
//保存用户信息时,不保存密码
User user = userService.login(username, password);
if (userService.checkAdminRole(user)) {
user.setPassword(null);
session.setAttribute(Constant.GOB_MALL_USER, user);
return ApiRestResponse.success(user);
}
return ApiRestResponse.error(GobMallExceptionEnum.NEED_ADMIN);
}
}
package cn.goodboyding.mall.controller;
/**
* 购物车控制器
*/
@RestController
@RequestMapping("/cart")
public class CartController {
private final CartService cartService;
@AfterReturning(returning = "res",pointcut = "webLog()")
public void doAfterReturning(Object res) throws JsonProcessingException {
//处理完请求返回内容
log.info("RESPONSE : " + new ObjectMapper().writeValueAsString(res));
}
}
package cn.goodboyding.mall.filter;
/**
* 管理员校验过滤器 : 对需要管理员权限的接口进行过滤,过滤拦截没有权限的请求,并返回错误信息
*/
public class AdminFilter implements Filter {
@Autowired
private UserService userService;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
User currentUser = UserHandler.checkUserLogin(request, response);
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
if (currentUser != null) {
boolean adminRole = userService.checkAdminRole(currentUser);
if (adminRole) {
chain.doFilter(request,response);
//循环保存每个商品到order_item表
for (OrderItem orderItem : orderItemList) {
orderItem.setOrderNo(order.getOrderNo());
orderItemMapper.insertSelective(orderItem);
}
//返回结构
return orderCode;
}
@Override
public OrderVO detail(String orderNo) {
Order order = validOrder(orderNo);
return this.orderToOrderVO(order);
}
@Override
public PageInfo listForCustomer(Integer pageNum, Integer pageSize) {
Integer userId = UserFilter.currentUser.get().getId();
PageHelper.startPage(pageNum, pageSize);
List<Order> orderList = orderMapper.selectForCustomer(userId);
List<OrderVO> orderVOList = this.orderListToOrderVOList(orderList);
PageInfo pageInfo = new PageInfo<>(orderList);
pageInfo.setList(orderVOList);
return pageInfo;
}
@Override
public void cancel(String orderNo) {
Order order = validOrder(orderNo);
if (order.getOrderStatus().equals(Constant.OrderStatusEnum.NOT_PAID.getCode())) {
order.setOrderStatus(Constant.OrderStatusEnum.CANCELED.getCode());
order.setEndTime(new Date());
orderMapper.updateByPrimaryKeySelective(order);
} else {
throw new GobMallException(GobMallExceptionEnum.WRONG_ORDER_STATUS);
}
}
@Override
public String qrCode(String orderNo) {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
return ApiRestResponse.error(GobMallExceptionEnum.NEED_PASSWORD);
}
//密码长度不能少于八位
if (password.length() < 8) {
return ApiRestResponse.error(GobMallExceptionEnum.PASSWORD_TOO_SHORT);
}
userService.register(username, password);
return ApiRestResponse.success();
}
/**
* 登录
*/
@ApiOperation("登录")
@PostMapping("login")
public ApiRestResponse<User> login(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) throws GobMallException {
if (StringUtils.isEmpty(username)) {
return ApiRestResponse.error(GobMallExceptionEnum.NEED_USER_NAME);
}
if (StringUtils.isEmpty(password)) {
return ApiRestResponse.error(GobMallExceptionEnum.NEED_PASSWORD);
}
//保存用户信息时,不保存密码
User user = userService.login(username, password);
user.setPassword(null);
session.setAttribute(Constant.GOB_MALL_USER, user);
return ApiRestResponse.success(user);
}
/**
* 更新个性签名
*/
@ApiOperation("更新个性签名")
@PostMapping("/user/update")
public ApiRestResponse<Object> updateUserInfo(@RequestParam("signature") String signature, HttpSession session) throws GobMallException {
User currentUser = (User) session.getAttribute(Constant.GOB_MALL_USER);
if (currentUser == null) {
return ApiRestResponse.error(GobMallExceptionEnum.NEED_LOGIN);
}
User user = new User();
user.setId(currentUser.getId());
user.setPersonalizedSignature(signature);
userService.updateInformation(user);
return ApiRestResponse.success();
}
/**
* 退出登录
/**
* 订单service实现类
*/
@Service
public class OrderServiceImpl implements OrderService {
private final Logger log = LoggerFactory.getLogger(OrderServiceImpl.class);
private final CartService cartService;
private final ProductMapper productMapper;
private final CartMapper cartMapper;
private final OrderMapper orderMapper;
private final OrderItemMapper orderItemMapper;
@Value("${file.upload.ip}")
private String ip;
public OrderServiceImpl(CartService cartService, ProductMapper productMapper, CartMapper cartMapper, OrderMapper orderMapper, OrderItemMapper orderItemMapper) {
this.cartService = cartService;
this.productMapper = productMapper;
this.cartMapper = cartMapper;
this.orderMapper = orderMapper;
this.orderItemMapper = orderItemMapper;
}
/**
* 判断商品状态是否可售且库存足够
*/
private void validSaleStatusAndStock(List<CartVO> cartVOList) {