大家好,又见面了,我是你们的朋友全栈君。
import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.eduservice.entity.UcenterMember;
import com.atguigu.eduservice.entity.vo.RegisterVo;
import com.atguigu.eduservice.mapper.UcenterMemberMapper;
import com.atguigu.eduservice.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
/**
*
* 会员表 服务实现类
*
*
* @author testjava
* @since 2020-06-05
*/
@Service
public class UcenterMemberServiceImpl extends ServiceImpl implements UcenterMemberService {
//注入
@Autowired
private RedisTemplate redisTemplate;
//登录的方法
@Override
public String login(UcenterMember member) {
String mobile = member.getMobile();
String password = member.getPassword();
//判断两者是否为空
if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
throw new GuliException(20001,”登录失败”);
}
//判断手机号是否正确
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq(“mobile”,mobile);
UcenterMember mobilMenber = baseMapper.selectOne(wrapper);
//判断查出来的对象是否为空
if (mobilMenber == null){//没有这个手机号
throw new GuliException(20001,”登录失败手机号不存在”);
}
//判断对应手机号的密码是否正确
//因为存储在数据库中的密码进行了加密, 解决办法:把输入的密码进行加密,在和数据库中的密码比较
//加密方式MD5
if (!MD5.encrypt(password).equals(mobilMenber.getPassword())){
throw new GuliException(20001,”密码错误”);
}
//判断用户是否禁用
if (mobilMenber.getIsDisabled()){
throw new GuliException(20001,”用户名已被禁用”);
}
//登录成功
//生成token字符串,使用JWT做到,传查出来的对象mobilMenber
String jwtToken = JwtUtils.getJwtToken(mobilMenber.getId(), mobilMenber.getNickname());
return jwtToken;
}
//注册
@Override
public void register(RegisterVo registerVo) {
//获取注册的数据
String code = registerVo.getCode();//验证码
String mobile = registerVo.getMobile();//手机号
String nickname = registerVo.getNickname();//昵称
String password = registerVo.getPassword();//密码
//非空判断
if (StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)){
throw new GuliException(20001,”注册失败”);
}
//判断验证码是否正确,跟redis中验证吗是否一样
String rediscode = redisTemplate.opsForValue().get(mobile);
if (!code.equals(rediscode)){
throw new GuliException(20001,”注册失败”);
}
//判断手机号是否重复
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq(“mobile”,mobile);
Integer integer = baseMapper.selectCount(wrapper);
if (integer > 0){
throw new GuliException(20001,”注册失败”);
}
//数据添加到数据库中
UcenterMember member = new UcenterMember();
member.setMobile(mobile);
member.setPassword(MD5.encrypt(password));
member.setNickname(nickname);
member.setIsDisabled(false);
member.setAvatar(“http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132”);
baseMapper.insert(member);
}
@Override
public UcenterMember getOpenIdMember(String openid) {
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq(“openid”,openid);
UcenterMember member = baseMapper.selectOne(wrapper);
return member;
}
}
三controller层
packagecom.atguigu.eduservice.controller;importcom.atguigu.commonutils.JwtUtils;importcom.atguigu.commonutils.R;importcom.atguigu.eduservice.entity.UcenterMember;importcom.atguigu.eduservice.entity.vo.RegisterVo;importcom.atguigu.eduservice.service.UcenterMemberService;importorg.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;importjavax.servlet.http.HttpServletRequest;/***
* 会员表 前端控制器
*
*
*@authortestjava
*@since2020-06-05*/@RestController
@RequestMapping(“/educenter/member”)
@CrossOriginpublic classUcenterMemberController {
@AutowiredprivateUcenterMemberService memberService;//登录
@PostMapping(“login”)publicR loginUser(@RequestBody UcenterMember member) {//member对象封装手机号和密码//调用service方法实现登录//返回token值,使用jwt生成
String token =memberService.login(member);return R.ok().data(“token”,token);
}//注册
@PostMapping(“register”)publicR registerUser(@RequestBody RegisterVo registerVo) {
memberService.register(registerVo);returnR.ok();
}//根据token获取用户信息
@GetMapping(“getMemberInfo”)publicR getMemberInfo(HttpServletRequest request) {//调用jwt工具类的方法。根据request对象获取头信息,返回用户id
String memberId =JwtUtils.getMemberIdByJwtToken(request);//查询数据库根据用户id获取用户信息
UcenterMember member =memberService.getById(memberId);return R.ok().data(“userInfo”,member);
}
}
四前端调用方法获得token
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/143368.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...