From 750b5743eab82714552271793a9facc95758f0f3 Mon Sep 17 00:00:00 2001 From: linfeng <2445465217@qq.com> Date: Thu, 9 Jun 2022 22:48:47 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E7=AB=AF=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/linfeng/common/utils/Constant.java | 10 +++- .../modules/admin/service/AppUserService.java | 2 + .../service/impl/AppUserServiceImpl.java | 41 +++++++++++++ .../app/controller/AppLoginController.java | 60 +++++++++++++++++++ .../modules/app/form/SmsLoginForm.java | 17 ++++++ .../linfeng/modules/app/utils/LocalUser.java | 59 ++++++++++++++++++ 6 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 src/main/java/io/linfeng/modules/app/controller/AppLoginController.java create mode 100644 src/main/java/io/linfeng/modules/app/form/SmsLoginForm.java create mode 100644 src/main/java/io/linfeng/modules/app/utils/LocalUser.java diff --git a/src/main/java/io/linfeng/common/utils/Constant.java b/src/main/java/io/linfeng/common/utils/Constant.java index ac4a758..a9910e3 100644 --- a/src/main/java/io/linfeng/common/utils/Constant.java +++ b/src/main/java/io/linfeng/common/utils/Constant.java @@ -87,7 +87,15 @@ public class Constant { public static final String CONTENT_COMMENT_REPLY = "用户【{}】回复了你在动态[{}]下的评论:{}"; public static final String CONTENT_POST_STAR = "用户【{}】点赞收藏了你的动态:{}"; public static final String ADMIN_POST_DOWN = "你的动态【{}】由于违反社区规定已被删除"; - /** + + + /** + * 手机验证码长度 + */ + public static final Integer SMS_SIZE = 6; + public static final String DEAULT_HEAD = "http://pic.linfeng.tech/test/20220126/4515fc2cbed74d0b9163d35a12bd4c3b.png"; + + /** * 菜单类型 */ public enum MenuType { diff --git a/src/main/java/io/linfeng/modules/admin/service/AppUserService.java b/src/main/java/io/linfeng/modules/admin/service/AppUserService.java index 6352e1a..4943480 100644 --- a/src/main/java/io/linfeng/modules/admin/service/AppUserService.java +++ b/src/main/java/io/linfeng/modules/admin/service/AppUserService.java @@ -34,5 +34,7 @@ public interface AppUserService extends IService { * @return HomeRateResponse */ HomeRateResponse indexDate(); + + Integer smsLogin(SmsLoginForm form, HttpServletRequest request); } diff --git a/src/main/java/io/linfeng/modules/admin/service/impl/AppUserServiceImpl.java b/src/main/java/io/linfeng/modules/admin/service/impl/AppUserServiceImpl.java index 917befb..164a6ea 100644 --- a/src/main/java/io/linfeng/modules/admin/service/impl/AppUserServiceImpl.java +++ b/src/main/java/io/linfeng/modules/admin/service/impl/AppUserServiceImpl.java @@ -1,11 +1,14 @@ package io.linfeng.modules.admin.service.impl; +import cn.hutool.core.util.RandomUtil; +import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import io.linfeng.common.exception.LinfengException; import io.linfeng.common.response.HomeRateResponse; import io.linfeng.common.utils.*; import io.linfeng.modules.admin.entity.PostEntity; import io.linfeng.modules.admin.service.*; +import io.linfeng.modules.app.form.SmsLoginForm; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; @@ -15,6 +18,8 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import io.linfeng.modules.admin.dao.AppUserDao; import io.linfeng.modules.admin.entity.AppUserEntity; +import javax.servlet.http.HttpServletRequest; + @Service public class AppUserServiceImpl extends ServiceImpl implements AppUserService { @@ -26,6 +31,8 @@ public class AppUserServiceImpl extends ServiceImpl i @Autowired private AppUserDao userDao; + @Autowired + private RedisUtils redisUtils; @Override public PageUtils queryPage(Map params) { @@ -84,6 +91,40 @@ public class AppUserServiceImpl extends ServiceImpl i return response; } + @Override + public Integer smsLogin(SmsLoginForm form, HttpServletRequest request) { + AppUserEntity appUserEntity = this.lambdaQuery().eq(AppUserEntity::getMobile, form.getMobile()).one(); + String codeKey = "code_" + form.getMobile(); + String s = redisUtils.get(codeKey); + if(!s.equals(form.getCode())){ + throw new LinfengException("验证码错误!"); + } + if(appUserEntity!=null){ + if(appUserEntity.getStatus()==1){ + throw new LinfengException("该账号已被禁用"); + } + return appUserEntity.getUid(); + }else { + List list=new ArrayList<>(); + list.add("萌新"); + AppUserEntity appUser = new AppUserEntity(); + appUser.setMobile(form.getMobile()); + appUser.setAvatar(Constant.DEAULT_HEAD); + appUser.setGender(0); + appUser.setUsername("LF_"+RandomUtil.randomNumbers(6)); + String tag = JSON.toJSONString(list); + appUser.setTagStr(tag); + appUser.setCreateTime(DateUtil.nowDateTime()); + appUser.setUpdateTime(DateUtil.nowDateTime()); + baseMapper.insert(appUser); + AppUserEntity appUsers = this.lambdaQuery().eq(AppUserEntity::getMobile, form.getMobile()).one(); + + + return appUsers.getUid(); + } + + } + private Integer getTotalNum() { return this.lambdaQuery().select(AppUserEntity::getUid).count(); } diff --git a/src/main/java/io/linfeng/modules/app/controller/AppLoginController.java b/src/main/java/io/linfeng/modules/app/controller/AppLoginController.java new file mode 100644 index 0000000..b7f78bd --- /dev/null +++ b/src/main/java/io/linfeng/modules/app/controller/AppLoginController.java @@ -0,0 +1,60 @@ +package io.linfeng.modules.app.controller; + +import io.linfeng.common.utils.R; +import io.linfeng.common.utils.RedisUtils; +import io.linfeng.modules.admin.service.AppUserService; +import io.linfeng.modules.app.form.SmsLoginForm; +import io.linfeng.modules.app.utils.JwtUtils; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +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; + +import javax.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.Map; + +/** + * APP登录接口 + * @author linfeng + * @date 2022/6/9 22:40 + */ +@RestController +@RequestMapping("/app/user") +@Api(tags = "APP登录接口") +public class AppLoginController { + + + @Autowired + private JwtUtils jwtUtils; + + @Autowired + private AppUserService appUserService; + + @Autowired + private RedisUtils redisUtils; + + /** + * 手机验证码登录 + */ + @PostMapping("/smsLogin") + @ApiOperation("手机验证码登录") + public R smsLogin(@RequestBody SmsLoginForm form, HttpServletRequest request){ + + //用户登录 + Integer userId = appUserService.smsLogin(form,request); + + //生成token + String token = jwtUtils.generateToken(userId); + + Map map = new HashMap<>(); + map.put("token", token); + map.put("expire", jwtUtils.getExpire()); + + return R.ok(map); + } + +} diff --git a/src/main/java/io/linfeng/modules/app/form/SmsLoginForm.java b/src/main/java/io/linfeng/modules/app/form/SmsLoginForm.java new file mode 100644 index 0000000..ab969b9 --- /dev/null +++ b/src/main/java/io/linfeng/modules/app/form/SmsLoginForm.java @@ -0,0 +1,17 @@ + +package io.linfeng.modules.app.form; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +@Data +@ApiModel(value = "手机验证码登录") +public class SmsLoginForm { + + @ApiModelProperty(value = "手机号") + private String mobile; + + @ApiModelProperty(value = "验证码") + private String code; +} diff --git a/src/main/java/io/linfeng/modules/app/utils/LocalUser.java b/src/main/java/io/linfeng/modules/app/utils/LocalUser.java new file mode 100644 index 0000000..84710b4 --- /dev/null +++ b/src/main/java/io/linfeng/modules/app/utils/LocalUser.java @@ -0,0 +1,59 @@ +package io.linfeng.modules.app.utils; + +import io.jsonwebtoken.Claims; +import io.linfeng.common.utils.RedisUtils; +import io.linfeng.modules.admin.entity.AppUserEntity; +import io.linfeng.modules.admin.service.AppUserService; +import org.apache.commons.lang.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; +import java.util.Objects; + +/** + * 获取登录用户信息,如果未登录返回空 + * @author linfeng + * @date 2022/6/9 22:38 + */ +@Component +public class LocalUser { + + @Autowired + private JwtUtils jwtUtils; + @Autowired + private RedisUtils redisUtils; + @Autowired + private AppUserService userService; + + + public AppUserEntity getUser(){ + //获取request + HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); + //获取token + String token = request.getHeader(jwtUtils.getHeader()); + if(StringUtils.isBlank(token)){ + token = request.getParameter(jwtUtils.getHeader()); + } + //凭证为空 + if(StringUtils.isBlank(token)){ + return null; + } + Claims claims = jwtUtils.getClaimByToken(token); + //这里要判空 不然会出现无效token干扰 + if(claims==null){ + return null; + } + long userId = Long.parseLong(claims.getSubject()); + AppUserEntity userInfo = redisUtils.get("userId:" + userId, AppUserEntity.class); + if (userInfo != null) { + return userInfo; + } + //重新获取用户信息 + AppUserEntity user = userService.getById(userId); + redisUtils.set("userId:" + userId, user, 7200); + return user; + } +} \ No newline at end of file