|
|
@@ -1,5 +1,6 @@
|
|
|
package org.springblade.wx.controller;
|
|
|
|
|
|
+import cn.hutool.core.lang.Assert;
|
|
|
import cn.hutool.crypto.Mode;
|
|
|
import cn.hutool.crypto.Padding;
|
|
|
import cn.hutool.crypto.symmetric.AES;
|
|
|
@@ -10,19 +11,39 @@ import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springblade.core.oss.model.BladeFile;
|
|
|
import org.springblade.core.tool.api.R;
|
|
|
import org.springblade.core.tool.utils.StringUtil;
|
|
|
+import org.springblade.modules.resource.builder.oss.OssBuilder;
|
|
|
import org.springblade.wx.config.ConfigForClient;
|
|
|
+import org.springblade.wx.constant.MiniProgramType;
|
|
|
+import org.springblade.wx.constant.WeChatApi;
|
|
|
import org.springblade.wx.dto.DataDto;
|
|
|
import org.springblade.wx.dto.JumpWxa;
|
|
|
import org.springblade.wx.dto.SchemeDto;
|
|
|
+import org.springblade.wx.service.WeChatService;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.mock.web.MockMultipartFile;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import sun.misc.BASE64Decoder;
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
-import java.util.Base64;
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.*;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
|
* @author cy-computer
|
|
|
@@ -31,11 +52,18 @@ import java.util.concurrent.TimeUnit;
|
|
|
@Slf4j
|
|
|
@RequestMapping("/wx-app")
|
|
|
@Api(tags = "微信小程序登录模块")
|
|
|
+@AllArgsConstructor
|
|
|
public class WxAppController {
|
|
|
- @Resource
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 对象存储构建类
|
|
|
+ */
|
|
|
+ private final OssBuilder ossBuilder;
|
|
|
+
|
|
|
private ConfigForClient configForClient;
|
|
|
- @Resource
|
|
|
private StringRedisTemplate stringRedisTemplate;
|
|
|
+ private WeChatService weChatService;
|
|
|
+
|
|
|
|
|
|
private static final String ACCESS_TOKEN = "access_token:";
|
|
|
|
|
|
@@ -105,4 +133,54 @@ public class WxAppController {
|
|
|
return R.data(jsonObject);
|
|
|
}
|
|
|
|
|
|
+ @ApiOperation("获取小程序码")
|
|
|
+ @GetMapping("/createWxaQrCode")
|
|
|
+ public R createWxaQrCode(String path,@RequestParam String type) throws IOException {
|
|
|
+ //根据类型获取accessToken
|
|
|
+ String accessToken = weChatService.fetchAccessToken(MiniProgramType.valueOf(type));
|
|
|
+ Assert.notNull(accessToken, "获取accessToken失败");
|
|
|
+
|
|
|
+ String url = WeChatApi.createWxaQrCode.getApi() + accessToken;
|
|
|
+ RestTemplate rest = new RestTemplate();
|
|
|
+
|
|
|
+
|
|
|
+ Map<String,Object> param = new HashMap<>(2);
|
|
|
+ param.put("path", path);
|
|
|
+ param.put("width", 430);
|
|
|
+
|
|
|
+ MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
|
|
|
+ HttpEntity requestEntity = new HttpEntity(param, headers);
|
|
|
+ ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
|
|
|
+ log.info("调用小程序生成微信永久二维码URL接口返回结果:" + entity.getBody());
|
|
|
+
|
|
|
+ byte[] result = entity.getBody();
|
|
|
+ BladeFile bladeFile = this.uploadImage(result);
|
|
|
+ return R.data(bladeFile);
|
|
|
+ }
|
|
|
+
|
|
|
+ private BladeFile uploadImage(byte[] bytes) throws IOException {
|
|
|
+ BladeFile bladeFile = new BladeFile();
|
|
|
+
|
|
|
+ InputStream inputStream = null;
|
|
|
+ try {
|
|
|
+ inputStream = new ByteArrayInputStream(bytes);
|
|
|
+ MultipartFile multipartFile =new MockMultipartFile("file", "shopQrCode.jpg", "text/plain", inputStream);
|
|
|
+ //上传到对象存储
|
|
|
+ bladeFile = ossBuilder.template().putFile(multipartFile.getOriginalFilename(), multipartFile.getInputStream());
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally {
|
|
|
+ if (inputStream != null) {
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return bladeFile;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String replaceEnter(String str){
|
|
|
+ String reg ="[\n-\r]";
|
|
|
+ Pattern p = Pattern.compile(reg);
|
|
|
+ Matcher m = p.matcher(str);
|
|
|
+ return m.replaceAll("");
|
|
|
+ }
|
|
|
}
|