mirror of
https://gitee.com/elegant_wings/xiongan-meeting.git
synced 2025-06-21 17:49:36 +08:00
122 lines
4.0 KiB
Java
122 lines
4.0 KiB
Java
![]() |
package com.ics.controller.mobile;
|
||
|
|
||
|
import com.alibaba.fastjson.JSON;
|
||
|
import com.alibaba.fastjson.JSONObject;
|
||
|
import okhttp3.*;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
|
|
||
|
public class SmallWxOkHttp {
|
||
|
|
||
|
static String APP_ID = "wxa29895567831500a";
|
||
|
static String SECRET = "46a11f73665fffaa888061d806661a93";
|
||
|
|
||
|
public static JSONObject sendGet(String url , Map<String, String> map){
|
||
|
OkHttpClient client = new OkHttpClient();
|
||
|
// 创建url
|
||
|
HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
|
||
|
// 添加参数
|
||
|
if(map != null){
|
||
|
for (Map.Entry<String, String> entry : map.entrySet()) {
|
||
|
urlBuilder.addQueryParameter(entry.getKey(), entry.getValue());
|
||
|
}
|
||
|
}
|
||
|
// 创建请求
|
||
|
Request request = new Request.Builder()
|
||
|
.url(urlBuilder.build().toString())
|
||
|
.build();
|
||
|
try {
|
||
|
Response response = client.newCall(request).execute();
|
||
|
String resultStr = response.body().string();
|
||
|
System.out.println(resultStr);
|
||
|
return JSON.parseObject(resultStr);
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
throw new RuntimeException(e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static JSONObject sendPost(String url, Map<String , String> paramMap) {
|
||
|
if(paramMap == null){
|
||
|
paramMap = new HashMap<>();
|
||
|
}
|
||
|
String jsonString = JSON.toJSONString(paramMap);
|
||
|
OkHttpClient client = new OkHttpClient().newBuilder().build();
|
||
|
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonString);
|
||
|
Request request = new Request.Builder()
|
||
|
.post(body)
|
||
|
.url(url)
|
||
|
.build();
|
||
|
Call call = client.newCall(request);
|
||
|
//返回请求结果
|
||
|
try {
|
||
|
Response response = call.execute();
|
||
|
String resultStr = response.body().string();
|
||
|
System.out.println(resultStr);
|
||
|
return JSON.parseObject(resultStr);
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
throw new RuntimeException(e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取AccessToken
|
||
|
*/
|
||
|
public static String getAccessToken(){
|
||
|
// url
|
||
|
String url = "https://api.weixin.qq.com/cgi-bin/token";
|
||
|
// 参数
|
||
|
Map<String, String> map = new HashMap<>();
|
||
|
map.put("grant_type", "client_credential");
|
||
|
map.put("appid", APP_ID);
|
||
|
map.put("secret", SECRET);
|
||
|
// 发送请求
|
||
|
JSONObject jsonObject = sendGet(url, map);
|
||
|
String accessToken = jsonObject.getString("access_token");
|
||
|
String expiresIn = jsonObject.getString("expires_in");
|
||
|
return accessToken;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 小程序登录
|
||
|
*/
|
||
|
public static JSONObject code2Session(String jsCode){
|
||
|
// url
|
||
|
String url = "https://api.weixin.qq.com/sns/jscode2session";
|
||
|
// 参数
|
||
|
Map<String, String> map = new HashMap<>();
|
||
|
map.put("appid", APP_ID);
|
||
|
map.put("secret", SECRET);
|
||
|
map.put("js_code", jsCode);
|
||
|
map.put("grant_type", "authorization_code");
|
||
|
// 发送请求
|
||
|
JSONObject jsonObject = sendGet(url, map);
|
||
|
return jsonObject;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取手机号
|
||
|
*/
|
||
|
public static JSONObject getPhoneNumber(String code, String openid, String accessToken){
|
||
|
// url
|
||
|
String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken;
|
||
|
// 参数
|
||
|
Map<String, String> map = new HashMap<>();
|
||
|
map.put("code", code);
|
||
|
map.put("openid", openid);
|
||
|
// 发送请求
|
||
|
JSONObject jsonObject = sendPost(url, map);
|
||
|
return jsonObject;
|
||
|
}
|
||
|
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
// getAccessToken();
|
||
|
getPhoneNumber("", "", "74_NGF_xru4Mt5gDVperBd9LYwtMjWaXGb7JNleZ-nqSOSGvtW3vIGYKkFY0ymMFn2aLYZaN9d1rAZ65X5X-mGX556bWQWFy1mawkWUorOvz37QH34q2YBJjsDCih8FYOfAHAWPF");
|
||
|
}
|
||
|
|
||
|
}
|