|
|
@@ -1,13 +1,17 @@
|
|
|
package com.fs.app.controller;
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
import com.fs.app.annotation.Login;
|
|
|
import com.fs.app.param.*;
|
|
|
import com.fs.app.utils.WxUtil;
|
|
|
+import com.fs.common.VerifyCodeUtil;
|
|
|
import com.fs.common.annotation.RepeatSubmit;
|
|
|
import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.core.redis.RedisCache;
|
|
|
+import com.fs.common.exception.ServiceException;
|
|
|
+import com.fs.common.service.ISmsService;
|
|
|
import com.fs.common.utils.sign.Md5Utils;
|
|
|
import com.fs.core.config.WxOpenProperties;
|
|
|
import com.fs.his.config.FsSysConfig;
|
|
|
@@ -35,13 +39,11 @@ import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.util.Calendar;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import static com.fs.his.utils.PhoneUtil.encryptPhone;
|
|
|
+import static com.fs.his.utils.PhoneUtil.encryptPhoneOldKey;
|
|
|
|
|
|
@Api("app登录接口")
|
|
|
@RestController
|
|
|
@@ -58,6 +60,9 @@ public class AppLoginController extends AppBaseController{
|
|
|
@Autowired
|
|
|
private WxOpenProperties openProperties;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ISmsService smsService;
|
|
|
+
|
|
|
@Autowired
|
|
|
private RedisCache redisCache;
|
|
|
@ApiOperation("注册app用户")
|
|
|
@@ -117,6 +122,82 @@ public class AppLoginController extends AppBaseController{
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @PostMapping("/registerSendCode")
|
|
|
+ public R registerSendCode(@RequestBody Map<String, String> body){
|
|
|
+ String phone = body.get("phone");
|
|
|
+ String encryptPhone = encryptPhone(phone);
|
|
|
+ List<FsUser> user = userService.selectFsUserListByPhone(encryptPhone);
|
|
|
+ if(CollectionUtil.isEmpty(user)){
|
|
|
+ user = userService.selectFsUserListByPhone(encryptPhoneOldKey(phone));
|
|
|
+ }
|
|
|
+ if (!CollectionUtil.isEmpty(user)){
|
|
|
+ return R.error("此电话号码已注册");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证码 key(存验证码,3分钟有效)
|
|
|
+ String smsCodeKey = "sms:code:" + phone;
|
|
|
+ // 发送冷却 key(限制60秒内不能再次发送)
|
|
|
+ String smsCooldownKey = "sms:cooldown:" + phone;
|
|
|
+
|
|
|
+ // 判断是否在60秒冷却时间内
|
|
|
+ if (redisCache.getCacheObject(smsCooldownKey) != null) {
|
|
|
+ return R.error("验证码已发送,请稍后再试");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成新的验证码
|
|
|
+ String smsCode = VerifyCodeUtil.generateCode();
|
|
|
+
|
|
|
+ // 发送短信
|
|
|
+ smsService.sendCaptcha(phone, smsCode, "验证码");
|
|
|
+
|
|
|
+ // 缓存验证码(3分钟有效)
|
|
|
+ redisCache.setCacheObject(smsCodeKey, smsCode, 180, TimeUnit.SECONDS);
|
|
|
+ // 设置冷却时间(60秒内不能再发)
|
|
|
+ redisCache.setCacheObject(smsCooldownKey, "1", 60, TimeUnit.SECONDS);
|
|
|
+
|
|
|
+ return R.ok("验证码已发送");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @PostMapping("/registerByPhone")
|
|
|
+ public R registerByPhone(@RequestBody Map<String,String> map){
|
|
|
+ String phone = map.get("phone");
|
|
|
+ String code = map.get("code");
|
|
|
+ String password = map.get("password");
|
|
|
+ String encryptPhone = encryptPhone(phone);
|
|
|
+ List<FsUser> users = userService.selectFsUserListByPhone(encryptPhone);
|
|
|
+ if (users == null || CollectionUtil.isEmpty(users)){
|
|
|
+ String s = encryptPhoneOldKey(phone);
|
|
|
+ users = userService.selectFsUserListByPhone(s);
|
|
|
+ }
|
|
|
+ if (!CollectionUtil.isEmpty(users)){
|
|
|
+ return R.error("此账号已经注册");
|
|
|
+ }
|
|
|
+ String redisCode = redisCache.getCacheObject("sms:code:" + phone);
|
|
|
+ if (StringUtils.isEmpty(redisCode)){
|
|
|
+ return R.error("验证码已过期,请重新发送");
|
|
|
+ }
|
|
|
+ if (!redisCode.equals(code)) {
|
|
|
+ return R.error("验证码错误");
|
|
|
+ }
|
|
|
+ FsUser user = new FsUser();
|
|
|
+ // 创建新用户
|
|
|
+ user.setPhone(phone);
|
|
|
+ user.setJpushId(map.get("jpushId"));
|
|
|
+ user.setSource(map.get("source"));
|
|
|
+ user.setNickName("app用户" + phone.substring(phone.length() - 4));
|
|
|
+ user.setStatus(1);
|
|
|
+ user.setAvatar("https://cos.his.cdwjyyh.com/fs/20240926/420728ee06e54575ba82665dedb4756b.png");
|
|
|
+ user.setPassword(Md5Utils.hash(password));
|
|
|
+ user.setCreateTime(new Date());
|
|
|
+ if (userService.insertFsUser(user) > 0) {
|
|
|
+ return R.ok("注册成功");
|
|
|
+ } else {
|
|
|
+ return R.error("注册失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
@@ -210,6 +291,61 @@ public class AppLoginController extends AppBaseController{
|
|
|
|
|
|
}
|
|
|
|
|
|
+ @PostMapping("/loginByPhone")
|
|
|
+ public R loginByPhone(@RequestBody Map<String,String> map){
|
|
|
+ String phone = map.get("phone");
|
|
|
+ String code = map.get("code");
|
|
|
+ String encryptPhone = encryptPhone(phone);
|
|
|
+ List<FsUser> user = userService.selectFsUserListByPhone(encryptPhone);
|
|
|
+ if (CollectionUtil.isEmpty(user)){
|
|
|
+ user = userService.selectFsUserListByPhone(encryptPhoneOldKey(phone));
|
|
|
+ }
|
|
|
+ if (CollectionUtil.isEmpty(user)){
|
|
|
+ return R.error("此电话号码未绑定用户");
|
|
|
+ }
|
|
|
+ if (user.size()>1){
|
|
|
+ //如果出现了一个手机号多个用户的情况,找出登陆过app的那个用户
|
|
|
+ user.removeIf(fsUser -> StringUtils.isEmpty(fsUser.getHistoryApp()));
|
|
|
+ }
|
|
|
+ String redisCode = redisCache.getCacheObject("sms:code:" + phone);
|
|
|
+ if (StringUtils.isEmpty(redisCode)){
|
|
|
+ return R.error("验证码已过期,请重新发送");
|
|
|
+ }
|
|
|
+ if (!redisCode.equals(code)) {
|
|
|
+ return R.error("验证码错误");
|
|
|
+ }
|
|
|
+ updateExistingUserJpushId(user.get(0), map.get("jpushId"));
|
|
|
+ return generateTokenAndReturn(user.get(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/resetPassword")
|
|
|
+ public R resetPassword(@RequestBody Map<String, String> body){
|
|
|
+ String phone = body.get("phone");
|
|
|
+ String code = body.get("code");
|
|
|
+ String newPassword = body.get("newPassword");
|
|
|
+ String confirmPassword = body.get("confirmPassword");
|
|
|
+ if (!newPassword.equals(confirmPassword)){
|
|
|
+ throw new ServiceException("两次输入密码不一致,请检查");
|
|
|
+ }
|
|
|
+ String encryptPhone = encryptPhone(phone);
|
|
|
+ List<FsUser> user = userService.selectFsUserListByPhone(encryptPhone);
|
|
|
+ if (CollectionUtil.isEmpty(user)){
|
|
|
+ user = userService.selectFsUserListByPhone(encryptPhoneOldKey(phone));
|
|
|
+ }
|
|
|
+ if (CollectionUtil.isEmpty(user)){
|
|
|
+ return R.error("此电话号码未绑定用户");
|
|
|
+ }
|
|
|
+ String redisCode = redisCache.getCacheObject("sms:code:" + phone);
|
|
|
+ if (StringUtils.isEmpty(redisCode)){
|
|
|
+ return R.error("验证码已过期,请重新发送");
|
|
|
+ }
|
|
|
+ if (!redisCode.equals(code)) {
|
|
|
+ return R.error("验证码错误");
|
|
|
+ }
|
|
|
+ String password = Md5Utils.hash(newPassword);
|
|
|
+ return userService.updatePasswordByPhone(password,encryptPhone);
|
|
|
+ }
|
|
|
+
|
|
|
@ApiOperation("绑定手机号")
|
|
|
@PostMapping("/setPhone")
|
|
|
public R setPhone(@Validated @RequestBody FsUserEditPhoneParam param) {
|
|
|
@@ -424,4 +560,40 @@ public class AppLoginController extends AppBaseController{
|
|
|
return R.error("用户不存在!");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @PostMapping("/sendCode")
|
|
|
+ public R sendCode(@RequestBody Map<String, String> body){
|
|
|
+ String phone = body.get("phone");
|
|
|
+ String encryptPhone = encryptPhone(phone);
|
|
|
+ List<FsUser> user = userService.selectFsUserListByPhone(encryptPhone);
|
|
|
+ if(CollectionUtil.isEmpty(user)){
|
|
|
+ user = userService.selectFsUserListByPhone(encryptPhoneOldKey(phone));
|
|
|
+ }
|
|
|
+ if (CollectionUtil.isEmpty(user)){
|
|
|
+ return R.error("此电话号码未绑定用户");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证码 key(3分钟有效)
|
|
|
+ String smsCodeKey = "sms:code:" + phone;
|
|
|
+ // 冷却 key(60秒内不能重复发送)
|
|
|
+ String smsCooldownKey = "sms:cooldown:" + phone;
|
|
|
+
|
|
|
+ // 判断是否在 60 秒冷却期
|
|
|
+ if (redisCache.getCacheObject(smsCooldownKey) != null) {
|
|
|
+ return R.error("验证码已发送,请稍后再试");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成新验证码
|
|
|
+ String smsCode = VerifyCodeUtil.generateCode();
|
|
|
+
|
|
|
+ // 发送短信
|
|
|
+ smsService.sendCaptcha(phone, smsCode, "验证码");
|
|
|
+
|
|
|
+ // 缓存验证码(3分钟有效)
|
|
|
+ redisCache.setCacheObject(smsCodeKey, smsCode, 180, TimeUnit.SECONDS);
|
|
|
+ // 设置冷却时间(60秒内不能再发)
|
|
|
+ redisCache.setCacheObject(smsCooldownKey, "1", 60, TimeUnit.SECONDS);
|
|
|
+
|
|
|
+ return R.ok("验证码已发送");
|
|
|
+ }
|
|
|
}
|