|
|
|
|
@ -2,13 +2,21 @@ package com.cyl.wechat;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.cyl.h5.config.SecurityUtil;
|
|
|
|
|
import com.cyl.wechat.response.WechatUserAuth;
|
|
|
|
|
import com.ruoyi.common.core.redis.RedisService;
|
|
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.http.HttpEntity;
|
|
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
|
|
|
import java.util.Base64;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@ -18,6 +26,12 @@ public class WechatAuthService {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private RestTemplate restTemplate;
|
|
|
|
|
@Autowired
|
|
|
|
|
private RedisService redisService;
|
|
|
|
|
@Value("${wechat.miniProgramAppId}")
|
|
|
|
|
private String appId;
|
|
|
|
|
@Value("${wechat.miniProgramSecret}")
|
|
|
|
|
private String secret;
|
|
|
|
|
|
|
|
|
|
public WechatUserAuth getUserToken(String code) {
|
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
|
|
@ -41,4 +55,65 @@ public class WechatAuthService {
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取二维码
|
|
|
|
|
*
|
|
|
|
|
* @return 二维码字符串
|
|
|
|
|
*/
|
|
|
|
|
public String getQRCode() {
|
|
|
|
|
String base64Str = redisService.getQrCode();
|
|
|
|
|
if (StringUtils.isNotEmpty(base64Str)) {
|
|
|
|
|
return base64Str;
|
|
|
|
|
}
|
|
|
|
|
String accessToken = getAccessToken();
|
|
|
|
|
if (StringUtils.isEmpty(accessToken)) {
|
|
|
|
|
log.error("accseetoken is null");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
String postUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
|
|
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
|
|
jsonObject.put("scene", "1"); //可以用作传参,必填,内容随意
|
|
|
|
|
jsonObject.put("page", "pages/index/index"); //扫码后跳转页面,选填
|
|
|
|
|
jsonObject.put("check_path", false); //是否检查跳转页面存不存在
|
|
|
|
|
jsonObject.put("env_version", "release"); //版本
|
|
|
|
|
byte[] string = null;
|
|
|
|
|
try {
|
|
|
|
|
string = restTemplate.exchange(postUrl, HttpMethod.POST, new HttpEntity<>(jsonObject, null), byte[].class).getBody();
|
|
|
|
|
log.info("wechat response code: {}",string);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("get wechat code error",e);
|
|
|
|
|
}
|
|
|
|
|
base64Str = Base64.getEncoder().encodeToString(string);
|
|
|
|
|
redisService.setQrCode(base64Str);
|
|
|
|
|
log.info("wechat code:{}",base64Str);
|
|
|
|
|
return base64Str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String getAccessToken() {
|
|
|
|
|
String token = redisService.getWechatToken();
|
|
|
|
|
|
|
|
|
|
log.info("redis token:{}",token);
|
|
|
|
|
if (StringUtils.isNotEmpty(token)) {
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
|
|
params.put("APPID", appId); //替换成自己的appId
|
|
|
|
|
params.put("APPSECRET",secret); //替换成自己的appSecret
|
|
|
|
|
ResponseEntity<String> responseEntity = restTemplate.getForEntity(
|
|
|
|
|
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}",
|
|
|
|
|
String.class, params);
|
|
|
|
|
String body = responseEntity.getBody();
|
|
|
|
|
JSONObject object = JSON.parseObject(body);
|
|
|
|
|
String accessToken = object.getString("access_token");
|
|
|
|
|
if (accessToken == null) {
|
|
|
|
|
log.error("获取accessToken失败");
|
|
|
|
|
return accessToken;
|
|
|
|
|
}
|
|
|
|
|
redisService.setWechatToken(accessToken);
|
|
|
|
|
log.info("get token:{}",accessToken);
|
|
|
|
|
return accessToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|