diff --git a/src/main/java/com/cultural/heritage/constant/RegexConstant.java b/src/main/java/com/cultural/heritage/constant/RegexConstant.java new file mode 100644 index 0000000..e3641e5 --- /dev/null +++ b/src/main/java/com/cultural/heritage/constant/RegexConstant.java @@ -0,0 +1,32 @@ +package com.cultural.heritage.constant; + +/** + * 正则表达式 + */ +public interface RegexConstant { + /** + * 手机号正则 + */ + String PHONE_REGEX = "^1[3-9]\\d{9}$"; + + /** + * 邮箱正则 + */ + String EMAIL_REGEX = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"; + + /** + * 18位身份证号正则 + */ + String ID_CARD_REGEX = "^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9xX]$"; + + /** + * 验证码正则, 6位数字或字母 + */ + String VERIFY_CODE_REGEX = "^[a-zA-Z\\d]{6}$"; + + /** + * 密码正则。4~32位的字母、数字、下划线 + */ + String PASSWORD_REGEX = "^\\w{4,32}$"; + +} diff --git a/src/main/java/com/cultural/heritage/controller/address/AddressController.java b/src/main/java/com/cultural/heritage/controller/address/AddressController.java index 380c0b5..fbfc3a8 100644 --- a/src/main/java/com/cultural/heritage/controller/address/AddressController.java +++ b/src/main/java/com/cultural/heritage/controller/address/AddressController.java @@ -9,10 +9,13 @@ import com.cultural.heritage.model.dto.CommonRequest; import com.cultural.heritage.model.dto.address.AddressAddRequest; import com.cultural.heritage.model.dto.address.AddressUpdateRequest; import com.cultural.heritage.model.entity.Address; +import com.cultural.heritage.model.entity.User; import com.cultural.heritage.service.address.AddressService; +import com.cultural.heritage.service.user.UserService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; +import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.web.bind.annotation.PostMapping; @@ -33,6 +36,11 @@ public class AddressController { private AddressService addressService; + + @Resource + private UserService userService; + + /** * 添加地址信息 * @param addressAddRequest 地址添加请求体 @@ -40,12 +48,16 @@ public class AddressController { */ @PostMapping("/add") @Operation(summary = "小程序端用户添加地址信息", description = "参数:地址添加请求体,权限:所有人,方法名:addAddress") - public BaseResponse addAddress(@RequestBody AddressAddRequest addressAddRequest) { + public BaseResponse addAddress(@RequestBody AddressAddRequest addressAddRequest, HttpServletRequest request) { if (addressAddRequest == null) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } + User loginUser = userService.getLoginUser(request); + Long userId = loginUser.getId(); Address address = new Address(); BeanUtils.copyProperties(addressAddRequest, address); + address.setUserId(userId); + addressService.validAddress(address, false); addressService.verifyIsDefault(address); boolean result = addressService.save(address); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); @@ -61,10 +73,11 @@ public class AddressController { */ @PostMapping("/delete") @Operation(summary = "小程序端用户删除地址信息", description = "参数:地址删除请求体,权限:所有人,方法名:delAddress") - public BaseResponse delAddress(@RequestBody CommonRequest deleteRequest) { + public BaseResponse delAddress(@RequestBody CommonRequest deleteRequest, HttpServletRequest request) { if (deleteRequest == null || deleteRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } + userService.getLoginUser(request); Long id = deleteRequest.getId(); boolean result = addressService.removeById(id); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); @@ -81,12 +94,16 @@ public class AddressController { */ @PostMapping("/update") @Operation(summary = "小程序端用户更新地址信息", description = "参数:地址更新请求体,权限:所有人,方法名:updateAddress") - public BaseResponse updateAddress(@RequestBody AddressUpdateRequest addressUpdateRequest) { + public BaseResponse updateAddress(@RequestBody AddressUpdateRequest addressUpdateRequest, HttpServletRequest request) { if (addressUpdateRequest == null || addressUpdateRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } + User loginUser = userService.getLoginUser(request); + Long userId = loginUser.getId(); Address address = new Address(); BeanUtils.copyProperties(addressUpdateRequest, address); + address.setUserId(userId); + addressService.validAddress(address, true); addressService.verifyIsDefault(address); boolean result = addressService.updateById(address); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); @@ -98,17 +115,14 @@ public class AddressController { /** * 查询用户地址信息 - * @param addressQueryRequest 地址查询请求体 * @return 用户地址列表 */ @PostMapping("/list") - @Operation(summary = "小程序端查询用户地址信息", description = "参数:地址查询请求体,权限:所有人,方法名:listAddress") - public BaseResponse> listAddress(@RequestBody CommonRequest addressQueryRequest) { - if (addressQueryRequest == null || addressQueryRequest.getId() <= 0) { - throw new BusinessException(ErrorCode.PARAMS_ERROR); - } - Long id = addressQueryRequest.getId(); - List
list = addressService.getUserAddressById(id); + @Operation(summary = "小程序端查询用户地址信息", description = "参数:无,权限:所有人,方法名:listAddress") + public BaseResponse> listAddress(HttpServletRequest request) { + User loginUser = userService.getLoginUser(request); + Long userId = loginUser.getId(); + List
list = addressService.getUserAddressById(userId); return ResultUtils.success(list); } diff --git a/src/main/java/com/cultural/heritage/controller/address/ContactsController.java b/src/main/java/com/cultural/heritage/controller/address/ContactsController.java index e54651e..752445b 100644 --- a/src/main/java/com/cultural/heritage/controller/address/ContactsController.java +++ b/src/main/java/com/cultural/heritage/controller/address/ContactsController.java @@ -9,10 +9,13 @@ import com.cultural.heritage.model.dto.CommonRequest; import com.cultural.heritage.model.dto.contacts.ContactsAddRequest; import com.cultural.heritage.model.dto.contacts.ContactsUpdateRequest; import com.cultural.heritage.model.entity.Contacts; +import com.cultural.heritage.model.entity.User; import com.cultural.heritage.service.address.ContactsService; +import com.cultural.heritage.service.user.UserService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; +import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.web.bind.annotation.PostMapping; @@ -33,6 +36,10 @@ public class ContactsController { private ContactsService contactsService; + @Resource + private UserService userService; + + /** * 添加联系人 * @param contactsAddRequest 联系人添加请求体 @@ -40,12 +47,16 @@ public class ContactsController { */ @PostMapping("/add") @Operation(summary = "小程序端用户添加联系人信息", description = "参数:联系人添加请求体,权限:所有人,方法名:addContacts") - public BaseResponse addContacts(@RequestBody ContactsAddRequest contactsAddRequest) { + public BaseResponse addContacts(@RequestBody ContactsAddRequest contactsAddRequest, HttpServletRequest request) { if (contactsAddRequest == null) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } + User loginUser = userService.getLoginUser(request); + Long userId = loginUser.getId(); Contacts contacts = new Contacts(); BeanUtils.copyProperties(contactsAddRequest, contacts); + contacts.setUserId(userId); + contactsService.validContacts(contacts, false); contactsService.verifyIsDefault(contacts); boolean result = contactsService.save(contacts); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); @@ -61,12 +72,16 @@ public class ContactsController { */ @PostMapping("/update") @Operation(summary = "小程序端用户更新联系人信息", description = "参数:联系人更新请求体,权限:所有人,方法名:updateContacts") - public BaseResponse updateContacts(@RequestBody ContactsUpdateRequest contactsUpdateRequest) { + public BaseResponse updateContacts(@RequestBody ContactsUpdateRequest contactsUpdateRequest, HttpServletRequest request) { if (contactsUpdateRequest == null || contactsUpdateRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } + User loginUser = userService.getLoginUser(request); + Long userId = loginUser.getId(); Contacts contacts = new Contacts(); BeanUtils.copyProperties(contactsUpdateRequest, contacts); + contacts.setUserId(userId); + contactsService.validContacts(contacts, true); contactsService.verifyIsDefault(contacts); boolean result = contactsService.updateById(contacts); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); @@ -83,10 +98,11 @@ public class ContactsController { */ @PostMapping("/delete") @Operation(summary = "小程序端用户删除联系人信息", description = "参数:联系人删除请求体,权限:所有人,方法名:deleteContacts") - public BaseResponse deleteContacts(@RequestBody CommonRequest contactsDeleteRequest) { + public BaseResponse deleteContacts(@RequestBody CommonRequest contactsDeleteRequest, HttpServletRequest request) { if (contactsDeleteRequest == null || contactsDeleteRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } + userService.getLoginUser(request); Long id = contactsDeleteRequest.getId(); boolean result = contactsService.removeById(id); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); @@ -97,17 +113,14 @@ public class ContactsController { /** * 查询用户联系人信息 - * @param contactsQueryRequest 联系人查询请求体 * @return 用户联系人列表 */ @PostMapping("/list") - @Operation(summary = "小程序端用户查询联系人信息", description = "参数:联系人查询请求体,权限:所有人,方法名:listUserContacts") - public BaseResponse> listUserContacts(@RequestBody CommonRequest contactsQueryRequest) { - if (contactsQueryRequest == null || contactsQueryRequest.getId() <= 0) { - throw new BusinessException(ErrorCode.PARAMS_ERROR); - } - Long id = contactsQueryRequest.getId(); - List list = contactsService.getUserAddressById(id); + @Operation(summary = "小程序端用户查询联系人信息", description = "参数:无,权限:所有人,方法名:listUserContacts") + public BaseResponse> listUserContacts(HttpServletRequest request) { + User loginUser = userService.getLoginUser(request); + Long userId = loginUser.getId(); + List list = contactsService.getUserContactsById(userId); return ResultUtils.success(list); } diff --git a/src/main/java/com/cultural/heritage/controller/file/FileController.java b/src/main/java/com/cultural/heritage/controller/file/FileController.java index 3ca7f53..ce9c52a 100644 --- a/src/main/java/com/cultural/heritage/controller/file/FileController.java +++ b/src/main/java/com/cultural/heritage/controller/file/FileController.java @@ -48,7 +48,7 @@ public class FileController { * @return 图片可访问地址 */ @PostMapping("/upload/server/not_login") - @Operation(summary = "未登录情况下的文件上传", description = "参数:(文档上是false,但biz是必传的),文件对象(multipartFile), 业务类型(biz),权限:所有人,方法名:uploadServerFileNotLogin") + @Operation(summary = "(未使用)未登录情况下的文件上传", description = "参数:(文档上是false,但biz是必传的),文件对象(multipartFile), 业务类型(biz),权限:所有人,方法名:uploadServerFileNotLogin") private BaseResponse uploadServerFileNotLogin(@RequestPart("file")MultipartFile multipartFile, UploadFileRequest uploadFileRequest) { //获取业务名称 String biz = uploadFileRequest.getBiz(); diff --git a/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java b/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java index c743593..0e233b0 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java @@ -6,20 +6,22 @@ import com.cultural.heritage.common.ErrorCode; import com.cultural.heritage.common.ResultUtils; import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.ThrowUtils; -import com.cultural.heritage.model.dto.CommonRequest; import com.cultural.heritage.model.dto.cart.CartRecordAddRequest; import com.cultural.heritage.model.dto.cart.CartRecordUpdateRequest; import com.cultural.heritage.model.entity.CartRecord; import com.cultural.heritage.model.entity.Good; +import com.cultural.heritage.model.entity.User; import com.cultural.heritage.model.vo.cart.CartOrderVO; import com.cultural.heritage.model.vo.cart.CartRecordQueryVO; import com.cultural.heritage.model.vo.cart.CartRecordVO; import com.cultural.heritage.model.vo.good.GoodVO; import com.cultural.heritage.service.good.CartRecordService; import com.cultural.heritage.service.good.GoodService; +import com.cultural.heritage.service.user.UserService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; +import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.util.CollectionUtils; @@ -44,6 +46,10 @@ public class CartRecordController { @Resource private GoodService goodService; + + @Resource + private UserService userService; + /** * 用户添加商品至购物车 * @param cartRecordAddRequest 购物车记录添加请求体 @@ -51,11 +57,11 @@ public class CartRecordController { */ @PostMapping("/add") @Operation(summary = "小程序端用户添加商品至购物车", description = "参数:购物车记录添加请求体,权限:所有人,方法名:addCartRecord") - public BaseResponse addCartRecord(@RequestBody CartRecordAddRequest cartRecordAddRequest) { + public BaseResponse addCartRecord(@RequestBody CartRecordAddRequest cartRecordAddRequest, HttpServletRequest request) { if (cartRecordAddRequest == null) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } - boolean result = cartRecordService.dealAlreadyExistGood(cartRecordAddRequest); + boolean result = cartRecordService.dealAlreadyExistGood(cartRecordAddRequest, request); return ResultUtils.success(result); } @@ -67,13 +73,16 @@ public class CartRecordController { */ @PostMapping("/update") @Operation(summary = "小程序端用户更新商品至购物车", description = "参数:购物车记录更新请求体,权限:所有人,方法名:updateCartRecord") - public BaseResponse updateCartRecord(@RequestBody List cartRecordUpdateRequestList) { + public BaseResponse updateCartRecord(@RequestBody List cartRecordUpdateRequestList, HttpServletRequest request) { if (CollectionUtils.isEmpty(cartRecordUpdateRequestList)) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } + User loginUser = userService.getLoginUser(request); + Long userId = loginUser.getId(); List cartRecordList = cartRecordUpdateRequestList.stream().map(cartRecordUpdateRequest -> { CartRecord cartRecord = new CartRecord(); BeanUtils.copyProperties(cartRecordUpdateRequest, cartRecord); + cartRecord.setUserId(userId); return cartRecord; }).toList(); boolean result = cartRecordService.updateBatchById(cartRecordList); @@ -89,10 +98,11 @@ public class CartRecordController { */ @PostMapping("/delete") @Operation(summary = "小程序端用户删除购物车的物品", description = "参数:购物车记录删除请求体(商品id列表,如[2,3,4]),权限:所有人,方法名:deleteCart") - public BaseResponse deleteCart(@RequestBody List idList) { + public BaseResponse deleteCart(@RequestBody List idList, HttpServletRequest request) { if (CollectionUtils.isEmpty(idList)) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } + userService.getLoginUser(request); boolean result = cartRecordService.removeBatchByIds(idList); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); return ResultUtils.success(true); @@ -101,17 +111,14 @@ public class CartRecordController { /** * 展示用户购物车中的商品 - * @param userIdRequest 用户id请求体 * @return */ @PostMapping("/list") - @Operation(summary = "小程序端用户查询购物车中的商品", description = "参数:购物车记录查询请求体,权限:所有人,方法名:listUserCartRecord") - public BaseResponse> listUserCartRecord(@RequestBody CommonRequest userIdRequest) { - if (userIdRequest == null || userIdRequest.getId() <= 0) { - throw new BusinessException(ErrorCode.PARAMS_ERROR); - } + @Operation(summary = "小程序端用户查询购物车中的商品", description = "参数:无,权限:所有人,方法名:listUserCartRecord") + public BaseResponse> listUserCartRecord(HttpServletRequest request) { // 根据用户id查询购物车记录 - Long userId = userIdRequest.getId(); + User loginUser = userService.getLoginUser(request); + Long userId = loginUser.getId(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("userId", userId); List list = cartRecordService.list(queryWrapper); @@ -126,10 +133,11 @@ public class CartRecordController { * @return 订单商品信息列表 */ @PostMapping("/cart/list") - public BaseResponse> listCartRecord(@RequestBody List cartRecordQueryVOList) { + public BaseResponse> listCartRecord(@RequestBody List cartRecordQueryVOList, HttpServletRequest request) { if (cartRecordQueryVOList == null) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } + userService.getLoginUser(request); List cartOrderVOList = cartRecordQueryVOList.stream().map(cartRecordQueryVO -> { Long goodId = cartRecordQueryVO.getGoodId(); Integer quantity = cartRecordQueryVO.getQuantity(); diff --git a/src/main/java/com/cultural/heritage/controller/good/CategoryController.java b/src/main/java/com/cultural/heritage/controller/good/CategoryController.java index e2523f6..2c7caa8 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CategoryController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CategoryController.java @@ -1,6 +1,7 @@ package com.cultural.heritage.controller.good; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.cultural.heritage.annotation.AuthCheck; import com.cultural.heritage.common.BaseResponse; import com.cultural.heritage.common.ErrorCode; @@ -8,6 +9,7 @@ import com.cultural.heritage.common.ResultUtils; import com.cultural.heritage.constant.UserConstant; import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.ThrowUtils; +import com.cultural.heritage.model.dto.CommonDelBatchRequest; import com.cultural.heritage.model.dto.CommonRequest; import com.cultural.heritage.model.dto.category.CategoryAddRequest; import com.cultural.heritage.model.dto.category.CategoryUpdateRequest; @@ -15,12 +17,15 @@ import com.cultural.heritage.model.entity.Category; import com.cultural.heritage.model.entity.Good; import com.cultural.heritage.service.good.CategoryService; import com.cultural.heritage.service.good.GoodService; +import com.cultural.heritage.service.user.UserService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; +import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; +import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -41,11 +46,13 @@ public class CategoryController { private CategoryService categoryService; - @Resource private GoodService goodService; + @Resource + private UserService userService; + /** * 添加商品类别 @@ -80,13 +87,49 @@ public class CategoryController { if (deleteCategoryRequest == null || deleteCategoryRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } + // 删除当前类别下的所有商品 Long id = deleteCategoryRequest.getId(); + // 根据类别id获取商品删除的条件 + QueryWrapper queryWrapper = categoryService.getDeleteQueryWrapper(id); + List goodList = goodService.list(queryWrapper); + boolean isSuccess = goodService.removeBatchByIds(goodList); + ThrowUtils.throwIf(!isSuccess, ErrorCode.OPERATION_ERROR); + // 删除当前类别 boolean result = categoryService.removeById(id); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); return ResultUtils.success(true, "类别删除成功"); } + /** + * 批量删除商品类别 + * @param commonDelBatchRequest 类别删除请求体 + * @return 是否删除成功 + */ + @PostMapping("/delBath") + @Operation(summary = "Web端管理员批量删除商品类别", description = "参数:类别删除请求体,权限:管理员(admin, boss),方法名:deleteCategory") + @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) + public BaseResponse delBatchCategory(@RequestBody CommonDelBatchRequest commonDelBatchRequest) { + if (commonDelBatchRequest == null || CollectionUtils.isEmpty(commonDelBatchRequest.getIdList())) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + List idList = commonDelBatchRequest.getIdList(); + // 删除当前类别下的所有商品 + idList.forEach(id -> { + // 根据类别id获取商品删除的条件 + QueryWrapper queryWrapper = categoryService.getDeleteQueryWrapper(id); + List goodList = goodService.list(queryWrapper); + boolean isSuccess = goodService.removeBatchByIds(goodList); + ThrowUtils.throwIf(!isSuccess, ErrorCode.OPERATION_ERROR); + // 删除当前类别 + boolean result = categoryService.removeById(id); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); + }); + return ResultUtils.success(true, "批量删除类别成功"); + } + + + /** * 更新商品类别 @@ -115,7 +158,8 @@ public class CategoryController { */ @PostMapping("/list") @Operation(summary = "(小程序端和Web端)用户查询商品类别", description = "参数:无,权限:所有人,方法名:listCategory") - public BaseResponse> listCategory() { + public BaseResponse> listCategory(HttpServletRequest request) { + userService.getLoginUser(request); List list = categoryService.list(); return ResultUtils.success(list); } @@ -129,10 +173,11 @@ public class CategoryController { */ @PostMapping("/list/type") @Operation(summary = "小程序端用户根据类别id查询该类的所有商品", description = "参数:类别id,方法名:listGoodByCategory") - public BaseResponse>> listGoodByCategory(@RequestBody CommonRequest categoryQueryRequest) { + public BaseResponse>> listGoodByCategory(@RequestBody CommonRequest categoryQueryRequest, HttpServletRequest request) { if (categoryQueryRequest == null || categoryQueryRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } + userService.getLoginUser(request); Long id = categoryQueryRequest.getId(); Category category = categoryService.getCategoryById(id); String typeName = category.getTypeName(); @@ -149,4 +194,7 @@ public class CategoryController { + + + } diff --git a/src/main/java/com/cultural/heritage/controller/good/CouponController.java b/src/main/java/com/cultural/heritage/controller/good/CouponController.java index 5ed8ad3..88bc13b 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CouponController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CouponController.java @@ -25,6 +25,7 @@ import com.cultural.heritage.service.user.UserService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; +import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.transaction.annotation.Transactional; @@ -145,12 +146,13 @@ public class CouponController { @PostMapping("/exchange") @Transactional(rollbackFor = Exception.class) @Operation(summary = "小程序端用户积分兑换优惠券", description = "参数:兑换记录添加请求体,权限:管理员(admin, boss),方法名:pointsExchangeCoupon") - public BaseResponse pointsExchangeCoupon(@RequestBody ExchangeAddRequest exchangeAddRequest) { + public BaseResponse pointsExchangeCoupon(@RequestBody ExchangeAddRequest exchangeAddRequest, HttpServletRequest request) { if (exchangeAddRequest == null) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } // 更新用户积分 - Long userId = exchangeAddRequest.getUserId(); + User loginUser = userService.getLoginUser(request); + Long userId = loginUser.getId(); Integer requirePoints = exchangeAddRequest.getRequirePoints(); Integer quantity = exchangeAddRequest.getQuantity(); if (userId <= 0) { @@ -210,10 +212,11 @@ public class CouponController { */ @PostMapping("/delRecord") @Operation(summary = "小程序端用户删除兑换记录", description = "参数:兑换记录删除请求体,权限:管理员(admin, boss),方法名:deleteExchangeRecord") - public BaseResponse deleteExchangeRecord(@RequestBody CommonRequest commonRequest) { + public BaseResponse deleteExchangeRecord(@RequestBody CommonRequest commonRequest, HttpServletRequest request) { if (commonRequest == null || commonRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } + userService.getLoginUser(request); Long id = commonRequest.getId(); boolean result = exchangeService.removeById(id); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); diff --git a/src/main/java/com/cultural/heritage/controller/good/GoodController.java b/src/main/java/com/cultural/heritage/controller/good/GoodController.java index 4f753f5..63a53ba 100644 --- a/src/main/java/com/cultural/heritage/controller/good/GoodController.java +++ b/src/main/java/com/cultural/heritage/controller/good/GoodController.java @@ -25,9 +25,11 @@ import com.cultural.heritage.model.vo.good.ServiceGoodCardVO; import com.cultural.heritage.model.vo.good.ServiceGoodVO; import com.cultural.heritage.service.good.AppointmentDateService; import com.cultural.heritage.service.good.GoodService; +import com.cultural.heritage.service.user.UserService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; +import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.util.CollectionUtils; @@ -51,6 +53,9 @@ public class GoodController { private GoodService goodService; + @Resource + private UserService userService; + @Resource private AppointmentDateService appointmentDateService; @@ -140,6 +145,7 @@ public class GoodController { */ @PostMapping("/delete/service") @Operation(summary = "Web端管理员删除服务类商品", description = "参数: 商品删除请求体,权限:管理员(admin, boss),方法名:deleteServiceGood") + @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) public BaseResponse deleteServiceGood(@RequestBody CommonRequest deleteRequest) { if (deleteRequest == null || deleteRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); @@ -210,7 +216,7 @@ public class GoodController { @Operation(summary = "Web端管理员批量删除商品", description = "参数:商品批量删除请求体,权限:管理员(admin, boss),方法名:delBatchGoods") @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) public BaseResponse delBatchGoods(@RequestBody CommonDelBatchRequest commonDelBatchRequest) { - List idList = commonDelBatchRequest.getIdList(); + List idList = commonDelBatchRequest.getIdList(); boolean result = goodService.removeBatchByIds(idList); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); return ResultUtils.success(true); @@ -226,11 +232,12 @@ public class GoodController { * @return 商品信息 */ @PostMapping("/getById") - @Operation(summary = "根据id查询商品", description = "参数:商品根据id查询请求体,权限:管理员(admin, boss), 方法名:getGoodById") - public BaseResponse getGoodById(@RequestBody CommonRequest getByIdRequest) { + @Operation(summary = "根据id查询商品", description = "参数:商品根据id查询请求体,权限:所有人, 方法名:getGoodById") + public BaseResponse getGoodById(@RequestBody CommonRequest getByIdRequest, HttpServletRequest request) { if (getByIdRequest == null || getByIdRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } + userService.getLoginUser(request); Long id = getByIdRequest.getId(); Good good = goodService.getById(id); ThrowUtils.throwIf(good == null, ErrorCode.NOT_FOUND_ERROR); @@ -245,7 +252,7 @@ public class GoodController { * @return 服务类商品列表 */ @PostMapping("/service/list/page") - @Operation(summary = "Web端管理员分页查询服务类商品") + @Operation(summary = "Web端管理员分页查询服务类商品", description = "参数:商品查询请求体,权限:管理员(admin, boss),方法名:listServiceGoodVOByPage") @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) public BaseResponse> listServiceGoodVOByPage(@RequestBody GoodQueryRequest goodQueryRequest) { if (goodQueryRequest == null) { @@ -283,12 +290,12 @@ public class GoodController { * @return 服务类商品信息 */ @PostMapping("/service/list/id") - @Operation(summary = "小程序端用户根据id查询服务类商品") - @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) - public BaseResponse listServiceGoodVO(@RequestBody CommonRequest commonRequest) { + @Operation(summary = "小程序端用户根据id查询服务类商品", description = "参数:根据id查询请求体,权限:所有人,方法名:listServiceGoodVO") + public BaseResponse listServiceGoodVO(@RequestBody CommonRequest commonRequest, HttpServletRequest request) { if (commonRequest == null || commonRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } + userService.getLoginUser(request); Long id = commonRequest.getId(); Good good = goodService.getById(id); ThrowUtils.throwIf(good == null, ErrorCode.NOT_FOUND_ERROR, "商品不存在"); @@ -312,9 +319,9 @@ public class GoodController { * @return 服务类商品卡片列表 */ @GetMapping("/service/list/card") - @Operation(summary = "小程序端展示服务类商品卡片") - @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) - public BaseResponse> listServiceGoodCardVO() { + @Operation(summary = "小程序端展示服务类商品卡片", description = "参数:无,权限:所有人,方法名:listServiceGoodCardVO") + public BaseResponse> listServiceGoodCardVO(HttpServletRequest request) { + userService.getLoginUser(request); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("isGoodType", 0); List goodList = goodService.list(queryWrapper); @@ -333,7 +340,7 @@ public class GoodController { * @return 是否更新成功 */ @PostMapping("/service/update") - @Operation(summary = "Web端管理员更新服务类商品") + @Operation(summary = "Web端管理员更新服务类商品", description = "参数:服务类商品更新请求体,权限:管理员(admin, boss),方法名:updateServiceGoodById") @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) public BaseResponse updateServiceGoodById(@RequestBody ServiceGoodUpdateRequest serviceGoodUpdateRequest) { if (serviceGoodUpdateRequest == null || serviceGoodUpdateRequest.getId() <= 0) { diff --git a/src/main/java/com/cultural/heritage/controller/order/OrderController.java b/src/main/java/com/cultural/heritage/controller/order/OrderController.java index bf516b5..0bb3c25 100644 --- a/src/main/java/com/cultural/heritage/controller/order/OrderController.java +++ b/src/main/java/com/cultural/heritage/controller/order/OrderController.java @@ -10,7 +10,6 @@ import com.cultural.heritage.common.ResultUtils; import com.cultural.heritage.constant.UserConstant; import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.ThrowUtils; -import com.cultural.heritage.model.dto.CommonRequest; import com.cultural.heritage.model.dto.order.OrderAddRequest; import com.cultural.heritage.model.dto.order.OrderItemAddRequest; import com.cultural.heritage.model.dto.order.OrderQueryRequest; @@ -18,12 +17,15 @@ import com.cultural.heritage.model.dto.order.OrderUpdateRequest; import com.cultural.heritage.model.dto.order.capital.OrderMainInfoAddRequest; import com.cultural.heritage.model.entity.Order; import com.cultural.heritage.model.entity.OrderItems; +import com.cultural.heritage.model.entity.User; import com.cultural.heritage.model.vo.order.OrderVO; import com.cultural.heritage.service.order.OrderItemService; import com.cultural.heritage.service.order.OrderService; +import com.cultural.heritage.service.user.UserService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; +import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.transaction.annotation.Transactional; @@ -50,6 +52,11 @@ public class OrderController { private OrderItemService orderItemService; + + @Resource + private UserService userService; + + /** * 用户创建订单 * @param orderMainInfoAddRequest 订单创建请求体 @@ -57,11 +64,13 @@ public class OrderController { */ @PostMapping("/add") @Transactional(rollbackFor = Exception.class) - @Operation(summary = "小程序端创建用户订单", description = "参数:订单创建请求体,排序字段 sortField: totalAmount,权限:所有人,方法名:addOrder") - public BaseResponse addOrder(@RequestBody OrderMainInfoAddRequest orderMainInfoAddRequest) { + @Operation(summary = "小程序端创建用户订单", description = "参数:订单创建请求体,排序字段 sortField: totalAmount id createTime,权限:所有人,方法名:addOrder") + public BaseResponse addOrder(@RequestBody OrderMainInfoAddRequest orderMainInfoAddRequest, HttpServletRequest request) { if (orderMainInfoAddRequest == null) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } + // 校验用户当前是否处于登录态 + User loginUser = userService.getLoginUser(request); // 封装订单详细信息请求体 OrderAddRequest orderAddRequest = orderService.initOrderMainInfo(orderMainInfoAddRequest); if (orderAddRequest == null) { @@ -70,6 +79,9 @@ public class OrderController { // 创建一个总订单 Order order = new Order(); BeanUtils.copyProperties(orderAddRequest, order); + order.setUserId(loginUser.getId()); + // 校验订单 + orderService.validOrder(order); boolean result = orderService.save(order); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); // 创建订单明细 @@ -130,16 +142,13 @@ public class OrderController { /** * 小程序端用户查询订单 - * @param commonRequest 用户id请求体 * @return 用户订单列表 */ @PostMapping("/list") @Operation(summary = "小程序端用户查询订单", description = "参数:用户id请求体,权限:所有人,方法名:listUserOrder") - public BaseResponse> listUserOrder(@RequestBody CommonRequest commonRequest) { - if (commonRequest == null || commonRequest.getId() <= 0) { - throw new BusinessException(ErrorCode.PARAMS_ERROR); - } - Long id = commonRequest.getId(); + public BaseResponse> listUserOrder(HttpServletRequest request) { + User loginUser = userService.getLoginUser(request); + Long id = loginUser.getId(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("userId", id); List orders = orderService.list(queryWrapper); diff --git a/src/main/java/com/cultural/heritage/controller/user/UserController.java b/src/main/java/com/cultural/heritage/controller/user/UserController.java index 3703f4a..16a02db 100644 --- a/src/main/java/com/cultural/heritage/controller/user/UserController.java +++ b/src/main/java/com/cultural/heritage/controller/user/UserController.java @@ -18,6 +18,7 @@ import com.cultural.heritage.model.dto.user.*; import com.cultural.heritage.model.entity.User; import com.cultural.heritage.model.vo.user.UserVO; import com.cultural.heritage.service.user.UserService; +import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; @@ -59,6 +60,7 @@ public class UserController { * @return 登录用户信息 */ @PostMapping("/login") + @ApiOperationSupport(order = 1) @Operation(summary = "Web端用户登录", description = "参数:{账号,密码}, 权限:所有人), 方法名:userLogin") public BaseResponse userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request){ if(userLoginRequest == null) { @@ -83,6 +85,7 @@ public class UserController { * @return 用户登录信息 */ @GetMapping("/login/wx_open") + @ApiOperationSupport(order = 2) @Operation(summary = "(无法测)微信小程序用户登录", description = "参数:code(用户登录凭证), 权限:所有人, 方法名:userLoginByWxOpen") public BaseResponse userLoginByWxOpen(@RequestParam("code") String code, HttpServletRequest request) { WxMaJscode2SessionResult sessionInfo; diff --git a/src/main/java/com/cultural/heritage/controller/wx/WeChatController.java b/src/main/java/com/cultural/heritage/controller/wx/WeChatController.java index 9ab33c0..1818a8a 100644 --- a/src/main/java/com/cultural/heritage/controller/wx/WeChatController.java +++ b/src/main/java/com/cultural/heritage/controller/wx/WeChatController.java @@ -69,39 +69,6 @@ public class WeChatController { return sb.toString(); } -// l2Xqw8jS3iBL3__mZ33T8TFMj_w2tnERt6rLLUAfwRP5OWzAj5BzJyhsmuMzo3GJ - -// private String getOneContent(String token) throws IOException { -// String path = " https://api.weixin.qq.com/cgi-bin/freepublish/getarticle?access_token=" + token; -// URL url = new URL(path); -// HttpURLConnection connection = (HttpURLConnection) url.openConnection(); -// connection.setRequestMethod("POST"); -// connection.setDoOutput(true); -// connection.setRequestProperty("content-type", "application/json;charset=utf-8"); -// connection.connect(); -// // post发送的参数 -// Map map = new HashMap<>(); -// map.put("article_id", "l2Xqw8jS3iBL3__mZ33T8TFMj_w2tnERt6rLLUAfwRP5OWzAj5BzJyhsmuMzo3GJ"); // news表示图文类型的素材,具体看API文档 -// // 将map转换成json字符串 -// String paramBody = JSON.toJSONString(map); // 这里用了Alibaba的fastjson -// -// OutputStream out = connection.getOutputStream(); -// BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out)); -// bw.write(paramBody); // 向流中写入参数字符串 -// bw.flush(); -// -// InputStream in = connection.getInputStream(); -// byte[] b = new byte[100]; -// int len = -1; -// StringBuffer sb = new StringBuffer(); -// while((len = in.read(b)) != -1) { -// sb.append(new String(b,0,len)); -// } -// -// in.close(); -// return sb.toString(); -// } - private String getContentList(String token) throws IOException { @@ -138,14 +105,6 @@ public class WeChatController { } -// @Test -// public void test() throws IOException { -// String result1 = getToken(); -// Map token = (Map) JSON.parseObject(result1); -// String result2 = getContentList(token.get("access_token").toString()); -// System.out.println(result2); -// } - } diff --git a/src/main/java/com/cultural/heritage/model/dto/CommonDelBatchRequest.java b/src/main/java/com/cultural/heritage/model/dto/CommonDelBatchRequest.java index 1b6eec1..5c98cc1 100644 --- a/src/main/java/com/cultural/heritage/model/dto/CommonDelBatchRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/CommonDelBatchRequest.java @@ -13,7 +13,7 @@ public class CommonDelBatchRequest implements Serializable { @Schema(description = "批量删除的商品id列表(id > 0)", example = "[8, 9, 17]") - private List idList; + private List idList; @Serial diff --git a/src/main/java/com/cultural/heritage/model/dto/address/AddressAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/address/AddressAddRequest.java index 4a8633c..df9d278 100644 --- a/src/main/java/com/cultural/heritage/model/dto/address/AddressAddRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/address/AddressAddRequest.java @@ -39,12 +39,6 @@ public class AddressAddRequest implements Serializable { private String detailAddress; - /** - * 用户id - */ - @Schema(description = "用户id(id > 0)", example = "2") - private Long userId; - /** * 是否为默认地址 */ diff --git a/src/main/java/com/cultural/heritage/model/dto/address/AddressUpdateRequest.java b/src/main/java/com/cultural/heritage/model/dto/address/AddressUpdateRequest.java index 2f9f564..661051b 100644 --- a/src/main/java/com/cultural/heritage/model/dto/address/AddressUpdateRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/address/AddressUpdateRequest.java @@ -44,12 +44,6 @@ public class AddressUpdateRequest implements Serializable { private String detailAddress; - /** - * 用户id - */ - @Schema(description = "用户id(id > 0)", example = "2") - private Long userId; - /** * 是否为默认地址 */ diff --git a/src/main/java/com/cultural/heritage/model/dto/cart/CartRecordAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/cart/CartRecordAddRequest.java index 0fbc6dc..772d3f2 100644 --- a/src/main/java/com/cultural/heritage/model/dto/cart/CartRecordAddRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/cart/CartRecordAddRequest.java @@ -10,12 +10,6 @@ import java.io.Serializable; @Schema(description = "购物车记录添加请求体", requiredProperties = {"userId", "goodId", "quantity", "subtotal", "isGoodType"}) public class CartRecordAddRequest implements Serializable { - /** - * 用户id - */ - @Schema(description = "用户Id(id > 0)", example = "2") - private Long userId; - /** * 商品id */ diff --git a/src/main/java/com/cultural/heritage/model/dto/cart/CartRecordUpdateRequest.java b/src/main/java/com/cultural/heritage/model/dto/cart/CartRecordUpdateRequest.java index e5ebd60..ceec065 100644 --- a/src/main/java/com/cultural/heritage/model/dto/cart/CartRecordUpdateRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/cart/CartRecordUpdateRequest.java @@ -16,11 +16,6 @@ public class CartRecordUpdateRequest implements Serializable { @Schema(description = "购物车记录Id(id > 0)", example = "5") private Long id; - /** - * 用户id - */ - @Schema(description = "用户Id(id > 0)", example = "2") - private Long userId; /** * 商品id diff --git a/src/main/java/com/cultural/heritage/model/dto/contacts/ContactsAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/contacts/ContactsAddRequest.java index a986b56..5cb94d4 100644 --- a/src/main/java/com/cultural/heritage/model/dto/contacts/ContactsAddRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/contacts/ContactsAddRequest.java @@ -28,12 +28,6 @@ public class ContactsAddRequest implements Serializable { private String phone; - /** - * 用户id - */ - @Schema(description = "用户id(id > 0)", example = "2") - private Long userId; - /** * 是否默认 */ diff --git a/src/main/java/com/cultural/heritage/model/dto/contacts/ContactsUpdateRequest.java b/src/main/java/com/cultural/heritage/model/dto/contacts/ContactsUpdateRequest.java index ebeedeb..63572cf 100644 --- a/src/main/java/com/cultural/heritage/model/dto/contacts/ContactsUpdateRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/contacts/ContactsUpdateRequest.java @@ -34,12 +34,6 @@ public class ContactsUpdateRequest implements Serializable { private String phone; - /** - * 用户id - */ - @Schema(description = "用户id(id > 0)", example = "2") - private Long userId; - /** * 是否默认 */ diff --git a/src/main/java/com/cultural/heritage/model/dto/exchange/ExchangeAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/exchange/ExchangeAddRequest.java index f6aa1f0..fe50604 100644 --- a/src/main/java/com/cultural/heritage/model/dto/exchange/ExchangeAddRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/exchange/ExchangeAddRequest.java @@ -15,12 +15,6 @@ import java.io.Serializable; public class ExchangeAddRequest implements Serializable { - /** - * 用户id - */ - @Schema(description = "用户id(id > 0)", example = "2") - private Long userId; - /** * 优惠券id */ diff --git a/src/main/java/com/cultural/heritage/model/dto/order/OrderAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/order/OrderAddRequest.java index 45e047a..f15950f 100644 --- a/src/main/java/com/cultural/heritage/model/dto/order/OrderAddRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/order/OrderAddRequest.java @@ -22,12 +22,12 @@ public class OrderAddRequest implements Serializable { @Schema(description = "订单类别", example = "product") private String orderType; - - /** - * 用户id - */ - @Schema(description = "用户id(id > 0)", example = "2") - private Long userId; +// +// /** +// * 用户id +// */ +// @Schema(description = "用户id(id > 0)", example = "2") +// private Long userId; /** diff --git a/src/main/java/com/cultural/heritage/model/dto/order/capital/OrderMainInfoAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/order/capital/OrderMainInfoAddRequest.java index ba612d5..7462c0d 100644 --- a/src/main/java/com/cultural/heritage/model/dto/order/capital/OrderMainInfoAddRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/order/capital/OrderMainInfoAddRequest.java @@ -13,11 +13,11 @@ import java.util.List; public class OrderMainInfoAddRequest implements Serializable { - /** - * 用户id - */ - @Schema(description = "用户id(id > 0)", example = "2") - private Long userId; +// /** +// * 用户id +// */ +// @Schema(description = "用户id(id > 0)", example = "2") +// private Long userId; /** diff --git a/src/main/java/com/cultural/heritage/model/entity/Order.java b/src/main/java/com/cultural/heritage/model/entity/Order.java index d72ee77..9c64112 100644 --- a/src/main/java/com/cultural/heritage/model/entity/Order.java +++ b/src/main/java/com/cultural/heritage/model/entity/Order.java @@ -40,7 +40,7 @@ public class Order implements Serializable { /** * 订单编号 */ - private String OrderNumber; + private String orderNumber; /** diff --git a/src/main/java/com/cultural/heritage/service/address/AddressService.java b/src/main/java/com/cultural/heritage/service/address/AddressService.java index 3ed434d..3ac2444 100644 --- a/src/main/java/com/cultural/heritage/service/address/AddressService.java +++ b/src/main/java/com/cultural/heritage/service/address/AddressService.java @@ -18,4 +18,8 @@ public interface AddressService extends IService
{ List
getUserAddressById(Long id); + /** + * 校验用户提交的地址信息 + */ + void validAddress(Address address, boolean update); } diff --git a/src/main/java/com/cultural/heritage/service/address/ContactsService.java b/src/main/java/com/cultural/heritage/service/address/ContactsService.java index d3a49e4..d887b39 100644 --- a/src/main/java/com/cultural/heritage/service/address/ContactsService.java +++ b/src/main/java/com/cultural/heritage/service/address/ContactsService.java @@ -1,9 +1,7 @@ package com.cultural.heritage.service.address; import com.baomidou.mybatisplus.extension.service.IService; -import com.cultural.heritage.model.entity.Address; import com.cultural.heritage.model.entity.Contacts; -import io.swagger.v3.oas.models.info.Contact; import java.util.List; @@ -17,5 +15,11 @@ public interface ContactsService extends IService { /** * 根据userId获取用户联系人 */ - List getUserAddressById(Long id); + List getUserContactsById(Long id); + + + /** + * 校验用户提交的地址信息 + */ + void validContacts(Contacts contacts, boolean update); } diff --git a/src/main/java/com/cultural/heritage/service/address/impl/AddressServiceImpl.java b/src/main/java/com/cultural/heritage/service/address/impl/AddressServiceImpl.java index ce6f726..38111d8 100644 --- a/src/main/java/com/cultural/heritage/service/address/impl/AddressServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/address/impl/AddressServiceImpl.java @@ -3,10 +3,14 @@ package com.cultural.heritage.service.address.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.cultural.heritage.common.ErrorCode; +import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.ThrowUtils; import com.cultural.heritage.mapper.AddressMapper; import com.cultural.heritage.model.entity.Address; import com.cultural.heritage.service.address.AddressService; +import com.cultural.heritage.utils.RegexUtils; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import java.util.List; @@ -37,4 +41,33 @@ public class AddressServiceImpl extends ServiceImpl impl return list; } + + /** + * 校验用户提交的地址信息 + */ + @Override + public void validAddress(Address address, boolean update) { + Long userId = address.getUserId(); + String name = address.getName(); + String phone = address.getPhone(); + String region = address.getRegion(); + String detailAddress = address.getDetailAddress(); + Integer isDefault = address.getIsDefault(); + + if (update) { + if (userId == null) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数id错误"); + } + } + if (ObjectUtils.isEmpty(isDefault)) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + if (StringUtils.isAnyBlank(name, region, detailAddress, phone)) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不全"); + } + if (StringUtils.isNotBlank(phone) && RegexUtils.isPhoneInvalid(phone)) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "手机号格式无效"); + } + } + } diff --git a/src/main/java/com/cultural/heritage/service/address/impl/ContactsServiceImpl.java b/src/main/java/com/cultural/heritage/service/address/impl/ContactsServiceImpl.java index df90c72..ddf7a20 100644 --- a/src/main/java/com/cultural/heritage/service/address/impl/ContactsServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/address/impl/ContactsServiceImpl.java @@ -3,10 +3,14 @@ package com.cultural.heritage.service.address.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.cultural.heritage.common.ErrorCode; +import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.ThrowUtils; import com.cultural.heritage.mapper.ContactsMapper; import com.cultural.heritage.model.entity.Contacts; import com.cultural.heritage.service.address.ContactsService; +import com.cultural.heritage.utils.RegexUtils; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import java.util.List; @@ -19,7 +23,7 @@ public class ContactsServiceImpl extends ServiceImpl i public void verifyIsDefault(Contacts contacts) { Integer isDefault = contacts.getIsDefault(); Long userId = contacts.getUserId(); - List list = this.getUserAddressById(userId); + List list = this.getUserContactsById(userId); if (isDefault == 1) { list.forEach(item -> item.setIsDefault(0)); } @@ -28,10 +32,37 @@ public class ContactsServiceImpl extends ServiceImpl i } @Override - public List getUserAddressById(Long userId) { + public List getUserContactsById(Long userId) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("userId", userId); List list = this.list(queryWrapper); return list; } + + + /** + * 校验用户提交的地址信息 + */ + @Override + public void validContacts(Contacts contacts, boolean update) { + String name = contacts.getName(); + String phone = contacts.getPhone(); + Long userId = contacts.getUserId(); + Integer isDefault = contacts.getIsDefault(); + + if (update) { + if (userId == null) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数id错误"); + } + } + if (ObjectUtils.isEmpty(isDefault)) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + if (StringUtils.isAnyBlank(name, phone)) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + if (StringUtils.isNotBlank(phone) && RegexUtils.isPhoneInvalid(phone)) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "手机号格式错误"); + } + } } diff --git a/src/main/java/com/cultural/heritage/service/good/CartRecordService.java b/src/main/java/com/cultural/heritage/service/good/CartRecordService.java index 2cbb438..5a4e991 100644 --- a/src/main/java/com/cultural/heritage/service/good/CartRecordService.java +++ b/src/main/java/com/cultural/heritage/service/good/CartRecordService.java @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService; import com.cultural.heritage.model.dto.cart.CartRecordAddRequest; import com.cultural.heritage.model.entity.CartRecord; import com.cultural.heritage.model.vo.cart.CartRecordVO; +import jakarta.servlet.http.HttpServletRequest; import java.util.List; @@ -12,11 +13,17 @@ public interface CartRecordService extends IService { /** * 处理用户购物车已存在该商品 */ - boolean dealAlreadyExistGood(CartRecordAddRequest cartRecordAddRequest); + boolean dealAlreadyExistGood(CartRecordAddRequest cartRecordAddRequest, HttpServletRequest request); /** * 封装用户购物车中的商品信息 */ List transformToCartRecordVOList(List list); + + + /** + * 校验购物车中的商品信息 + */ + void validCart(CartRecord cartRecord, boolean update); } diff --git a/src/main/java/com/cultural/heritage/service/good/CategoryService.java b/src/main/java/com/cultural/heritage/service/good/CategoryService.java index 858f793..dcd7eca 100644 --- a/src/main/java/com/cultural/heritage/service/good/CategoryService.java +++ b/src/main/java/com/cultural/heritage/service/good/CategoryService.java @@ -1,7 +1,9 @@ package com.cultural.heritage.service.good; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.IService; import com.cultural.heritage.model.entity.Category; +import com.cultural.heritage.model.entity.Good; import java.util.List; @@ -24,4 +26,10 @@ public interface CategoryService extends IService { */ void validCategory(Category category, boolean add); + /** + * 根据类别id获取商品删除的条件 + */ + QueryWrapper getDeleteQueryWrapper(Long id); + + } diff --git a/src/main/java/com/cultural/heritage/service/good/impl/CartRecordServiceImpl.java b/src/main/java/com/cultural/heritage/service/good/impl/CartRecordServiceImpl.java index 7056b00..e133fab 100644 --- a/src/main/java/com/cultural/heritage/service/good/impl/CartRecordServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/good/impl/CartRecordServiceImpl.java @@ -9,10 +9,13 @@ import com.cultural.heritage.mapper.GoodMapper; import com.cultural.heritage.model.dto.cart.CartRecordAddRequest; import com.cultural.heritage.model.entity.CartRecord; import com.cultural.heritage.model.entity.Good; +import com.cultural.heritage.model.entity.User; import com.cultural.heritage.model.vo.cart.CartRecordVO; import com.cultural.heritage.model.vo.good.GoodVO; import com.cultural.heritage.service.good.CartRecordService; +import com.cultural.heritage.service.user.UserService; import jakarta.annotation.Resource; +import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; @@ -26,12 +29,16 @@ public class CartRecordServiceImpl extends ServiceImpl queryWrapper = new QueryWrapper<>(); queryWrapper.eq("userId", userId); @@ -45,6 +52,8 @@ public class CartRecordServiceImpl extends ServiceImpl i } } + + + /** + * 根据类别id获取商品删除的条件 + */ + @Override + public QueryWrapper getDeleteQueryWrapper(Long id) { + Category category = this.getById(id); + String typeName = category.getTypeName(); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("type", typeName); + return queryWrapper; + } } diff --git a/src/main/java/com/cultural/heritage/service/order/OrderService.java b/src/main/java/com/cultural/heritage/service/order/OrderService.java index 1fb6e06..329b80b 100644 --- a/src/main/java/com/cultural/heritage/service/order/OrderService.java +++ b/src/main/java/com/cultural/heritage/service/order/OrderService.java @@ -20,4 +20,10 @@ public interface OrderService extends IService { * 封装订单详细信息请求体 */ OrderAddRequest initOrderMainInfo(OrderMainInfoAddRequest orderMainInfoAddRequest); + + + /** + * 校验订单 + */ + void validOrder(Order order); } diff --git a/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java b/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java index 33bc50c..846366e 100644 --- a/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java @@ -91,7 +91,6 @@ public class OrderServiceImpl extends ServiceImpl implements */ @Override public OrderAddRequest initOrderMainInfo(OrderMainInfoAddRequest orderMainInfoAddRequest) { - Long userId = orderMainInfoAddRequest.getUserId(); String userName = orderMainInfoAddRequest.getUserName(); Long addressId = orderMainInfoAddRequest.getAddressId(); Long contactsId = orderMainInfoAddRequest.getContactsId(); @@ -143,7 +142,6 @@ public class OrderServiceImpl extends ServiceImpl implements // 创建订单详细信息请求体 OrderAddRequest orderAddRequest = new OrderAddRequest(); - orderAddRequest.setUserId(userId); orderAddRequest.setUserName(userName); orderAddRequest.setOrderNumber(orderNumber); orderAddRequest.setOrderStatus(orderStatus); @@ -158,6 +156,31 @@ public class OrderServiceImpl extends ServiceImpl implements } + + /** + * 校验订单 + */ + @Override + public void validOrder(Order order) { + String orderType = order.getOrderType(); + String orderNumber = order.getOrderNumber(); + Long userId = order.getUserId(); + String userName = order.getUserName(); + Double totalAmount = order.getTotalAmount(); + String orderStatus = order.getOrderStatus(); + if (ObjectUtils.isEmpty(totalAmount) || totalAmount.floatValue() <= 0) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "价格错误"); + } + if (ObjectUtils.isEmpty(userId)) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + if (StringUtils.isAnyBlank(orderType, orderNumber, userName, orderStatus)) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不全"); + } + } + + + /** * 根据id获取订单(地址,联系人,优惠券,商品)信息 */ diff --git a/src/main/java/com/cultural/heritage/utils/RegexUtils.java b/src/main/java/com/cultural/heritage/utils/RegexUtils.java new file mode 100644 index 0000000..266c4de --- /dev/null +++ b/src/main/java/com/cultural/heritage/utils/RegexUtils.java @@ -0,0 +1,57 @@ +package com.cultural.heritage.utils; + +import org.apache.commons.lang3.StringUtils; + +import static com.cultural.heritage.constant.RegexConstant.*; + + +public class RegexUtils { + + /** + * 是否是无效手机格式 + * + * @param phone 要校验的手机号 + * @return true:符合,false:不符合 + */ + public static boolean isPhoneInvalid(String phone) { + return mismatch(phone, PHONE_REGEX); + } + + /** + * 是否是无效邮箱格式 + * + * @param email 要校验的邮箱 + * @return true:符合,false:不符合 + */ + public static boolean isEmailInvalid(String email) { + return mismatch(email, EMAIL_REGEX); + } + + /** + * 是否是无效18位身份证格式 + * + * @param idCard 要校验的身份证号码 + * @return true:符合,false:不符合 + */ + public static boolean isIdCardInvalid(String idCard) { + return mismatch(idCard, ID_CARD_REGEX); + } + + /** + * 是否是无效验证码格式 + * + * @param code 要校验的验证码 + * @return true:符合,false:不符合 + */ + public static boolean isCodeInvalid(String code) { + return mismatch(code, VERIFY_CODE_REGEX); + } + + // 校验是否不符合正则格式 + private static boolean mismatch(String str, String regex) { + if (StringUtils.isBlank(str)) { + return true; + } + return !str.matches(regex); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 7be0d24..3d5fa94 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -56,4 +56,8 @@ wx: appSecret: 847bdda7c2b01e88d59948b9ba50ef8d official: appId: wx5d04ca2de0e628a8 - appSecret: 495af5bc4df1b86ffcfc21bb12daea76 \ No newline at end of file + appSecret: 495af5bc4df1b86ffcfc21bb12daea76 + + +knife4j: + enable: true \ No newline at end of file diff --git a/src/test/java/com/cultural/heritage/test/Test.java b/src/test/java/com/cultural/heritage/test/Test.java index 540cf1a..35571fb 100644 --- a/src/test/java/com/cultural/heritage/test/Test.java +++ b/src/test/java/com/cultural/heritage/test/Test.java @@ -1,5 +1,7 @@ package com.cultural.heritage.test; +import org.apache.commons.lang3.ObjectUtils; + public class Test { public static void main(String[] args) { // Date date = new Date(); @@ -15,8 +17,11 @@ public class Test { // System.out.println(format); // System.out.println(StringUtils.isBlank(null)); +// +// double num = 3.1415926; +// System.out.printf("%.2f", num); // 输出:3.14 - double num = 3.1415926; - System.out.printf("%.2f", num); // 输出:3.14 +// System.out.println(StringUtils.isAnyBlank("333", "333", null, "dfs")); + System.out.println(ObjectUtils.anyNull(null)); } }