12 changed files with 318 additions and 30 deletions
@ -0,0 +1,105 @@ |
|||
package com.ccsens.wechatutil.wxh5; |
|||
|
|||
import cn.hutool.core.util.StrUtil; |
|||
import cn.hutool.http.HttpRequest; |
|||
import com.ccsens.util.DateUtil; |
|||
import com.ccsens.util.JacksonUtil; |
|||
import com.ccsens.util.exception.BaseException; |
|||
import com.ccsens.wechatutil.bean.po.WxOauth2AccessToken; |
|||
import com.ccsens.wechatutil.bean.po.WxOauth2UserInfo; |
|||
import com.ccsens.wechatutil.util.WxCodeError; |
|||
import com.ccsens.wechatutil.util.WxConstant; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.io.IOException; |
|||
|
|||
/** |
|||
* @author 逗 |
|||
*/ |
|||
@Slf4j |
|||
@Component |
|||
public class H5SigninUtil { |
|||
|
|||
private static String appIdH5; |
|||
private static String secretH5; |
|||
|
|||
@Value("${h5.appId:}") |
|||
public void setAppIdOfficial(String appIdOfficial) { |
|||
H5SigninUtil.appIdH5 = appIdOfficial; |
|||
} |
|||
@Value("${h5.secret:}") |
|||
public void setApplication(String secretOfficial) { |
|||
H5SigninUtil.secretH5 = secretOfficial; |
|||
} |
|||
|
|||
public static WxOauth2UserInfo signinByH5(String code) { |
|||
return signinByH5(code, appIdH5, secretH5); |
|||
} |
|||
|
|||
public static WxOauth2UserInfo signinByH5(String code, String appId, String secret) { |
|||
//获取accessToken
|
|||
WxOauth2AccessToken wxOauth2AccessToken = getOauth2AccessToken(code, appId, secret); |
|||
//获取用户信息
|
|||
return getOauth2UserInfo(wxOauth2AccessToken.getAccessToken(), wxOauth2AccessToken.getOpenId()); |
|||
} |
|||
|
|||
/** |
|||
* 获取网页授权凭证 |
|||
* @param code OAuth2授权码 |
|||
* @return WxOauth2AccessToken |
|||
*/ |
|||
private static WxOauth2AccessToken getOauth2AccessToken(String code, String appId, String secret) { |
|||
WxOauth2AccessToken wxOauth2AccessToken; |
|||
//拼接访问路径
|
|||
String url = String.format(WxConstant.URL_GET_OAUTH2_ACCESS_TOKEN, appId, secret, code); |
|||
//调用微信接口
|
|||
String response = HttpRequest.get(url).execute().body(); |
|||
log.info("url: {}\nresponse: {}", url, response); |
|||
try { |
|||
if (StrUtil.isEmpty(response) || null == (wxOauth2AccessToken = JacksonUtil.jsonToBean(response, WxOauth2AccessToken.class))) { |
|||
throw new BaseException(WxCodeError.WX_HTTP_ERROR); |
|||
} |
|||
} catch (IOException e) { |
|||
throw new BaseException(e.getMessage()); |
|||
} |
|||
if (null != wxOauth2AccessToken.getErrcode()) { |
|||
throw new BaseException(wxOauth2AccessToken.getErrcode(), wxOauth2AccessToken.getErrmsg()); |
|||
} |
|||
if (StrUtil.isEmpty(wxOauth2AccessToken.getAccessToken())) { |
|||
throw new BaseException(WxCodeError.ACCESS_TOKEN_ERROR); |
|||
} |
|||
wxOauth2AccessToken.setCreatedAt(DateUtil.currentSeconds()); |
|||
return wxOauth2AccessToken; |
|||
} |
|||
|
|||
/** |
|||
* 通过网页授权accessToken获取用户信息 |
|||
* @param accessToken 网页授权接口调用凭证 |
|||
* @param openId 用户标识 |
|||
* @return SNSUserInfo |
|||
*/ |
|||
private static WxOauth2UserInfo getOauth2UserInfo(String accessToken, String openId) { |
|||
WxOauth2UserInfo wxOauth2UserInfo; |
|||
//拼接访问路径
|
|||
String url = String.format(WxConstant.URL_GET_OAUTH2_USER_INFO, accessToken, openId); |
|||
//调用微信接口
|
|||
String response = HttpRequest.get(url).execute().body(); |
|||
log.info("url: {}\nresponse: {}", url, response); |
|||
try { |
|||
if (StrUtil.isEmpty(response) || null == (wxOauth2UserInfo = JacksonUtil.jsonToBean(response, WxOauth2UserInfo.class))) { |
|||
throw new BaseException(WxCodeError.WX_HTTP_ERROR); |
|||
} |
|||
} catch (IOException e) { |
|||
throw new BaseException(e.getMessage()); |
|||
} |
|||
if (null != wxOauth2UserInfo.getErrcode()) { |
|||
throw new BaseException(wxOauth2UserInfo.getErrcode(), wxOauth2UserInfo.getErrmsg()); |
|||
} |
|||
if (StrUtil.isEmpty(wxOauth2UserInfo.getOpenId())) { |
|||
throw new BaseException(WxCodeError.OPENID_ERROR); |
|||
} |
|||
return wxOauth2UserInfo; |
|||
} |
|||
} |
@ -0,0 +1,122 @@ |
|||
package com.ccsens.wechatutil.wxofficial; |
|||
|
|||
import cn.hutool.core.collection.CollectionUtil; |
|||
import cn.hutool.core.date.DateUtil; |
|||
import cn.hutool.core.util.StrUtil; |
|||
import cn.hutool.http.HttpRequest; |
|||
import cn.hutool.json.JSON; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.ccsens.cloudutil.bean.tall.vo.UserVo; |
|||
import com.ccsens.util.*; |
|||
import com.ccsens.util.annotation.OperateType; |
|||
import com.ccsens.util.bean.message.common.InMessage; |
|||
import com.ccsens.util.bean.message.common.MessageConstant; |
|||
import com.ccsens.util.config.RabbitMQConfig; |
|||
import com.ccsens.util.exception.BaseException; |
|||
import com.ccsens.util.wx.WxGzhUtil; |
|||
import com.ccsens.wechatutil.bean.dto.WxTemplateMessage; |
|||
import com.ccsens.wechatutil.bean.po.WxBaseEntity; |
|||
import com.ccsens.wechatutil.bean.po.WxOauth2UserInfo; |
|||
import com.ccsens.wechatutil.util.WxCodeError; |
|||
import com.ccsens.wechatutil.util.WxConstant; |
|||
import com.ccsens.wechatutil.wxcommon.WxCommonUtil; |
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.BeanUtils; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.io.IOException; |
|||
import java.util.ArrayList; |
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* @author 逗 |
|||
*/ |
|||
@Slf4j |
|||
@Component |
|||
public class OfficialAccountMessageUtil { |
|||
|
|||
|
|||
private static String appIdOfficial; |
|||
private static String secretOfficial; |
|||
|
|||
@Value("${official.appId:}") |
|||
public void setAppIdOfficial(String appIdOfficial) { |
|||
OfficialAccountMessageUtil.appIdOfficial = appIdOfficial; |
|||
} |
|||
@Value("${official.secret:}") |
|||
public void setApplication(String secretOfficial) { |
|||
OfficialAccountMessageUtil.secretOfficial = secretOfficial; |
|||
} |
|||
|
|||
// public static void officialMessage() {
|
|||
// WxTemplateMessage message = new WxTemplateMessage();
|
|||
//
|
|||
// String url = String.format(WechatUtil.PROJECT_URL, 123);
|
|||
//// WxTemplateMessage.MiniProgram miniProgram = new WxTemplateMessage.MiniProgram(WechatUtil.appid,url);
|
|||
//
|
|||
//// message.setMiniprogram(miniProgram);
|
|||
//
|
|||
//
|
|||
// message.setTemplate_id(WxGzhUtil.Template.TASK_FINISH.templateId);
|
|||
//// WxTemplateMessage.TemplateData data = new WxTemplateMessage.TemplateData();
|
|||
//// data.setFirst(new WxTemplateMessage.TemplateSettings("测试项目"));
|
|||
//// data.setKeyword1(new WxTemplateMessage.TemplateSettings("测试任务"));
|
|||
//// data.setKeyword2(new WxTemplateMessage.TemplateSettings(DateUtil.now()));
|
|||
//
|
|||
// Map<String, WxTemplateMessage.TemplateSettings> data = new HashMap<>();
|
|||
// data.put("zzz", new WxTemplateMessage.TemplateSettings("测试fzz"));
|
|||
// data.put("first", new WxTemplateMessage.TemplateSettings("测试first"));
|
|||
// data.put("keyword1", new WxTemplateMessage.TemplateSettings("测试keyword1"));
|
|||
// data.put("keyword2", new WxTemplateMessage.TemplateSettings("测试keyword2"));
|
|||
// data.put("remark", new WxTemplateMessage.TemplateSettings("测试remark"));
|
|||
//
|
|||
// message.setData(data);
|
|||
//
|
|||
// List<String> openIdList = new ArrayList<>();
|
|||
// openIdList.add("oALvgw-_70_fXptB97xJpYDhMJRU");
|
|||
//
|
|||
// officialMessage(message,openIdList);
|
|||
// }
|
|||
|
|||
/** |
|||
* 公众号发送订阅消息(使用默认openId) |
|||
* @param templateMessage 消息 |
|||
* @param openIdList 推送的用户 |
|||
*/ |
|||
public static void officialMessage(WxTemplateMessage templateMessage, List<String> openIdList) { |
|||
officialMessage(templateMessage,openIdList,appIdOfficial, secretOfficial); |
|||
} |
|||
|
|||
/** |
|||
* 公众号发送订阅消息(传入openId) |
|||
* @param templateMessage 消息 |
|||
* @param openIdList 推送的用户 |
|||
*/ |
|||
public static void officialMessage(WxTemplateMessage templateMessage, List<String> openIdList,String appId, String secret) { |
|||
//判断是否有openId
|
|||
if (CollectionUtil.isNotEmpty(openIdList)) { |
|||
openIdList.forEach(openid -> { |
|||
//拼接访问路径
|
|||
String url = String.format(WxConstant.MESSAGE_TEMPLATE_SEND, WxCommonUtil.getAccessToken(appId, secret)); |
|||
//调用微信接口
|
|||
String response = RestTemplateUtil.postBody(url, templateMessage); |
|||
log.info("url: {}\nresponse: {}", url, response); |
|||
WxBaseEntity wxBaseEntity; |
|||
try { |
|||
if (StrUtil.isEmpty(response) || null == (wxBaseEntity = JacksonUtil.jsonToBean(response, WxBaseEntity.class))) { |
|||
throw new BaseException(WxCodeError.WX_HTTP_ERROR); |
|||
} |
|||
} catch (IOException e) { |
|||
throw new BaseException(e.getMessage()); |
|||
} |
|||
if (null != wxBaseEntity.getErrcode()) { |
|||
throw new BaseException(wxBaseEntity.getErrcode(), wxBaseEntity.getErrmsg()); |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue