package com.nuvole.util;
|
|
import cn.binarywang.wx.miniapp.api.WxMaService;
|
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
|
import cn.binarywang.wx.miniapp.bean.WxMaTemplateData;
|
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
|
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
|
import cn.hutool.http.HttpUtil;
|
import cn.hutool.json.JSONUtil;
|
import com.alibaba.fastjson.JSONObject;
|
import com.nuvole.common.domain.dto.AppDTO;
|
import com.nuvole.common.domain.wx.PushMsg;
|
import lombok.extern.slf4j.Slf4j;
|
import me.chanjar.weixin.common.error.WxErrorException;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpMethod;
|
import org.springframework.http.MediaType;
|
import org.springframework.http.ResponseEntity;
|
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.util.HtmlUtils;
|
|
import java.util.*;
|
|
/**
|
* 小程序工具类
|
*
|
* @author liujun
|
* @Date 2019/5/27 16:40
|
*/
|
@Slf4j
|
public class WxUtil {
|
|
/**
|
* 获取临时登录凭证code
|
*
|
* @param appid AppID(小程序ID)
|
* @param secret AppSecret(小程序密钥)
|
* @param jsCode 登录时获取的Code
|
* @return
|
*/
|
public static String getOpenidSessionKey(String appid, String secret, String jsCode) {
|
String url = "https://api.weixin.qq.com/sns/jscode2session";
|
Map<String, Object> paramMap = new HashMap<>(3);
|
paramMap.put("appid", appid);
|
paramMap.put("secret", secret);
|
paramMap.put("js_code", jsCode);
|
paramMap.put("grant_type", "authorization_code");
|
return HttpUtil.get(url, paramMap);
|
}
|
|
/**
|
* 获取AccessToken
|
*
|
* @param appid AppID(小程序ID)
|
* @param secret AppSecret(小程序密钥)
|
* @return
|
*/
|
public static String getAccessToken(String appid, String secret) {
|
String url = "https://api.weixin.qq.com/cgi-bin/token";
|
Map<String, Object> paramMap = new HashMap<>(3);
|
paramMap.put("appid", appid);
|
paramMap.put("secret", secret);
|
paramMap.put("grant_type", "client_credential");
|
String s = HttpUtil.get(url, paramMap);
|
log.info("获取AccessToken 参数= 》appid:{},secret:{}", appid, secret);
|
log.info("获取AccessToken = 》{}", s);
|
return s;
|
}
|
|
/**
|
* 获取AccessToken
|
*
|
* @param accessToken getAccessToken方法获取的access_token
|
* @return
|
*/
|
public static String getJsapTicket(String accessToken) {
|
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
|
Map<String, Object> paramMap = new HashMap<>(3);
|
paramMap.put("type", "jsapi");
|
paramMap.put("access_token", accessToken);
|
return HttpUtil.get(url, paramMap);
|
}
|
|
/**
|
* Createed by PKZ Date 2019/6/18 17:11 Description:微信公众号获取access_token
|
**/
|
public static String getWechatAccessToken(String appid, String secret, String code) {
|
String url = "https://api.weixin.qq.com/sns/oauth2/access_token";
|
Map<String, Object> paramMap = new HashMap<>(3);
|
paramMap.put("appid", appid);
|
paramMap.put("secret", secret);
|
paramMap.put("code", code);
|
paramMap.put("grant_type", "authorization_code");
|
return HttpUtil.get(url, paramMap);
|
}
|
|
/**
|
* Createed by PKZ Date 2019/6/18 17:13 Description:微信公众号获取用户信息
|
**/
|
public static String getWechatUserInfo(String access_token, String openid) {
|
String url = "https://api.weixin.qq.com/sns/userinfo";
|
Map<String, Object> paramMap = new HashMap<>(3);
|
paramMap.put("access_token", access_token);
|
paramMap.put("openid", openid);
|
paramMap.put("lang", "zh_CN");
|
return HttpUtil.get(url, paramMap);
|
}
|
|
/**
|
* 该接口用于将 code 换取用户手机号
|
*
|
* @param access_token
|
* @param code
|
* @return
|
*/
|
public static String getUserPhoneNumber(String access_token, String code) {
|
String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + access_token;
|
Map<String, Object> paramMap = new HashMap<>(1);
|
paramMap.put("code", code);
|
String result = HttpUtil.post(url, JSONUtil.toJsonStr(paramMap));
|
log.info("getUserPhoneNumber=>{}", result);
|
JSONObject resultInfo = JSONObject.parseObject(result);
|
if (resultInfo.getIntValue("errcode") == 0) {
|
return resultInfo.getJSONObject("phone_info").getString("purePhoneNumber");
|
}
|
return null;
|
}
|
|
/**
|
* 获取 URL Link
|
*
|
* @param access_token
|
* @return
|
*/
|
public static String generateUrllink(String access_token) {
|
String url = "https://api.weixin.qq.com/wxa/generate_urllink?access_token=" + access_token;
|
Map<String, Object> paramMap = new HashMap<>(1);
|
String result = HttpUtil.post(url, JSONUtil.toJsonStr(paramMap));
|
log.info("generateUrllink=>{}", result);
|
JSONObject resultInfo = JSONObject.parseObject(result);
|
if (resultInfo.getIntValue("errcode") == 0) {
|
return resultInfo.getString("url_link");
|
}
|
return null;
|
}
|
|
|
/*
|
* @Author : liu.q [916000612@qq.com]
|
*
|
* @Date : 2019-06-27 09:49
|
*
|
* @Description :获取小程序码(前提是已发布)
|
*
|
* @secne 参数
|
*
|
* @width 码宽
|
*
|
* @page 码跳转路径
|
*/
|
public static byte[] getQRcode(String access_token, String scene, int width, String page) {
|
RestTemplate rest = new RestTemplate();
|
byte[] result = null;
|
try {
|
String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + access_token;
|
Map<String, Object> param = new HashMap<>();
|
param.put("scene", scene);
|
// param.put("access_token", access_token); //access_token接在url后
|
param.put("page", page);
|
param.put("width", width);
|
param.put("auto_color", true);
|
Map<String, Object> line_color = new HashMap<>();
|
line_color.put("r", 54);
|
line_color.put("g", 56);
|
line_color.put("b", 115);
|
param.put("line_color", line_color);
|
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
|
AppDTO sto = new AppDTO();
|
AppDTO s = new AppDTO();
|
sto.setMap(param);
|
sto.setPro(url);
|
BeanUtils.copyProperties(sto, s);
|
// String result22 = HttpUtil.post(url, JSONUtil.toJsonStr(param));
|
// log.info("getUserPhoneNumber=>{}",result22);
|
HttpEntity requestEntity = new HttpEntity(s.getMap(), headers);
|
ResponseEntity<byte[]> entity = rest.exchange(s.getPro(), HttpMethod.POST, requestEntity, byte[].class,
|
new Object[0]);
|
result = entity.getBody();
|
if (entity.getHeaders().getContentType().isCompatibleWith(MediaType.APPLICATION_JSON)) {
|
log.error(new String(result));
|
return null;
|
}
|
System.out.println(entity.toString());
|
|
String imageStr = Base64.getEncoder().encodeToString(result);
|
imageStr = HtmlUtils.htmlEscape(imageStr);
|
result = Base64.getDecoder().decode(imageStr);
|
} catch (Exception e) {
|
e.printStackTrace();
|
log.error("调用小程序生成微信永久小程序码URL接口异常", e);
|
}
|
return result;
|
}
|
|
/**
|
* 推送消息
|
*
|
* @param appId
|
* @param secret
|
* @param openId
|
* @param formId
|
* @param templateId
|
* @param page
|
* @param items
|
*/
|
public static void push(String appId, String secret, String openId, String formId, String templateId, String page,
|
List<String> items) {
|
// 1,配置小程序信息
|
WxMaInMemoryConfig wxConfig = new WxMaInMemoryConfig();
|
// 小程序appid
|
wxConfig.setAppid(appId);
|
// 小程序AppSecret
|
wxConfig.setSecret(secret);
|
WxMaService wxMaService = new WxMaServiceImpl();
|
wxMaService.setWxMaConfig(wxConfig);
|
// 2,设置模版信息(keyword1:类型,keyword2:内容)
|
List<WxMaTemplateData> templateDataList = new ArrayList<>(items.size());
|
for (int i = 0; i < items.size(); i++) {
|
WxMaTemplateData data = new WxMaTemplateData("keyword" + (i + 1), items.get(i));
|
templateDataList.add(data);
|
}
|
// 3,设置推送消息
|
WxMaTemplateMessage templateMessage = WxMaTemplateMessage.builder().toUser(openId).formId(formId)
|
.templateId(templateId).data(templateDataList).page(page).build();
|
// 4,发起推送
|
try {
|
wxMaService.getMsgService().sendTemplateMsg(templateMessage);
|
} catch (WxErrorException e) {
|
log.info("任务失败:" + e.getMessage());
|
}
|
}
|
|
/* *
|
* 小程序-订阅消息-推送消息
|
*
|
* @author lc
|
* @date 2019/12/30 9:52
|
*/
|
public static String pushSend(PushMsg pushMsg, String accessToken) {
|
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
|
try {
|
System.out.println("is :" + JSONUtil.toJsonStr(pushMsg));
|
String result = HttpUtil.post(url, JSONUtil.toJsonStr(pushMsg));
|
log.info("is push result:" + result);
|
return result;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/* *
|
* 公众号-模板消息
|
*
|
*/
|
public static String h5PubpushSend(PushMsg pushMsg, String accessToken) {
|
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
|
try {
|
System.out.println("is :" + JSONUtil.toJsonStr(pushMsg));
|
String result = HttpUtil.post(url, JSONUtil.toJsonStr(pushMsg));
|
log.info("is push result:" + result);
|
return result;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
|
}
|