From efdb99f8cecc4afb592afad79c761081d5d5cf22 Mon Sep 17 00:00:00 2001 From: lee <4766465@qq.com> Date: Wed, 18 Dec 2024 13:27:00 +0800 Subject: [PATCH] init --- yami-shop-api/src/main/java/com/yami/shop/api/controller/cdn/CdnRealNameController.java | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 163 insertions(+), 0 deletions(-) diff --git a/yami-shop-api/src/main/java/com/yami/shop/api/controller/cdn/CdnRealNameController.java b/yami-shop-api/src/main/java/com/yami/shop/api/controller/cdn/CdnRealNameController.java new file mode 100644 index 0000000..3ce5823 --- /dev/null +++ b/yami-shop-api/src/main/java/com/yami/shop/api/controller/cdn/CdnRealNameController.java @@ -0,0 +1,163 @@ +package com.yami.shop.api.controller.cdn; + +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.yami.shop.bean.model.CdnRealName; +import com.yami.shop.bean.model.CdnUserBank; +import com.yami.shop.bean.model.User; +import com.yami.shop.bean.param.CdnRealNameParam; +import com.yami.shop.common.constants.DictConstant; +import com.yami.shop.common.response.ServerResponseEntity; +import com.yami.shop.common.util.IdCardCheck; +import com.yami.shop.security.api.util.SecurityUtils; +import com.yami.shop.service.CdnRealNameService; +import com.yami.shop.service.CdnUserBankService; +import com.yami.shop.service.UserService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.Parameters; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import javax.validation.Valid; +import javax.xml.crypto.Data; +import java.text.ParseException; +import java.util.Date; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +@RestController +@RequestMapping("/p/cdn/realName") +@Tag(name = "Cdn实名认证相关接口") +public class CdnRealNameController { + @Autowired + private CdnRealNameService cdnRealNameService; + @Autowired + private CdnUserBankService cdnUserBankService; + @Resource + private UserService userService; + private static final String PHONE_NUMBER_PATTERN = "^1[3-9]\\d{9}$"; + + @GetMapping + @Operation(summary = "实名认证展示", description = "实名认证展示") + @Parameters({ + @Parameter(name = "userId",description = "用户id") + }) + public ServerResponseEntity<CdnRealName> getRealNameInfo(@RequestParam(name = "userId") String userId){ + CdnRealName cdnRealName = cdnRealNameService.getOne(Wrappers.lambdaQuery(CdnRealName.class).eq(CdnRealName::getUserId, userId)); + return ServerResponseEntity.success(cdnRealName); + } + + @PostMapping + @Operation(summary = "实名认证保存/修改", description = "实名认证保存/修改") + public ServerResponseEntity modifyRealName(@Valid @RequestBody CdnRealNameParam cdnRealNameParam) throws ParseException { + String userId = SecurityUtils.getUser().getUserId(); + IdCardCheck idCardCheck = new IdCardCheck(); + Boolean validateIdCard = idCardCheck.checkIdCardNum(cdnRealNameParam.getCard()); + if (cdnRealNameParam.getCard() == null || !validateIdCard){ + return ServerResponseEntity.showFailMsg("身份证号格式错误"); + } + if (cdnRealNameParam.getBankCode() == null || !checkBankCard(cdnRealNameParam.getBankCode())){ + return ServerResponseEntity.showFailMsg("银行卡号格式错误"); + } + CdnRealName cdnRealName = cdnRealNameService.getOne(Wrappers.lambdaQuery(CdnRealName.class).eq(CdnRealName::getUserId, cdnRealNameParam.getUserId())); + if (cdnRealName != null){ + cdnRealName.setName(cdnRealNameParam.getName()); + cdnRealName.setCard(cdnRealNameParam.getCard()); + cdnRealName.setBankAddr(cdnRealNameParam.getBankAddr()); + cdnRealName.setBankCode(cdnRealNameParam.getBankCode()); + cdnRealName.setUpdateTime(new Date()); + updateUserTranLeve(userId, cdnRealName); + cdnRealNameService.updateById(cdnRealName); + }else { + cdnRealName = new CdnRealName(); + cdnRealName.setName(cdnRealNameParam.getName()); + cdnRealName.setUserId(cdnRealNameParam.getUserId()); + cdnRealName.setCard(cdnRealNameParam.getCard()); + cdnRealName.setBankAddr(cdnRealNameParam.getBankAddr()); + cdnRealName.setBankCode(cdnRealNameParam.getBankCode()); + cdnRealName.setCreateTime(new Date()); + updateUserTranLeve(userId, cdnRealName); + cdnRealNameService.save(cdnRealName); + } + return ServerResponseEntity.success(); + } + + private void updateUserTranLeve(String userId, CdnRealName cdnRealName) { + if(!StringUtils.isEmpty(cdnRealName.getCardZ()) && !StringUtils.isEmpty(cdnRealName.getCardF())){ + User byId = userService.getById(userId); + if(!StringUtils.isEmpty(cdnRealName.getCardVideo())){ + byId.setTranLeve(DictConstant.TRAN_LEVEL_2); + }else{ + byId.setTranLeve(DictConstant.TRAN_LEVEL_1); + } + userService.updateById(byId); + } + } + + @PostMapping("/addBank") + @Operation(summary = "添加银行卡", description = "添加银行卡") + public ServerResponseEntity addBank(@RequestBody @Valid CdnUserBank cdnUserBank){ + if (!checkBankCard(cdnUserBank.getBankcode())){ + return ServerResponseEntity.showFailMsg("银行卡号格式错误"); + } + if (!isValidPhoneNumber(cdnUserBank.getPhone())){ + return ServerResponseEntity.showFailMsg("手机号格式错误 "); + } + boolean save = cdnUserBankService.save(cdnUserBank); + if (!save){ + return ServerResponseEntity.showFailMsg("添加失败"); + } + return ServerResponseEntity.success(); + } + + /** + * 校验银行卡号方法 + * @param bankCard + * @return + */ + public boolean checkBankCard(String bankCard) { + if(bankCard.length() < 15 || bankCard.length() > 19) { + return false; + } +// char bit = getBankCardCheckCode(bankCard.substring(0, bankCard.length() - 1)); +// if(bit == 'N'){ +// return false; +// } +// return bankCard.charAt(bankCard.length() - 1) == bit; + return true; + } + + + /** + * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位 + * @param nonCheckCodeBankCard + * @return + */ + public char getBankCardCheckCode(String nonCheckCodeBankCard){ + if(nonCheckCodeBankCard == null || nonCheckCodeBankCard.trim().length() == 0 + || !nonCheckCodeBankCard.matches("\\d+")) { + //如果传的不是数据返回N + return 'N'; + } + char[] chs = nonCheckCodeBankCard.trim().toCharArray(); + int luhmSum = 0; + for(int i = chs.length - 1, j = 0; i >= 0; i--, j++) { + int k = chs[i] - '0'; + if(j % 2 == 0) { + k *= 2; + k = k / 10 + k % 10; + } + luhmSum += k; + } + return (luhmSum % 10 == 0) ? '0' : (char)((10 - luhmSum % 10) + '0'); + } + + public static boolean isValidPhoneNumber(String phoneNumber) { + Pattern pattern = Pattern.compile(PHONE_NUMBER_PATTERN); + Matcher matcher = pattern.matcher(phoneNumber); + return matcher.matches(); + } +} -- Gitblit v1.9.3