|
@@ -1,5 +1,6 @@
|
|
|
package com.szwl.controller;
|
|
|
|
|
|
+import com.rabbitmq.http.client.domain.UserInfo;
|
|
|
import com.szwl.model.bo.R;
|
|
|
import com.szwl.model.bo.ResponseModel;
|
|
|
import com.szwl.model.bo.UserDetailBO;
|
|
@@ -26,6 +27,7 @@ import java.util.*;
|
|
|
@Api(value = "/WxLoginController", tags = {"微信登录接口"})
|
|
|
@RestController
|
|
|
@RequestMapping("/wxLogin")
|
|
|
+@CrossOrigin(origins = {"http://szwltest.sunzee.com.cn", "https://szwl.sunzee.com.cn", "https://sz.sunzee.com.cn"}) // 允许来自 http://xxx 的跨域请求
|
|
|
public class WxLoginController {
|
|
|
|
|
|
@Autowired
|
|
@@ -44,177 +46,85 @@ public class WxLoginController {
|
|
|
@Value("${oauth.callback.http}")
|
|
|
private String http;
|
|
|
|
|
|
- @ApiOperation(value = "用户默认授权")
|
|
|
- @CrossOrigin(value = "https://open.weixin.qq.com/")
|
|
|
+ @ApiOperation(value = "用户默认授权,获取code")
|
|
|
@GetMapping("/menuOauth")
|
|
|
- public R menuOauth(HttpServletResponse response) throws IOException {
|
|
|
- String path = http + "/SZWL-SERVER/wxLogin/callback?";
|
|
|
-// String path = http + "/wxLogin/callback?";
|
|
|
-
|
|
|
+ public R<String> getOpenid() {
|
|
|
+ String path = http + "/shenze/#/home";
|
|
|
try {
|
|
|
- path = URLEncoder.encode(path, "UTF-8");
|
|
|
+ String redirectUrl = URLEncoder.encode(path, "UTF-8");
|
|
|
+ // 第一步:用户静默授权,获取code
|
|
|
+ String authUrl = "http://szwltest.sunzee.com.cn/openWeixin/connect/oauth2/authorize?"
|
|
|
+// String authUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?"
|
|
|
+ + "appid=" + appid
|
|
|
+ + "&redirect_uri=" + redirectUrl
|
|
|
+ + "&response_type=code"
|
|
|
+ + "&scope=snsapi_base"
|
|
|
+ + "&state=STATE"
|
|
|
+ + "#wechat_redirect";
|
|
|
+ return R.ok(authUrl);
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ return R.fail("微信授权失败");
|
|
|
}
|
|
|
- // 第一步:用户同意授权,获取code
|
|
|
- // 静默授权,只能获取用户openid
|
|
|
- String url = "https://szwltest.sunzee.com.cn/openWeixin/connect/oauth2/authorize?"
|
|
|
-// String url = "https://open.weixin.qq.com/connect/oauth2/authorize?"
|
|
|
- + "appid=" + appid
|
|
|
- + "&redirect_uri=" + path
|
|
|
- + "&response_type=code"
|
|
|
- + "&scope=snsapi_base"
|
|
|
- + "&state=STATE" +
|
|
|
- "#wechat_redirect";
|
|
|
- return R.ok(url);
|
|
|
-// response.sendRedirect(url);
|
|
|
}
|
|
|
|
|
|
- @ApiOperation(value = "微信登录回调")
|
|
|
- @CrossOrigin(value = "https://api.weixin.qq.com/")
|
|
|
+ @ApiOperation(value = "微信授权回调,返回openid")
|
|
|
@GetMapping("/callback")
|
|
|
- public void callback(HttpServletRequest request) throws IOException {
|
|
|
- // code 只能用一次,5分钟过期
|
|
|
+ public void callback(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
+ // 第二步:使用code换取access_token和openid
|
|
|
String code = request.getParameter("code");
|
|
|
+ String url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
|
|
|
+ + "appid=" + appid
|
|
|
+ + "&secret=" + appsecret
|
|
|
+ + "&code=" + code
|
|
|
+ + "&grant_type=authorization_code";
|
|
|
|
|
|
- // 第二步:通过code换取网页授权access_token
|
|
|
- String url = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
|
|
|
- "appid=" + appid +
|
|
|
- "&secret=" + appsecret +
|
|
|
- "&code=" + code +
|
|
|
- "&grant_type=authorization_code";
|
|
|
JSONObject jsonObject = HttpClientUtils.get(url);
|
|
|
|
|
|
String openid = jsonObject.getString("openid");
|
|
|
- String accessToken = jsonObject.getString("access_token");
|
|
|
-
|
|
|
- // snsapi_base式的网页授权流程即到此为止。
|
|
|
- // 根据 openid 去查找已绑定的用户信息
|
|
|
- TWechat tWechat = tWechatService.getById(openid);
|
|
|
- if (Objects.nonNull(tWechat)) {
|
|
|
- String adminId = tWechat.getAdminId();
|
|
|
- TAdmin tAdmin = tAdminService.getById(adminId);
|
|
|
- String username = tAdmin.getUsername();
|
|
|
- String password = tAdmin.getPassword();
|
|
|
- // 然后根据 username 和 password 去登录
|
|
|
- TAdminController tAdminController = new TAdminController();
|
|
|
- ResponseModel<UserDetailBO> login = tAdminController.login(username, password);
|
|
|
-
|
|
|
- } else {
|
|
|
- // 说明未绑定
|
|
|
-
|
|
|
- }
|
|
|
|
|
|
+ // 返回openid给前端
|
|
|
+ response.setContentType("application/json");
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ response.getWriter().write("{\"openid\":\"" + openid + "\"}");
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- /**
|
|
|
- * 前往微信开发平台申请,根据 code 获取 openid
|
|
|
- * @param code
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- @CrossOrigin(value = "https://api.weixin.qq.com/")
|
|
|
- private String getOpenidByCode(String code) throws IOException {
|
|
|
-
|
|
|
- String grantType = "authorization_code";
|
|
|
-
|
|
|
- // 拼接请求地址
|
|
|
- String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
|
|
|
- "appid=" + appid +
|
|
|
- "&secret=" + appsecret +
|
|
|
- "&code=" + code +
|
|
|
- "&grant_type=" + grantType;
|
|
|
-
|
|
|
- JSONObject jsonObject = HttpClientUtils.get(requestUrl);
|
|
|
- String accessToken = jsonObject.getString("access_token");
|
|
|
- String openId = jsonObject.getString("openid");
|
|
|
-
|
|
|
- // 拉取用户信息(需scope为 snsapi_userinfo)
|
|
|
-// String userinfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openId + "&lang=zh_CN";
|
|
|
-// JSONObject object = HttpClientUtils.get(userinfoUrl);
|
|
|
-
|
|
|
-
|
|
|
-// String s = JSON.toJSONString(object);
|
|
|
- return openId;
|
|
|
- }
|
|
|
-
|
|
|
- @ApiOperation(value = "获取微信签名")
|
|
|
- @GetMapping("/wx/signature")
|
|
|
- public Map<String, String> getSignature(@RequestParam String url) throws IOException {
|
|
|
- HashMap<String, String> signatureMap = new HashMap<>();
|
|
|
-
|
|
|
- String nonceStr = UUID.randomUUID().toString();
|
|
|
- String timestamp = Long.toString(System.currentTimeMillis() / 1000);
|
|
|
- String jsapiTicket = getJsapiTicket();
|
|
|
- String signature = "";
|
|
|
-
|
|
|
- // 按照微信官方要求构造含有 nonceStr、timestamp、jsapi_ticket 和 url 的字符串
|
|
|
- String rawString = "jsapi_ticket=" + jsapiTicket +
|
|
|
- "&noncestr=" + nonceStr +
|
|
|
- "×tamp=" + timestamp +
|
|
|
- "&url=" + url;
|
|
|
-
|
|
|
- try {
|
|
|
- MessageDigest crypt = MessageDigest.getInstance("SHA-1");
|
|
|
- crypt.reset();
|
|
|
- crypt.update(rawString.getBytes("UTF-8"));
|
|
|
- signature = byteToHex(crypt.digest());
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- signatureMap.put("signature", signature);
|
|
|
- signatureMap.put("appId", appid);
|
|
|
- signatureMap.put("timestamp", timestamp);
|
|
|
- signatureMap.put("nonceStr", nonceStr);
|
|
|
-
|
|
|
- return signatureMap;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取授权页ticket ticket 有效期,一般为 7200 秒
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- @CrossOrigin(value = "https://api.weixin.qq.com/")
|
|
|
- private String getJsapiTicket() throws IOException {
|
|
|
- // 获取 jsapi_ticket 的逻辑,向微信服务器发送 GET 请求
|
|
|
- String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket"
|
|
|
- + "?access_token=" + getAccessToken() // 先获取 access_token
|
|
|
- + "&type=jsapi"; // 指定获取 jsapi_ticket
|
|
|
-
|
|
|
- JSONObject jsonObject = HttpClientUtils.get(url);
|
|
|
- String ticket = jsonObject.getString("ticket");
|
|
|
- // 返回获取到的 jsapi_ticket
|
|
|
- return ticket;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取接口调用凭据 token 有效期: 7200s
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- @CrossOrigin(value = "https://api.weixin.qq.com/")
|
|
|
- private String getAccessToken() throws IOException {
|
|
|
- // 构造 GET 请求,获取 access_token
|
|
|
- String url = "https://api.weixin.qq.com/cgi-bin/token"
|
|
|
- + "?grant_type=client_credential"
|
|
|
- + "&appid=" + appid
|
|
|
- + "&secret=" + appsecret;
|
|
|
-
|
|
|
- JSONObject jsonObject = HttpClientUtils.get(url);
|
|
|
- return jsonObject.getString("access_token");
|
|
|
- }
|
|
|
-
|
|
|
- private static String byteToHex(final byte[] hash) {
|
|
|
- Formatter formatter = new Formatter();
|
|
|
- for (byte b : hash) {
|
|
|
- formatter.format("%02x", b);
|
|
|
+ @ApiOperation(value = "微信登录")
|
|
|
+ @GetMapping("/wxLogin")
|
|
|
+ public R<String> wxLogin(@RequestParam("openid") String openid) {
|
|
|
+ // 根据openid获取用户信息
|
|
|
+ TWechat tWechat = tWechatService.lambdaQuery().eq(TWechat::getOpenId, openid).one();
|
|
|
+ if (tWechat != null) {
|
|
|
+ String adminId = tWechat.getAdminId();
|
|
|
+ if (adminId != null) {
|
|
|
+ // 根据adminId获取用户名和密码
|
|
|
+ TAdmin tAdmin = tAdminService.getById(adminId);
|
|
|
+ if (tAdmin != null) {
|
|
|
+ String username = tAdmin.getUsername();
|
|
|
+ String password = tAdmin.getPassword();
|
|
|
+ // 调用已有的登录接口实现登录,使用用户名和密码
|
|
|
+ TAdminController tAdminController = new TAdminController();
|
|
|
+ ResponseModel<UserDetailBO> loginResult = tAdminController.login(username, password);
|
|
|
+ if (loginResult.getCode().equals("00000")) {
|
|
|
+ // 登录成功
|
|
|
+ // 解析重定向url参数,并重定向到home页面
|
|
|
+ String redirectUrl = "http://szwltest.sunzee.com.cn/shenze/#/home";
|
|
|
+ return R.ok(redirectUrl);
|
|
|
+ } else {
|
|
|
+ // 登录失败
|
|
|
+ return R.fail("登录失败");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 用户信息不存在
|
|
|
+ return R.fail("用户信息不存在");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 当前账户未绑定微信
|
|
|
+ return R.fail("当前账户未绑定微信");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 获取用户信息失败
|
|
|
+ return R.fail("获取用户信息失败");
|
|
|
}
|
|
|
- String result = formatter.toString();
|
|
|
- formatter.close();
|
|
|
- return result;
|
|
|
}
|
|
|
|
|
|
}
|