微信小程序二维码获取

This commit is contained in:
donqi 2022-07-13 16:35:59 +08:00
parent be11078984
commit 09623068b6
2 changed files with 97 additions and 7 deletions

View File

@ -14,6 +14,8 @@ import com.ghy.system.domain.SysDeptConfig;
import com.ghy.system.service.ISysDeptConfigService;
import com.ghy.worker.domain.Worker;
import com.ghy.worker.service.WorkerService;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@ -157,6 +159,41 @@ public class WxController extends BaseController {
return wxUser.getString("openid");
}
@PostMapping("/unlimited/wxacode")
@ResponseBody
public AjaxResult getUnlimitedWxacode(@RequestBody JSONObject requestObj, HttpServletRequest request) throws Exception {
String base64Code = null;
try {
String scene = requestObj.getString("scene");
String envVersion = requestObj.getString("env_version");
String from = request.getHeader("from");
String deptId = request.getHeader("deptId");
SysDeptConfig sysDeptConfig = sysDeptConfigService.selectByDeptId(Long.parseLong(deptId));
String token = null;
if("customer".equals(from)) {
token = getAccessToken(sysDeptConfig.getWxAppId(), sysDeptConfig.getWxSecret());
} else {
token = getAccessToken(sysDeptConfig.getServWxAppId(), sysDeptConfig.getServWxSecret());
}
String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token;
JSONObject params = new JSONObject();
// params.put("access_token", token);
params.put("scene", scene);
params.put("env_version", envVersion);
byte[] byteRes = HttpUtils.sendPostForBuffer(url, params.toJSONString());
base64Code = new String(Base64.encodeBase64(byteRes));
} catch (Exception e) {
e.printStackTrace();
logger.error(ExceptionUtils.getStackTrace(e));
return AjaxResult.error(e.getMessage());
}
return AjaxResult.success("", base64Code);
}
private static String getAccessToken(String appId, String appSecret){
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ appId +"&secret=" + appSecret;
String result = HttpUtils.sendGet(url);

View File

@ -1,10 +1,6 @@
package com.ghy.common.utils.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.*;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.URL;
@ -23,7 +19,7 @@ import com.ghy.common.utils.StringUtils;
/**
* 通用http发送方法
*
*
* @author clunt
*/
public class HttpUtils
@ -189,6 +185,63 @@ public class HttpUtils
return result.toString();
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url 发送请求的 URL
* @param param 请求体内容
* @return 字节数组
*/
public static byte[] sendPostForBuffer(String url, String param) {
PrintWriter out = null;
InputStream in = null;
byte[] result = null;
try {
String urlNameString = url;
log.info("sendPost - {}", urlNameString);
URL realUrl = new URL(urlNameString);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestProperty("contentType", "utf-8");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
in = conn.getInputStream();
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int readLength = 0;
while ((readLength = in.read(data, 0, data.length)) > 0) {
swapStream.write(data, 0, readLength);
}
result = swapStream.toByteArray();
} catch (ConnectException e) {
log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
} catch (SocketTimeoutException e) {
log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
} catch (IOException e) {
log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
} catch (Exception e) {
log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
}
}
return result;
}
public static String sendSSLPost(String url, String param)
{
StringBuilder result = new StringBuilder();
@ -271,4 +324,4 @@ public class HttpUtils
return true;
}
}
}
}