|
@@ -1,17 +1,12 @@
|
|
|
package com.futu.course.user.service.impl;
|
|
|
|
|
|
-<<<<<<< HEAD
|
|
|
+
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.futu.course.user.domain.User;
|
|
|
import com.futu.course.user.service.UserService;
|
|
|
import com.futu.course.user.mapper.UserMapper;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-/**
|
|
|
-* @author yuu
|
|
|
-* @description 针对表【user(用户表)】的数据库操作Service实现
|
|
|
-* @createDate 2025-05-05 18:52:53
|
|
|
-=======
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
@@ -50,6 +45,12 @@ import java.nio.charset.StandardCharsets;
|
|
|
import java.util.List;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
+
|
|
|
+/**
|
|
|
+* @author yuu
|
|
|
+* @description 针对表【user(用户表)】的数据库操作Service实现
|
|
|
+* @createDate 2025-05-05 18:52:53
|
|
|
+=======
|
|
|
/**
|
|
|
* @author yuu
|
|
|
* @description 针对表【user(用户表)】的数据库操作Service实现
|
|
@@ -58,11 +59,8 @@ import java.util.concurrent.TimeUnit;
|
|
|
*/
|
|
|
@Service
|
|
|
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
|
- implements UserService{
|
|
|
-<<<<<<< HEAD
|
|
|
+ implements UserService {
|
|
|
|
|
|
-}
|
|
|
-=======
|
|
|
@Autowired
|
|
|
private UserMapper userMapper;
|
|
|
|
|
@@ -75,12 +73,13 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
|
private final static String APP_SECRET = "60efa9e4ff7fae35c7f561de7763da7a";
|
|
|
// 微信小程序登录校验请求地址
|
|
|
private final static String LOGIN_URL = "https://api.weixin.qq.com/sns/jscode2session";
|
|
|
+
|
|
|
@Override
|
|
|
public R appLogin(UserDTO dto) {
|
|
|
|
|
|
- SnowflakeIdWorker worker=new SnowflakeIdWorker(1,1);
|
|
|
+ SnowflakeIdWorker worker = new SnowflakeIdWorker(1, 1);
|
|
|
System.out.println(dto);
|
|
|
- String url = LOGIN_URL + "?appid=" + APP_ID + "&secret="+ APP_SECRET + "&grant_type=authorization_code&js_code=" + dto.getCode();
|
|
|
+ String url = LOGIN_URL + "?appid=" + APP_ID + "&secret=" + APP_SECRET + "&grant_type=authorization_code&js_code=" + dto.getCode();
|
|
|
HttpClient client = HttpClients.createDefault(); // 创建默认http连接
|
|
|
HttpGet getRequest = new HttpGet(url);// 创建一个post请求
|
|
|
LoginDTO loginDTO = new LoginDTO();
|
|
@@ -92,13 +91,13 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
|
// 把响应实体转成文本
|
|
|
String html = EntityUtils.toString(entity);
|
|
|
loginDTO = JSON.parseObject(html, LoginDTO.class);
|
|
|
- if(null == loginDTO.getErrCode()) {
|
|
|
+ if (null == loginDTO.getErrCode()) {
|
|
|
dto.setWxId(loginDTO.getOpenid());
|
|
|
} else {
|
|
|
- return R.restResult(loginDTO,loginDTO.getErrCode(),loginDTO.getErrMsg());
|
|
|
+ return R.restResult(loginDTO, loginDTO.getErrCode(), loginDTO.getErrMsg());
|
|
|
}
|
|
|
List<User> users = userMapper.selectList(new LambdaQueryWrapper<User>().eq(User::getOpenId, dto.getWxId()));
|
|
|
- if(users.size() > 0) {
|
|
|
+ if (users.size() > 0) {
|
|
|
User user = users.get(0);
|
|
|
// 生成一个 256 位(32 字节)的安全密钥
|
|
|
SecretKey key = Keys.secretKeyFor(SignatureAlgorithm.HS256);
|
|
@@ -108,10 +107,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
|
.signWith(key)
|
|
|
.compact();
|
|
|
user.setToken(jws);
|
|
|
- redisTemplate.opsForValue().set("user",user,1, TimeUnit.DAYS);
|
|
|
+ redisTemplate.opsForValue().set("user", user, 1, TimeUnit.DAYS);
|
|
|
return R.ok(user);
|
|
|
- }
|
|
|
- else {
|
|
|
+ } else {
|
|
|
User user = new User();
|
|
|
String sessionKey = loginDTO.getSession_key();
|
|
|
|
|
@@ -130,10 +128,10 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
|
user.setSessionKey(loginDTO.getSession_key());
|
|
|
user.setUnionId(dto.getWxId());
|
|
|
user.setAvatar(dto.getWxHeadPic());
|
|
|
- user.setUsername(dto.getWxUsername());
|
|
|
- user.setNickname(dto.getWxUsername());
|
|
|
- user.setId(worker.nextId());
|
|
|
- user.setPassword(null);
|
|
|
+ user.setUsername(dto.getWxUsername());
|
|
|
+ user.setNickname(dto.getWxUsername());
|
|
|
+ user.setId(worker.nextId());
|
|
|
+ user.setPassword(null);
|
|
|
// 生成一个 256 位(32 字节)的安全密钥
|
|
|
SecretKey key = Keys.secretKeyFor(SignatureAlgorithm.HS256);
|
|
|
// 使用密钥生成 JWT
|
|
@@ -142,11 +140,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
|
.signWith(key)
|
|
|
.compact();
|
|
|
user.setToken(jws);
|
|
|
- userMapper.insert(user);
|
|
|
- redisTemplate.opsForValue().set("user",user,1, TimeUnit.DAYS);
|
|
|
- return R.ok(user);
|
|
|
- }
|
|
|
- } catch (ClientProtocolException e) {
|
|
|
+ userMapper.insert(user);
|
|
|
+ redisTemplate.opsForValue().set("user", user, 1, TimeUnit.DAYS);
|
|
|
+ return R.ok(user);
|
|
|
+ }
|
|
|
+ } catch (ClientProtocolException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
} catch (Exception e) {
|
|
|
throw new RuntimeException(e);
|
|
@@ -156,8 +154,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
|
|
|
|
@Override
|
|
|
public R logout(UserDTO dto) {
|
|
|
- if (redisTemplate.opsForValue().get("user") != null)
|
|
|
- {
|
|
|
+ if (redisTemplate.opsForValue().get("user") != null) {
|
|
|
redisTemplate.delete("user");
|
|
|
}
|
|
|
return R.ok("登出成功");
|
|
@@ -180,9 +177,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
|
byte[] decryptedBytes = cipher.doFinal(encryptedDataBytes);
|
|
|
return new String(decryptedBytes, StandardCharsets.UTF_8);
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- }
|
|
|
->>>>>>> bba2fa3 ("微信一键登录实现")
|
|
|
|
|
|
|
|
|
|