+
This commit is contained in:
parent
7d12de1fe6
commit
16ff649459
@ -0,0 +1,20 @@
|
||||
package com.evotech.hd.cloud.rpc;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@FeignClient(value = "wechat-server")
|
||||
public interface WechatService {
|
||||
|
||||
@GetMapping(value = "/wechat/xcx/qrcode/get2",
|
||||
consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
|
||||
public String getQRCode2(@RequestParam String path, @RequestParam String width, @RequestParam String env_version);
|
||||
|
||||
|
||||
@GetMapping(value = "/wechat/xcx/qrcode/create2",
|
||||
consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
|
||||
public String createQRCode2(@RequestParam String path, @RequestParam String width);
|
||||
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
package com.evotech.hd.wechat.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.evotech.hd.wechat.service.XCXQRCodeService;
|
||||
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
//@Tag(name = "小程序码")
|
||||
@RestController
|
||||
@RequestMapping("/xcx/qrcode")
|
||||
@ApiSupport(order = 16)
|
||||
public class XCXQRCodeController {
|
||||
|
||||
|
||||
@Resource
|
||||
private XCXQRCodeService xcxQRCodeService;
|
||||
|
||||
|
||||
// @Operation(summary = "获取小程序码")
|
||||
@GetMapping("/get")
|
||||
// @ApiOperationSupport(order = 1)
|
||||
public void getQRCode(String path, @RequestParam(required = false) String width, @RequestParam(required = false)String env_version, HttpServletResponse response) {
|
||||
xcxQRCodeService.getQRCode(path, width, env_version, response);
|
||||
}
|
||||
|
||||
|
||||
// @Operation(summary = "获取小程序二维码")
|
||||
@GetMapping("/create")
|
||||
// @ApiOperationSupport(order = 2)
|
||||
public void createQRCode(String path, @RequestParam(required = false) String width, HttpServletResponse response) {
|
||||
xcxQRCodeService.createQRCode(path, width, response);
|
||||
}
|
||||
|
||||
|
||||
// @Operation(summary = "获取小程序码")
|
||||
@GetMapping("/get2")
|
||||
// @ApiOperationSupport(order = 1)
|
||||
public String getQRCode2(String path, @RequestParam(required = false) String width, @RequestParam(required = false)String env_version) {
|
||||
return xcxQRCodeService.getQRCode2(path, width, env_version);
|
||||
}
|
||||
|
||||
// @Operation(summary = "获取小程序二维码")
|
||||
@GetMapping("/create2")
|
||||
// @ApiOperationSupport(order = 2)
|
||||
public String createQRCode2(String path, @RequestParam(required = false) String width) {
|
||||
return xcxQRCodeService.createQRCode2(path, width);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package com.evotech.hd.wechat.service;
|
||||
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
public interface XCXQRCodeService {
|
||||
|
||||
public void getQRCode(String path, String width, String env_version, HttpServletResponse response);
|
||||
|
||||
public void createQRCode(String path, String width, HttpServletResponse response);
|
||||
|
||||
public String getQRCode2(String path, String width, String env_version);
|
||||
|
||||
public String createQRCode2(String path, String width);
|
||||
|
||||
}
|
||||
@ -0,0 +1,89 @@
|
||||
package com.evotech.hd.wechat.service.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Base64;
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.evotech.hd.wechat.service.AccessTokenService;
|
||||
import com.evotech.hd.wechat.service.XCXQRCodeService;
|
||||
import com.evotech.hd.wechat.utils.xcx.QRCodeUtil;
|
||||
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.ServletOutputStream;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
@Service
|
||||
public class XCXQRCodeServiceImpl implements XCXQRCodeService {
|
||||
|
||||
|
||||
@Resource
|
||||
private AccessTokenService accessTokenService;
|
||||
|
||||
@Override
|
||||
public void getQRCode(String path, String width, String env_version, HttpServletResponse response) {
|
||||
byte[] res = QRCodeUtil.getQRCode(accessTokenService.getAccessToken(), path, width, env_version);
|
||||
String fileName = "小程序码" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) + ".png";
|
||||
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
||||
try {
|
||||
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
ServletOutputStream out = null;
|
||||
try {
|
||||
out = response.getOutputStream();
|
||||
out.write(res);
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
IoUtil.close(out);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createQRCode(String path, String width, HttpServletResponse response) {
|
||||
byte[] res = QRCodeUtil.createQRCode(accessTokenService.getAccessToken(), path, width);
|
||||
String fileName = "小程序二维码" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) + ".png";
|
||||
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
||||
try {
|
||||
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
ServletOutputStream out = null;
|
||||
try {
|
||||
out = response.getOutputStream();
|
||||
out.write(res);
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
IoUtil.close(out);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getQRCode2(String path, String width, String env_version) {
|
||||
byte[] res = QRCodeUtil.getQRCode(accessTokenService.getAccessToken(), path, width, env_version);
|
||||
return Base64.getEncoder().encodeToString(res);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String createQRCode2(String path, String width) {
|
||||
byte[] res = QRCodeUtil.createQRCode(accessTokenService.getAccessToken(), path, width);
|
||||
return Base64.getEncoder().encodeToString(res);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,69 @@
|
||||
package com.evotech.hd.wechat.utils.xcx;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
|
||||
public class QRCodeUtil {
|
||||
|
||||
private static String getQRCodeUrl = "https://api.weixin.qq.com/wxa/getwxacode?access_token=";
|
||||
private static String createQRCodeUrl = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=";
|
||||
|
||||
/**
|
||||
* 获取小程序码
|
||||
* POST
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
public static byte[] getQRCode(String access_token, String path, String width, String env_version) {
|
||||
Map<String, Object> m = new HashMap<String, Object>();
|
||||
m.put("path", path);
|
||||
m.put("width", StringUtils.hasText(width)?width:"1280");
|
||||
m.put("env_version", env_version);
|
||||
|
||||
HttpResponse response = HttpRequest.post(getQRCodeUrl + access_token).body(JSONUtil.toJsonStr(m)).execute();
|
||||
byte[] bodyBytes = response.bodyBytes();
|
||||
// HttpRequest.post(getQRCodeUrl + access_token).body(JSONUtil.toJsonStr(m)).execute().bodyBytes();
|
||||
if (bodyBytes == null) {
|
||||
throw new RuntimeException("获取小程序码异常:" + response.body());
|
||||
}
|
||||
if (bodyBytes.length < 200) {
|
||||
throw new RuntimeException("获取小程序码失败!");
|
||||
}
|
||||
|
||||
return bodyBytes;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取小程序二维码
|
||||
* POST
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
public static byte[] createQRCode(String access_token, String path, String width) {
|
||||
Map<String, Object> m = new HashMap<String, Object>();
|
||||
m.put("path", path);
|
||||
m.put("width", StringUtils.hasText(width)?width:"1280");
|
||||
HttpResponse response = HttpRequest.post(createQRCodeUrl + access_token).body(JSONUtil.toJsonStr(m)).execute();
|
||||
byte[] bodyBytes = response.bodyBytes();
|
||||
// HttpRequest.post(getQRCodeUrl + access_token).body(JSONUtil.toJsonStr(m)).execute().bodyBytes();
|
||||
if (bodyBytes == null) {
|
||||
throw new RuntimeException("获取小程序二维码异常:" + response.body());
|
||||
}
|
||||
if (bodyBytes.length < 200) {
|
||||
throw new RuntimeException("获取小程序二维码失败!");
|
||||
}
|
||||
return bodyBytes;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user