姜永辉 2 лет назад
Родитель
Сommit
cb34111d65

+ 34 - 0
src/main/java/com/dk/oauth/config/WxConfig.java

@@ -0,0 +1,34 @@
+package com.dk.oauth.config;
+
+import lombok.Getter;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Configuration;
+
+@Getter
+@Configuration
+public class WxConfig {
+    /**
+     * @desc   : code2Session-小程序url
+     * @author : 姜永辉
+     * @date   : 2022/09/08 11:30
+     */
+    @Value("${wx.code2Session}")
+    private String code2Session;
+    /**
+     * @desc   : appId-小程序
+     * @author : 姜永辉
+     * @date   : 2022/09/08 11:30
+     */
+    @Value("${wx.appId}")
+    private String appId;
+    /**
+     * @desc   : appSecret-小程序
+     * @author : 姜永辉
+     * @date   : 2022/09/08 11:30
+     */
+    @Value("${wx.appSecret}")
+    private String appSecret;
+
+
+
+}

+ 4 - 0
src/main/java/com/dk/oauth/service/IAuthAccessTokenService.java

@@ -1,5 +1,6 @@
 package com.dk.oauth.service;
 
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.dk.common.response.ResponseResultVO;
 import com.dk.oauth.dto.AuthAccessTokenDto;
@@ -9,6 +10,7 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestParam;
 
 import javax.servlet.http.HttpServletRequest;
+import java.util.Map;
 
 /**
  * (AuthAccessToken)表服务接口
@@ -22,6 +24,8 @@ public interface IAuthAccessTokenService extends IService<AuthAccessToken> {
 
     String getCurrentToken(Long userId);
 
+    ResponseResultVO<JSONObject> loginWechat(Map<String, Object> map);
+
     ResponseResultVO token(HttpServletRequest request);
 
     ResponseResultVO wxToken(UserWxLogin userWxLogin);

+ 27 - 0
src/main/java/com/dk/oauth/service/impl/AuthAccessTokenServiceImpl.java

@@ -1,5 +1,6 @@
 package com.dk.oauth.service.impl;
 
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -9,6 +10,8 @@ import com.dk.common.infrastructure.enums.ErrorCodeEnum;
 import com.dk.common.response.ResponseCodeEnum;
 import com.dk.common.response.ResponseResultUtil;
 import com.dk.common.response.ResponseResultVO;
+import com.dk.common.util.HttpUtils;
+import com.dk.oauth.config.WxConfig;
 import com.dk.oauth.dto.AuthAccessTokenDto;
 import com.dk.oauth.entity.*;
 import com.dk.oauth.mapper.AuthAccessTokenMapper;
@@ -24,6 +27,7 @@ import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
 import org.apache.shiro.SecurityUtils;
 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.data.redis.core.StringRedisTemplate;
 import org.springframework.stereotype.Service;
@@ -33,6 +37,7 @@ import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.util.Date;
+import java.util.Map;
 
 /**
  * (AuthAccessToken)表服务实现类
@@ -60,6 +65,9 @@ public class AuthAccessTokenServiceImpl extends ServiceImpl<AuthAccessTokenMappe
     @Resource
     private StringRedisTemplate stringRedisTemplate;
 
+    @Autowired
+    private WxConfig config;
+
     /**
      * 分页查询
      *
@@ -86,6 +94,25 @@ public class AuthAccessTokenServiceImpl extends ServiceImpl<AuthAccessTokenMappe
     }
 
     /**
+     * @desc : 登录凭证校验
+     * @author : 姜永辉
+     * @date : 2022/5/12 9:33
+     */
+    public ResponseResultVO<JSONObject> loginWechat(Map<String, Object> map) {
+        ResponseResultVO<JSONObject> res = HttpUtils.get(config.getCode2Session()
+                + "appid=" + config.getAppId()
+                + "&secret=" + config.getAppSecret()
+                + "&js_code=" + map.get("code")
+                + "&grant_type=authorization_code");
+        if (res.getData() != null && res.getData().get("errcode") != null) {
+            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL.getCode(),
+                    res.getData().get("errmsg") == null ? res.getData().getString("errcode")
+                            : "微信服务器异常:" + res.getData().getString("errmsg"));
+        }
+        return res;
+    }
+
+    /**
      * @desc   : 获取token
      * @author : 洪旭东
      * @date   : 2024-02-20 14:04

+ 5 - 0
src/main/resources/dev/bootstrap.yml

@@ -90,6 +90,11 @@ mybatis:
 swagger:
   enabled: true
 
+wx:
+  code2Session: https://api.weixin.qq.com/sns/jscode2session?
+  appId: wxa65eeeaf814993aa
+  appSecret: c31c4a154a1329b862936504ed3fa5bb
+
 dongke:
   base:
     pojo: