| | |
| | | import com.mzl.flower.constant.Constants; |
| | | 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.TosService; |
| | | 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.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.validation.annotation.Validated; |
| | | import org.springframework.web.bind.annotation.*; |
| | |
| | | @Slf4j |
| | | public class SupplierController extends BaseController { |
| | | |
| | | private static final R ALLOWED_CONTENT_TYPES = R.valueOf("jpg"); |
| | | // private static final R ALLOWED_CONTENT_TYPES = R.valueOf("jpg"); |
| | | private final SupplierService supplierService; |
| | | |
| | | @Autowired |
| | | private CustomerService customerService; |
| | | |
| | | @Autowired |
| | | private TosService tosService; |
| | | |
| | | public SupplierController(SupplierService supplierService) { |
| | | this.supplierService = supplierService; |
| | |
| | | } |
| | | |
| | | // 2. 检查文件类型 |
| | | String contentType = avatar.getContentType(); |
| | | if (!ALLOWED_CONTENT_TYPES.contains(contentType)) { |
| | | throw new ValidationException("不支持的文件类型"); |
| | | } |
| | | // String contentType = avatar.getContentType(); |
| | | // if (!ALLOWED_CONTENT_TYPES.contains(contentType)) { |
| | | // throw new ValidationException("不支持的文件类型"); |
| | | // } |
| | | |
| | | // 3. 生成文件名 |
| | | String fileName = "avatar/" + UUID.randomUUID().toString() + |
| | | getFileExtension(avatar.getOriginalFilename()); |
| | | String originalFilename = avatar.getOriginalFilename(); // 原始文件名可能没有后缀 |
| | | String contentType = avatar.getContentType(); // 例如 image/jpeg |
| | | String extension = getExtensionFromContentType(contentType); // 我们下面实现这个方法 |
| | | |
| | | String newFileName = originalFilename + (extension != null ? extension : ""); |
| | | |
| | | // 4. 上传到 OSS |
| | | //avatarUrl = ossService.uploadFile(fileName, avatar.getInputStream()); |
| | | avatarUrl = tosService.uploadFile( avatar.getInputStream(),newFileName); |
| | | } |
| | | |
| | | // 5. 更新用户信息 |
| | | //supplierService.operationUpdate(nickname, avatarUrl); |
| | | // 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) { |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | 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<ReturnDataDTO<Page<SupplierDTO>>> querySupplier(QuerySupplierDTO dto, Page page) { |