86183
2022-09-09 0d999e33085c0a25c5525242748f6aa62a401159
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package cn.mb.cloud.auth.security.controller;
 
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.BCrypt;
import cn.mb.cloud.auth.security.entity.User;
import cn.mb.cloud.auth.security.service.IUserService;
import cn.mb.cloud.auth.security.util.SmsUtil;
import cn.mb.cloud.auth.security.util.StringUtil;
import cn.mb.cloud.auth.security.util.VerifyCode;
import cn.mb.cloud.common.cache.RedisFastJsonTemplate;
import cn.mb.cloud.common.core.constant.CacheConstants;
import cn.mb.cloud.common.core.constant.enums.ErrorCodeConstants;
import cn.mb.cloud.common.core.constant.enums.LoginTypeEnum;
import cn.mb.cloud.common.core.exception.BusinessException;
import cn.mb.cloud.common.core.exception.ValidateCodeException;
import cn.mb.cloud.common.core.util.ResponseData;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
 
/**
 * 系统社交登录账号表
 *
 * @author jason
 * @date 2018-08-16 21:30:41
 */
@Slf4j
@RestController
@RequestMapping("/social")
@AllArgsConstructor
@Api(value = "三方账号管理模块", tags = "三方账号管理模块")
public class SysSocialDetailsController {
 
 
 
    private final RedisFastJsonTemplate redisFastJsonTemplate;
 
 
    private final IUserService userService;
 
 
    @ResponseBody
    @PostMapping("/register")
    @ApiOperation(value = "小程序注册")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "手机号", name = "phone", required = true, dataType = "string"),
            @ApiImplicitParam(value = "验证码", name = "code",  dataType = "string"),
            @ApiImplicitParam(value = "密码", name = "password",  dataType = "string"),
            @ApiImplicitParam(value = "昵称(微信)", name = "nickName",  dataType = "string"),
            @ApiImplicitParam(value = "头像(微信)", name = "avatarUrl", dataType = "string"),
            @ApiImplicitParam(value = "性别(微信)", name = "gender", dataType = "Integer"),
            @ApiImplicitParam(value = "来源(传名称)", name = "source",  dataType = "string"),
            @ApiImplicitParam(value = "openId", name = "openId",  dataType = "string"),
    })
    public ResponseData phoneRegister(String phone, String code,String password,String source,
                                      String nickName,String avatarUrl,Integer gender,String openId){
        try {
            //验证验证码
            if (StringUtils.isEmpty(phone)) {
                return ResponseData.fail("手机号不能为空");
            }
            if(StringUtils.isEmpty(openId)){
                //验证验证码
                ResponseData responseData = validateSms(1, phone, code);
                String dataCode = responseData.getCode();
                if(!dataCode.equals(ErrorCodeConstants.SUCCESS.getValue())){
                    return responseData;
                }
            }
            //验证手机号是否注册
            User user = userService.getOne(new QueryWrapper<User>()
                    .eq("username",phone)
                    .eq("del_flag",0));
            if(user == null){
                user = new User();
                if(StringUtils.isEmpty(password)){
                    password = phone;
                }
                //手机号注册
                String pswMd5 = BCrypt.hashpw(password, BCrypt.gensalt());
                user.setUsername(phone);
                user.setPassword(pswMd5);
                user.setSource(source);
                user.setName(nickName);
                user.setWechatName(nickName);
                user.setOpenId(openId);
                if(StringUtils.isEmpty(nickName)){
                    user.setName(phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
                }
                user.setAvatar(avatarUrl);
                user.setGender(gender);
                user.setCreateDate(LocalDateTime.now());
                userService.save(user);
            }else {
                user.setWechatName(nickName);
                user.setOpenId(openId);
                userService.updateById(user);
            }
            return ResponseData.success(user.getId());
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.fail();
        }
    }
 
    @ResponseBody
    @PostMapping("/forgetPassword")
    @ApiOperation(value = "忘记密码")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "手机号", name = "phone", required = true, dataType = "string"),
            @ApiImplicitParam(value = "验证码", name = "code", required = true, dataType = "string"),
            @ApiImplicitParam(value = "密码", name = "password", required = true, dataType = "string"),
    })
    public ResponseData forgetPassword(String phone,String code,String password){
        try {
            if (StringUtils.isEmpty(phone)) {
                return ResponseData.fail("手机号不能为空");
            }
            if (StringUtils.isEmpty(code)) {
                return ResponseData.fail("验证码不能为空");
            }
            if (StringUtils.isEmpty(password)) {
                return ResponseData.fail("密码不能为空");
            }
            //验证验证码
            ResponseData responseData = validateSms(2, phone, code);
            String dataCode = responseData.getCode();
            if(!dataCode.equals(ErrorCodeConstants.SUCCESS.getValue())){
                return responseData;
            }
            //修改密码
            String pswMd5 = BCrypt.hashpw(password, BCrypt.gensalt());
            return  userService.forgetPassword(phone,pswMd5);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.fail();
        }
    }
 
    @GetMapping("/sms/captcha")
    @ApiOperation(value = "获取验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "type", value = "类型(1=注册,2=忘记密码 )", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "mobile", value = "手机号", required = true, dataType = "String"),
    })
    public ResponseData sendSmsCode(@NotBlank(message = "手机号码不能为空") String mobile, @NotBlank(message = "类型不能为空") Byte type)
            throws BusinessException, ValidateCodeException, ClientException {
        if (StringUtils.isEmpty(mobile)) {
            return ResponseData.fail("请先绑定手机号码");
        }
        User member = userService.getOne(Wrappers.<User>query()
                .lambda().eq(User::getUsername, mobile).eq(User::getDelFlag, 0));
 
        if (member == null && type == 2) {
            log.info("手机号未注册:{}", mobile);
            return ResponseData.fail("手机号未注册");
        }
        StringUtil.checkNull(mobile, "手机账号不能为空");
        //判格式
        StringUtil.checkFormat(mobile, "请输入格式正确的11位号码");
        Object codeObj = redisFastJsonTemplate.opsForValue()
                .get(CacheConstants.DEFAULT_CODE_KEY + type + StringPool.AT + mobile);
 
        if (codeObj != null) {
            log.info("手机号验证码未过期:{},{}", mobile, codeObj);
            //return ResponseData.fail("验证码发送过于频繁");
        }
        String code = RandomUtil.randomNumbers(Integer.parseInt(VerifyCode.CODE_SIZE));
        log.info("手机号生成验证码成功:{},{}", mobile, code);
 
        SendSmsResponse response = null;
        try {
            String[] mobileSet = {"+86"+mobile};
            String[] templateParamSet={code+""};
            response = SmsUtil.sendSms(mobileSet, templateParamSet);
        } catch (com.netflix.client.ClientException e) {
            e.printStackTrace();
        }
 
        // 测试环境不发短信
        //SendSmsResponse response = new SendSmsResponse();
        SendStatus sendStatus = response.getSendStatusSet()[0];
        if (sendStatus.getCode().equals("Ok")) {
            redisFastJsonTemplate.opsForValue().set(
                    CacheConstants.DEFAULT_CODE_KEY + type + StringPool.AT + mobile, code,
                    5 * 60, TimeUnit.SECONDS);
        } else {
            // 判断存在时销毁
            redisFastJsonTemplate.delete(CacheConstants.DEFAULT_CODE_KEY + type + StringPool.AT + mobile);
        }
        return ResponseData.success(code);
    }
 
    @PostMapping("/sms/captcha/validate")
    @ApiOperation(value = "验证短信")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型(1=注册,2=忘记密码 )", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String", paramType = "query"),
    })
    public ResponseData validateSms(Integer type, String phone, String code) {
        if (type==null) {
            return ResponseData.fail("类型不能为空");
        }
        if (StringUtils.isEmpty(phone)) {
            return ResponseData.fail("手机号不能为空");
        }
        if (StringUtils.isEmpty(code)) {
            return ResponseData.fail("验证码不能为空");
        }
        return userService.verifyCode(type, phone, code);
 
    }
 
}