package com.mzl.flower.web.supplier; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.mzl.flower.base.BaseController; import com.mzl.flower.base.R; import com.mzl.flower.base.ReturnDataDTO; import com.mzl.flower.config.exception.ValidationException; import com.mzl.flower.config.security.SecurityUtils; import com.mzl.flower.dto.request.supplier.*; import com.mzl.flower.dto.response.supplier.SupplierDTO; import com.mzl.flower.entity.customer.Customer; import com.mzl.flower.service.oss.TosOssService; import com.mzl.flower.service.customer.CustomerService; import com.mzl.flower.service.supplier.SupplierService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.validation.constraints.NotNull; import static sun.font.CreatedFontTracker.MAX_FILE_SIZE; @RestController @RequestMapping("/api/supplier") @Api(value = "供应商管理", tags = "供应商管理") @Validated @Slf4j public class SupplierController extends BaseController { // private static final R ALLOWED_CONTENT_TYPES = R.valueOf("jpg"); private final SupplierService supplierService; @Autowired private CustomerService customerService; @Autowired private TosOssService tosOssService; public SupplierController(SupplierService supplierService) { this.supplierService = supplierService; } @PostMapping("/addOrUpdate") @ApiOperation(value = "供应商信息登记、修改", notes = "供应商信息登记、修改") public ResponseEntity addOrUpdateSupplier(@Validated @RequestBody UpdateSupplierDTO dto) { supplierService.addOrUpdateSupplier(dto); return returnData(R.SUCCESS.getCode(),null); } @PostMapping("/update/info") @ApiOperation(value = "供应商修改头像、昵称、店铺简介", notes = "供应商修改头像、昵称、店铺简介,传id、name、cover、description字段") public ResponseEntity updateSupplier(@Validated @RequestBody UpdateSupplierDTO dto) { supplierService.updateSupplier(dto); return returnData(R.SUCCESS.getCode(),null); } @PostMapping("/operation/update") @ApiOperation(value = "运营修改", notes = "运营修改") public ResponseEntity operationUpdate( @RequestParam("nickname") String nickname, @RequestParam(value = "avatar", required = false) MultipartFile avatar ) { try { String avatarUrl = null; if (avatar != null && !avatar.isEmpty()) { // 1. 检查文件大小 if (avatar.getSize() > MAX_FILE_SIZE) { throw new ValidationException("文件大小不能超过5MB"); } // 2. 检查文件类型 // String contentType = avatar.getContentType(); // if (!ALLOWED_CONTENT_TYPES.contains(contentType)) { // throw new ValidationException("不支持的文件类型"); // } // 3. 生成文件名 String originalFilename = avatar.getOriginalFilename(); // 原始文件名可能没有后缀 String contentType = avatar.getContentType(); // 例如 image/jpeg String extension = getExtensionFromContentType(contentType); // 我们下面实现这个方法 String newFileName = originalFilename + (extension != null ? extension : ""); // 4. 上传到 OSS avatarUrl = tosOssService.uploadFile( avatar.getInputStream(),newFileName); } // 5. 更新用户信息 // supplierService.operationUpdate(nickname, avatarUrl); String userId = SecurityUtils.getUserId(); Customer customer=new Customer(); customer.setUserId(userId); customer.setCover(avatarUrl); customer.setName(nickname); customerService.updateMemberInfoByUserId(customer); return returnData(R.SUCCESS.getCode(),null); } catch (Exception e) { log.error("更新用户信息失败", e); return returnData(R.RUNTIME_EXCEPTION.getCode(),null); } } private String getExtensionFromContentType(String contentType) { if (contentType == null) return null; switch (contentType) { case "image/jpeg": return ".jpg"; case "image/png": return ".png"; case "image/gif": return ".gif"; case "image/webp": return ".webp"; case "image/bmp": return ".bmp"; default: return ".png"; } } @GetMapping("/page") @ApiOperation(value = "运营端-供应商分页查询", notes = "运营端-供应商分页查询") public ResponseEntity>> querySupplier(QuerySupplierDTO dto, Page page) { // if(!Constants.USER_TYPE.admin.name().equals(SecurityUtils.getUserType())){ // throw new ValidationException("无权限访问"); // } return returnData(R.SUCCESS.getCode(),supplierService.querySupplier(dto,page)); } @PostMapping("/page/update") @ApiOperation(value = "运营修改", notes = "运营修改") public ResponseEntity operationUpdate(@Validated @RequestBody UpdateSupplierOpDTO dto) { supplierService.operationUpdate(dto); return returnData(R.SUCCESS.getCode(),null); } @GetMapping("/detail/{id}") @ApiOperation(value = "供应商详情", notes = "供应商详情") public ResponseEntity> findSupplierDetail(@PathVariable("id") Long id) { return returnData(R.SUCCESS.getCode(),supplierService.findSupplierDetail(id)); } @GetMapping("/info/{phone}") @ApiOperation(value = "用户详情", notes = "用户详情") public ResponseEntity> findSupplierByPhone(@PathVariable("phone") String phone) { return returnData(R.SUCCESS.getCode(),supplierService.findSupplierByPhone(phone)); } @PostMapping("/audit") @ApiOperation(value = "运营端-供应商审核", notes = "运营端-供应商审核") public ResponseEntity auditSupplier(@Validated @RequestBody AuditSupplierDTO dto) { // if(!Constants.USER_TYPE.admin.name().equals(SecurityUtils.getUserType())){ // throw new ValidationException("无权限访问"); // } supplierService.auditSupplier(dto); return returnData(R.SUCCESS.getCode(),null); } @PostMapping("/change/station") @ApiOperation(value = "运营端-修改集货站(传参数:id,stationId)", notes = "运营端-修改集货站") public ResponseEntity changeStation(@Validated @RequestBody ChangeStationDTO dto) { // if(!Constants.USER_TYPE.admin.name().equals(SecurityUtils.getUserType())){ // throw new ValidationException("无权限访问"); // } if(dto.getId()==null || dto.getStationId()==null){ throw new ValidationException("参数不能为空"); } supplierService.changeStation(dto); return returnData(R.SUCCESS.getCode(),null); } @GetMapping("/page/showed") @ApiOperation(value = "显示/隐藏商品", notes = "显示/隐藏商品") public ResponseEntity> configShow(@NotNull(message = "id不能为空") Long id) { supplierService.configShow(id); return returnData(R.SUCCESS.getCode(),null); } @GetMapping("/page/isEnable") @ApiOperation(value = "启用/禁用", notes = "启用/禁用商品") public ResponseEntity> isEnable(@NotNull(message = "id不能为空") Long id) { supplierService.isEnable(id); return returnData(R.SUCCESS.getCode(),null); } }