|
@@ -1,17 +1,33 @@
|
|
package com.zanxiang.manage.service.Impl;
|
|
package com.zanxiang.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.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.zanxiang.common.exception.BaseException;
|
|
|
|
+import com.zanxiang.common.utils.URIUtil;
|
|
import com.zanxiang.common.utils.bean.BeanUtils;
|
|
import com.zanxiang.common.utils.bean.BeanUtils;
|
|
|
|
+import com.zanxiang.manage.domain.dto.GameDTO;
|
|
import com.zanxiang.manage.domain.params.GameAccountUpdateParam;
|
|
import com.zanxiang.manage.domain.params.GameAccountUpdateParam;
|
|
import com.zanxiang.manage.domain.vo.GameAccountVO;
|
|
import com.zanxiang.manage.domain.vo.GameAccountVO;
|
|
import com.zanxiang.manage.service.GameAppletService;
|
|
import com.zanxiang.manage.service.GameAppletService;
|
|
|
|
+import com.zanxiang.manage.service.GameService;
|
|
|
|
+import com.zanxiang.module.oss.service.IOssService;
|
|
import com.zanxiang.mybatis.entity.GameApplet;
|
|
import com.zanxiang.mybatis.entity.GameApplet;
|
|
import com.zanxiang.mybatis.mapper.GameAppletMapper;
|
|
import com.zanxiang.mybatis.mapper.GameAppletMapper;
|
|
|
|
+import com.zanxiangnet.module.util.JsonUtil;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
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.stereotype.Service;
|
|
|
|
|
|
|
|
+import java.io.BufferedInputStream;
|
|
|
|
+import java.io.PrintWriter;
|
|
|
|
+import java.net.HttpURLConnection;
|
|
|
|
+import java.net.URL;
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author : lingfeng
|
|
* @author : lingfeng
|
|
@@ -22,6 +38,12 @@ import java.time.LocalDateTime;
|
|
@Service
|
|
@Service
|
|
public class GameAppletServiceImpl extends ServiceImpl<GameAppletMapper, GameApplet> implements GameAppletService {
|
|
public class GameAppletServiceImpl extends ServiceImpl<GameAppletMapper, GameApplet> implements GameAppletService {
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private IOssService ossService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private GameService gameService;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 获取账号信息配置
|
|
* 获取账号信息配置
|
|
*
|
|
*
|
|
@@ -66,4 +88,95 @@ public class GameAppletServiceImpl extends ServiceImpl<GameAppletMapper, GameApp
|
|
gameApplet.setUpdateTime(LocalDateTime.now());
|
|
gameApplet.setUpdateTime(LocalDateTime.now());
|
|
return super.saveOrUpdate(gameApplet);
|
|
return super.saveOrUpdate(gameApplet);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取游戏小程序二维码
|
|
|
|
+ *
|
|
|
|
+ * @param gameId : 游戏id
|
|
|
|
+ * @return : 返回游戏二维码
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public String getQrCod(Long gameId) {
|
|
|
|
+ GameDTO gameDTO = gameService.getById(gameId);
|
|
|
|
+ if (gameDTO == null) {
|
|
|
|
+ throw new BaseException("游戏不存在");
|
|
|
|
+ }
|
|
|
|
+ GameApplet gameApplet = super.getOne(new LambdaQueryWrapper<GameApplet>().eq(GameApplet::getGameId, gameId));
|
|
|
|
+ if (gameApplet == null || Strings.isBlank(gameApplet.getAppId()) || Strings.isBlank(gameApplet.getAppSecret())) {
|
|
|
|
+ throw new BaseException("该游戏未设置小程序信息");
|
|
|
|
+ }
|
|
|
|
+ String token = this.getAppletTokenApi(gameApplet.getAppId(), gameApplet.getAppSecret());
|
|
|
|
+ return this.getQrCodeApi(token, gameDTO.getName() + ".jpg");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取小程序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 accessToken : 接口调用凭证
|
|
|
|
+ */
|
|
|
|
+ private String getQrCodeApi(String accessToken, String appletName) {
|
|
|
|
+ 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", "a=1");
|
|
|
|
+ printWriter.write(paramJson.toString());
|
|
|
|
+ // flush输出流的缓冲
|
|
|
|
+ printWriter.flush();
|
|
|
|
+ //开始获取数据
|
|
|
|
+ BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream());
|
|
|
|
+ return ossService.upload(appletName, inputStream);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new BaseException("生成微信小程序二维码异常");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|