陶杰
2024-08-22 ee9032d9baf5f33e376d2d2699136e0a7b26bec7
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
package com.mzl.flower.web.register;
 
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.mzl.flower.base.BaseController;
import com.mzl.flower.base.R;
import com.mzl.flower.base.ReturnDataDTO;
import com.mzl.flower.base.cache.StringCacheClient;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.dto.request.register.RegisterCustomerDTO;
import com.mzl.flower.dto.request.register.RegisterDTO;
import com.mzl.flower.dto.request.register.RegisterPartnerDTO;
import com.mzl.flower.entity.system.User;
import com.mzl.flower.service.register.RegisterService;
import com.mzl.flower.service.system.UserService;
import com.mzl.flower.service.system.WeChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("/api/register")
@Api(value = "注册管理", tags = "注册管理")
@Validated
@Slf4j
public class RegisterController extends BaseController {
 
    public static final String SMS_CODE_KEY = "SMS-CODE-KEY";
 
    public static final String SEPARATOR = ":";
 
    private final RegisterService registerService;
 
    private final StringCacheClient stringCacheClient;
 
    private final WeChatService weChatService;
 
    private final UserService userService;
 
    public RegisterController(RegisterService registerService, StringCacheClient stringCacheClient, WeChatService weChatService, UserService userService) {
        this.registerService = registerService;
        this.stringCacheClient = stringCacheClient;
        this.weChatService = weChatService;
        this.userService = userService;
    }
 
 
    @PostMapping("/supplier")
    @ApiOperation(value = "供应商账号注册")
    public ResponseEntity<ReturnDataDTO<String>> registerSupplier(@Validated @RequestBody RegisterDTO dto) {
        //从缓存中获取验证码
        String smsCacheCode = stringCacheClient.get(SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.supplier.name() + SEPARATOR + dto.getTel());
        if (StringUtils.isBlank(smsCacheCode)) {
            throw new ValidationException("手机验证码已过期");
        }
        if (!StringUtils.equals(dto.getSmsCode(), smsCacheCode)) {
            throw new ValidationException("手机验证码不正确");
        }
        registerService.registerUser(dto, Constants.USER_TYPE.supplier.name());
        //删除缓存中的验证码
        stringCacheClient.delete(SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.supplier.name() + SEPARATOR + dto.getTel());
        return returnData(R.SUCCESS.getCode(),null);
    }
 
 
    @PostMapping("/partner")
    @ApiOperation(value = "合伙人账号注册")
    public ResponseEntity<ReturnDataDTO<String>> registerPartner(@Validated @RequestBody RegisterPartnerDTO dto) {
        //从缓存中获取验证码
        String smsCacheCode = stringCacheClient.get(SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.partner.name() + SEPARATOR + dto.getTel());
        if (StringUtils.isBlank(smsCacheCode)) {
            throw new ValidationException("手机验证码已过期");
        }
        if (!StringUtils.equals(dto.getSmsCode(), smsCacheCode)) {
            throw new ValidationException("手机验证码不正确");
        }
        registerService.registerPartnerUser(dto);
 
        //删除缓存中的验证码
        stringCacheClient.delete(SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.partner.name() + SEPARATOR + dto.getTel());
        return returnData(R.SUCCESS.getCode(),null);
    }
 
 
    @PostMapping("/customer")
    @ApiOperation(value = "商户账号注册")
    public ResponseEntity<ReturnDataDTO<String>> registerCustomer(@Validated @RequestBody RegisterCustomerDTO dto) throws Exception{
        //从缓存中获取验证码
        String smsCacheCode = stringCacheClient.get(SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.customer.name() + SEPARATOR + dto.getTel());
        if (StringUtils.isBlank(smsCacheCode)) {
            throw new ValidationException("手机验证码已过期");
        }
        if (!StringUtils.equals(dto.getSmsCode(), smsCacheCode)) {
            throw new ValidationException("手机验证码不正确");
        }
        WxMaJscode2SessionResult session = weChatService.getWxMaSessionInfo(dto.getWxcode());
        String openId = session.getOpenid();
        String sessionKey = session.getSessionKey();
        String unionId = session.getUnionid();
 
        registerService.registerCustomerUser(dto, Constants.USER_TYPE.customer.name(),openId,sessionKey,unionId);
 
        //删除缓存中的验证码
        stringCacheClient.delete(SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.customer.name() + SEPARATOR + dto.getTel());
        return returnData(R.SUCCESS.getCode(),null);
    }
}