|
@@ -1,33 +1,32 @@
|
|
|
package com.szwl.controller;
|
|
|
|
|
|
-import com.rabbitmq.http.client.domain.UserInfo;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.szwl.exception.MyException;
|
|
|
import com.szwl.model.bo.R;
|
|
|
-import com.szwl.model.bo.ResponseModel;
|
|
|
import com.szwl.model.bo.UserDetailBO;
|
|
|
import com.szwl.model.entity.TAdmin;
|
|
|
import com.szwl.model.entity.TWechat;
|
|
|
-import com.szwl.model.utils.HttpClientUtils;
|
|
|
import com.szwl.service.TAdminService;
|
|
|
import com.szwl.service.TWechatService;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
-import org.json.JSONObject;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URLEncoder;
|
|
|
-import java.security.MessageDigest;
|
|
|
-import java.util.*;
|
|
|
-
|
|
|
+@Slf4j
|
|
|
@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
|
|
@@ -49,7 +48,7 @@ public class WxLoginController {
|
|
|
@ApiOperation(value = "用户默认授权,获取code")
|
|
|
@GetMapping("/menuOauth")
|
|
|
public R<String> getOpenid() {
|
|
|
- String path = http + "/shenze/#/home";
|
|
|
+ String path = http + "/shenzeVue/wxLogin";
|
|
|
try {
|
|
|
String redirectUrl = URLEncoder.encode(path, "UTF-8");
|
|
|
// 第一步:用户静默授权,获取code
|
|
@@ -67,64 +66,31 @@ public class WxLoginController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @ApiOperation(value = "微信授权回调,返回openid")
|
|
|
- @GetMapping("/callback")
|
|
|
- public void callback(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
+ public static void main(String[] args) {
|
|
|
+
|
|
|
+ }
|
|
|
+ @ApiOperation(value = "用微信code 获取openid,再获取用户信息")
|
|
|
+ @GetMapping("/getUserDetailByWxCode")
|
|
|
+ public R getUserDetailByWxCode(String code) 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";
|
|
|
-
|
|
|
- JSONObject jsonObject = HttpClientUtils.get(url);
|
|
|
-
|
|
|
+ HttpResponse httpResponse = HttpRequest.get(url).execute();
|
|
|
+ JSONObject jsonObject = JSON.parseObject(httpResponse.body());
|
|
|
+ log.info("getUserDetailByWxCode:{}",jsonObject);
|
|
|
String openid = jsonObject.getString("openid");
|
|
|
-
|
|
|
- // 返回openid给前端
|
|
|
- response.setContentType("application/json");
|
|
|
- response.setCharacterEncoding("UTF-8");
|
|
|
- response.getWriter().write("{\"openid\":\"" + openid + "\"}");
|
|
|
- }
|
|
|
-
|
|
|
- @ApiOperation(value = "微信登录")
|
|
|
- @GetMapping("/wxLogin")
|
|
|
- public R<String> wxLogin(@RequestParam("openid") String openid) {
|
|
|
- // 根据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("获取用户信息失败");
|
|
|
+ if(null == tWechat){
|
|
|
+ log.info("没有找到绑定的用户信息,请绑定后再登录,openid:",openid);
|
|
|
+ throw new MyException("没有找到绑定的用户信息,请绑定后再登录");
|
|
|
}
|
|
|
+ TAdmin tAdmin = tAdminService.getById(tWechat.getAdminId());
|
|
|
+ UserDetailBO userDetailBO = tAdminService.getUserDetailBO(tAdmin);
|
|
|
+ return R.ok(userDetailBO);
|
|
|
}
|
|
|
|
|
|
}
|