You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					136 lines
				
				5.2 KiB
			
		
		
			
		
	
	
					136 lines
				
				5.2 KiB
			| 
											2 years ago
										 | package com.ynxbd.wx.wxfactory.base.auth;
 | ||
|  | 
 | ||
|  | import com.ynxbd.common.helper.common.JsonHelper;
 | ||
|  | import com.ynxbd.common.helper.http.OkHttpHelper;
 | ||
|  | import com.ynxbd.wx.wxfactory.WxCacheHelper;
 | ||
|  | import com.ynxbd.wx.wxfactory.base.auth.models.RespAccessToken;
 | ||
|  | import com.ynxbd.wx.wxfactory.base.auth.models.RespJsapiTicket;
 | ||
|  | import com.ynxbd.wx.wxfactory.bean.SnsOath2AccessToken;
 | ||
|  | import com.ynxbd.wx.wxfactory.bean.SnsUserInfo;
 | ||
|  | import lombok.NoArgsConstructor;
 | ||
|  | import lombok.extern.slf4j.Slf4j;
 | ||
|  | import weixin.popular.util.EmojiUtil;
 | ||
|  | 
 | ||
|  | @Slf4j
 | ||
|  | @NoArgsConstructor
 | ||
|  | public class Client {
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 获取微信token
 | ||
|  |      *
 | ||
|  |      * @param appId     appId
 | ||
|  |      * @param appSecret appSecret
 | ||
|  |      * @return accessToken
 | ||
|  |      */
 | ||
|  |     public synchronized RespAccessToken getAccessToken(String appId, String appSecret) {
 | ||
|  |         String respJson = OkHttpHelper.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential", params -> {
 | ||
|  |             params.put("appid", appId);
 | ||
|  |             params.put("secret", appSecret);
 | ||
|  |         });
 | ||
|  |         if (respJson == null) {
 | ||
|  |             return new RespAccessToken();
 | ||
|  |         }
 | ||
|  |         RespAccessToken response = JsonHelper.parseObject(respJson, RespAccessToken.class);
 | ||
|  |         if (response == null) {
 | ||
|  |             log.error("[access_token]请求失败 resp={}", respJson);
 | ||
|  |             return new RespAccessToken();
 | ||
|  |         }
 | ||
|  |         return response;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public synchronized RespJsapiTicket getJsapiTicket() {
 | ||
|  |         String respJson = OkHttpHelper.get("https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi", params -> {
 | ||
|  |             params.put("access_token", WxCacheHelper.getAccessToken());
 | ||
|  |         });
 | ||
|  |         if (respJson == null) {
 | ||
|  |             return new RespJsapiTicket();
 | ||
|  |         }
 | ||
|  |         RespJsapiTicket response = JsonHelper.parseObject(respJson, RespJsapiTicket.class);
 | ||
|  |         if (response == null) {
 | ||
|  |             log.error("[jsapi_ticket]请求失败 resp={}", respJson);
 | ||
|  |             return new RespJsapiTicket();
 | ||
|  |         }
 | ||
|  |         return response;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 获取微信token
 | ||
|  |      *
 | ||
|  |      * @param appId     appId
 | ||
|  |      * @param appSecret appSecret
 | ||
|  |      * @return accessToken
 | ||
|  |      */
 | ||
|  |     public SnsOath2AccessToken oauth2AccessToken(String appId, String appSecret, String code) {
 | ||
|  |         String respJson = OkHttpHelper.get("https://api.weixin.qq.com/sns/oauth2/access_token", params -> {
 | ||
|  |             params.put("appid", appId);
 | ||
|  |             params.put("secret", appSecret);
 | ||
|  |             params.put("code", code);
 | ||
|  |             params.put("grant_type", "authorization_code");
 | ||
|  |         });
 | ||
|  |         if (respJson == null) {
 | ||
|  |             log.error("[oath2]请求失败");
 | ||
|  |             return null;
 | ||
|  |         }
 | ||
|  |         SnsOath2AccessToken response = JsonHelper.parseObject(respJson, SnsOath2AccessToken.class);
 | ||
|  |         if (response == null) {
 | ||
|  |             log.error("[oath2]请求失败 resp={}", respJson);
 | ||
|  |             return null;
 | ||
|  |         }
 | ||
|  |         if (!response.isSuccess()) {
 | ||
|  |             log.error("[oath2]请求失败 errCode={}, errMsg={}", response.getErrCode(), response.getErrMsg());
 | ||
|  |             return null;
 | ||
|  |         }
 | ||
|  |         return response;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public SnsUserInfo snsUserInfo(String accessToken, String openid, String lang, Integer emoji) {
 | ||
|  |         String respJson = OkHttpHelper.get("https://api.weixin.qq.com/sns/userinfo", params -> {
 | ||
|  |             params.put("access_token", accessToken);
 | ||
|  |             params.put("openid", openid);
 | ||
|  |             params.put("lang", lang);
 | ||
|  |         });
 | ||
|  |         if (respJson == null) {
 | ||
|  |             log.error("[sns-user_info]请求失败");
 | ||
|  |             return null;
 | ||
|  |         }
 | ||
|  |         SnsUserInfo response = JsonHelper.parseObject(respJson, SnsUserInfo.class);
 | ||
|  |         if (response == null) {
 | ||
|  |             log.error("[sns-user_info]请求失败 respJson={}", respJson);
 | ||
|  |             return null;
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         if (!response.isSuccess()) {
 | ||
|  |             log.error("[sns-user_info]请求失败 errCode={}, errMsg={}", response.getErrCode(), response.getErrMsg());
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         if (emoji != 0 && response.getNickname() != null) {
 | ||
|  |             response.setNickname_emoji(EmojiUtil.parse(response.getNickname(), emoji));
 | ||
|  |         }
 | ||
|  |         return response;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  | 
 | ||
|  | //    public static String getSnsToken() {
 | ||
|  | //        if (ACCESS_TOKEN_CACHE == null) {
 | ||
|  | //            createAccessTokenCache();
 | ||
|  | //        }
 | ||
|  | //        String accessToken = ACCESS_TOKEN_CACHE.get("access_token");
 | ||
|  | //        if (ObjectUtils.isEmpty(accessToken)) {
 | ||
|  | //            Map<String, Object> map = HttpClientHelper.createParamsMap();
 | ||
|  | //            map.put("appid", WeChatConfig.getAppId());
 | ||
|  | //            map.put("secret", WeChatConfig.getAppSecret());
 | ||
|  | //            JSONObject jsonObject = HttpClientHelper.getJson("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential", map, null);
 | ||
|  | //            if (jsonObject == null) {
 | ||
|  | //                log.error("[微信]获取accessToken失败{}", JSONObject.toJSONString(jsonObject));
 | ||
|  | //                return null;
 | ||
|  | //            }
 | ||
|  | //            accessToken = jsonObject.getString("access_token");
 | ||
|  | //            if (accessToken != null) {
 | ||
|  | //                ACCESS_TOKEN_CACHE.put("access_token", accessToken);
 | ||
|  | //            }
 | ||
|  | //        }
 | ||
|  | //        return accessToken;
 | ||
|  | //    }
 | ||
|  | }
 |