基于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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许,不可做商业用途!
 *
 * 版权所有,侵权必究!
 */
package com.yami.shop.api.controller;
 
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Maps;
import com.yami.shop.bean.app.param.CheckRegisterSmsParam;
import com.yami.shop.bean.app.param.SendSmsParam;
import com.yami.shop.bean.dto.ShopUserRegisterDto;
import com.yami.shop.bean.enums.SendType;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.i18n.LanguageEnum;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PrincipalUtil;
import com.yami.shop.common.util.RedisUtil;
import com.yami.shop.security.common.bo.UserInfoInTokenBO;
import com.yami.shop.security.common.enums.SysTypeEnum;
import com.yami.shop.security.common.manager.PasswordManager;
import com.yami.shop.security.common.manager.TokenStore;
import com.yami.shop.security.common.vo.TokenInfoVO;
import com.yami.shop.service.SmsLogService;
import com.yami.shop.service.SysConfigService;
import com.yami.shop.sys.common.model.ShopEmployee;
import com.yami.shop.sys.common.service.ShopEmployeeService;
import com.yami.shop.sys.common.service.ShopMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.Valid;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * @Author lth
 * @Date 2021/7/7 14:43
 */
@RestController
@RequestMapping("/p/shop/shopUserRegister")
@Tag(name = "店铺注册接口")
@AllArgsConstructor
public class ShopUserRegisterController {
 
    private final SysConfigService sysConfigService;
    private final SmsLogService smsLogService;
    private final ShopEmployeeService shopEmployeeService;
    private final PasswordEncoder passwordEncoder;
    private final PasswordManager passwordManager;
    private final ShopMenuService shopMenuService;
    private final TokenStore tokenStore;
 
    private static final String CHECK_UPDATE_PWD_SMS_FLAG = "updateShopEmployeePwdSmsFlag";
 
    @GetMapping("/getMerchantRegisterProtocol")
    @Operation(summary = "获取商家注册协议" , description = "获取商家注册协议")
    public ServerResponseEntity<String> getMerchantRegisterProtocol() {
        Integer dbLang = I18nMessage.getDbLang();
        if(Objects.equals(LanguageEnum.LANGUAGE_EN.getLang(), dbLang)) {
            return ServerResponseEntity.success(sysConfigService.getValue(Constant.MERCHANT_REGISTER_PROTOCOL_EN));
        }
        return ServerResponseEntity.success(sysConfigService.getValue(Constant.MERCHANT_REGISTER_PROTOCOL_CN));
    }
 
    @GetMapping("/getShopProtocol")
    @Operation(summary = "获取开店协议" , description = "获取开店协议")
    public ServerResponseEntity<String> getShopProtocol() {
        Integer dbLang = I18nMessage.getDbLang();
        if(Objects.equals(LanguageEnum.LANGUAGE_EN.getLang(), dbLang)) {
            return ServerResponseEntity.success(sysConfigService.getValue(Constant.SHOP_PROTOCOL_EN));
        }
        return ServerResponseEntity.success(sysConfigService.getValue(Constant.SHOP_PROTOCOL_CN));
    }
 
    @PostMapping("/sendCode")
    @Operation(summary = "发送申请开店验证码" , description = "发送申请开店验证码")
    public ServerResponseEntity<Void> sendCode(@Valid @RequestBody SendSmsParam sendSmsParam) {
        int count = shopEmployeeService.count(Wrappers.lambdaQuery(ShopEmployee.class).eq(ShopEmployee::getMobile, sendSmsParam.getMobile()));
        if (count > 0) {
            // 手机号已存在
            throw new YamiShopBindException("yami.phone.number.already.exists");
        }
        smsLogService.sendSms(SendType.VALID, sendSmsParam.getMobile(), sendSmsParam.getMobile(), Maps.newHashMap());
        return ServerResponseEntity.success();
    }
 
    @PostMapping
    @Operation(summary = "注册商家" , description = "注册商家")
    public ServerResponseEntity<TokenInfoVO> register(@Valid @RequestBody ShopUserRegisterDto shopUserRegisterDTO) {
        ShopEmployee shopEmployee = shopEmployeeService.registerMerchant(shopUserRegisterDTO);
        UserInfoInTokenBO userInfoInToken = new UserInfoInTokenBO();
        userInfoInToken.setUserId(shopEmployee.getEmployeeId().toString());
        userInfoInToken.setSysType(SysTypeEnum.MULTISHOP.value());
        userInfoInToken.setEnabled(shopEmployee.getStatus() == 1);
        userInfoInToken.setShopId(shopEmployee.getShopId());
        userInfoInToken.setOtherId(shopEmployee.getEmployeeId());
        userInfoInToken.setPerms(shopMenuService.getShopPermissions(shopEmployee));
        // 存储token返回vo
        TokenInfoVO tokenInfoVO = tokenStore.storeAndGetVo(userInfoInToken);
        return ServerResponseEntity.success(tokenInfoVO);
    }
 
    @PostMapping("/sendUpdatePwdCode")
    @Operation(summary = "发送修改密码验证码接口" , description = "发送修改密码验证码接口")
    public ServerResponseEntity<Void> sendUpdatePwdCode(@Valid @RequestBody SendSmsParam sendSmsParam) {
        ShopEmployee shopEmployee = shopEmployeeService.getOne(Wrappers.lambdaQuery(ShopEmployee.class).eq(ShopEmployee::getMobile, sendSmsParam.getMobile()));
        if (Objects.isNull(shopEmployee)) {
            // 手机号不存在
            throw new YamiShopBindException("yami.phone.number.not.exists");
        }
        smsLogService.sendSms(SendType.VALID, shopEmployee.getEmployeeId().toString(), sendSmsParam.getMobile(), Maps.newHashMap());
        return ServerResponseEntity.success();
    }
 
    @PutMapping("/checkUpdatePwdSms")
    @Operation(summary = "修改密码校验验证码" , description = "校验验证码返回校验成功的标识")
    public ServerResponseEntity<String> checkUpdatePwdSms(@Valid @RequestBody CheckRegisterSmsParam checkRegisterSmsParam) {
        boolean isCheckPass = false;
        if (Objects.nonNull(checkRegisterSmsParam) && Objects.nonNull(checkRegisterSmsParam.getMobile())) {
            Matcher m = Pattern.compile(PrincipalUtil.MOBILE_REGEXP).matcher(checkRegisterSmsParam.getMobile());
            isCheckPass = m.matches();
        }
        if (!isCheckPass) {
            throw new YamiShopBindException("yami.user.err.phone");
        }
 
        ShopEmployee shopEmployee = shopEmployeeService.getOne(Wrappers.lambdaQuery(ShopEmployee.class).eq(ShopEmployee::getMobile, checkRegisterSmsParam.getMobile()));
        if (Objects.isNull(shopEmployee)) {
            // 手机号不存在
            throw new YamiShopBindException("yami.phone.number.not.exists");
        }
        if(StrUtil.isBlank(checkRegisterSmsParam.getValidCode())){
            throw new YamiShopBindException("yami.user.code.empty");
        }
        String defaultCode = sysConfigService.getValue("UPDATE_DEFAULT_CODE");
        // 验证码登录
        if(!defaultCode.equals(checkRegisterSmsParam.getValidCode())) {
            if (!smsLogService.checkValidCode(shopEmployee.getMobile(), checkRegisterSmsParam.getValidCode(), SendType.VALID)) {
                // 验证码有误或已过期
                throw new YamiShopBindException("yami.user.code.error");
            }
        }
        String checkRegisterSmsFlag = IdUtil.simpleUUID();
        RedisUtil.set(CHECK_UPDATE_PWD_SMS_FLAG + checkRegisterSmsFlag, checkRegisterSmsParam.getMobile(), 600);
        return ServerResponseEntity.success(checkRegisterSmsFlag);
    }
 
    @PutMapping("/updatePwd")
    @Operation(summary = "修改密码" , description = "修改密码")
    public ServerResponseEntity<Void> updatePwd(@RequestBody ShopUserRegisterDto shopUserRegisterDto) {
        ShopEmployee shopEmployee = shopEmployeeService.getOne(Wrappers.lambdaQuery(ShopEmployee.class).eq(ShopEmployee::getMobile, shopUserRegisterDto.getMobile()));
        if (Objects.isNull(shopEmployee)) {
            // 手机号不存在
            throw new YamiShopBindException("yami.phone.number.not.exists");
        }
        // 看看有没有校验验证码成功的标识
        if (StrUtil.isBlank(shopUserRegisterDto.getCheckRegisterSmsFlag())) {
            // 验证码已过期,请重新发送验证码校验
            throw new YamiShopBindException("yami.verification.expire");
        } else {
            String checkRegisterSmsFlag = CHECK_UPDATE_PWD_SMS_FLAG + shopUserRegisterDto.getCheckRegisterSmsFlag();
            String checkRegisterSmsFlagMobile = RedisUtil.get(checkRegisterSmsFlag);
            if (!Objects.equals(checkRegisterSmsFlagMobile, shopUserRegisterDto.getMobile())) {
                // 验证码已过期,请重新发送验证码校验
                throw new YamiShopBindException("yami.verification.expire");
            }
        }
        String decryptPassword = passwordManager.decryptPassword(shopUserRegisterDto.getPassword());
        if (StrUtil.isBlank(decryptPassword)) {
            // 新密码不能为空
            throw new YamiShopBindException("yami.user.password.no.exist");
        }
        if (StrUtil.equals(passwordEncoder.encode(decryptPassword), shopEmployee.getPassword())) {
            // 新密码不能与原密码相同!
            throw new YamiShopBindException("yami.user.password.check");
        }
        Boolean updateRes = shopEmployeeService.updatePasswordByEmployeeId(shopEmployee.getEmployeeId(), passwordEncoder.encode(decryptPassword));
        if (!updateRes) {
            throw new YamiShopBindException("更新失败");
        } else {
            RedisUtil.del(CHECK_UPDATE_PWD_SMS_FLAG + shopUserRegisterDto.getCheckRegisterSmsFlag());
        }
        return ServerResponseEntity.success();
    }
 
}