移动端登录逻辑
This commit is contained in:
parent
03be7caee1
commit
750b5743ea
@ -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 {
|
||||
|
@ -34,5 +34,7 @@ public interface AppUserService extends IService<AppUserEntity> {
|
||||
* @return HomeRateResponse
|
||||
*/
|
||||
HomeRateResponse indexDate();
|
||||
|
||||
Integer smsLogin(SmsLoginForm form, HttpServletRequest request);
|
||||
}
|
||||
|
||||
|
@ -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<AppUserDao, AppUserEntity> implements AppUserService {
|
||||
@ -26,6 +31,8 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserDao, AppUserEntity> i
|
||||
@Autowired
|
||||
private AppUserDao userDao;
|
||||
|
||||
@Autowired
|
||||
private RedisUtils redisUtils;
|
||||
|
||||
@Override
|
||||
public PageUtils queryPage(Map<String, Object> params) {
|
||||
@ -84,6 +91,40 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserDao, AppUserEntity> 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<String> 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();
|
||||
}
|
||||
|
@ -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<String, Object> map = new HashMap<>();
|
||||
map.put("token", token);
|
||||
map.put("expire", jwtUtils.getExpire());
|
||||
|
||||
return R.ok(map);
|
||||
}
|
||||
|
||||
}
|
17
src/main/java/io/linfeng/modules/app/form/SmsLoginForm.java
Normal file
17
src/main/java/io/linfeng/modules/app/form/SmsLoginForm.java
Normal file
@ -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;
|
||||
}
|
59
src/main/java/io/linfeng/modules/app/utils/LocalUser.java
Normal file
59
src/main/java/io/linfeng/modules/app/utils/LocalUser.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user