Browse Source

add:短信验证

ct 3 days ago
parent
commit
acd6fcbc3f

+ 3 - 0
fs-service/src/main/java/com/fs/his/domain/FsUser.java

@@ -183,6 +183,9 @@ public class FsUser extends BaseEntity
     /** 推广上级用户ID */
     private Long spreadUserId;
 
+    /** app登录后不为null(表示是否下载app) */
+    private String historyApp;
+
     public void setNickName(String nickname)
     {
         if(StringUtils.isNotEmpty(nickname)){

+ 2 - 0
fs-service/src/main/java/com/fs/his/mapper/FsUserMapper.java

@@ -469,4 +469,6 @@ public interface FsUserMapper
     void updateUserOrderCountAndAmount(@Param("userId") Long userId, @Param("amount") BigDecimal amount);
 
     List<FsUser> selectFsUserListByPhone(String phone);
+
+    void updatePasswordByPhone(@Param("password")String password, @Param("encryptPhone")String encryptPhone);
 }

+ 2 - 0
fs-service/src/main/java/com/fs/his/service/IFsUserService.java

@@ -230,4 +230,6 @@ public interface IFsUserService
     HisFsUserVO getHisUserIntegralWithLogs(FsUser fsUser);
 
     List<FsUser> selectFsUserListByPhone(String phone);
+
+    R updatePasswordByPhone(String password, String encryptPhone);
 }

+ 6 - 0
fs-service/src/main/java/com/fs/his/service/impl/FsUserServiceImpl.java

@@ -1566,4 +1566,10 @@ public class FsUserServiceImpl implements IFsUserService {
         return fsUserMapper.selectFsUserListByPhone(phone);
     }
 
+    @Override
+    public R updatePasswordByPhone(String password, String encryptPhone) {
+        fsUserMapper.updatePasswordByPhone(password, encryptPhone);
+        return R.ok();
+    }
+
 }

+ 3 - 0
fs-service/src/main/resources/mapper/his/FsUserMapper.xml

@@ -721,6 +721,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             user_id = #{userId}
         </foreach>
     </update>
+    <update id="updatePasswordByPhone">
+        update fs_user set password = #{password} where phone = #{encryptPhone}
+    </update>
 
     <select id="selectUserListByMap" resultType="com.fs.his.vo.OptionsVO">
         select

+ 92 - 0
fs-user-app/src/main/java/com/fs/app/controller/AppLoginController.java

@@ -10,6 +10,7 @@ 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;
@@ -300,6 +301,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) {
@@ -456,6 +512,42 @@ public class AppLoginController extends AppBaseController{
 
     }
 
+    @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("验证码已发送");
+    }
+
     private List<FsUser> findUsersByPhone(String phone) {
         // 先根据加密手机号查询用户
         String jiami = (encryptPhone(phone));