基于mall4j产品的二开项目后端
lee
2024-12-18 921461a3f906d74403aeb6a27051deb77eca10fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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();
    }
}