|
|
@@ -0,0 +1,165 @@
|
|
|
+package org.springblade.auth.wechat;
|
|
|
+
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
|
|
+import com.ruoyi.common.core.controller.BaseController;
|
|
|
+import com.ruoyi.common.core.domain.entity.SysUser;
|
|
|
+import com.ruoyi.common.utils.StringUtils;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiImplicitParam;
|
|
|
+import io.swagger.annotations.ApiImplicitParams;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
+import okhttp3.Request;
|
|
|
+import okhttp3.Response;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+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.utils.StringUtil;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 与微信产生交换的控制器
|
|
|
+ *
|
|
|
+ * @author BladeX
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+@AllArgsConstructor
|
|
|
+@RequestMapping("/wechat")
|
|
|
+@Api(value = "微信小程序", tags = "微信小程序接口")
|
|
|
+public class WechatController extends BaseController {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信授权登录
|
|
|
+ *
|
|
|
+ * @param code
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ @PostMapping(value = "/programLogin")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(paramType = "query", dataType = "String", name = "code", value = "code", required = true)
|
|
|
+ })
|
|
|
+ @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)){
|
|
|
+ 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*/
|
|
|
+ //自己的appSecret
|
|
|
+ url += "&secret=" + TokenUtil.APP_SECRET;
|
|
|
+ url += "&js_code=" + code;
|
|
|
+ url += "&grant_type=authorization_code";
|
|
|
+ url += "&connect_redirect=1";
|
|
|
+
|
|
|
+ String res = null;
|
|
|
+ CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
+ // DefaultHttpClient();
|
|
|
+ //GET方式
|
|
|
+ HttpGet httpget = new HttpGet(url);
|
|
|
+ HttpGet accessTokenUrlGet = new HttpGet(url);
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ // 配置信息
|
|
|
+ // 设置连接超时时间(单位毫秒)
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom()
|
|
|
+ // 设置请求超时时间(单位毫秒)
|
|
|
+ .setConnectTimeout(5000)
|
|
|
+ // socket读写超时时间(单位毫秒)
|
|
|
+ .setConnectionRequestTimeout(5000)
|
|
|
+ // 设置是否允许重定向(默认为true)
|
|
|
+ .setSocketTimeout(5000)
|
|
|
+ // 将上面的配置信息 运用到这个Get请求里
|
|
|
+ .setRedirectsEnabled(false).build();
|
|
|
+ // 由客户端执行(发送)Get请求
|
|
|
+ httpget.setConfig(requestConfig);
|
|
|
+ accessTokenUrlGet.setConfig(requestConfig);
|
|
|
+ // 从响应模型中获取响应实体
|
|
|
+ response = httpClient.execute(httpget);
|
|
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
+ log.info("响应状态为=====>" + response.getStatusLine());
|
|
|
+ if (responseEntity != null) {
|
|
|
+ res = EntityUtils.toString(responseEntity);
|
|
|
+ log.info("响应内容长度为=====>" + responseEntity.getContentLength());
|
|
|
+ log.info("响应内容为=====>" + res);
|
|
|
+ }
|
|
|
+ // 释放资源
|
|
|
+ if (httpClient != null) {
|
|
|
+ httpClient.close();
|
|
|
+ }
|
|
|
+ if (response != null) {
|
|
|
+ response.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ OkHttpClient client = new OkHttpClient();
|
|
|
+ //获得token
|
|
|
+ Request request = new Request.Builder().url(TokenUtil.ACCESS_TOKEN_URL + "&appid=" + TokenUtil.APP_ID + "&secret=" + TokenUtil.APP_SECRET).build();
|
|
|
+ try {
|
|
|
+ Response responseToken = client.newCall(request).execute();
|
|
|
+ //如果请求成功,解析数据
|
|
|
+ if (responseToken.isSuccessful()) {
|
|
|
+ String body = responseToken.body().string();
|
|
|
+ //得到一个JSON对象
|
|
|
+ JSONObject object = JSON.parseObject(body);
|
|
|
+ String accessToken = object.getString("access_token");
|
|
|
+ if (StringUtils.isNotEmpty(accessToken)) {
|
|
|
+ log.info("通过code获取token异常:" + object.toJSONString());
|
|
|
+ map.put("accessToken",accessToken);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.info("用户获取token异常:" + e);
|
|
|
+ }
|
|
|
+
|
|
|
+ //获得手机号
|
|
|
+ Request requestPhone = new Request.Builder().url(TokenUtil.PHONE_URL + "&access_token=" + map.get("accessToken") + "&code=" + code).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 (StringUtils.isNotEmpty(phoneNumber)) {
|
|
|
+ log.info("获取手机号异常:" + object.toJSONString());
|
|
|
+ map.put("phoneNumber",phoneNumber);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.info("用户获取手机号异常:" + e);
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject jo = JSONObject.parseObject(res);
|
|
|
+ String openid = jo.getString("openid");
|
|
|
+ log.info("openid=====>" + jo.toString());
|
|
|
+ map.put("openId", openid);
|
|
|
+
|
|
|
+ return R.data(map);
|
|
|
+ }
|
|
|
+}
|