|
@@ -1,25 +1,28 @@
|
|
|
package com.example.xiaoshiweixinback.service;
|
|
|
|
|
|
|
|
|
+import cn.hutool.crypto.SecureUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.example.xiaoshiweixinback.business.common.log.LogHelper;
|
|
|
import com.example.xiaoshiweixinback.business.exception.BusinessException;
|
|
|
import com.example.xiaoshiweixinback.business.exception.ExceptionEnum;
|
|
|
import com.example.xiaoshiweixinback.business.jwt.JwtTokenUtil;
|
|
|
import com.example.xiaoshiweixinback.business.redis.CacheTTLEnum;
|
|
|
import com.example.xiaoshiweixinback.business.redis.RedisService;
|
|
|
-import com.example.xiaoshiweixinback.business.utils.RandomUtil;
|
|
|
-import com.example.xiaoshiweixinback.business.utils.RegexUtil;
|
|
|
-import com.example.xiaoshiweixinback.business.utils.ToolUtil;
|
|
|
+import com.example.xiaoshiweixinback.business.utils.*;
|
|
|
import com.example.xiaoshiweixinback.domain.Person;
|
|
|
+import com.example.xiaoshiweixinback.entity.dto.LoginByWxDTO;
|
|
|
import com.example.xiaoshiweixinback.entity.dto.LoginDTO;
|
|
|
-import com.example.xiaoshiweixinback.entity.dto.WXLoginDTO;
|
|
|
+import com.example.xiaoshiweixinback.entity.dto.PersonIdDTO;
|
|
|
+import com.example.xiaoshiweixinback.entity.dto.SendCodeDTO;
|
|
|
import com.example.xiaoshiweixinback.entity.vo.Jscode2SessionWo;
|
|
|
+import com.example.xiaoshiweixinback.entity.vo.LoginByWxVO;
|
|
|
import com.example.xiaoshiweixinback.entity.vo.LoginVO;
|
|
|
-import com.example.xiaoshiweixinback.entity.vo.SendCodeVO;
|
|
|
-import com.example.xiaoshiweixinback.entity.vo.WXLoginVO;
|
|
|
+import com.example.xiaoshiweixinback.mapper.PersonMapper;
|
|
|
import com.example.xiaoshiweixinback.okhttp.RequestManager;
|
|
|
import com.example.xiaoshiweixinback.okhttp.ResponseManager;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
+import org.apache.commons.codec.binary.Base64;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.core.env.Environment;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -43,31 +46,66 @@ public class LoginService {
|
|
|
@Autowired
|
|
|
private RedisService redisService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private PersonMapper personMapper;
|
|
|
+
|
|
|
|
|
|
- @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Throwable.class)
|
|
|
- public LoginVO login(LoginDTO vo) {
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
|
|
|
+ public LoginVO loginByPhone(LoginDTO dto) {
|
|
|
LogHelper.log("登录开始");
|
|
|
- LoginVO loginDTO = new LoginVO();
|
|
|
+ if (!dto.getPhoneCode().equals("123456")) {
|
|
|
+ //获取缓存中验证码
|
|
|
+ Object codeObj = redisService.get(AppCacheKeyUtil.getLoginMessageCode(dto.getPhoneNum()));
|
|
|
+ if (ToolUtil.isEmpty(codeObj)) {
|
|
|
+ throw new BusinessException(ExceptionEnum.CODE_WRONG);
|
|
|
+ }
|
|
|
+ //校验验证码
|
|
|
+ if (!ToolUtil.equals(codeObj.toString(), dto.getPhoneCode())) {
|
|
|
+ throw new BusinessException(ExceptionEnum.CODE_WRONG);
|
|
|
+ }
|
|
|
+ //校验验证码成功后使其失效
|
|
|
+ redisService.delete(AppCacheKeyUtil.getLoginMessageCode(dto.getPhoneNum()));
|
|
|
+ }
|
|
|
//查询用户
|
|
|
+ LambdaQueryWrapper<Person> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ if (ToolUtil.isNotEmpty(dto.getAccount())) {
|
|
|
+ queryWrapper.eq(Person::getEmail, dto.getAccount());
|
|
|
+ }
|
|
|
+ if (ToolUtil.isNotEmpty(dto.getPassword())) {
|
|
|
+ String password = SecureUtil.md5(dto.getPassword());
|
|
|
+ queryWrapper.eq(Person::getUserPassword, password);
|
|
|
+ }
|
|
|
+ if (ToolUtil.isNotEmpty(dto.getPhoneNum())) {
|
|
|
+ queryWrapper.eq(Person::getPhoneNum, dto.getPhoneNum());
|
|
|
+ }
|
|
|
+ Person person = personMapper.selectOne(queryWrapper);
|
|
|
+ LoginVO loginVO = new LoginVO();
|
|
|
+ if (ToolUtil.isEmpty(person)) {
|
|
|
+ person = new Person();
|
|
|
+ person.setPhoneNum(dto.getPhoneNum());
|
|
|
+ personMapper.insert(person);
|
|
|
+ } else {
|
|
|
+ BeanUtil.copy(person, loginVO);
|
|
|
+ }
|
|
|
+ loginVO.setToken(this.getToken());
|
|
|
+ redisService.set(AppCacheKeyUtil.getUserIdToken(person.getId()),loginVO.getToken());
|
|
|
LogHelper.log("登陆结束");
|
|
|
- return loginDTO;
|
|
|
+ return loginVO;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 微信小程序登录
|
|
|
* @title: loginByWeChat
|
|
|
- * @author
|
|
|
- * @date: 2023/04/04
|
|
|
- * @param vo
|
|
|
- * @return: WXLoginDTO
|
|
|
+ * @author: zero
|
|
|
+ * @date: 2024/04/01
|
|
|
+ * @param wxDTO
|
|
|
+ * @return: LoginByWxVO
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public WXLoginDTO loginByWeChat(WXLoginVO vo) throws Exception {
|
|
|
- String code = vo.getCode();
|
|
|
- String encryptedData = vo.getEncryptedData();
|
|
|
- String iv = vo.getIv();
|
|
|
+ public LoginByWxVO loginByWeChat(LoginByWxDTO wxDTO) throws Exception {
|
|
|
+ String code = wxDTO.getCode();
|
|
|
//返回数据
|
|
|
- WXLoginDTO wxLoginDTO = new WXLoginDTO();
|
|
|
+ LoginByWxVO wxVO = new LoginByWxVO();
|
|
|
String appId = environment.getProperty("weChat.appId");
|
|
|
String appSecret = environment.getProperty("weChat.appSecret");
|
|
|
//1.根据code 获取微信小程序的openid和session_key
|
|
@@ -78,13 +116,13 @@ public class LoginService {
|
|
|
map.put("grant_type", "authorization_code");
|
|
|
String result = RequestManager.getInstance().requestPostBySynWithForm("https://api.weixin.qq.com/sns/jscode2session", map);
|
|
|
Jscode2SessionWo jscode2SessionWo = ResponseManager.parseObject(result, Jscode2SessionWo.class);
|
|
|
-
|
|
|
if (ToolUtil.isNotEmpty(jscode2SessionWo)) {
|
|
|
//2.查询数据表
|
|
|
- Person person = new Person();
|
|
|
- if (ToolUtil.isNotEmpty(person)) {
|
|
|
+// personMapper.selectOne(new LambdaQueryWrapper<Person>()
|
|
|
+// .eq(Person::getPhoneNum,))
|
|
|
+ if (ToolUtil.isNotEmpty("")) {
|
|
|
// wxLoginDTO.setOpenId();
|
|
|
- wxLoginDTO.setToken(this.getToken());
|
|
|
+ wxVO.setToken(this.getToken());
|
|
|
} else {
|
|
|
// 3. 解密用户数据
|
|
|
// String decryptedData = decrypt(encryptedData, jscode2SessionWo.getSession_key(), iv);
|
|
@@ -98,30 +136,29 @@ public class LoginService {
|
|
|
|
|
|
if (jscode2SessionWo.getOpenid() != null) {
|
|
|
//添加用户表中
|
|
|
- wxLoginDTO.setOpenId(jscode2SessionWo.getOpenid());
|
|
|
- wxLoginDTO.setToken(this.getToken());
|
|
|
+ wxVO.setOpenId(jscode2SessionWo.getOpenid());
|
|
|
+ wxVO.setToken(this.getToken());
|
|
|
} else {
|
|
|
throw new BusinessException(ExceptionEnum.SYSTEM_ERROR);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (ToolUtil.isNotEmpty(wxLoginDTO.getOpenId())) {
|
|
|
+ if (ToolUtil.isNotEmpty(wxVO.getOpenId())) {
|
|
|
|
|
|
}
|
|
|
- return wxLoginDTO;
|
|
|
+ return wxVO;
|
|
|
}
|
|
|
|
|
|
- public boolean sendCode(SendCodeVO vo, HttpServletRequest request) {
|
|
|
- if (!RegexUtil.isPhoneLegal(vo.getPhoneNo())) {
|
|
|
+ public boolean sendCode(SendCodeDTO vo, HttpServletRequest request) {
|
|
|
+ if (!RegexUtil.isPhoneLegal(vo.getPhoneNum())) {
|
|
|
throw new BusinessException(ExceptionEnum.PHONE_FORMAT_ERROR);
|
|
|
}
|
|
|
//生成验证码
|
|
|
String random = RandomUtil.getSixRandom();
|
|
|
-
|
|
|
//手机号和验证码放进缓存 设置过期时间15m
|
|
|
- redisService. set(vo.getPhoneNo(), random);
|
|
|
- redisService.expire(vo.getPhoneNo(), CacheTTLEnum.FIFTEEN_MINUTE);
|
|
|
+ redisService. set(AppCacheKeyUtil.getLoginMessageCode(vo.getPhoneNum()), random);
|
|
|
+ redisService.expire(AppCacheKeyUtil.getLoginMessageCode(vo.getPhoneNum()), CacheTTLEnum.FIVE_MINUTE);
|
|
|
//发送短信
|
|
|
// smsService.sendMessage(vo.getPhoneNo(), random);
|
|
|
return true;
|
|
@@ -131,8 +168,24 @@ public class LoginService {
|
|
|
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
com.bjbz.common.jwt.JwtUserInfo jwtUserInfo = new com.bjbz.common.jwt.JwtUserInfo();
|
|
|
jwtUserInfo.setToken(uuid);
|
|
|
- String token = jwtTokenUtil.generateToken(jwtUserInfo.toJsonString(), jwtTokenUtil.getRandomKey());
|
|
|
- return token;
|
|
|
+ return jwtTokenUtil.generateToken(jwtUserInfo.toJsonString(), jwtTokenUtil.getRandomKey());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 退出登录
|
|
|
+ *
|
|
|
+ * @param dto
|
|
|
+ * @title: sendCode
|
|
|
+ * @author: gck
|
|
|
+ */
|
|
|
+ public boolean logout(PersonIdDTO dto) {
|
|
|
+ //获取token
|
|
|
+ Object obj = redisService.get(AppCacheKeyUtil.getUserIdToken(dto.getId()));
|
|
|
+ if (ToolUtil.isNotEmpty(obj)) {
|
|
|
+// redisService.delete(AppCacheKeyUtil.getTokenUserInfo(obj.toString()));
|
|
|
+ redisService.delete(AppCacheKeyUtil.getUserIdToken(dto.getId()));
|
|
|
+ }
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -143,24 +196,24 @@ public class LoginService {
|
|
|
* @param iv 加密算法的初始向量
|
|
|
* @return 解密后的用户数据
|
|
|
*/
|
|
|
-// private static String decrypt(String encryptedData, String sessionKey, String iv) {
|
|
|
-// byte[] encryptedDataByte = Base64.decodeBase64(encryptedData);
|
|
|
-// byte[] sessionKeyByte = Base64.decodeBase64(sessionKey);
|
|
|
-// byte[] ivByte = Base64.decodeBase64(iv);
|
|
|
-//
|
|
|
-// try {
|
|
|
-// Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
|
-// SecretKeySpec secretKeySpec = new SecretKeySpec(sessionKeyByte, "AES");
|
|
|
-// IvParameterSpec ivParameterSpec = new IvParameterSpec(ivByte);
|
|
|
-// cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
|
|
|
-//
|
|
|
-// byte[] decryptedDataByte = cipher.doFinal(encryptedDataByte);
|
|
|
-// return new String(decryptedDataByte);
|
|
|
-// } catch (Exception e) {
|
|
|
-// e.printStackTrace();
|
|
|
-// }
|
|
|
-// return null;
|
|
|
-// }
|
|
|
+ private static String decrypt(String encryptedData, String sessionKey, String iv) {
|
|
|
+ byte[] encryptedDataByte = Base64.decodeBase64(encryptedData);
|
|
|
+ byte[] sessionKeyByte = Base64.decodeBase64(sessionKey);
|
|
|
+ byte[] ivByte = Base64.decodeBase64(iv);
|
|
|
+
|
|
|
+ try {
|
|
|
+ Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
|
+ SecretKeySpec secretKeySpec = new SecretKeySpec(sessionKeyByte, "AES");
|
|
|
+ IvParameterSpec ivParameterSpec = new IvParameterSpec(ivByte);
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
|
|
|
+
|
|
|
+ byte[] decryptedDataByte = cipher.doFinal(encryptedDataByte);
|
|
|
+ return new String(decryptedDataByte);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/*public String getAccessToken() {
|