|
@@ -4,20 +4,36 @@ package com.management.platform.controller;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.management.platform.entity.User;
|
|
|
+import com.management.platform.entity.UserQrCode;
|
|
|
import com.management.platform.entity.WechatAccount;
|
|
|
+import com.management.platform.service.UserQrCodeService;
|
|
|
import com.management.platform.service.UserService;
|
|
|
import com.management.platform.service.WechatAccountService;
|
|
|
import com.management.platform.util.HttpRespMsg;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.ssl.SSLContexts;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.*;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -41,8 +57,21 @@ public class WechatAccountController {
|
|
|
@Resource
|
|
|
private RestTemplate restTemplate;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private UserQrCodeService userQrCodeService;
|
|
|
+
|
|
|
+ @Value(value ="${upload.path}")
|
|
|
+ private String uploadPath;
|
|
|
+
|
|
|
+ private final static String prefixUrl="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存修改并获取公司对应的accessToken
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ */
|
|
|
@RequestMapping("/getAccessToken")
|
|
|
- private HttpRespMsg getAccessToken(HttpServletRequest request) {
|
|
|
+ public HttpRespMsg getAccessToken(HttpServletRequest request) {
|
|
|
HttpRespMsg httpRespMsg = new HttpRespMsg();
|
|
|
String token = request.getHeader("Token");
|
|
|
User user = userService.getById(token);
|
|
@@ -77,10 +106,144 @@ public class WechatAccountController {
|
|
|
}
|
|
|
|
|
|
|
|
|
- @RequestMapping(value = "test")
|
|
|
- public HttpRespMsg test(String accessToken,String sceneStr) {
|
|
|
- String foreverStrTicket = createForeverStrTicket(accessToken, sceneStr);
|
|
|
- return new HttpRespMsg();
|
|
|
+ //根据用户id获取对应的二维码信息
|
|
|
+ @RequestMapping(value = "getQrCodeFromTableOrWX")
|
|
|
+ public HttpRespMsg getQrCodeFromTableOrWX(String userId,HttpServletRequest request) {
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ String token = request.getHeader("Token");
|
|
|
+ User user = userService.getById(token);
|
|
|
+ UserQrCode qrCode = userQrCodeService.getOne(new QueryWrapper<UserQrCode>().eq("user_id", userId));
|
|
|
+ if (qrCode!=null){
|
|
|
+ qrCode.setUrlPlusTicket(prefixUrl+qrCode.getTicket());
|
|
|
+ msg.data=qrCode;
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ WechatAccount wechatAccount = wechatAccountService.getOne(new QueryWrapper<WechatAccount>().eq("company_id", user.getCompanyId()));
|
|
|
+ if (wechatAccount==null){
|
|
|
+ msg.setError("该公司没有配置公众号相关的参数");
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ //获取公众号token
|
|
|
+ String accessToken = wechatAccountService.getAccessToken(user.getCompanyId(), wechatAccount.getAppId());
|
|
|
+ try {
|
|
|
+ //获取二维码相关信息相关
|
|
|
+ String result =createForeverStrTicket(accessToken, userId);
|
|
|
+ if (result==null|| StringUtils.isEmpty(result)){
|
|
|
+ msg.setError("获取二维码信息失败");
|
|
|
+ return msg;
|
|
|
+ }else {
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
+
|
|
|
+ if (null != jsonObject) {
|
|
|
+ String ticket = jsonObject.getString("ticket");
|
|
|
+ System.out.println("创建永久带参二维码成功,ticket=" + ticket);
|
|
|
+ String url = jsonObject.getString("url");
|
|
|
+ System.out.println("创建永久带参二维码成功,url=" + url);
|
|
|
+
|
|
|
+ UserQrCode userQrCode = new UserQrCode();
|
|
|
+ userQrCode.setUserId(userId);
|
|
|
+ userQrCode.setTicket(ticket);
|
|
|
+ userQrCode.setUrl(url);
|
|
|
+ userQrCode.setCompanyId(user.getCompanyId());
|
|
|
+
|
|
|
+ CompletableFuture<String> future = saveWeChatQrCodeImgHttpClient(prefixUrl + ticket, ticket, uploadPath);
|
|
|
+ String imgName = future.get(10, TimeUnit.SECONDS);// 添加超时
|
|
|
+ if (imgName.isEmpty()){
|
|
|
+ msg.setError("二维码图片保存失败");
|
|
|
+ return msg;
|
|
|
+ }else {
|
|
|
+ userQrCode.setImg(imgName);
|
|
|
+ }
|
|
|
+
|
|
|
+ userQrCodeService.save(userQrCode);
|
|
|
+ userQrCode.setUrlPlusTicket(prefixUrl+userQrCode.getTicket());
|
|
|
+ msg.setData(userQrCode);
|
|
|
+ }
|
|
|
+
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ msg.setData(e.getMessage());
|
|
|
+ }
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Async("taskExecutor")
|
|
|
+ public CompletableFuture<String> saveWeChatQrCodeImg(String qrCodeUrl, String ticket, String uploadPath) {
|
|
|
+ try {
|
|
|
+ log.info("开始保存二维码图片,ticket: {}", ticket);
|
|
|
+
|
|
|
+ // 确保目录存在
|
|
|
+ File uploadDir = new File(uploadPath);
|
|
|
+ if (!uploadDir.exists() && !uploadDir.mkdirs()) {
|
|
|
+ throw new IOException("无法创建目录: " + uploadPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建目标文件路径
|
|
|
+ String fileName = ticket + ".jpg";
|
|
|
+ File destFile = new File(uploadPath, fileName);
|
|
|
+
|
|
|
+ // 从URL下载图片
|
|
|
+ log.info("qrCodeUrl==>{}", qrCodeUrl);
|
|
|
+ ResponseEntity<byte[]> response = restTemplate.getForEntity(qrCodeUrl, byte[].class);
|
|
|
+
|
|
|
+
|
|
|
+ if (!response.getStatusCode().is2xxSuccessful()) {
|
|
|
+ throw new IOException("下载失败,HTTP 状态码: " + response.getStatusCodeValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 写入文件
|
|
|
+ try (FileOutputStream fos = new FileOutputStream(destFile)) {
|
|
|
+ fos.write(response.getBody());
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("二维码图片保存成功,文件名: {}", fileName);
|
|
|
+ return CompletableFuture.completedFuture(fileName);
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("保存二维码图片失败,ticket: {}", ticket, e);
|
|
|
+ return CompletableFuture.completedFuture("");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Async("taskExecutor")
|
|
|
+ public CompletableFuture<String> saveWeChatQrCodeImgHttpClient(String qrCodeUrl, String ticket, String uploadPath) {
|
|
|
+ try {
|
|
|
+ log.info("开始保存二维码图片,ticket: {}", ticket);
|
|
|
+
|
|
|
+ // 确保目录存在
|
|
|
+ File uploadDir = new File(uploadPath);
|
|
|
+ if (!uploadDir.exists() && !uploadDir.mkdirs()) {
|
|
|
+ throw new IOException("无法创建目录: " + uploadPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建目标文件路径
|
|
|
+ String fileName = ticket + ".jpg";
|
|
|
+ File destFile = new File(uploadPath, fileName);
|
|
|
+
|
|
|
+
|
|
|
+ log.info("qrCodeUrl==>{}", qrCodeUrl);
|
|
|
+ // 创建信任所有证书的HttpClient
|
|
|
+ HttpClient client = HttpClients.custom()
|
|
|
+ .setSSLContext(SSLContexts.custom().loadTrustMaterial(null, (chain, authType) -> true).build())
|
|
|
+ .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ HttpGet request = new HttpGet(qrCodeUrl);
|
|
|
+ try (CloseableHttpResponse response = (CloseableHttpResponse) client.execute(request);
|
|
|
+ FileOutputStream fos = new FileOutputStream(destFile)) {
|
|
|
+ if (response.getStatusLine().getStatusCode() != 200) {
|
|
|
+ throw new IOException("下载失败: " + response.getStatusLine());
|
|
|
+ }
|
|
|
+ response.getEntity().writeTo(fos);
|
|
|
+ }
|
|
|
+ return CompletableFuture.completedFuture(fileName);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return CompletableFuture.completedFuture("");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
@@ -113,12 +276,11 @@ public class WechatAccountController {
|
|
|
|
|
|
if (responseEntity.getStatusCode() == HttpStatus.OK) {
|
|
|
JSONObject json = JSONObject.parseObject(responseEntity.getBody());
|
|
|
- log.info("返回:" + json.toJSONString());
|
|
|
- if (json.getIntValue("errcode") == 0) {
|
|
|
- log.info("json==>"+json.toJSONString());
|
|
|
- }
|
|
|
+ assert json != null;
|
|
|
+ log.info("json.toJSONString()"+json.toJSONString());
|
|
|
+ return json.toJSONString();
|
|
|
}
|
|
|
- return "";
|
|
|
+ else return "";
|
|
|
}
|
|
|
|
|
|
}
|