|
@@ -1,5 +1,7 @@
|
|
|
package com.zanxiang.game.module.manage.service.impl;
|
|
|
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.zanxiang.game.module.base.utils.BeanUtils;
|
|
@@ -9,9 +11,11 @@ import com.zanxiang.game.module.manage.pojo.params.GameAccountUpdateParam;
|
|
|
import com.zanxiang.game.module.manage.pojo.vo.GameAccountVO;
|
|
|
import com.zanxiang.game.module.manage.service.IGameAppletService;
|
|
|
import com.zanxiang.game.module.manage.service.IGameService;
|
|
|
-import com.zanxiang.game.module.manage.service.api.MiniAppletApiService;
|
|
|
import com.zanxiang.game.module.mybatis.entity.GameApplet;
|
|
|
import com.zanxiang.game.module.mybatis.mapper.GameAppletMapper;
|
|
|
+import com.zanxiang.module.oss.service.IOssService;
|
|
|
+import com.zanxiang.module.util.JsonUtil;
|
|
|
+import com.zanxiang.module.util.URIUtil;
|
|
|
import com.zanxiang.module.util.bean.BeanUtil;
|
|
|
import com.zanxiang.module.util.exception.BaseException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -19,7 +23,14 @@ import org.apache.logging.log4j.util.Strings;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.io.PrintWriter;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
/**
|
|
|
* @author : lingfeng
|
|
@@ -31,10 +42,10 @@ import java.time.LocalDateTime;
|
|
|
public class GameAppletServiceImpl extends ServiceImpl<GameAppletMapper, GameApplet> implements IGameAppletService {
|
|
|
|
|
|
@Autowired
|
|
|
- private MiniAppletApiService miniAppletApiService;
|
|
|
+ private IGameService gameService;
|
|
|
|
|
|
@Autowired
|
|
|
- private IGameService gameService;
|
|
|
+ private IOssService ossService;
|
|
|
|
|
|
@Override
|
|
|
public GameAppletDTO getByGameId(Long gameId) {
|
|
@@ -80,7 +91,7 @@ public class GameAppletServiceImpl extends ServiceImpl<GameAppletMapper, GameApp
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String getQrCod(Long gameId) {
|
|
|
+ public String getQrCodByGameId(Long gameId) {
|
|
|
GameDTO gameDTO = gameService.getById(gameId);
|
|
|
if (gameDTO == null) {
|
|
|
throw new BaseException("游戏不存在");
|
|
@@ -89,6 +100,101 @@ public class GameAppletServiceImpl extends ServiceImpl<GameAppletMapper, GameApp
|
|
|
if (gameApplet == null || Strings.isBlank(gameApplet.getAppId()) || Strings.isBlank(gameApplet.getAppSecret())) {
|
|
|
throw new BaseException("该游戏未设置小程序信息");
|
|
|
}
|
|
|
- return miniAppletApiService.getQrCode(gameApplet.getAppId(), gameApplet.getAppSecret(), gameDTO.getName());
|
|
|
+ return this.getQrCode(gameApplet.getAppId(), gameApplet.getAppSecret(), gameDTO.getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取游戏小程序二维码
|
|
|
+ *
|
|
|
+ * @param appId : 小程序appId
|
|
|
+ * @param appSecret : 小程序密钥
|
|
|
+ * @param ossFileName : oss文件名
|
|
|
+ * @return : 返回oss二维码图片地址
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String getQrCode(String appId, String appSecret, String ossFileName) {
|
|
|
+ if (Strings.isBlank(appId) || Strings.isBlank(appSecret)) {
|
|
|
+ throw new BaseException("参数错误, 缺少小程序信息");
|
|
|
+ }
|
|
|
+ //oss文件名
|
|
|
+ ossFileName = Strings.isBlank(ossFileName) ? UUID.randomUUID().toString() + ".jpg" : ossFileName + ".jpg";
|
|
|
+ //获取小程序token
|
|
|
+ String token = this.getAppletTokenApi(appId, appSecret);
|
|
|
+ //获取小程序二维码
|
|
|
+ BufferedInputStream inputStream = this.getQrCodeApi(null, token);
|
|
|
+ //二维码上传到oss, 返回网络地址
|
|
|
+ return ossService.upload(ossFileName, inputStream);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取小程序token
|
|
|
+ *
|
|
|
+ * @param appId : 小程序appId
|
|
|
+ * @param secret : 小程序密钥
|
|
|
+ * @return : 返回小程token
|
|
|
+ */
|
|
|
+ private String getAppletTokenApi(String appId, String secret) {
|
|
|
+ Map<String, String> paramMap = new HashMap<>(3);
|
|
|
+ paramMap.put("grant_type", "client_credential");
|
|
|
+ paramMap.put("appid", appId);
|
|
|
+ paramMap.put("secret", secret);
|
|
|
+ String url = "https://api.weixin.qq.com/cgi-bin/token";
|
|
|
+ //拼接url请求参数
|
|
|
+ String uri = URIUtil.fillUrlParams(url, paramMap, Boolean.TRUE);
|
|
|
+ // 带参GET请求
|
|
|
+ Map<String, String> resultMap = new HashMap<>(4);
|
|
|
+ try {
|
|
|
+ String result = HttpUtil.get(uri);
|
|
|
+ if (Strings.isNotBlank(result)) {
|
|
|
+ resultMap = JsonUtil.toMap(result, Map.class, String.class, String.class);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取小程序token出现异常, appId : {},secret: {} e : {}", appId, secret, e.getMessage());
|
|
|
+ throw new BaseException("获取小程序token出现异常");
|
|
|
+ }
|
|
|
+ //返回token
|
|
|
+ if (resultMap.containsKey("access_token")) {
|
|
|
+ return resultMap.get("access_token");
|
|
|
+ }
|
|
|
+ //没有拿到token
|
|
|
+ log.error("获取token发生错误, appId : {}, secret: {}, errcode : {}, errmsg : {}", appId, secret,
|
|
|
+ resultMap.get("errcode"), resultMap.get("errmsg"));
|
|
|
+ throw new BaseException("http状态正确, 获取token发生错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成微信小程序二维码
|
|
|
+ *
|
|
|
+ * @param scene : 二维码携带的参数
|
|
|
+ * @param accessToken : 小程序密钥
|
|
|
+ * @return : 返回二维码文件流
|
|
|
+ */
|
|
|
+ private BufferedInputStream getQrCodeApi(String scene, String accessToken) {
|
|
|
+ try {
|
|
|
+ //调用微信接口生成二维码
|
|
|
+ URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken);
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
+ //连接超时 单位毫秒
|
|
|
+ connection.setConnectTimeout(10000);
|
|
|
+ //读取超时 单位毫秒
|
|
|
+ connection.setReadTimeout(2000);
|
|
|
+ // 发送POST请求必须设置如下两行
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+ // 获取URLConnection对象对应的输出流
|
|
|
+ PrintWriter printWriter = new PrintWriter(connection.getOutputStream());
|
|
|
+ // 发送请求参数
|
|
|
+ JSONObject paramJson = new JSONObject();
|
|
|
+ //这就是你二维码里携带的参数 String型 名称不可变
|
|
|
+ paramJson.set("scene", Strings.isBlank(scene) ? "a=1" : scene);
|
|
|
+ printWriter.write(paramJson.toString());
|
|
|
+ // flush输出流的缓冲
|
|
|
+ printWriter.flush();
|
|
|
+ //返回数据流
|
|
|
+ return new BufferedInputStream(connection.getInputStream());
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new BaseException("获取小程序二维码异常");
|
|
|
+ }
|
|
|
}
|
|
|
}
|