Althars123 3 năm trước cách đây
mục cha
commit
67a9c17d84

+ 67 - 31
twzd-admin/src/main/java/com/miaxis/system/controller/system/SysLoginController.java

@@ -1,5 +1,6 @@
 package com.miaxis.system.controller.system;
 
+import com.alibaba.fastjson.JSONObject;
 import com.miaxis.common.core.domain.Response;
 import com.miaxis.common.core.domain.entity.SysMenu;
 import com.miaxis.common.core.domain.entity.SysUser;
@@ -7,6 +8,7 @@ import com.miaxis.common.core.domain.model.LoginBody;
 import com.miaxis.common.core.domain.model.LoginBodyNoCode;
 import com.miaxis.common.core.domain.model.LoginUser;
 import com.miaxis.common.enums.StudentLoginTypeEnum;
+import com.miaxis.common.exception.CustomException;
 import com.miaxis.common.utils.SecurityUtils;
 import com.miaxis.common.utils.ServletUtils;
 import com.miaxis.framework.web.service.SysLoginService;
@@ -16,8 +18,12 @@ import com.miaxis.system.dto.common.RouterDTO;
 import com.miaxis.system.dto.system.TokenDTO;
 import com.miaxis.system.dto.system.UserInfoDTO;
 import com.miaxis.system.service.ISysMenuService;
+import com.miaxis.common.core.domain.model.WxResult;
+import com.miaxis.wx.service.WxService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -50,6 +56,24 @@ public class SysLoginController
     private TokenService tokenService;
 
 
+    @Autowired
+    private WxService wxService;
+    private static Logger logger = LoggerFactory.getLogger(SysLoginController.class);
+
+
+
+
+    @Value("${app.appId}")
+    private String appid;
+
+    @Value("${app.appSecret}")
+    private String appSecret;
+
+
+
+
+
+
 
 
 
@@ -84,48 +108,60 @@ public class SysLoginController
         tokenDTO.setToken(token);
         return Response.success(tokenDTO);
     }
-//
-//    /**
-//     * 用户 jscode登录
-//     *
-//     */
-//    @PostMapping("/login/jscode")
-//    @ApiOperation("用户jscode登录")
-//    public Response<TokenDTO> getInfo(String jscode){
-//        String result = wxService.getWxInfo(appid, secret, jscode,"authorization_code");
-//        JSONObject jsonString = JSONObject.parseObject(result);
-//        WxResult wxResult = JSONObject.toJavaObject(jsonString, WxResult.class);
-//        TokenDTO tokenDTO = new TokenDTO();
-//        // 生成令牌
-//        if (wxResult.getOpenid() != null){
-//            // 生成令牌
-//            String token = loginService.login(wxResult.getOpenid(),null, StudentLoginTypeEnum.OPENID_LOGIN.getCode());
-//            tokenDTO.setToken(token);
-//            return Response.success(tokenDTO) ;
-//        }
-//        else{
-//            return Response.error(500,wxResult.getErrmsg());
-//        }
-//
-//    }
-
 
     /**
-     * 用户 jscode登录
+     * 用户 openid登录
      *
      */
-    @PostMapping("/login/jscode-test")
-    @ApiOperation("用户jscode登录(测试,固定openid)")
-    public Response<TokenDTO> getInfoTest(String jscode){
+    @PostMapping("/login/code")
+    @ApiOperation("用户授权码模式登录")
+    public Response<TokenDTO> loginByAuthorizationCode(String authorizationCode ){
+        String wxResultStr = wxService.getWxToken(appid, appSecret, authorizationCode, "authorization_code");
+        logger.info("微信授权码登录返回值:"+wxResultStr);
+        WxResult wxResult = JSONObject.parseObject(wxResultStr,WxResult.class);
+
+        // 生成令牌
+        if (wxResult.getErrcode() != null){
+            throw new CustomException("微信授权无效,请重新授权");
+        }
+        String token = loginService.login(wxResult.getOpenid(),null, StudentLoginTypeEnum.AUTHORIZATION_CODE_LOGIN.getCode());
         TokenDTO tokenDTO = new TokenDTO();
-            // 生成令牌
-        String token = loginService.login("oO7PJ5CPQJo62kZWA3uiUX2KG2s4",null, StudentLoginTypeEnum.OPENID_LOGIN.getCode());
         tokenDTO.setToken(token);
+        tokenDTO.setWxResult(wxResult);
         return Response.success(tokenDTO) ;
 
 
+
+    }
+    @PostMapping("/login/code/test")
+    @ApiOperation("用户授权码模式登录--测试")
+    public Response<TokenDTO> testloginByAuthorizationCode(String authorizationCode ){
+        String token = loginService.login("oN0Np5sK6JeTRa06hlE4-OkHDlDY",null, StudentLoginTypeEnum.AUTHORIZATION_CODE_LOGIN.getCode());
+        TokenDTO tokenDTO = new TokenDTO();
+        tokenDTO.setToken(token);
+        return Response.success(tokenDTO) ;
+
+
+
     }
 
+//
+//    /**
+//     * 用户 jscode登录
+//     *
+//     */
+//    @PostMapping("/login/jscode-test")
+//    @ApiOperation("用户jscode登录(测试,固定openid)")
+//    public Response<TokenDTO> getInfoTest(String jscode){
+//        TokenDTO tokenDTO = new TokenDTO();
+//            // 生成令牌
+//        String token = loginService.login("oO7PJ5CPQJo62kZWA3uiUX2KG2s4",null, StudentLoginTypeEnum.AUTHORIZATION_CODE_LOGIN.getCode());
+//        tokenDTO.setToken(token);
+//        return Response.success(tokenDTO) ;
+//
+//
+//    }
+
 
     /**
      * 获取用户信息

+ 4 - 1
twzd-admin/src/main/resources/application-dev.yml

@@ -57,6 +57,7 @@ spring:
                         multi-statement-allow: true
     # redis 配置
     redis:
+        database: 2
         # 地址
         host: 192.168.8.213
         # 端口,默认为6379
@@ -79,7 +80,9 @@ spring:
 
 
 
-
+app:
+    appId: wxc439503456129660
+    appSecret: f9e43ce4a2709eb118e268827aa63cae
 
 
 

+ 3 - 0
twzd-admin/src/main/resources/application-prod.yml

@@ -76,3 +76,6 @@ spring:
                 # #连接池最大阻塞等待时间(使用负值表示没有限制)
                 max-wait: -1ms
 
+app:
+    appId: wxc439503456129660
+    appSecret: f9e43ce4a2709eb118e268827aa63cae

+ 17 - 0
twzd-common/src/main/java/com/miaxis/common/core/domain/model/WxResult.java

@@ -0,0 +1,17 @@
+package com.miaxis.common.core.domain.model;
+
+
+import lombok.Data;
+
+@Data
+public class WxResult {
+     String openid;
+     String access_token;
+     String refresh_token;
+     Integer expire_in;
+     String scope;
+     String unionid;
+     String errcode;
+     String errmsg;
+
+}

+ 1 - 1
twzd-common/src/main/java/com/miaxis/common/enums/StudentLoginTypeEnum.java

@@ -7,7 +7,7 @@ package com.miaxis.common.enums;
  */
 public enum StudentLoginTypeEnum
 {
-   OPENID_LOGIN("1", "openid登录");
+    AUTHORIZATION_CODE_LOGIN("1", "授权码登录");
 
     private final String code;
     private final String info;

+ 1 - 1
twzd-framework/src/main/java/com/miaxis/framework/web/service/SysLoginService.java

@@ -98,7 +98,7 @@ public class SysLoginService
 
         ServletUtils.getRequest().setAttribute("loginType",loginType);
         // 用户验证
-        if (StudentLoginTypeEnum.OPENID_LOGIN.getCode().equals(loginType)){
+        if (StudentLoginTypeEnum.AUTHORIZATION_CODE_LOGIN.getCode().equals(loginType)){
             return authenticate(principal,new OpenIdAuthenticationToken(principal));
         }
         else{

+ 1 - 1
twzd-framework/src/main/java/com/miaxis/framework/web/service/UserDetailsServiceImpl.java

@@ -64,7 +64,7 @@ public class UserDetailsServiceImpl implements UserDetailsService
                 throw new BaseException("对不起,您的账号:" + identification + " 已停用");
             }
             return createLoginUser(user);
-        } else if (StudentLoginTypeEnum.OPENID_LOGIN.getCode().equals(loginType)){
+        } else if (StudentLoginTypeEnum.AUTHORIZATION_CODE_LOGIN.getCode().equals(loginType)){
             {
                 UserInfo userInfo = userService.getStudentByOpenId(identification);
                 if (userInfo == null) {

+ 33 - 0
twzd-service/src/main/java/com/miaxis/wx/service/WxService.java

@@ -0,0 +1,33 @@
+package com.miaxis.wx.service;
+
+
+import com.miaxis.common.config.FeignConfig;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.stereotype.Component;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+/**
+ *
+ * 微信接口
+ */
+@FeignClient(name="wxService",
+        url = "https://api.weixin.qq.com",configuration = FeignConfig.class)
+@Component
+public interface WxService {
+    /**
+     *  获取openid
+     * @param appid
+     * @param secret
+     * @param code
+     * @param grantType
+     * @return
+     */
+    @GetMapping(value = "/sns/oauth2/access_token")
+    String getWxToken(
+            @RequestParam("appid") String appid,
+            @RequestParam("secret") String secret,
+            @RequestParam("code") String code,
+            @RequestParam("grant_type") String grantType);
+
+}

+ 3 - 0
twzd-system/src/main/java/com/miaxis/system/dto/system/TokenDTO.java

@@ -1,5 +1,6 @@
 package com.miaxis.system.dto.system;
 
+import com.miaxis.common.core.domain.model.WxResult;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
@@ -16,4 +17,6 @@ public class TokenDTO {
     private static final long serialVersionUID = 1L;
     @ApiModelProperty(value = "令牌")
     private String token;
+    @ApiModelProperty(value = "微信返回数据")
+    private WxResult wxResult;
 }