导出班级学员成绩

This commit is contained in:
wuchunlei
2025-05-06 14:11:39 +08:00
parent f0202bbc47
commit dab1ae8202
2 changed files with 157 additions and 0 deletions

View File

@@ -6,9 +6,18 @@ import com.peanut.common.utils.HttpClientUtils;
import com.peanut.common.utils.R;
import com.peanut.modules.book.service.MyUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -24,6 +33,88 @@ public class WeixinApiController {
@Autowired
private MyUserService userInfoService;
public static void main(String[] args) {
String token = getAccessToken("ed1eb116f91a49a4ec1ec37c6f8f9495");
String tel = login(token,"0a3nul100iDgdU1T0J300E82Yk1nul1O");
System.out.println(tel);
}
public static String getAccessToken(String secret) {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 拼接URL
String url = "https://api.weixin.qq.com/cgi-bin/token?" +
"appid=wxff97e3bb73cf57d2"+
"&secret="+secret+
"&grant_type=client_credential";
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Content-Type", "application/json;charset=utf8");
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
HttpEntity responseEntity = response.getEntity();
JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(responseEntity));
String token = jsonObject.getString("access_token");
return token;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return "";
}
public static String login(String accessToken,String code) {
// 创建httpClient对象
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?" +
"access_token="+accessToken;
Map<String, Object> entity = new HashMap<>();
entity.put("code", code);
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
httpPost.setEntity(new StringEntity(JSONObject.toJSONString(entity),"utf-8"));
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(responseEntity));
JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
String tel = phoneInfo.getString("phoneNumber");
return tel;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return "";
}
//微信扫描后回调的方法
// @GetMapping("callback")