Skip to content

Commit

Permalink
aaa
Browse files Browse the repository at this point in the history
  • Loading branch information
Qi-Zhen-xin committed Mar 30, 2024
1 parent 501c630 commit aed0750
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ public R<String> acceptFriend(@Validated @RequestParam @NotNull Long friendId, @
//查看自己的所有好友
public R<List<UserVo>> allFriends() {
List<Friend> friends = friendService.query().eq("user_id", UserHolder.getUserId()).list();
return new R<>(ResultCode.SUCCESS, friends.stream().filter(f -> friendService.judgeIfFriend(f.getFriendId())).map(friend -> {
return new R<>(ResultCode.SUCCESS,
friends.stream().filter(f -> friendService.judgeIfFriend(f.getFriendId())).map(friend -> {
UserVo userVoById = userService.getUserVoById(friend.getFriendId());
String alterName = friend.getAlterName();
if (StrUtil.isNotBlank(alterName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ public class WechatController {
* 注册接口
*/
@GetMapping("/register")
public R<WxMaJscode2SessionResult> registor(@RequestParam("code") String code, @RequestParam("chsiCode") String chsiCode) {
public R<WxMaJscode2SessionResult> registor(@RequestParam("code") String code,
@RequestParam("chsiCode") String chsiCode) {
return new R<>(ResultCode.SUCCESS, userInfoService.register(code, chsiCode));
}

/**
* login接口
*/
@GetMapping("/login")
public R<WxMaJscode2SessionResult> registor(@RequestParam("code") String code) {
return new R<>(ResultCode.SUCCESS, userInfoService.login(code));
}

/**
* <pre>
* 获取用户信息接口
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/qzx/xdupartner/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ public class User implements Serializable {
*/
private String openId;

/**
/**
* sessionKey
*/
@TableField(exist = false)
@TableField(exist = false)
private String sessionKey;

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/qzx/xdupartner/entity/vo/UserVo.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class UserVo {
private Long id;
private String icon;
private String nickName;

public UserVo(String icon, String nickName) {
this.icon = icon;
this.nickName = nickName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class ConnectHandler {
StringRedisTemplate stringRedisTemplate;

public void execute(ReqMessage reqMessage, ChannelHandlerContext ctx) {
User user = JSONUtil.toBean(stringRedisTemplate.opsForValue().get(RedisConstant.LOGIN_PREFIX + reqMessage.getToken()), User.class);
User user =
JSONUtil.toBean(stringRedisTemplate.opsForValue().get(RedisConstant.LOGIN_PREFIX + reqMessage.getToken()), User.class);
if (!reqMessage.getFromId().equals(user.getId())) {
ctx.channel().writeAndFlush(RspMessage.getSystemTextFrame("token有误"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
public class WebsocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
@Resource
ConnectHandler connectHandler;

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
Expand Down
15 changes: 2 additions & 13 deletions src/main/java/com/qzx/xdupartner/service/UserInfoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,10 @@

public interface UserInfoService {

/**
* 登录
*
* @param code code
* @param chsiCode 学信网认证码
* @return WxMaJscode2SessionResult
*/
WxMaJscode2SessionResult register(String code, String chsiCode);

/**
* 获取用户信息
*
* @param userInfo 包含一些加密的信息
* @return WxMaUserInfo
*/
WxMaJscode2SessionResult login(String code);

WxMaUserInfo getUserInfo(WxUserInfo userInfo);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ExecutorService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,41 @@ public WxMaJscode2SessionResult register(String code, String chsiCode) {
return null;
}
//TODO 可以增加自己的逻辑,关联业务相关数据
User user = userService.insertNewUser(wxMaJscode2SessionResult.getOpenid(), schoolInfoDto.getStuId());
User user = userService.insertNewUser(wxMaJscode2SessionResult.getOpenid(), schoolInfoDto.getStuId());
user.setSessionKey(session.get().getSessionKey());
User finalUser = user;
executor.submit(() -> stringRedisTemplate.opsForValue().set(RedisConstant.LOGIN_PREFIX + session.get().getSessionKey(), JSONUtil.toJsonStr(finalUser),
stringRedisTemplate.opsForValue().set(RedisConstant.LOGIN_PREFIX + session.get().getSessionKey(),
JSONUtil.toJsonStr(user),
RedisConstant.LOGIN_VALID_TTL,
TimeUnit.DAYS));
TimeUnit.DAYS);
return wxMaJscode2SessionResult;
}

@Override
public WxMaJscode2SessionResult login(String code) {
try {
WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(code);
log.info(session.getSessionKey());
log.info(session.getOpenid());
//TODO 可以增加自己的逻辑,关联业务相关数据
User user = userService.lambdaQuery().eq(User::getOpenId, session.getOpenid()).one();
if (ObjectUtil.isNull(user)) {
return null;
}
user.setSessionKey(session.getSessionKey());
User finalUser = user;
stringRedisTemplate.opsForValue().set(RedisConstant.LOGIN_PREFIX + session.getSessionKey(),
JSONUtil.toJsonStr(finalUser),
RedisConstant.LOGIN_VALID_TTL,
TimeUnit.DAYS);
return session;
} catch (WxErrorException e) {
log.error(e.getMessage(), e);
return null;
} finally {
WxMaConfigHolder.remove();//清理ThreadLocal
}
}

@Override
public WxMaUserInfo getUserInfo(WxUserInfo userInfo) {

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qzx/xdupartner/util/UserHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class UserHolder {
private static final ThreadLocal<User> tl = new ThreadLocal<>();
private static AtomicLong anyId = new AtomicLong(Long.MIN_VALUE);
private static final AtomicLong anyId = new AtomicLong(Long.MIN_VALUE);

public static void saveUser(User user) {
tl.set(user);
Expand Down

0 comments on commit aed0750

Please sign in to comment.