生成二维码并上传

master
guochaojie 4 months ago
parent 3ca81d0b8e
commit 93afcce2cf

@ -49,7 +49,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
"/static/**", "/static/**",
"/upload/**", "/upload/**",
"/qrcode/**.txt", "/qrcode/**.txt",
"/wx-message/**",//微信消息推送验证 "/wx/**",//微信消息推送验证
"/webSocketServer/**",//socket通信 "/webSocketServer/**",//socket通信
"/doc.html" "/doc.html"
); );

@ -18,5 +18,6 @@ public class WxHsyProperties {
private String sendMessageUrl; private String sendMessageUrl;
private String EncodingAESKey; private String EncodingAESKey;
private String token; private String token;
private String envVersion;
} }

@ -18,4 +18,5 @@ public class WxShProperties {
private String sendMessageUrl; private String sendMessageUrl;
private String EncodingAESKey; private String EncodingAESKey;
private String token; private String token;
private String envVersion;
} }

@ -164,6 +164,7 @@ public class DeviceController {
fillPoster(pics, respVO); fillPoster(pics, respVO);
result.setData(respVO); result.setData(respVO);
result.setMsg("success"); result.setMsg("success");
result.setCode(200);
return result; return result;
} }
@ -316,6 +317,9 @@ public class DeviceController {
respVO.setToken(userDTO.getToken()); respVO.setToken(userDTO.getToken());
respVO.setTimeExpire(userDTO.getTimeExpire()); respVO.setTimeExpire(userDTO.getTimeExpire());
respVO.setRole(1); respVO.setRole(1);
result.setData(respVO);
result.setCode(200);
result.setMsg("登录成功");
return result; return result;
} }
@ -344,7 +348,7 @@ public class DeviceController {
respVO.setStatus(recycler.getStatus()); respVO.setStatus(recycler.getStatus());
respVO.setToken(userDTO.getToken()); respVO.setToken(userDTO.getToken());
respVO.setTimeExpire(userDTO.getTimeExpire()); respVO.setTimeExpire(userDTO.getTimeExpire());
respVO.setRole(1); respVO.setRole(2);
return CommonResult.success(respVO); return CommonResult.success(respVO);
} }

@ -1,5 +1,8 @@
package cc.yunxi.controller; package cc.yunxi.controller;
import cc.yunxi.common.domain.CommonResult;
import cc.yunxi.domain.vo.file.FileUploadRespVO;
import cc.yunxi.domain.vo.vxmessage.QRCodeVo;
import cc.yunxi.service.impl.WxServiceImpl; import cc.yunxi.service.impl.WxServiceImpl;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
@ -9,7 +12,10 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@Api(tags = "测试接口") import java.util.ArrayList;
import java.util.List;
@Api(tags = "微信相关接口")
@RestController @RestController
@RequestMapping("/wx") @RequestMapping("/wx")
@RequiredArgsConstructor @RequiredArgsConstructor
@ -18,12 +24,11 @@ public class WxController {
private final WxServiceImpl wxService; private final WxServiceImpl wxService;
@ApiOperation("生成设备二维码") @ApiOperation("生成设备二维码")
@PostMapping("/generate-device-qr-code") @PostMapping("/generateDeviceQRCode")
public String generateDeviceQRCode(@RequestBody String paramAJsonStr) { public CommonResult<List<FileUploadRespVO>> generateDeviceQRCode(@RequestBody QRCodeVo qrCodeVo) {
JSONObject paramAJsonObj = JSONUtil.parseObj(paramAJsonStr); FileUploadRespVO fileUploadRespVO = wxService.generateDeviceQRCode(qrCodeVo);
String strDevCode = paramAJsonObj.getStr("devCode"); ArrayList<FileUploadRespVO> list = new ArrayList<>();
String strPage = paramAJsonObj.getStr("page"); list.add(fileUploadRespVO);
wxService.generateDeviceQRCode(strDevCode, strPage); return CommonResult.success(list);
return strDevCode;
} }
} }

@ -0,0 +1,19 @@
package cc.yunxi.domain.vo.vxmessage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@Data
@ApiModel("生成二维码参数")
public class QRCodeVo {
@ApiModelProperty(value = "设备编号",required = true,example = "DE000021")
@NotBlank(message = "设备编号不能为空")
private String deviceCode;
@ApiModelProperty(value = "跳转页面",example = "pages/selectPeople/selectPeople")
@NotBlank()
private String page="pages/selectPeople/selectPeople";
}

@ -61,6 +61,14 @@ public interface IFileService extends IService<File> {
*/ */
FileUploadRespVO uploadImage(String image64, String filename); FileUploadRespVO uploadImage(String image64, String filename);
/**
* byte
*
* @param image
* @return FileUploadRespVO
*/
FileUploadRespVO uploadImage(byte[] image, String filename);
/** /**
* *
*/ */

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

@ -17,6 +17,7 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.jndi.toolkit.url.Uri;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -188,6 +189,10 @@ public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements IF
byte[] image = decodeToBase64(image64); byte[] image = decodeToBase64(image64);
return base64ImageUpload(image, fileName, FileStyleEnum.IMAGE); return base64ImageUpload(image, fileName, FileStyleEnum.IMAGE);
} }
@Override
public FileUploadRespVO uploadImage(byte[] image, String fileName) {
return base64ImageUpload(image, fileName, FileStyleEnum.IMAGE);
}
private FileUploadRespVO base64ImageUpload(byte[] content, String fileName, FileStyleEnum fileStyleEnum) { private FileUploadRespVO base64ImageUpload(byte[] content, String fileName, FileStyleEnum fileStyleEnum) {
try { try {

@ -1,14 +1,15 @@
package cc.yunxi.service.impl; package cc.yunxi.service.impl;
import cc.yunxi.common.exception.BizIllegalException;
import cc.yunxi.config.props.WxHsyProperties; import cc.yunxi.config.props.WxHsyProperties;
import cc.yunxi.config.props.WxShProperties; import cc.yunxi.config.props.WxShProperties;
import cc.yunxi.domain.vo.file.FileUploadRespVO;
import cc.yunxi.domain.vo.vxmessage.AccessToken; import cc.yunxi.domain.vo.vxmessage.AccessToken;
import cc.yunxi.domain.vo.vxmessage.ResultVo; import cc.yunxi.domain.vo.vxmessage.QRCodeVo;
import cc.yunxi.service.IRecyclerService;
import cc.yunxi.service.IWxService; import cc.yunxi.service.IWxService;
import cn.hutool.core.img.Img; import cc.yunxi.utils.VerifyUtil;
import cn.hutool.core.io.FileUtil; import cc.yunxi.utils.WeChatMessageUtil;
import cn.hutool.core.io.file.FileWriter; import cn.hutool.core.util.StrUtil;
import cn.hutool.http.Header; import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpResponse;
@ -21,6 +22,7 @@ import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
/** /**
* Wx * Wx
@ -29,98 +31,121 @@ import java.util.HashMap;
@Slf4j @Slf4j
@Validated @Validated
public class WxServiceImpl implements IWxService { public class WxServiceImpl implements IWxService {
private final String GET_QR_CODE_URL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=";
private final String DEFAULT_PAGE = "pages/selectPeople/selectPeople";
@Resource @Resource
private WxShProperties wxShProperties; private WxShProperties wxShProperties;
@Resource @Resource
private WxHsyProperties wxHsyProperties; private WxHsyProperties wxHsyProperties;
@Resource
private FileServiceImpl fileService;
@Resource
private WeChatMessageUtil weChatMessageUtil;
@Override @Override
public void syncTest() { public void syncTest() {
log.info("等待main执行"); log.info("等待main执行");
} }
// 生成设备二维码 // 生成设备二维码
// 设备编码 // 设备编码
@Override @Override
public void generateDeviceQRCode(String strDevCode, String strPage){ public FileUploadRespVO generateDeviceQRCode(QRCodeVo qrCodeVo) {
log.info("--- generateDeviceQRCode(设备编码): " + strDevCode + " ---"); log.info("--- generateDeviceQRCode(设备编码): " + qrCodeVo.getDeviceCode() + " ---");
String token = this.getToken("WxSh"); String token = getToken("client");
HttpResponse resDevQrCode = getResDevQrCode(token, strDevCode, strPage); String page = qrCodeVo.getPage();
saveDevQrCode(strDevCode, resDevQrCode); if (StrUtil.isBlank(page)) {
qrCodeVo.setPage(DEFAULT_PAGE);
}
HttpResponse resDevQrCode = getResDevQrCode(token, qrCodeVo.getDeviceCode(), qrCodeVo.getPage());
FileUploadRespVO fileUploadRespVO = saveDevQrCode(qrCodeVo.getDeviceCode(), resDevQrCode);
log.info("<--- generateDeviceQRCode "); log.info("<--- generateDeviceQRCode ");
return fileUploadRespVO;
} }
// 散户-WxSh||回收员-WxHsy 获取token // 散户-WxSh||回收员-WxHsy 获取token
// strWxUserType --- WxSh|WxHsy // strWxUserType --- WxSh|WxHsy
@Override @Override
public String getToken(String strWxUserType) { public String getToken(String endpointType) {
log.info("---> getToken(散户-WxSh||回收员-WxHsy 获取token)" + strWxUserType) ; log.info("---> getToken(散户-WxSh||回收员-WxHsy 获取token)" + endpointType);
String result = "";
String strToken = "";
try { try {
if ("WxHsy".equals(strWxUserType)) { String result = "";
HashMap<String, Object> paramMap = new HashMap<>(); String token = "";
paramMap.put("grant_type", "client_credential"); Map<String, Object> param = new HashMap<>();
paramMap.put("appid", wxHsyProperties.getAppId()); param.put("grant_type", "client_credential");//固定值
paramMap.put("secret", wxHsyProperties.getAppSecret()); if ("client".equals(endpointType)) {//预约端
result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token",paramMap); param.put("appid", wxShProperties.getAppId());
} else if ("WxSh".equals(strWxUserType)) { param.put("secret", wxShProperties.getAppSecret());
HashMap<String, Object> paramMap = new HashMap<>(); result = HttpUtil.get(wxShProperties.getTokenUrl(), param, 2000);
paramMap.put("grant_type", "client_credential"); } else if ("recycler".equals(endpointType)) {//回收端
paramMap.put("appid", wxShProperties.getAppId()); param.put("appid", wxHsyProperties.getAppId());
paramMap.put("secret", wxShProperties.getAppSecret()); param.put("secret", wxHsyProperties.getAppSecret());
result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token",paramMap); result = HttpUtil.get(wxHsyProperties.getTokenUrl(), param, 2000);
} else { } else {
throw new RuntimeException("微信用户类型参数错误:选用(散户-WxSh||回收员-WxHsy); 当前:" + strWxUserType); throw new BizIllegalException("endpointType参数错误");
} }
JSONObject resultJsonObj = JSONUtil.parseObj(result); JSONObject json = JSONUtil.parseObj(result);
strToken = resultJsonObj.getStr("access_token"); token = json.getStr("access_token");
log.info("微信Token" + token);
log.info("<--- getToken");
return token;
} catch (RuntimeException e) { } catch (RuntimeException e) {
// log.error("getToken:" + e.toString()); e.printStackTrace();
log.error("getToken:" + e.getMessage());
throw new BizIllegalException("获取微信平台token失败");
} }
log.info("微信Token" + strToken);
log.info("<--- getToken") ;
return strToken;
} }
// 获取设备微信二维码Res // 获取设备微信二维码Res
// wxToken --- 微信token // wxToken --- 微信token
// devCode --- 设备码 // devCode --- 设备码
@Override @Override
public HttpResponse getResDevQrCode(String wxToken, String devCode, String strPage) { public HttpResponse getResDevQrCode(String wxToken, String devCode, String page) {
log.info("---> getResDevQrCode(获取设备微信二维码Res)微信token:" + wxToken + ",设备码:"+ devCode + ",跳转页:" + strPage) ; log.info("---> getResDevQrCode(获取设备微信二维码Res)微信token:" + wxToken + ",设备码:" + devCode + ",跳转页:" + page);
HttpResponse res = null;
try { try {
JSONObject jsonObjA = JSONUtil.createObj() HttpResponse res = null;
.put("scene", devCode) Map<String, Object> param = new HashMap<>();
.put("page", strPage) param.put("scene", devCode);
.put("check_path", false) param.put("page", page);
.put("env_version", "develop"); param.put("check_path", false);
String jsonStrA = jsonObjA.toStringPretty(); param.put("env_version", wxShProperties.getEnvVersion());//客户端
res = HttpRequest.post("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+wxToken).header(Header.USER_AGENT, "yhs") res = HttpRequest.post(GET_QR_CODE_URL + wxToken).header(Header.USER_AGENT, "yhs")
.body(jsonStrA) .body(JSONUtil.toJsonStr(param))
.timeout(20000) .timeout(2000)
.execute(); .execute();
log.info("响应状态:" + res.getStatus()); return res;
log.info("Cache-Control" + res.header("Cache-Control")); // log.info("响应状态:" + res.getStatus());
log.info("Content-disposition" + res.header("Content-disposition")); // log.info("Cache-Control" + res.header("Cache-Control"));
log.info("Content-Length" + res.header("Content-Length")); // log.info("Content-disposition" + res.header("Content-disposition"));
log.info("Content-Type" + res.header("Content-Type")); // log.info("Content-Length" + res.header("Content-Length"));
// log.info("Content-Type" + res.header("Content-Type"));
// log.info("Response Body" + res.body()); // log.info("Response Body" + res.body());
log.info("<--- getResDevQrCode") ; // log.info("<--- getResDevQrCode");
} catch (RuntimeException e) { } catch (RuntimeException e) {
// log.error("getToken:" + e.toString()); log.error(":" + e.getMessage());
e.printStackTrace();
throw new BizIllegalException("生成设备小程序二维码失败!");
} }
return res;
} }
// 设备二维码保存 // 设备二维码保存
// strDevCode --- 设备编码 // strDevCode --- 设备编码
// resDevQrCode --- 微信二维码Res // resDevQrCode --- 微信二维码Res
@Override @Override
public void saveDevQrCode(String strDevCode, HttpResponse resDevQrCode){ public FileUploadRespVO saveDevQrCode(String strDevCode, HttpResponse response) {
log.info("---> saveDevQrCode(设备二维码保存)") ; // log.info("---> saveDevQrCode(设备二维码保存)");
try { // try {
String filePath = System.getProperty("user.dir") + java.io.File.separator +"qrcode"+ java.io.File.separator + "dev" + java.io.File.separator + "dev-" + strDevCode + ".jpg"; // byte[] bytes = response.bodyBytes();
Img.from(resDevQrCode.bodyStream()).write(FileUtil.file(filePath)); // return fileService.uploadImage(bytes, strDevCode);
} catch (RuntimeException e) { // String filePath = System.getProperty("user.dir") + java.io.File.separator + "qrcode" + java.io.File.separator + "dev" + java.io.File.separator + "dev-" + strDevCode + ".jpg";
// log.error("getToken:" + e.toString()); // Img.from(response.bodyStream()).write(FileUtil.file(filePath));
} // } catch (RuntimeException e) {
log.info("<--- saveDevQrCode") ; //// log.error("getToken:" + e.toString());
// }
// log.info("<--- saveDevQrCode");
byte[] bytes = response.bodyBytes();
return fileService.uploadImage(bytes, strDevCode + ".jpg");
} }
} }

@ -22,3 +22,9 @@ logging:
file: file:
path: "${user.dir}/logs/${spring.application.name}" path: "${user.dir}/logs/${spring.application.name}"
name: "${logging.file.path}/log_error.log" name: "${logging.file.path}/log_error.log"
nxhs:
wx:
client:
env_version: "release"
recycler:
env_version: "release"
Loading…
Cancel
Save