package com.stylefeng.guns.modular.system.util;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import org.apache.commons.codec.binary.Base64;
|
import org.apache.commons.codec.digest.DigestUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.http.*;
|
import org.springframework.stereotype.Component;
|
import org.springframework.web.client.RestTemplate;
|
|
import java.io.ByteArrayInputStream;
|
import java.io.InputStream;
|
import java.io.UnsupportedEncodingException;
|
import java.net.URLDecoder;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 微信工具类
|
*/
|
@Component
|
public class WeChatUtil {
|
|
@Value("${wx.appletsAppid}")
|
private String wxAppletsAppid;
|
|
@Value("${wx.appletsAppSecret}")
|
private String wxAppletsAppSecret;
|
|
@Autowired
|
private RestTemplate restTemplate;
|
|
|
/**
|
* 小程序使用jscode获取openid
|
* @param jscode
|
* @return
|
*/
|
public Map<String, String> code2Session(String jscode){
|
String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + wxAppletsAppid + "&secret=" + wxAppletsAppSecret
|
+ "&js_code=" + jscode + "&grant_type=authorization_code";
|
String forObject = restTemplate.getForObject(url, String.class);
|
JSONObject jsonObject = JSON.parseObject(forObject);
|
int errcode = jsonObject.getIntValue("errcode");
|
Map<String, String> map = new HashMap<>();
|
if(errcode == 0){//成功
|
map.put("openid", jsonObject.getString("openid"));
|
map.put("sessionKey", jsonObject.getString("session_key"));
|
map.put("unionid", jsonObject.getString("unionid"));
|
return map;
|
}
|
if(errcode == -1){//系统繁忙,此时请开发者稍候再试
|
map.put("msg", jsonObject.getString("errmsg"));
|
return map;
|
}
|
if(errcode == 40029){//code 无效
|
map.put("msg", jsonObject.getString("errmsg"));
|
return map;
|
}
|
if(errcode == 45011){//频率限制,每个用户每分钟100次
|
map.put("msg", jsonObject.getString("errmsg"));
|
return map;
|
}
|
return null;
|
}
|
|
|
/**
|
* 通过config接口注入权限验证配置(公众号)
|
* 附录1-JS-SDK使用权限签名算法,
|
* @return
|
*/
|
public Map<String,Object> getSignatureConfig(String url){
|
//获取token
|
try {
|
url = URLDecoder.decode(url, "UTF-8");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
String ticket = getJSApiTicket();
|
String noncestr = UUIDUtil.getRandomCode();
|
Long timestamp = System.currentTimeMillis();
|
String content = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url=" + url;
|
String signature = DigestUtils.sha1Hex(content);
|
Map<String,Object> map=new HashMap<>();
|
map.put("appId", "wx0e72f86394831b34");
|
map.put("timestamp", timestamp);
|
map.put("nonceStr", noncestr);
|
map.put("signature", signature);
|
return map;
|
}
|
|
|
|
/***
|
* 获取jsapiTicket(公众号)
|
* 来源 www.vxzsk.com
|
* @return
|
*/
|
public String getJSApiTicket(){
|
//获取token
|
String acess_token= this.getAccessToken();
|
String urlStr = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + acess_token + "&type=jsapi";
|
String backData = restTemplate.getForObject(urlStr, String.class);
|
System.out.println(backData);
|
String ticket = JSONObject.parseObject(backData).getString("ticket");
|
return ticket;
|
}
|
|
|
/***
|
* 获取acess_token (公众号)
|
* 来源www.vxzsk.com
|
* @return
|
*/
|
public String getAccessToken(){
|
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx0e72f86394831b34&secret=930f857abc74f7bb5cbd89e1544c5669";
|
String backData = restTemplate.getForObject(url, String.class);
|
String accessToken = JSONObject.parseObject(backData).getString("access_token");
|
return accessToken;
|
}
|
/***
|
* 获取acess_token (小程序)
|
* 来源www.vxzsk.com
|
* @return
|
*/
|
public String getAccessToken2(){
|
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxd997f17beb1dd7a4&secret=a403388bafbfceb639e0a451e5403a12";
|
String backData = restTemplate.getForObject(url, String.class);
|
String accessToken = JSONObject.parseObject(backData).getString("access_token");
|
return accessToken;
|
}
|
/**
|
* 获取小程序二维码
|
* @param page 跳转页 例如 pages/index/index
|
* @param scene 参数 a=1&b=2
|
*/
|
public InputStream getwxacodeunlimit(String page, String scene, String envVersion) throws Exception{
|
try {
|
//String wxAppletsAccessToken = redisUtil.getValue("wxAppletsAccessToken");
|
String wxAppletsAccessToken = getAccessToken2();
|
String url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + wxAppletsAccessToken;
|
Map<String, Object> param = new HashMap<>();
|
param.put("scene", scene);
|
param.put("page", page);
|
param.put("env_version", envVersion);
|
HttpHeaders httpHeaders = new HttpHeaders();
|
MediaType type=MediaType.parseMediaType("application/json;charset=UTF-8");
|
httpHeaders.setContentType(type);
|
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(param, httpHeaders);
|
ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class, new Object[0]);
|
String body1 = exchange.getBody();
|
// System.err.println(body1);
|
ResponseEntity<byte[]> entity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
|
byte[] body = entity.getBody();
|
System.err.println(JSON.toJSON(entity));
|
System.err.println(Base64.encodeBase64String(body));
|
return new ByteArrayInputStream(body);
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
return null;
|
}
|
}
|