Browse Source

绑定微信

master
nili 7 months ago
parent
commit
f4ff4d6e0c
  1. 1
      game-dao/src/main/java/awesome/group/game/dao/bean/MatrixUser.java
  2. 3
      game-dao/src/main/java/awesome/group/game/dao/mapper/MatrixUserMapper.java
  3. 12
      game-service/src/main/java/awesome/group/game/service/bo/citrus/WxAuth.java
  4. 18
      game-service/src/main/java/awesome/group/game/service/bo/citrus/WxUserInfo.java
  5. 63
      game-service/src/main/java/awesome/group/game/service/citrus/WeiXinService.java
  6. 3
      game-service/src/main/java/awesome/group/game/service/util/HttpUtils.java
  7. 15
      game-service/src/test/java/awesome/group/game/service/citrus/WeiXinServiceTest.java
  8. 11
      game-web/src/main/java/awesome/group/game/web/rest/citrus/UserController.java

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

@ -16,6 +16,7 @@ public class MatrixUser {
private String aliPayAccount; private String aliPayAccount;
private String nickname; private String nickname;
private String avatar; private String avatar;
private String wxOpenId;
private Integer money;//分,已提现金额 private Integer money;//分,已提现金额
private String pwd; private String pwd;
private String inviteCode; private String inviteCode;

3
game-dao/src/main/java/awesome/group/game/dao/mapper/MatrixUserMapper.java

@ -21,4 +21,7 @@ public interface MatrixUserMapper extends BaseMapper<MatrixUser> {
@Update("update matrix_user set ali_pay_account = #{aliPayAccount}, name = #{name} where id = #{userId}") @Update("update matrix_user set ali_pay_account = #{aliPayAccount}, name = #{name} where id = #{userId}")
int updateAliPayAccount(int userId, String aliPayAccount, String name); int updateAliPayAccount(int userId, String aliPayAccount, String name);
@Update("update matrix_user set wx_open_id = #{wxOpenId}, nickname = #{nickname}, avatar = #{avatar} where id = #{userId}")
int updateWx(String wxOpenId, String nickname, String avatar, int userId);
} }

12
game-service/src/main/java/awesome/group/game/service/bo/citrus/WxAuth.java

@ -0,0 +1,12 @@
package awesome.group.game.service.bo.citrus;
public class WxAuth {
public String access_token;
public Integer expires_in;
public String refresh_token;
public String openid;
public String scope;
public String unionid;
public Integer errcode;
public String errmsg;
}

18
game-service/src/main/java/awesome/group/game/service/bo/citrus/WxUserInfo.java

@ -0,0 +1,18 @@
package awesome.group.game.service.bo.citrus;
import java.util.List;
public class WxUserInfo {
public String openid;
public String nickname;
public Integer sex;
public String province;
public String city;
public String country;
public String headimgurl;
public List<String> privilege;
public String unionid;
public Integer errcode;
public String errmsg;
}

63
game-service/src/main/java/awesome/group/game/service/citrus/WeiXinService.java

@ -0,0 +1,63 @@
package awesome.group.game.service.citrus;
import awesome.group.game.dao.bean.MatrixUser;
import awesome.group.game.dao.mapper.MatrixAppMapper;
import awesome.group.game.dao.mapper.MatrixUserMapper;
import awesome.group.game.service.bo.citrus.WxAuth;
import awesome.group.game.service.bo.citrus.WxUserInfo;
import awesome.group.game.service.bo.matrix.WxConfig;
import awesome.group.game.service.common.exception.PaganiException;
import awesome.group.game.service.common.exception.PaganiExceptionCode;
import awesome.group.game.service.util.HttpUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@Service
public class WeiXinService {
@Autowired
private MatrixAppMapper appMapper;
@Autowired
private MatrixUserMapper userMapper;
private Gson gson = new Gson();
public void bindWx(Integer userId, String accessToken, String openId) {
MatrixUser user = userMapper.selectById(userId);
if (StringUtils.hasText(user.getWxOpenId()) && !user.getWxOpenId().equals(openId)) {
throw new PaganiException(PaganiExceptionCode.GENERAL_ERROR, "账号已绑定微信,不可换绑");
}
WxUserInfo userInfo = getWxUserInfo(accessToken, openId);
LambdaQueryWrapper<MatrixUser> query = Wrappers.lambdaQuery();
query.eq(MatrixUser::getWxOpenId, userInfo.openid);
query.eq(MatrixUser::getAppId, user.getAppId());
MatrixUser exist = userMapper.selectOne(query);
if (exist != null && !exist.getId().equals(userId)) {
throw new PaganiException(PaganiExceptionCode.GENERAL_ERROR, "该微信已绑定其他账号");
}
userMapper.updateWx(userInfo.openid, userInfo.nickname, userInfo.headimgurl, userId);
}
public WxAuth getWxAuth(String code, WxConfig config) {
String api = String.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", config.appId, config.appSecret, code);
WxAuth auth = HttpUtils.get(api, null, null, WxAuth.class);
if (auth.errcode != null) {
throw new PaganiException(PaganiExceptionCode.GENERAL_ERROR, "微信授权失败," + auth.errmsg);
}
return auth;
}
public WxUserInfo getWxUserInfo(String accessToken, String openId) {
String api = String.format("https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s", accessToken, openId);
WxUserInfo userInfo = HttpUtils.get(api, null, null, WxUserInfo.class);
if (userInfo.errcode != null) {
throw new PaganiException(PaganiExceptionCode.GENERAL_ERROR, "获取微信用户信息失败," + userInfo.errmsg);
}
return userInfo;
}
}

3
game-service/src/main/java/awesome/group/game/service/util/HttpUtils.java

@ -64,9 +64,12 @@ public class HttpUtils {
public static <T> T get(String url, Map<String, String> queryStr, Map<String, String> headers, Class<T> dataType) { public static <T> T get(String url, Map<String, String> queryStr, Map<String, String> headers, Class<T> dataType) {
HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder(); HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
if (queryStr != null) {
for (Map.Entry<String, String> kv : queryStr.entrySet()) { for (Map.Entry<String, String> kv : queryStr.entrySet()) {
urlBuilder.addQueryParameter(kv.getKey(), kv.getValue()); urlBuilder.addQueryParameter(kv.getKey(), kv.getValue());
} }
}
Request.Builder requestBuilder = new Request.Builder().get().url(urlBuilder.build()); Request.Builder requestBuilder = new Request.Builder().get().url(urlBuilder.build());
if (!CollectionUtils.isEmpty(headers)) { if (!CollectionUtils.isEmpty(headers)) {
headers.forEach(requestBuilder::addHeader); headers.forEach(requestBuilder::addHeader);

15
game-service/src/test/java/awesome/group/game/service/citrus/WeiXinServiceTest.java

@ -0,0 +1,15 @@
package awesome.group.game.service.citrus;
import awesome.group.game.service.BaseTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
class WeiXinServiceTest extends BaseTest {
@Autowired
private WeiXinService weiXinService;
@Test
void bindWx() {
weiXinService.bindWx(2, "80_XU6xni5qtJDwumDWJZtgBaJ_x7ptTTcl3eF4gmuKbHTYrKklgVjgo2VnTijhpQJVhAvm-QlCF_wqjSOBpZYrT1x51Vs_4BCYzdFHqpR2nKM", "ojV4f6y86g6Hpvc6CYIx-OkHMmto");
}
}

11
game-web/src/main/java/awesome/group/game/web/rest/citrus/UserController.java

@ -5,6 +5,7 @@ import awesome.group.game.service.bo.citrus.CashRecord;
import awesome.group.game.service.bo.citrus.UserBo; import awesome.group.game.service.bo.citrus.UserBo;
import awesome.group.game.service.citrus.AdvRecordService; import awesome.group.game.service.citrus.AdvRecordService;
import awesome.group.game.service.citrus.UserService; import awesome.group.game.service.citrus.UserService;
import awesome.group.game.service.citrus.WeiXinService;
import awesome.group.game.service.common.response.R; import awesome.group.game.service.common.response.R;
import awesome.group.game.web.RequestContext; import awesome.group.game.web.RequestContext;
import awesome.group.game.web.aop.RestApi; import awesome.group.game.web.aop.RestApi;
@ -23,6 +24,9 @@ public class UserController {
@Autowired @Autowired
private AdvRecordService advRecordService; private AdvRecordService advRecordService;
@Autowired
private WeiXinService weiXinService;
@GetMapping("/current") @GetMapping("/current")
@ -57,4 +61,11 @@ public class UserController {
Long res = advRecordService.saveRecordV3(RequestContext.getCitrusUid(), bo, RequestContext.getIpAddr(RequestContext.getRequest())); Long res = advRecordService.saveRecordV3(RequestContext.getCitrusUid(), bo, RequestContext.getIpAddr(RequestContext.getRequest()));
return new R<>(res); return new R<>(res);
} }
@PostMapping("/bindWx")
@RestApi
public R<Void> bindWx(@RequestParam String accessToken, @RequestParam String openId) {
weiXinService.bindWx(RequestContext.getCitrusUid(), accessToken, openId);
return new R<>(null);
}
} }

Loading…
Cancel
Save