提交
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package com.chaozhanggui.system.cashierservice.wxUtil;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.chaozhanggui.system.cashierservice.util.HttpClientUtil;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class WechatUtil {
|
||||
|
||||
public static JSONObject getSessionKeyOrOpenId(String code, String appId, String secrete) {
|
||||
String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
|
||||
Map<String, String> requestUrlParam = new HashMap<>();
|
||||
// https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN
|
||||
//小程序appId
|
||||
requestUrlParam.put("appid", appId);
|
||||
//小程序secret
|
||||
requestUrlParam.put("secret", secrete);
|
||||
//小程序端返回的code
|
||||
requestUrlParam.put("js_code", code);
|
||||
//默认参数
|
||||
requestUrlParam.put("grant_type", "authorization_code");
|
||||
//发送post请求读取调用微信接口获取openid用户唯一标识
|
||||
JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doPost(requestUrl,requestUrlParam));
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static JSONObject getAccessToken(String appId, String secrete){
|
||||
String requestUrl = "https://api.weixin.qq.com/cgi-bin/token";
|
||||
Map<String, String> requestUrlParam = new HashMap<>();
|
||||
//小程序appId
|
||||
requestUrlParam.put("appid", appId);
|
||||
//小程序secret
|
||||
requestUrlParam.put("secret", secrete);
|
||||
//默认参数
|
||||
requestUrlParam.put("grant_type", "client_credential");
|
||||
JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doGet(requestUrl,requestUrlParam));
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
|
||||
public static String getPhone(String code,String accessToken){
|
||||
String requestUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+accessToken;
|
||||
Map<String, String> requestUrlParam = new HashMap<>();
|
||||
//小程序appId
|
||||
requestUrlParam.put("code", code);
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
|
||||
HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(requestUrlParam,headers);
|
||||
//通过RestTemplate发送请求,获取到用户手机号码
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
ResponseEntity<JSONObject> response = restTemplate.postForEntity(requestUrl, httpEntity, JSONObject.class);
|
||||
String errmsg = response.getBody().getString("errmsg");
|
||||
if (errmsg.equals("ok")) {
|
||||
JSONObject phoneInfoJson = response.getBody().getJSONObject("phone_info");
|
||||
String phoneNumber = phoneInfoJson.getString("phoneNumber");
|
||||
return phoneNumber;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user