Browse Source

邀请

master
nili 1 year ago
parent
commit
780cb389e6
  1. 1
      game-dao/src/main/java/awesome/group/game/dao/bean/GameApp.java
  2. 13
      game-dao/src/main/java/awesome/group/game/dao/bean/InviteRecord.java
  3. 8
      game-dao/src/main/java/awesome/group/game/dao/bean/User.java
  4. 1
      game-dao/src/main/java/awesome/group/game/dao/bean/WeGameUser.java
  5. 13
      game-dao/src/main/java/awesome/group/game/dao/mapper/InviteRecordMapper.java
  6. 7
      game-dao/src/main/java/awesome/group/game/dao/mapper/UserMapper.java
  7. 1
      game-dao/src/main/java/awesome/group/game/dao/mapper/WeGameUserMapper.java
  8. 6
      game-service/pom.xml
  9. 16
      game-service/src/main/java/awesome/group/game/service/UserService.java
  10. 75
      game-service/src/main/java/awesome/group/game/service/WxService.java
  11. 6
      game-service/src/main/java/awesome/group/game/service/bo/LoginBo.java
  12. 10
      game-service/src/main/java/awesome/group/game/service/bo/UserInfo.java
  13. 2
      game-service/src/main/java/awesome/group/game/service/bo/WxUserInfo.java
  14. 4
      game-service/src/main/java/awesome/group/game/service/util/JwtUtils.java
  15. 27
      game-web/src/main/java/awesome/group/controller/LoginController.java
  16. 2
      game-web/src/main/java/awesome/group/interceptor/InterceptorConfig.java
  17. 9
      pom.xml

1
game-dao/src/main/java/awesome/group/game/dao/bean/GameApp.java

@ -1,7 +1,6 @@
package awesome.group.game.dao.bean; package awesome.group.game.dao.bean;
import lombok.Data; import lombok.Data;
import org.springframework.context.annotation.Bean;
@Data @Data
public class GameApp { public class GameApp {

13
game-dao/src/main/java/awesome/group/game/dao/bean/InviteRecord.java

@ -0,0 +1,13 @@
package awesome.group.game.dao.bean;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@Data
public class InviteRecord {
@TableId(value = "id", type = IdType.AUTO)
public Integer id;
public Integer userId;
public Integer invitedUserId;
}

8
game-dao/src/main/java/awesome/group/game/dao/bean/User.java

@ -1,8 +0,0 @@
package awesome.group.game.dao.bean;
import lombok.Data;
@Data
public class User {
private Integer id;
}

1
game-dao/src/main/java/awesome/group/game/dao/bean/WeGameUser.java

@ -20,6 +20,7 @@ public class WeGameUser {
private String unionId; private String unionId;
private String sessionKey; private String sessionKey;
private Integer score; private Integer score;
private String inviteCode;
public String getNickname() { public String getNickname() {
if (StringUtils.hasText(nickname)) { if (StringUtils.hasText(nickname)) {

13
game-dao/src/main/java/awesome/group/game/dao/mapper/InviteRecordMapper.java

@ -0,0 +1,13 @@
package awesome.group.game.dao.mapper;
import awesome.group.game.dao.bean.InviteRecord;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface InviteRecordMapper extends BaseMapper<InviteRecord> {
@Select("select invited_user_id from invite_record where user_id = #{userId}")
List<Integer> query(@Param("userId") Integer userId);
}

7
game-dao/src/main/java/awesome/group/game/dao/mapper/UserMapper.java

@ -1,7 +0,0 @@
package awesome.group.game.dao.mapper;
import awesome.group.game.dao.bean.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface UserMapper extends BaseMapper<User> {
}

1
game-dao/src/main/java/awesome/group/game/dao/mapper/WeGameUserMapper.java

@ -1,7 +1,6 @@
package awesome.group.game.dao.mapper; package awesome.group.game.dao.mapper;
import awesome.group.game.dao.bean.WeGameUser; import awesome.group.game.dao.bean.WeGameUser;
import awesome.group.game.dao.bean.WxUserInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;

6
game-service/pom.xml

@ -62,6 +62,12 @@
<artifactId>okhttp</artifactId> <artifactId>okhttp</artifactId>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
</dependencies> </dependencies>

16
game-service/src/main/java/awesome/group/game/service/UserService.java

@ -1,16 +0,0 @@
package awesome.group.game.service;
import awesome.group.game.dao.bean.User;
import awesome.group.game.dao.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Resource
private UserMapper userMapper;
public User getUser(Integer uid) {
return userMapper.selectById(uid);
}
}

75
game-service/src/main/java/awesome/group/game/service/WxService.java

@ -2,23 +2,26 @@ package awesome.group.game.service;
import awesome.group.game.dao.bean.GameApp; import awesome.group.game.dao.bean.GameApp;
import awesome.group.game.service.bo.CurrentUser; import awesome.group.game.dao.bean.InviteRecord;
import awesome.group.game.service.bo.RankList; import awesome.group.game.dao.mapper.InviteRecordMapper;
import awesome.group.game.service.bo.UserInfo; import awesome.group.game.service.bo.*;
import awesome.group.game.dao.bean.WeGameUser; import awesome.group.game.dao.bean.WeGameUser;
import awesome.group.game.dao.bean.WxUserInfo; import awesome.group.game.service.bo.WxUserInfo;
import awesome.group.game.dao.mapper.GameAppMapper; import awesome.group.game.dao.mapper.GameAppMapper;
import awesome.group.game.dao.mapper.WeGameUserMapper; import awesome.group.game.dao.mapper.WeGameUserMapper;
import awesome.group.game.service.bo.Code2SessionBo;
import awesome.group.game.service.common.exception.PaganiException; import awesome.group.game.service.common.exception.PaganiException;
import awesome.group.game.service.common.exception.PaganiExceptionCode; import awesome.group.game.service.common.exception.PaganiExceptionCode;
import awesome.group.game.service.util.EncryptUtil; import awesome.group.game.service.util.EncryptUtil;
import awesome.group.game.service.util.HttpUtils; import awesome.group.game.service.util.HttpUtils;
import awesome.group.game.service.util.JwtUtils; import awesome.group.game.service.util.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.gson.Gson; import com.google.gson.Gson;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
@ -34,12 +37,39 @@ public class WxService {
@Resource @Resource
private GameAppMapper gameAppMapper; private GameAppMapper gameAppMapper;
@Resource
private InviteRecordMapper inviteRecordMapper;
public CurrentUser currentUser(Integer userId) { public CurrentUser currentUser(Integer userId) {
WeGameUser user = weGameUserMapper.selectById(userId); WeGameUser user = weGameUserMapper.selectById(userId);
return new CurrentUser(user); return new CurrentUser(user);
} }
public String login(String code, String appId) { public void setInviteCode() {
List<WeGameUser> list = weGameUserMapper.selectList(Wrappers.emptyWrapper());
list.forEach(x -> {
x.setInviteCode(RandomStringUtils.randomAlphanumeric(10));
weGameUserMapper.updateById(x);
});
InviteRecord record = new InviteRecord();
record.setUserId(1000003);
record.setInvitedUserId(1000001);
inviteRecordMapper.insert(record);
}
public List<UserInfo> myInviteRecord(Integer userId) {
List<Integer> list = inviteRecordMapper.query(userId);
if (CollectionUtils.isEmpty(list)) {
return new ArrayList<>();
}
LambdaQueryWrapper<WeGameUser> query = Wrappers.lambdaQuery();
query.in(WeGameUser::getId, list);
List<WeGameUser> data = weGameUserMapper.selectList(query);
return data.stream().map(UserInfo::new).toList();
}
public WeGameUser login(String code, String appId) {
Code2SessionBo data = getSession(code, appId); Code2SessionBo data = getSession(code, appId);
if (data == null || data.openid == null) { if (data == null || data.openid == null) {
throw new PaganiException(PaganiExceptionCode.GENERAL_ERROR, "登陆失败,非法code"); throw new PaganiException(PaganiExceptionCode.GENERAL_ERROR, "登陆失败,非法code");
@ -51,13 +81,46 @@ public class WxService {
u.setAppId(appId); u.setAppId(appId);
u.setUnionId(data.unionid); u.setUnionId(data.unionid);
u.setSessionKey(data.session_key); u.setSessionKey(data.session_key);
u.setInviteCode(RandomStringUtils.randomAlphanumeric(10));
weGameUserMapper.insert(u); weGameUserMapper.insert(u);
} else { } else {
weGameUserMapper.updateSessionKey(data.session_key, u.getId()); weGameUserMapper.updateSessionKey(data.session_key, u.getId());
} }
return u;
}
public String loginV1(String code, String appId) {
WeGameUser u = login(code, appId);
return JwtUtils.generatorToken(u.getId() + "", 20 * 86400); return JwtUtils.generatorToken(u.getId() + "", 20 * 86400);
} }
public LoginBo loginV2(String code, String appId, String inviteCode) {
WeGameUser u = login(code, appId);
LoginBo res = new LoginBo();
res.inviteCode = u.getInviteCode();
res.jwtToken = JwtUtils.generatorToken(u.getId() + "", 20 * 86400);
if (!StringUtils.hasText(inviteCode)) {
return res;
}
LambdaQueryWrapper<WeGameUser> query = Wrappers.lambdaQuery();
query.eq(WeGameUser::getInviteCode, inviteCode);
WeGameUser user = weGameUserMapper.selectOne(query);
if (user == null || user.getId().equals(u.getId())) {
return res;
}
LambdaQueryWrapper<InviteRecord> inviteQuery = Wrappers.lambdaQuery();
inviteQuery.eq(InviteRecord::getInvitedUserId, u.getId());
if (inviteRecordMapper.selectOne(inviteQuery) != null) {
return res;
}
InviteRecord record = new InviteRecord();
record.userId = user.getId();
record.invitedUserId = u.getId();
inviteRecordMapper.insert(record);
return res;
}
public void updateUserInfo(Integer userId, String rawData, String signature) { public void updateUserInfo(Integer userId, String rawData, String signature) {
if (!verify(userId, rawData, signature)) { if (!verify(userId, rawData, signature)) {
throw new PaganiException(PaganiExceptionCode.GENERAL_ERROR, "签名有误"); throw new PaganiException(PaganiExceptionCode.GENERAL_ERROR, "签名有误");

6
game-service/src/main/java/awesome/group/game/service/bo/LoginBo.java

@ -0,0 +1,6 @@
package awesome.group.game.service.bo;
public class LoginBo {
public String jwtToken;
public String inviteCode;
}

10
game-service/src/main/java/awesome/group/game/service/bo/UserInfo.java

@ -1,5 +1,6 @@
package awesome.group.game.service.bo; package awesome.group.game.service.bo;
import awesome.group.game.dao.bean.WeGameUser;
import lombok.Data; import lombok.Data;
@Data @Data
@ -7,4 +8,13 @@ public class UserInfo {
public String avatarUrl; public String avatarUrl;
public String nickName; public String nickName;
public Integer score; public Integer score;
public UserInfo() {
}
public UserInfo(WeGameUser user) {
this.avatarUrl = user.getAvatarUrl();
this.nickName = user.getNickname();
this.score = user.getScore();
}
} }

2
game-dao/src/main/java/awesome/group/game/dao/bean/WxUserInfo.java → game-service/src/main/java/awesome/group/game/service/bo/WxUserInfo.java

@ -1,4 +1,4 @@
package awesome.group.game.dao.bean; package awesome.group.game.service.bo;
import lombok.Data; import lombok.Data;

4
game-service/src/main/java/awesome/group/game/service/util/JwtUtils.java

@ -17,6 +17,10 @@ public class JwtUtils {
private static final String JWT_KEY_USERID = "userId"; private static final String JWT_KEY_USERID = "userId";
public static void main(String[] args) {
System.out.println(generatorToken("1000001", 20 * 86400));
}
public static String generatorToken(String userId, long expireSeconds) { public static String generatorToken(String userId, long expireSeconds) {
LocalDateTime localDateTimeNow = LocalDateTime.now(); LocalDateTime localDateTimeNow = LocalDateTime.now();
LocalDateTime localDateTimeExpire = localDateTimeNow.plusSeconds(expireSeconds); LocalDateTime localDateTimeExpire = localDateTimeNow.plusSeconds(expireSeconds);

27
game-web/src/main/java/awesome/group/controller/LoginController.java

@ -4,11 +4,15 @@ import awesome.group.RequestContext;
import awesome.group.aop.RestApi; import awesome.group.aop.RestApi;
import awesome.group.game.service.WxService; import awesome.group.game.service.WxService;
import awesome.group.game.service.bo.CurrentUser; import awesome.group.game.service.bo.CurrentUser;
import awesome.group.game.service.bo.LoginBo;
import awesome.group.game.service.bo.RankList; import awesome.group.game.service.bo.RankList;
import awesome.group.game.service.bo.UserInfo;
import awesome.group.game.service.common.response.R; import awesome.group.game.service.common.response.R;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController @RestController
@RequestMapping("/api/game/auth") @RequestMapping("/api/game/auth")
public class LoginController { public class LoginController {
@ -32,9 +36,30 @@ public class LoginController {
@PostMapping("/login") @PostMapping("/login")
@RestApi @RestApi
public R<String> wxLogin(@RequestParam String code, @RequestParam String appId) { public R<String> wxLogin(@RequestParam String code, @RequestParam String appId) {
return new R<>(R.CODE_SUCCESS, "", wxService.login(code, appId)); return new R<>(R.CODE_SUCCESS, "", wxService.loginV1(code, appId));
}
@PostMapping("/loginV2")
@RestApi
public R<LoginBo> wxLoginV2(@RequestParam String code, @RequestParam String appId, @RequestParam(required = false) String inviteCode) {
return new R<>(R.CODE_SUCCESS, "", wxService.loginV2(code, appId, inviteCode));
} }
@GetMapping("/test")
@RestApi
public R<Void> setInviteCode() {
wxService.setInviteCode();
return new R<>(R.CODE_SUCCESS, "", null);
}
@GetMapping("/myInviteRecord")
@RestApi
public R<List<UserInfo>> myInviteRecord() {
Integer userId = RequestContext.getUid();
return new R<>(R.CODE_SUCCESS, "", wxService.myInviteRecord(userId));
}
/** /**
* 更新用户信息 * 更新用户信息
* 必须先登陆需在login调用30分钟内 * 必须先登陆需在login调用30分钟内

2
game-web/src/main/java/awesome/group/interceptor/InterceptorConfig.java

@ -21,6 +21,6 @@ public class InterceptorConfig implements WebMvcConfigurer {
.addPathPatterns("/**") .addPathPatterns("/**")
//不拦截的路径 //不拦截的路径
.excludePathPatterns("/api/game/auth/login") .excludePathPatterns("/api/game/auth/login")
.excludePathPatterns("/api/game/user/demo"); .excludePathPatterns("/api/game/auth/test");
} }
} }

9
pom.xml

@ -45,6 +45,15 @@
<version>4.11.0</version> <version>4.11.0</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>

Loading…
Cancel
Save