图片二维码生成

master
mhsnet 5 months ago
parent 5a8cf11d5f
commit dd64b29e8b

@ -15,11 +15,14 @@ import cc.yunxi.utils.JwtTool;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
@ -40,10 +43,14 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;
import java.math.BigDecimal;
import java.security.cert.X509Certificate;
import java.util.Collections;
@ -55,6 +62,7 @@ import java.util.HashMap;
@RequiredArgsConstructor
@Slf4j
public class TestController {
private String basePath;
private final ITestService testService;
@ -68,6 +76,41 @@ public class TestController {
@Lazy
private JwtProperties jwtProperties;
@ApiOperation("测试接口成功")
@GetMapping("/test00001")
public String test00001() {
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("grant_type", "client_credential");
paramMap.put("appid", "wx630bc4f43990c80c");
paramMap.put("secret", "37028048aad5e1877c2b2aeacdfdc01b");
String result1= HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token",paramMap);
return result1;
}
@ApiOperation("测试接口成功")
@GetMapping("/test00002")
public String test00002() {
JSONObject jsonObjA = JSONUtil.createObj()
.put("scene", "123456");
String jsonStrA = jsonObjA.toStringPretty();
// String result1 = HttpRequest.post("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=81_hJxxSXjgtT1xZ0bvofFSsR--Nendqgrjeqz_x6Xkn_UR6cvyy5lYH6u_yHJoVCqxvZHUcE9SVo0q9ABMnEX9D71GWag6gqBxeA4BHnbNyUTBRgXjRSjFQEVBoAAEIZbAEANRI")
// .header(Header.USER_AGENT, "yhs")
// .body(jsonStrA)
// .timeout(20000)
// .execute().body();
HttpResponse res = HttpRequest.post("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=81_Bufhdg6uag9raShEklr-LGyfXuEgDHTbiRsa8f1zKtULBRQkWd42fICjcqaJ42dZdPPvIeaP772hlCQGsT2_koiIPVGh0G3adxLKRdA8Qfq88Qi_edSplMWAXRsIIIeAAAEEE").execute();
String result1 = res.toString();
// 保存二维码
String filePath = System.getProperty("user.dir") + java.io.File.separator +"upload"+ java.io.File.separator + "code" + java.io.File.separator + "cs.jpeg";
FileWriter writer = new FileWriter(filePath);
writer.write(result1);
// HashMap<String, Object> paramMap = new HashMap<>();
// paramMap.put("scene", "123456");
// String result1= HttpUtil.post("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=81_Qwr3CjKBQ9L67yhJzFtjcTpcyaTjvufjpfsQtg0m_W4Xgru6r6QYPW_sDESO-AmkCPQPR26fwDD4kMACKqBik2aKK14sywpULY8ygSrbSN9O65J_Tt4-mWnSbWkCVMdAFAQJA",paramMap);
return result1;
}
@ApiOperation("测试接口成功")
@GetMapping("/test01")
public CommonResult<String> success() {

@ -0,0 +1,29 @@
package cc.yunxi.controller;
import cc.yunxi.service.impl.WxServiceImpl;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
@Api(tags = "测试接口")
@RestController
@RequestMapping("/wx")
@RequiredArgsConstructor
@Slf4j
public class WxController {
private final WxServiceImpl wxService;
@ApiOperation("生成设备二维码")
@PostMapping("/generate-device-qr-code")
public String generateDeviceQRCode(@RequestBody String paramAJsonStr) {
JSONObject paramAJsonObj = JSONUtil.parseObj(paramAJsonStr);
String strDevCode = paramAJsonObj.getStr("devCode");
String strPage = paramAJsonObj.getStr("page");
wxService.generateDeviceQRCode(strDevCode, strPage);
return strDevCode;
}
}

@ -39,6 +39,8 @@ public class UserDTO {
@ApiModelProperty(value = "访问token", required = false)
private String token; // 返回时有用
@ApiModelProperty(value = "当前设备", required = false)
private String devCode;
@ApiModelProperty(value = "公司id", required = false)
private String companyId;

@ -25,4 +25,7 @@ public class WxLoginDTO {
@ApiModelProperty(value = "userType (1散户 2回收员)", required = true, example = "1")
@NotBlank(message = "用户类型缺失")
private String userType;
@ApiModelProperty(value = "devCode (设备码)", example = "tcode-000001")
private String devCode;
}

@ -0,0 +1,21 @@
package cc.yunxi.service;
import cn.hutool.http.HttpResponse;
import org.springframework.web.bind.annotation.RequestBody;
/**
* Wx
*/
public interface IWxService {
// 同步方法
void syncTest();
// 生成设备二维码
void generateDeviceQRCode(String strDevCode, String strPage);
// 散户-WxSh||回收员-WxHsy 获取token
String getToken(String strWxUserType);
// 获取设备微信二维码Res
HttpResponse getResDevQrCode(String token, String strDevCode, String strPage);
// 设备二维码保存
void saveDevQrCode(String strDevCode, HttpResponse resDevQrCode);
}

@ -106,6 +106,7 @@ public class CommonService implements ICommonService {
UserDTO userDTO = new UserDTO();
userDTO.setOpenid(openId);
userDTO.setPhone(phoneNumber);
userDTO.setDevCode(wxLoginDTO.getDevCode());
return userDTO;
}

@ -0,0 +1,126 @@
package cc.yunxi.service.impl;
import cc.yunxi.config.props.WxHsyProperties;
import cc.yunxi.config.props.WxShProperties;
import cc.yunxi.domain.vo.vxmessage.AccessToken;
import cc.yunxi.domain.vo.vxmessage.ResultVo;
import cc.yunxi.service.IRecyclerService;
import cc.yunxi.service.IWxService;
import cn.hutool.core.img.Img;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.HashMap;
/**
* Wx
*/
@Service
@Slf4j
@Validated
public class WxServiceImpl implements IWxService {
@Resource
private WxShProperties wxShProperties;
@Resource
private WxHsyProperties wxHsyProperties;
@Override
public void syncTest() {
log.info("等待main执行");
}
// 生成设备二维码
// 设备编码
@Override
public void generateDeviceQRCode(String strDevCode, String strPage){
log.info("--- generateDeviceQRCode(设备编码): " + strDevCode + " ---");
String token = this.getToken("WxSh");
HttpResponse resDevQrCode = getResDevQrCode(token, strDevCode, strPage);
saveDevQrCode(strDevCode, resDevQrCode);
log.info("<--- generateDeviceQRCode ");
}
// 散户-WxSh||回收员-WxHsy 获取token
// strWxUserType --- WxSh|WxHsy
@Override
public String getToken(String strWxUserType) {
log.info("---> getToken(散户-WxSh||回收员-WxHsy 获取token)" + strWxUserType) ;
String result = "";
String strToken = "";
try {
if ("WxHsy".equals(strWxUserType)) {
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("grant_type", "client_credential");
paramMap.put("appid", wxHsyProperties.getAppId());
paramMap.put("secret", wxHsyProperties.getAppSecret());
result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token",paramMap);
} else if ("WxSh".equals(strWxUserType)) {
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("grant_type", "client_credential");
paramMap.put("appid", wxShProperties.getAppId());
paramMap.put("secret", wxShProperties.getAppSecret());
result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token",paramMap);
} else {
throw new RuntimeException("微信用户类型参数错误:选用(散户-WxSh||回收员-WxHsy); 当前:" + strWxUserType);
}
JSONObject resultJsonObj = JSONUtil.parseObj(result);
strToken = resultJsonObj.getStr("access_token");
} catch (RuntimeException e) {
// log.error("getToken:" + e.toString());
}
log.info("微信Token" + strToken);
log.info("<--- getToken") ;
return strToken;
}
// 获取设备微信二维码Res
// wxToken --- 微信token
// devCode --- 设备码
@Override
public HttpResponse getResDevQrCode(String wxToken, String devCode, String strPage) {
log.info("---> getResDevQrCode(获取设备微信二维码Res)微信token:" + wxToken + ",设备码:"+ devCode + ",跳转页:" + strPage) ;
HttpResponse res = null;
try {
JSONObject jsonObjA = JSONUtil.createObj()
.put("scene", devCode)
.put("page", strPage)
.put("check_path", false)
.put("env_version", "develop");
String jsonStrA = jsonObjA.toStringPretty();
res = HttpRequest.post("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+wxToken).header(Header.USER_AGENT, "yhs")
.body(jsonStrA)
.timeout(20000)
.execute();
log.info("响应状态:" + res.getStatus());
log.info("Cache-Control" + res.header("Cache-Control"));
log.info("Content-disposition" + res.header("Content-disposition"));
log.info("Content-Length" + res.header("Content-Length"));
log.info("Content-Type" + res.header("Content-Type"));
// log.info("Response Body" + res.body());
log.info("<--- getResDevQrCode") ;
} catch (RuntimeException e) {
// log.error("getToken:" + e.toString());
}
return res;
}
// 设备二维码保存
// strDevCode --- 设备编码
// resDevQrCode --- 微信二维码Res
@Override
public void saveDevQrCode(String strDevCode, HttpResponse resDevQrCode){
log.info("---> saveDevQrCode(设备二维码保存)") ;
try {
String filePath = System.getProperty("user.dir") + java.io.File.separator +"qrcode"+ java.io.File.separator + "dev" + java.io.File.separator + "dev-" + strDevCode + ".jpg";
Img.from(resDevQrCode.bodyStream()).write(FileUtil.file(filePath));
} catch (RuntimeException e) {
// log.error("getToken:" + e.toString());
}
log.info("<--- saveDevQrCode") ;
}
}
Loading…
Cancel
Save