|
@@ -0,0 +1,104 @@
|
|
|
|
+package com.flyer.foster.util;
|
|
|
|
+
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.flyer.exception.BusinessException;
|
|
|
|
+import com.flyer.foster.pojo.Code2SessionResp;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
|
+import org.springframework.http.MediaType;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.web.client.RestClientException;
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
+
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 微信小程序api工具类
|
|
|
|
+ */
|
|
|
|
+@Component
|
|
|
|
+@Slf4j
|
|
|
|
+public class WeChatApiUtil {
|
|
|
|
+ @Autowired
|
|
|
|
+ private RestTemplate restTemplate;
|
|
|
|
+
|
|
|
|
+ @PostConstruct
|
|
|
|
+ public void initRestTemplate() {
|
|
|
|
+ // 解决调用微信服务返回信息无法反序列化
|
|
|
|
+// restTemplate.getMessageConverters().add(new WxMappingJackson2HttpMessageConverter());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 根据code(临时调用凭证),请求微信api获取用户unionid
|
|
|
|
+ *
|
|
|
|
+ * @param code 前端调用微信登录接口生成的临时凭证
|
|
|
|
+ * @param appId 微信小程序的appId
|
|
|
|
+ * @param appSecret 小程序appId对应的秘钥
|
|
|
|
+ */
|
|
|
|
+ public Code2SessionResp getCode2Session(String code, String appId, String appSecret) {
|
|
|
|
+ String url = "https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&js_code={code}&grant_type={authorization_code}";
|
|
|
|
+ HashMap<String, String> map = new HashMap<>();
|
|
|
|
+ map.put("appid", appId);
|
|
|
|
+ map.put("secret", appSecret);
|
|
|
|
+ map.put("code", code);
|
|
|
|
+ map.put("authorization_code", "authorization_code");
|
|
|
|
+ Code2SessionResp resp = new Code2SessionResp();
|
|
|
|
+ try {
|
|
|
|
+ resp = restTemplate.getForObject(url, Code2SessionResp.class, map);
|
|
|
|
+ } catch (RestClientException e) {
|
|
|
|
+ throw new BusinessException("调用微信服务错误");
|
|
|
|
+ }
|
|
|
|
+ return resp;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取微信调用凭证
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public String getAccessToken(String appId, String secret) {
|
|
|
|
+ String accessToken = null;
|
|
|
|
+ String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type={grantType}&appid={appId}&secret={secret}";
|
|
|
|
+ HashMap<String, String> map = new HashMap<>();
|
|
|
|
+ map.put("grantType", "client_credential");
|
|
|
|
+ map.put("appId", appId);
|
|
|
|
+ map.put("secret", secret);
|
|
|
|
+
|
|
|
|
+ JSONObject res;
|
|
|
|
+ try {
|
|
|
|
+ res = restTemplate.getForObject(url, JSONObject.class, map);
|
|
|
|
+ if (res != null) {
|
|
|
|
+ accessToken = String.valueOf(res.get("access_token"));
|
|
|
|
+ }
|
|
|
|
+ } catch (RestClientException e) {
|
|
|
|
+ throw new BusinessException("调用微信服务错误");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return accessToken;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public String getPhoneNumber(String appId, String appSecret, String code) {
|
|
|
|
+ String accessToken = this.getAccessToken(appId, appSecret);
|
|
|
|
+ String tel = "";
|
|
|
|
+ String url = StrUtil.format("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token={}", accessToken);
|
|
|
|
+ try {
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
+ HashMap<String, String> bodyParams = new HashMap<>();
|
|
|
|
+ bodyParams.put("code", code);
|
|
|
|
+ // 用HttpEntity封装整个请求报文
|
|
|
|
+ HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(bodyParams, headers);
|
|
|
|
+ JSONObject res = restTemplate.postForObject(url, httpEntity, JSONObject.class);
|
|
|
|
+ if (res != null) {
|
|
|
|
+ tel = res.getJSONObject("phone_info").getString("phoneNumber");
|
|
|
|
+ }
|
|
|
|
+ } catch (RestClientException e) {
|
|
|
|
+ throw new BusinessException("调用微信服务错误");
|
|
|
|
+ }
|
|
|
|
+ return tel;
|
|
|
|
+ }
|
|
|
|
+}
|