Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -1,20 +1,28 @@
|
||||
package com.czg.service.account.util;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.symmetric.AES;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.czg.config.RedisCst;
|
||||
import com.czg.resp.CzgResult;
|
||||
import com.czg.service.RedisService;
|
||||
import com.czg.system.dto.SysParamsDTO;
|
||||
import com.czg.system.enums.SysParamCodeEnum;
|
||||
import com.czg.system.service.SysParamsService;
|
||||
import com.google.gson.JsonObject;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.dubbo.config.annotation.DubboReference;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
@@ -27,6 +35,10 @@ import java.util.Map;
|
||||
public class WechatAuthUtil {
|
||||
@DubboReference(check = false)
|
||||
private SysParamsService sysParamsService;
|
||||
@Resource
|
||||
private RedisService redisService;
|
||||
@Resource
|
||||
private AliOssUtil aliOssUtil;
|
||||
|
||||
// @Value("${wx.appId}")
|
||||
private String appId;
|
||||
@@ -38,6 +50,9 @@ public class WechatAuthUtil {
|
||||
// @Value("${wx.account.secrete}")
|
||||
private String accountSecrete;
|
||||
|
||||
private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
|
||||
private static final String QR_CODE_URL = "https://api.weixin.qq.com/wxa/getwxacode";
|
||||
|
||||
|
||||
static LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
|
||||
|
||||
@@ -87,6 +102,49 @@ public class WechatAuthUtil {
|
||||
return JSONObject.parseObject(resp).getString("openid");
|
||||
}
|
||||
|
||||
//获取小程序token
|
||||
private String getAccessToken() {
|
||||
String url = String.format("%s?grant_type=client_credential&appid=%s&secret=%s", TOKEN_URL, appId, secrete);
|
||||
String response = HttpUtil.get(url);
|
||||
com.alibaba.fastjson.JSONObject jsonResponse = com.alibaba.fastjson.JSONObject.parseObject(response);
|
||||
if (!jsonResponse.containsKey("access_token")) {
|
||||
throw new RuntimeException("Failed to retrieve access token: " + response);
|
||||
}
|
||||
return jsonResponse.getString("access_token");
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 小程序码 跳转对应页面
|
||||
*
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
public String getFetchQrCode(Map<String, Object> params) throws Exception {
|
||||
String url = aliOssUtil.upload(fetchQrCode(params), aliOssUtil.getPath("shopVip", "png"));
|
||||
redisService.set(RedisCst.SHOP_VIP_CODE + params.get("shopId"), url);
|
||||
return url;
|
||||
}
|
||||
|
||||
//生成页面地址
|
||||
private InputStream fetchQrCode(Map<String, Object> params) {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
//路径
|
||||
jsonObject.addProperty("path", STR."\{sysParamsService.getSysParamValue(SysParamCodeEnum.WX_MINI_VIP_URL.getCode())}?shopId=\{params.get("shopId")}");
|
||||
//是否需要透明底色,为 true 时,生成透明底色的小程序码
|
||||
jsonObject.addProperty("is_hyaline", true);
|
||||
//正式版为 release,体验版为 trial,开发版为 develop
|
||||
if (params.containsKey("env_version")) {
|
||||
jsonObject.addProperty("env_version", "trial");
|
||||
}
|
||||
String accessToken = getAccessToken();
|
||||
String url = String.format("%s?access_token=%s", QR_CODE_URL, accessToken);
|
||||
return HttpUtil.createPost(url)
|
||||
.body(jsonObject.toString(), "application/json")
|
||||
.execute()
|
||||
.bodyStream();
|
||||
}
|
||||
|
||||
|
||||
public JSONObject getSession(String code) {
|
||||
String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
|
||||
Map<String, Object> requestUrlParam = new HashMap<>();
|
||||
|
||||
Reference in New Issue
Block a user