|
@@ -13,12 +13,17 @@ import com.zanxiang.game.module.mybatis.entity.GameExt;
|
|
|
import com.zanxiang.game.module.mybatis.entity.UserApplet;
|
|
|
import com.zanxiang.game.module.mybatis.mapper.UserAppletMapper;
|
|
|
import com.zanxiang.module.util.JsonUtil;
|
|
|
+import com.zanxiang.module.util.URIUtil;
|
|
|
import com.zanxiang.module.util.exception.BaseException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.logging.log4j.util.Strings;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
/**
|
|
@@ -36,6 +41,9 @@ public class UserAppletServiceImpl extends ServiceImpl<UserAppletMapper, UserApp
|
|
|
@Autowired
|
|
|
private IGameExtService gameExtService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
@Override
|
|
|
public boolean userAppletSubmit(UserAppletSubmitParam param) {
|
|
|
GameApplet gameApplet = gameAppletService.getOne(new LambdaQueryWrapper<GameApplet>()
|
|
@@ -47,13 +55,16 @@ public class UserAppletServiceImpl extends ServiceImpl<UserAppletMapper, UserApp
|
|
|
}
|
|
|
GameExt gameExt = gameExtService.getByGameId(gameApplet.getGameId());
|
|
|
this.signCheck(gameExt.getLoginKey(), param);
|
|
|
- return super.save(this.transform(param));
|
|
|
+ //获取用户小程序 openId
|
|
|
+ String openId = this.getAppletOpenId(param.getCode(), param.getAppId(), gameApplet.getAppSecret());
|
|
|
+ //数据保存且返回结果
|
|
|
+ return super.save(this.transform(param, openId));
|
|
|
}
|
|
|
|
|
|
- private UserApplet transform(UserAppletSubmitParam param) {
|
|
|
+ private UserApplet transform(UserAppletSubmitParam param, String openId) {
|
|
|
return UserApplet.builder()
|
|
|
.appId(param.getAppId())
|
|
|
- .openId(param.getOpenId())
|
|
|
+ .openId(openId)
|
|
|
.channel(param.getChannel())
|
|
|
.isDelete(DeleteEnum.NO.getCode())
|
|
|
.createTime(LocalDateTime.now())
|
|
@@ -63,11 +74,31 @@ public class UserAppletServiceImpl extends ServiceImpl<UserAppletMapper, UserApp
|
|
|
|
|
|
private void signCheck(String loginKey, UserAppletSubmitParam param) {
|
|
|
String signStr = "cpServerKey=" + loginKey + "appId=" + param.getAppId()
|
|
|
- + "openId=" + param.getOpenId() + "signTime=" + param.getSignTime();
|
|
|
+ + "code=" + param.getCode() + "signTime=" + param.getSignTime();
|
|
|
+ log.error("加密字符串 : signStr : {}", signStr);
|
|
|
String mySign = SignUtil.md5(signStr, Boolean.TRUE);
|
|
|
if (Objects.equals(mySign, param.getSign())) {
|
|
|
log.error("加密验证失败, sign : {}, mySign : {}", param.getSign(), mySign);
|
|
|
throw new BaseException("加密标识错误");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private String getAppletOpenId(String code, String appId, String secret) {
|
|
|
+ // 请求微信服务器,使用code获取openid
|
|
|
+ Map<String, String> paramMap = new HashMap<>(4);
|
|
|
+ paramMap.put("appid", appId);
|
|
|
+ paramMap.put("secret", secret);
|
|
|
+ paramMap.put("js_code", code);
|
|
|
+ paramMap.put("grant_type", "authorization_code");
|
|
|
+ // 发送请求
|
|
|
+ String url = URIUtil.fillUrlParams("https://api.weixin.qq.com/sns/jscode2session", paramMap, Boolean.FALSE);
|
|
|
+ String sr = restTemplate.getForObject(url, String.class);
|
|
|
+ // 解析相应内容(转换成json对象)
|
|
|
+ Map<String, String> resultMap = JsonUtil.toMap(sr, Map.class, String.class);
|
|
|
+ if (resultMap == null || Strings.isBlank(resultMap.get("openid"))) {
|
|
|
+ log.error("获取用户小程序/小游戏openId失败, appId : {}, resultMap : {}", appId, JsonUtil.toString(resultMap));
|
|
|
+ throw new BaseException("获取用户小程序/小游戏openId失败");
|
|
|
+ }
|
|
|
+ return resultMap.get("openid");
|
|
|
+ }
|
|
|
}
|