|
|
@@ -1,15 +1,12 @@
|
|
|
-package org.springblade.auth.wechat;
|
|
|
+package org.springblade.client.wechat;
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
-import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
|
|
-import io.swagger.annotations.ApiOperation;
|
|
|
-import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import okhttp3.OkHttpClient;
|
|
|
import okhttp3.Request;
|
|
|
+import okhttp3.RequestBody;
|
|
|
import okhttp3.Response;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
@@ -18,10 +15,15 @@ import org.apache.http.client.methods.HttpGet;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
-import org.springblade.auth.utils.TokenUtil;
|
|
|
import org.springblade.core.tool.api.R;
|
|
|
+import org.springblade.core.tool.jackson.JsonUtil;
|
|
|
+import org.springblade.core.tool.utils.ObjectUtil;
|
|
|
import org.springblade.core.tool.utils.StringUtil;
|
|
|
-import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.util.ObjectUtils;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.HashMap;
|
|
|
@@ -33,7 +35,7 @@ import java.util.Map;
|
|
|
* @author BladeX
|
|
|
*/
|
|
|
@Slf4j
|
|
|
-@AllArgsConstructor
|
|
|
+@RestController
|
|
|
@RequestMapping("/wechat")
|
|
|
public class WechatController {
|
|
|
|
|
|
@@ -43,25 +45,20 @@ public class WechatController {
|
|
|
* @param code
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- @PostMapping(value = "/programLogin")
|
|
|
- @ResponseBody
|
|
|
- @ApiOperationSupport(order = 1)
|
|
|
- @ApiOperation(value = "获取openId接口", notes = "获取openId接口")
|
|
|
- public R<Map<String, Object>> weChatProgramLogin(@RequestParam("code") String code) throws IOException {
|
|
|
- log.info("小程序code=====>"+code);
|
|
|
- if (StringUtil.isBlank(code)){
|
|
|
+ @GetMapping("programLogin")
|
|
|
+ public R<Map<String, Object>> weChatProgramLogin(@RequestParam("loginCode") String loginCode, @RequestParam("telCode") String telCode) throws IOException {
|
|
|
+ log.info("小程序code=====>" + loginCode);
|
|
|
+ if (StringUtil.isBlank(loginCode)) {
|
|
|
throw new SecurityException("登录code获取失败");
|
|
|
}
|
|
|
Map<String, Object> map = new HashMap<>(16);
|
|
|
|
|
|
String url = "https://api.weixin.qq.com/sns/jscode2session";
|
|
|
- /*url += "?appid=wx80f69041c69c25cd";//自己的appid*/
|
|
|
//自己的appid
|
|
|
- url += "?appid=" + TokenUtil.APP_ID;
|
|
|
- /*url += "&secret=46dd2608845110ab2f3557fc472b54f4";//自己的appSecret*/
|
|
|
+ url += "?appid=wx0a0feca24b695636";
|
|
|
//自己的appSecret
|
|
|
- url += "&secret=" + TokenUtil.APP_SECRET;
|
|
|
- url += "&js_code=" + code;
|
|
|
+ url += "&secret=b3efb530f9ef97626e2ec31fc7a3af56";
|
|
|
+ url += "&js_code=" + loginCode;
|
|
|
url += "&grant_type=authorization_code";
|
|
|
url += "&connect_redirect=1";
|
|
|
|
|
|
@@ -101,8 +98,9 @@ public class WechatController {
|
|
|
|
|
|
OkHttpClient client = new OkHttpClient();
|
|
|
//获得token
|
|
|
- Request request = new Request.Builder().url(TokenUtil.ACCESS_TOKEN_URL + "&appid=" + TokenUtil.APP_ID + "&secret=" + TokenUtil.APP_SECRET).build();
|
|
|
+ Request request = new Request.Builder().url("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx0a0feca24b695636&secret=b3efb530f9ef97626e2ec31fc7a3af56").build();
|
|
|
try {
|
|
|
+
|
|
|
Response responseToken = client.newCall(request).execute();
|
|
|
//如果请求成功,解析数据
|
|
|
if (responseToken.isSuccessful()) {
|
|
|
@@ -110,31 +108,52 @@ public class WechatController {
|
|
|
//得到一个JSON对象
|
|
|
JSONObject object = JSON.parseObject(body);
|
|
|
String accessToken = object.getString("access_token");
|
|
|
- if (ObjectUtils.isNotNull(accessToken)) {
|
|
|
+ if (ObjectUtil.isNotEmpty(accessToken)) {
|
|
|
log.info("通过code获取token异常:" + object.toJSONString());
|
|
|
- map.put("accessToken",accessToken);
|
|
|
+ map.put("accessToken", accessToken);
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("access_token获取失败");
|
|
|
}
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
log.info("用户获取token异常:" + e);
|
|
|
}
|
|
|
-
|
|
|
+ Map<String, String> map1 = new HashMap<>();
|
|
|
+ map1.put("code", telCode);
|
|
|
+ System.out.println(JsonUtil.toJson(map1));
|
|
|
//获得手机号
|
|
|
- Request requestPhone = new Request.Builder().url(TokenUtil.PHONE_URL + "&access_token=" + map.get("accessToken") + "&code=" + code).build();
|
|
|
+ Request requestPhone = new Request.Builder()
|
|
|
+// .url("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + map.get("accessToken") + "&code=" + telCode)
|
|
|
+ .url("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + map.get("accessToken") + "&")
|
|
|
+ .method("post", RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), JsonUtil.toJson(map1)))
|
|
|
+ .post(RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), JsonUtil.toJson(map1)))
|
|
|
+ .build();
|
|
|
try {
|
|
|
+
|
|
|
Response responsePhone = client.newCall(requestPhone).execute();
|
|
|
//如果请求成功,解析数据
|
|
|
if (responsePhone.isSuccessful()) {
|
|
|
String body = responsePhone.body().string();
|
|
|
//得到一个JSON对象
|
|
|
JSONObject object = JSON.parseObject(body);
|
|
|
- JSONObject jsonObject = object.getJSONObject("phone_info");
|
|
|
- String phoneNumber = jsonObject.getString("phoneNumber");
|
|
|
- if (ObjectUtils.isNotNull(phoneNumber)) {
|
|
|
- log.info("获取手机号异常:" + object.toJSONString());
|
|
|
- map.put("phoneNumber",phoneNumber);
|
|
|
+ if (object.getString("errcode").equals("0")) {
|
|
|
+ JSONObject jsonObject = object.getJSONObject("phone_info");
|
|
|
+ if (ObjectUtil.isNotEmpty(jsonObject)) {
|
|
|
+ String phoneNumber = jsonObject.getString("phoneNumber");
|
|
|
+ if (ObjectUtils.isEmpty(phoneNumber)) {
|
|
|
+ log.info("获取手机号异常:" + object.toJSONString());
|
|
|
+ throw new RuntimeException("获取手机号异常");
|
|
|
+ } else {
|
|
|
+ map.put("phoneNumber", phoneNumber);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (object.getString("errcode").equals("-1")) {
|
|
|
+ throw new RuntimeException("系统繁忙,此时请开发者稍候再试");
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("不合法的telCode(telCode不存在、已过期或者使用过)");
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|