|
|
@ -21,6 +21,17 @@ import javax.annotation.Resource; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader; |
|
|
|
import java.io.DataOutputStream; |
|
|
|
import java.io.InputStreamReader; |
|
|
|
import java.net.HttpURLConnection; |
|
|
|
import java.net.URL; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
/** |
|
|
|
* @Author lzx |
|
|
|
* @Date 2022-05-13 15:55 |
|
|
@ -105,17 +116,157 @@ public class AppletLoginServiceImpl implements AppletLoginService { |
|
|
|
result.setCode(200); |
|
|
|
// result.setMessage(member.getNickName()+"已于"+member.getCreateTime()+"注册成功!请勿重复注册!"); |
|
|
|
// // 未绑定手机号,跳转授权绑定手机号 |
|
|
|
// if (StringUtils.isBlank(member.getPhone())) { |
|
|
|
// result.setMessage("该微信用户尚未绑定手机号,请授权手机进行绑定"); |
|
|
|
// result.setCode(905); |
|
|
|
// result.setResult(map); |
|
|
|
// return result; |
|
|
|
// } |
|
|
|
if (StringUtils.isBlank(member.getPhone())) { |
|
|
|
result.setMessage("该微信用户尚未绑定手机号,请授权手机进行绑定"); |
|
|
|
result.setCode(200); |
|
|
|
result.setResult(map); |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// private static final String APP_ID = mpAppId; // 替换为你的小程序AppID |
|
|
|
// private static final String APP_SECRET = "YOUR_APP_SECRET"; // 替换为你的小程序AppSecret |
|
|
|
private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"; |
|
|
|
|
|
|
|
public String getAccessToken() throws Exception { |
|
|
|
String requestUrl = String.format(TOKEN_URL, mpAppId, mpAppSecret); |
|
|
|
URL url = new URL(requestUrl); |
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
|
|
|
conn.setRequestMethod("GET"); |
|
|
|
|
|
|
|
try (BufferedReader br = new BufferedReader( |
|
|
|
new InputStreamReader(conn.getInputStream(), "UTF-8"))) { |
|
|
|
StringBuilder response = new StringBuilder(); |
|
|
|
String responseLine; |
|
|
|
while ((responseLine = br.readLine()) != null) { |
|
|
|
response.append(responseLine.trim()); |
|
|
|
} |
|
|
|
|
|
|
|
org.json.JSONObject jsonResponse = new org.json.JSONObject(response.toString()); |
|
|
|
return jsonResponse.getString("access_token"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static final String API_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber"; |
|
|
|
|
|
|
|
public String getPhoneNumber(String code) throws Exception { |
|
|
|
URL url = new URL(API_URL + "?access_token=" + this.getAccessToken()); |
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
|
|
|
conn.setRequestMethod("POST"); |
|
|
|
conn.setRequestProperty("Content-Type", "application/json; utf-8"); |
|
|
|
conn.setRequestProperty("Accept", "application/json"); |
|
|
|
conn.setDoOutput(true); |
|
|
|
|
|
|
|
JSONObject jsonInput = new JSONObject(); |
|
|
|
jsonInput.put("code", code); |
|
|
|
|
|
|
|
try (DataOutputStream os = new DataOutputStream(conn.getOutputStream())) { |
|
|
|
byte[] input = jsonInput.toString().getBytes(StandardCharsets.UTF_8); |
|
|
|
os.write(input, 0, input.length); |
|
|
|
} |
|
|
|
|
|
|
|
try (BufferedReader br = new BufferedReader( |
|
|
|
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { |
|
|
|
StringBuilder response = new StringBuilder(); |
|
|
|
String responseLine; |
|
|
|
while ((responseLine = br.readLine()) != null) { |
|
|
|
response.append(responseLine.trim()); |
|
|
|
} |
|
|
|
//获取手机号码 |
|
|
|
return response.toString(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
@Override |
|
|
|
public Result<Object> bindPhone(String code) { |
|
|
|
try { |
|
|
|
String phoneNumber = this.getPhoneNumber(code); |
|
|
|
return Result.OK(phoneNumber); |
|
|
|
}catch (Exception e){ |
|
|
|
return Result.error(e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// |
|
|
|
// // 替换为你的小程序AppID和AppSecret |
|
|
|
// private static final String APP_ID = "your_app_id"; |
|
|
|
// private static final String APP_SECRET = "your_app_secret"; |
|
|
|
// |
|
|
|
// // 获取access_token的URL |
|
|
|
// private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APP_ID + "&secret=" + APP_SECRET; |
|
|
|
// |
|
|
|
// // 获取手机号的URL模板(需替换ACCESS_TOKEN) |
|
|
|
// private static final String PHONE_NUMBER_URL_TEMPLATE = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s"; |
|
|
|
// |
|
|
|
// public static String getPhoneNumber(String code) throws IOException { |
|
|
|
// // Step 1: 获取access_token |
|
|
|
// String accessToken = getAccessToken(); |
|
|
|
// |
|
|
|
// // Step 2: 使用access_token和code获取手机号 |
|
|
|
// String phoneNumberUrl = String.format(PHONE_NUMBER_URL_TEMPLATE, accessToken); |
|
|
|
// |
|
|
|
// CloseableHttpClient httpClient = HttpClients.createDefault(); |
|
|
|
// HttpPost postRequest = new HttpPost(phoneNumberUrl); |
|
|
|
// |
|
|
|
// // 设置请求头(如果需要) |
|
|
|
// // postRequest.setHeader("Content-Type", "application/json"); |
|
|
|
// |
|
|
|
// // 设置请求体(包含code) |
|
|
|
// JSONObject jsonBody = new JSONObject(); |
|
|
|
// jsonBody.put("code", code); |
|
|
|
// StringEntity entity = new StringEntity(jsonBody.toString()); |
|
|
|
// postRequest.setEntity(entity); |
|
|
|
// |
|
|
|
// // 执行请求并获取响应 |
|
|
|
// CloseableHttpResponse response = httpClient.execute(postRequest); |
|
|
|
// String responseString = EntityUtils.toString(response.getEntity()); |
|
|
|
// |
|
|
|
// // 关闭响应和客户端 |
|
|
|
// response.close(); |
|
|
|
// httpClient.close(); |
|
|
|
// |
|
|
|
// // 解析响应并返回手机号信息(或错误信息) |
|
|
|
// JSONObject responseJson = new JSONObject(responseString); |
|
|
|
// int errcode = responseJson.getInt("errcode"); |
|
|
|
// if (errcode == 0) { |
|
|
|
// JSONObject phoneInfo = responseJson.getJSONObject("phone_info"); |
|
|
|
// return phoneInfo.getString("phoneNumber"); // 或根据需要返回其他信息 |
|
|
|
// } else { |
|
|
|
// // 处理错误(例如记录日志、返回错误信息给前端等) |
|
|
|
// return "Error: " + responseJson.getString("errmsg"); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// |
|
|
|
// private static String getAccessToken() throws IOException { |
|
|
|
// CloseableHttpClient httpClient = HttpClients.createDefault(); |
|
|
|
// try { |
|
|
|
// // 执行GET请求获取access_token |
|
|
|
// CloseableHttpResponse response = httpClient.execute(new HttpPost(ACCESS_TOKEN_URL)); |
|
|
|
// String responseString = EntityUtils.toString(response.getEntity()); |
|
|
|
// |
|
|
|
// // 解析响应并返回access_token |
|
|
|
// JSONObject responseJson = new JSONObject(responseString); |
|
|
|
// return responseJson.getString("access_token"); |
|
|
|
// } finally { |
|
|
|
// httpClient.close(); |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /** |
|
|
|
// * 微信小程序登录绑定手机号码接口 |
|
|
|
// * @param loginReq |
|
|
|