|
@@ -3,6 +3,7 @@ package com.caimei.components;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.caimei.model.ResponseJson;
|
|
import com.caimei.model.ResponseJson;
|
|
|
|
+import com.caimei.util.FastDfsUtil;
|
|
import com.caimei.util.HttpClientUtil;
|
|
import com.caimei.util.HttpClientUtil;
|
|
import com.caimei.util.HttpRequest;
|
|
import com.caimei.util.HttpRequest;
|
|
import com.caimei.util.OkHttpUtil;
|
|
import com.caimei.util.OkHttpUtil;
|
|
@@ -15,18 +16,21 @@ import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
import javax.crypto.Cipher;
|
|
import javax.crypto.Cipher;
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
-import java.io.ByteArrayInputStream;
|
|
|
|
-import java.io.FileOutputStream;
|
|
|
|
|
|
+import java.io.*;
|
|
import java.nio.Buffer;
|
|
import java.nio.Buffer;
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
import java.security.AlgorithmParameters;
|
|
import java.security.AlgorithmParameters;
|
|
import java.security.Security;
|
|
import java.security.Security;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
import java.util.Optional;
|
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 微信 服务工具类
|
|
* 微信 服务工具类
|
|
@@ -42,6 +46,14 @@ public class WeChatService {
|
|
private String heHeAppId;
|
|
private String heHeAppId;
|
|
@Value("${wx.AppSecret}")
|
|
@Value("${wx.AppSecret}")
|
|
private String heHeAppSecret;
|
|
private String heHeAppSecret;
|
|
|
|
+ @Value("${caimei.cloudApi}")
|
|
|
|
+ private String cloudApi;
|
|
|
|
+ @Value("${spring.profiles.active}")
|
|
|
|
+ private String profile;
|
|
|
|
+ @Value("${caimei.imageDomain}")
|
|
|
|
+ private String imageDomain;
|
|
|
|
+ @Resource
|
|
|
|
+ private FastDfsUtil fastDfsUtil;
|
|
|
|
|
|
private RedisService redisService;
|
|
private RedisService redisService;
|
|
@Autowired
|
|
@Autowired
|
|
@@ -188,7 +200,7 @@ public class WeChatService {
|
|
Map<String, Object> map = JSONObject.parseObject(result, Map.class);
|
|
Map<String, Object> map = JSONObject.parseObject(result, Map.class);
|
|
access_token = (String) map.get("access_token");
|
|
access_token = (String) map.get("access_token");
|
|
// 将token存入redis, access_token的有效期目前为2个小时(redis存1.5小时)
|
|
// 将token存入redis, access_token的有效期目前为2个小时(redis存1.5小时)
|
|
- redisService.set("access_token:"+heHeAppId, access_token, 5400L);
|
|
|
|
|
|
+ redisService.set("access_token:"+heHeAppId, access_token, 1800L);
|
|
return access_token;
|
|
return access_token;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -218,9 +230,9 @@ public class WeChatService {
|
|
/**
|
|
/**
|
|
* 根据accessToken生成小程序二维码
|
|
* 根据accessToken生成小程序二维码
|
|
*/
|
|
*/
|
|
- public String generateWxacode(String access_token,String params) throws Exception {
|
|
|
|
|
|
+ public String generateWxacodeBySelf(String access_token,String params) throws Exception {
|
|
String requestUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + access_token;
|
|
String requestUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + access_token;
|
|
- ByteArrayInputStream inputStream = HttpClientUtil.sendPost(requestUrl, params);
|
|
|
|
|
|
+ /*ByteArrayInputStream inputStream = HttpClientUtil.sendPost(requestUrl, params);
|
|
FileOutputStream outputStream = new FileOutputStream("D:/123.png");
|
|
FileOutputStream outputStream = new FileOutputStream("D:/123.png");
|
|
int i = 0;
|
|
int i = 0;
|
|
byte[] buffer = new byte[200];
|
|
byte[] buffer = new byte[200];
|
|
@@ -229,7 +241,7 @@ public class WeChatService {
|
|
}
|
|
}
|
|
outputStream.flush();
|
|
outputStream.flush();
|
|
outputStream.close();
|
|
outputStream.close();
|
|
- inputStream.close();
|
|
|
|
|
|
+ inputStream.close();*/
|
|
// 成功时微信服务直接返回小程序码的二进制数据,字节数组的长度会很大(大概是60000多)
|
|
// 成功时微信服务直接返回小程序码的二进制数据,字节数组的长度会很大(大概是60000多)
|
|
/*if (entity.getStatusCode() != HttpStatus.OK || buffer == null || buffer.length < 200) {
|
|
/*if (entity.getStatusCode() != HttpStatus.OK || buffer == null || buffer.length < 200) {
|
|
log.error("请求获取小程序码失败, response: {}", Optional.ofNullable(buffer).map(String::new).orElse("response is null"));
|
|
log.error("请求获取小程序码失败, response: {}", Optional.ofNullable(buffer).map(String::new).orElse("response is null"));
|
|
@@ -238,5 +250,53 @@ public class WeChatService {
|
|
return "success";
|
|
return "success";
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 根据accessToken生成小程序二维码
|
|
|
|
+ */
|
|
|
|
+ public ResponseJson<String> generateWxacode(String access_token,String params) throws Exception {
|
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + access_token;
|
|
|
|
+ ByteArrayInputStream inputStream = HttpClientUtil.sendPost(requestUrl, params);
|
|
|
|
+ try {
|
|
|
|
+ String fileUrl = null;
|
|
|
|
+ String url = saveFile(inputStream);
|
|
|
|
+ fileUrl = imageDomain + "/" + url;
|
|
|
|
+ log.info("【图片上传】>>>>>>>>>>>>>>>>上传成功:" + fileUrl);
|
|
|
|
+ return ResponseJson.success(fileUrl);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.error("【图片上传】>>>>>>>>>>>>>>>>上传失败:" + e);
|
|
|
|
+ return ResponseJson.error("图片上传失败", null);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 保存文件到FastDFS
|
|
|
|
+ */
|
|
|
|
+ private String saveFile(ByteArrayInputStream inputStream) throws IOException {
|
|
|
|
+ String randomStr = UUID.randomUUID().toString();
|
|
|
|
+ String name = ".jpeg";
|
|
|
|
+ // 图片暂存本地服务器路径
|
|
|
|
+ String filePath = "/mnt/newdatadrive/data/runtime/jar-instance/mall2c/tempImage/";
|
|
|
|
+ if ("dev".equals(profile)){
|
|
|
|
+ filePath = "D:\\";
|
|
|
|
+ }
|
|
|
|
+ filePath += randomStr + name;
|
|
|
|
+ FileOutputStream outputStream = new FileOutputStream(filePath);
|
|
|
|
+ int i = 0;
|
|
|
|
+ byte[] buffer = new byte[200];
|
|
|
|
+ while ((i = inputStream.read(buffer)) != -1) {
|
|
|
|
+ outputStream.write(buffer, 0, i);
|
|
|
|
+ }
|
|
|
|
+ outputStream.flush();
|
|
|
|
+ outputStream.close();
|
|
|
|
+ inputStream.close();
|
|
|
|
+ // 临时图片
|
|
|
|
+ File tempFile = new File(filePath);
|
|
|
|
+ log.info("【图片上传】>>>>>>>>>>>>>>>>图片临时路径:" + filePath);
|
|
|
|
+ String fileUrl = fastDfsUtil.uploadFile(filePath);
|
|
|
|
+ // 删除临时图片
|
|
|
|
+ boolean delete = tempFile.delete();
|
|
|
|
+ log.info("【图片上传】>>>>>>>>>>>>>>>>删除临时图片:" + delete);
|
|
|
|
+ return fileUrl;
|
|
|
|
+ }
|
|
}
|
|
}
|