nili
7 months ago
8 changed files with 129 additions and 3 deletions
@ -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; |
|||
} |
@ -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; |
|||
} |
@ -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; |
|||
} |
|||
|
|||
} |
@ -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"); |
|||
} |
|||
} |
Loading…
Reference in new issue