From 0a890eebeacf3eef199684ba2b08382e3c78f6d6 Mon Sep 17 00:00:00 2001
From: k94314517 <8417338+k94314517@user.noreply.gitee.com>
Date: 星期五, 25 八月 2023 15:20:50 +0800
Subject: [PATCH] 111

---
 server/pom.xml                                                             |   17 ++++
 server/src/main/java/doumeemes/service/system/impl/WxLoginServiceImpl.java |   44 +++++-----
 server/src/main/java/doumeemes/core/utils/WxMiniConfig.java                |   95 +++++++++++++++++++++++
 server/src/main/java/doumeemes/config/shiro/ShiroConfig.java               |    1 
 server/src/main/java/doumeemes/core/utils/SpringUtils.java                 |   69 +++++++++++++++++
 server/src/main/java/doumeemes/core/utils/Constants.java                   |    6 +
 6 files changed, 210 insertions(+), 22 deletions(-)

diff --git a/server/pom.xml b/server/pom.xml
index 495dc4f..02dbb44 100644
--- a/server/pom.xml
+++ b/server/pom.xml
@@ -28,6 +28,10 @@
         <oshi.version>5.7.0</oshi.version>
         <jna.version>5.7.0</jna.version>
         <poi.version>5.0.0</poi.version>
+        <!-- 寰俊灏忕▼搴� -->
+        <weixin-java-miniapp.version>4.1.0</weixin-java-miniapp.version>
+        <!-- 寰俊鏀粯 -->
+        <weixin-java-pay.version>4.1.0</weixin-java-pay.version>
     </properties>
 
     <dependencies>
@@ -144,6 +148,19 @@
             <artifactId>fastjson</artifactId>
             <version>${fastjson.version}</version>
         </dependency>
+
+        <!-- WeChat 灏忕▼搴� -->
+        <dependency>
+            <groupId>com.github.binarywang</groupId>
+            <artifactId>weixin-java-miniapp</artifactId>
+            <version>${weixin-java-miniapp.version}</version>
+        </dependency>
+        <!-- WeChat 鏀粯 -->
+        <dependency>
+            <groupId>com.github.binarywang</groupId>
+            <artifactId>weixin-java-pay</artifactId>
+            <version>${weixin-java-pay.version}</version>
+        </dependency>
         <!--easypoi 瀵煎嚭鎻掍欢-->
        <!-- <dependency>
             <groupId>cn.afterturn</groupId>
diff --git a/server/src/main/java/doumeemes/config/shiro/ShiroConfig.java b/server/src/main/java/doumeemes/config/shiro/ShiroConfig.java
index 60e1b4f..526121a 100644
--- a/server/src/main/java/doumeemes/config/shiro/ShiroConfig.java
+++ b/server/src/main/java/doumeemes/config/shiro/ShiroConfig.java
@@ -72,6 +72,7 @@
         // 璺緞鎷︽埅閰嶇疆
         map.put("/system/login", "anon");
         map.put("/system/wxLogin", "anon");
+        map.put("/system/wxProgramLogin", "anon");
         map.put("/system/wxAccountLogin", "anon");
         map.put("/system/initCompany", "anon");
         map.put("/system/logout", "anon");
diff --git a/server/src/main/java/doumeemes/core/utils/Constants.java b/server/src/main/java/doumeemes/core/utils/Constants.java
index 0e12d13..522faf8 100644
--- a/server/src/main/java/doumeemes/core/utils/Constants.java
+++ b/server/src/main/java/doumeemes/core/utils/Constants.java
@@ -31,8 +31,14 @@
     public static final String WX_CONFIG = "WX_CONFIG";
     public static final String APPID = "APPID";
     public static final String APPSECRET = "APPSECRET";
+    public static final String MINIAPPID = "MINIAPPID";
+    public static final String MINIAPPSECRET = "MINIAPPSECRET";
     public static final String WX_TOKEN = "WX_TOKEN";
 
+
+    public static final String OPENID_WX = "wx_";
+    public static final String OPENID_MINI = "mini_";
+
     public static final String ACCESS_KEY = "ACCESS_KEY";
     public static final String ENDPOINT = "ENDPOINT";
     public static final String RESOURCE_PATH = "RESOURCE_PATH";
diff --git a/server/src/main/java/doumeemes/core/utils/SpringUtils.java b/server/src/main/java/doumeemes/core/utils/SpringUtils.java
new file mode 100644
index 0000000..f4869b2
--- /dev/null
+++ b/server/src/main/java/doumeemes/core/utils/SpringUtils.java
@@ -0,0 +1,69 @@
+package doumeemes.core.utils;
+
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+import org.springframework.beans.factory.support.BeanDefinitionRegistry;
+import org.springframework.beans.factory.support.DefaultListableBeanFactory;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.stereotype.Component;
+
+/**
+ * NOTICE: NEVER USE IT WHEN REGISTERING A SPRING BEAN.
+ *
+ * @author Guang YANG
+ */
+@Component
+public class SpringUtils implements ApplicationContextAware {
+
+    private static ApplicationContext CTX;
+
+    public static ApplicationContext get() {
+        return CTX;
+    }
+
+    @Override
+    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+        CTX = applicationContext;
+    }
+
+    /**
+     * 鍚戝鍣ㄤ腑鍔ㄦ�佹坊鍔燘ean
+     *
+     * @param ctx
+     * @param beanName
+     * @param beanClass
+     */
+    public static void addBean(ApplicationContext ctx, String beanName, Class beanClass) {
+        BeanDefinitionRegistry beanDefReg = (DefaultListableBeanFactory)ctx.getAutowireCapableBeanFactory();
+        BeanDefinitionBuilder beanDefBuilder = BeanDefinitionBuilder.genericBeanDefinition(beanClass);
+        BeanDefinition beanDef = beanDefBuilder.getBeanDefinition();
+        if (!beanDefReg.containsBeanDefinition(beanName)) {
+            beanDefReg.registerBeanDefinition(beanName, beanDef);
+        }
+    }
+
+    /**
+     * 浠庡鍣ㄤ腑绉婚櫎Bean
+     *
+     * @param ctx
+     * @param beanName
+     */
+   public static void removeBean(ApplicationContext ctx, String beanName) {
+        BeanDefinitionRegistry beanDefReg = (DefaultListableBeanFactory) ctx.getAutowireCapableBeanFactory();
+        beanDefReg.getBeanDefinition(beanName);
+        beanDefReg.removeBeanDefinition(beanName);
+    }
+
+    /**
+     * 閬嶅巻杈撳嚭鎵�鏈塀ean鐨勪俊鎭�
+     */
+    static void showAllBeans(ApplicationContext ctx) {
+        //閬嶅巻
+        for (String name : ctx.getBeanDefinitionNames()) {
+            System.out.println("name:" + name + ",class:" + ctx.getBean(name).getClass());
+        }
+    }
+
+}
diff --git a/server/src/main/java/doumeemes/core/utils/WxMiniConfig.java b/server/src/main/java/doumeemes/core/utils/WxMiniConfig.java
new file mode 100644
index 0000000..54e3620
--- /dev/null
+++ b/server/src/main/java/doumeemes/core/utils/WxMiniConfig.java
@@ -0,0 +1,95 @@
+package doumeemes.core.utils;
+
+import cn.binarywang.wx.miniapp.api.WxMaService;
+import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
+import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
+import com.github.binarywang.wxpay.config.WxPayConfig;
+import com.github.binarywang.wxpay.constant.WxPayConstants;
+import com.github.binarywang.wxpay.service.WxPayService;
+import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
+import doumeemes.biz.system.SystemDictDataBiz;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+
+import javax.annotation.PostConstruct;
+
+/**
+ * 寰俊灏忕▼搴忕粍浠�
+ */
+@Configuration
+public class WxMiniConfig {
+
+    /********寰俊灏忕▼搴忔湇鍔�**********/
+    public static WxMaService wxMaService;
+    /********寰俊灏忕▼搴忔敮浠�**********/
+//    public static WxPayService wxPayService;
+    /********寰俊APP鏀粯**********/
+//    public static WxPayService wxAppPayService;
+
+    @Autowired
+    private SystemDictDataBiz systemDictDataBiz;
+
+    public static WxMiniConfig me() {
+        return SpringUtils.get().getBean(WxMiniConfig.class);
+    }
+
+
+    @PostConstruct
+    void init() {
+        this.load_WxMaService();
+//        this.load_wxPayService();
+//        this.load_wxAppPayService();
+    }
+    /**
+     * 鍒濆鍖栧井淇″皬绋嬪簭
+     */
+    public void load_WxMaService() {
+        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
+        config.setAppid(systemDictDataBiz.queryByCode(Constants.WX_CONFIG,Constants.MINIAPPID).getCode());
+        config.setSecret(systemDictDataBiz.queryByCode(Constants.WX_CONFIG,Constants.MINIAPPSECRET).getCode());
+        config.setMsgDataFormat("JSON");
+        //config.setToken("");
+        //config.setAesKey("");
+        WxMaService wxMaService = new WxMaServiceImpl();
+        wxMaService.setWxMaConfig(config);
+        this.wxMaService = wxMaService;
+    }
+
+    /**
+     * 鍒濆鍖栧井淇″皬绋嬪簭鏀粯
+     */
+//    public void load_wxPayService() {
+//        WxPayConfig payConfig = new WxPayConfig();
+//        payConfig.setTradeType(WxPayConstants.TradeType.JSAPI);
+//        payConfig.setSignType(WxPayConstants.SignType.MD5);
+//        payConfig.setAppId(StringUtils.trimToNull(wxPayTeacherProperties.getAppId()));
+//        payConfig.setMchId(StringUtils.trimToNull(wxPayTeacherProperties.getMchId()));
+//        payConfig.setMchKey(StringUtils.trimToNull(wxPayTeacherProperties.getMchKey()));
+//        payConfig.setKeyPath(StringUtils.trimToNull(wxPayTeacherProperties.getKeyPath()));
+//        payConfig.setNotifyUrl(StringUtils.trimToNull(wxPayTeacherProperties.getNotifyUrl()));
+//        WxPayService wxPayService = new WxPayServiceImpl();
+//        wxPayService.setConfig(payConfig);
+//        this.wxPayService = wxPayService;
+//    }
+
+
+
+//    /**
+//     * 鍒濆鍖朅pp鏀粯
+//     */
+//    public void load_wxAppPayService() {
+//        WxPayConfig payConfig = new WxPayConfig();
+//        payConfig.setTradeType(WxPayConstants.TradeType.APP);
+//        payConfig.setSignType(WxPayConstants.SignType.MD5);
+//        payConfig.setAppId("wx684e49fd8a611334");
+//        payConfig.setMchId(StringUtils.trimToNull(sysDictService.getSysDictValue(SysDictEnum.WX_MINI_MchId.getCode())));
+//        payConfig.setMchKey(StringUtils.trimToNull(sysDictService.getSysDictValue(SysDictEnum.WX_MINI_MchKey.getCode())));
+//        payConfig.setKeyPath(StringUtils.trimToNull(sysDictService.getSysDictValue(SysDictEnum.WX_MINI_KeyPath.getCode())));
+//        payConfig.setNotifyUrl(StringUtils.trimToNull(sysDictService.getSysDictValue(SysDictEnum.WX_MINI_NotifyUrl.getCode())));
+//        WxPayService wxPayService = new WxPayServiceImpl();
+//        wxPayService.setConfig(payConfig);
+//        this.wxAppPayService = wxPayService;
+//    }
+
+}
diff --git a/server/src/main/java/doumeemes/service/system/impl/WxLoginServiceImpl.java b/server/src/main/java/doumeemes/service/system/impl/WxLoginServiceImpl.java
index 2c8fba5..dc927b2 100644
--- a/server/src/main/java/doumeemes/service/system/impl/WxLoginServiceImpl.java
+++ b/server/src/main/java/doumeemes/service/system/impl/WxLoginServiceImpl.java
@@ -1,5 +1,6 @@
 package doumeemes.service.system.impl;
 
+import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
@@ -11,6 +12,7 @@
 import doumeemes.core.utils.Constants;
 import doumeemes.core.utils.HttpsUtil;
 import doumeemes.core.utils.Utils;
+import doumeemes.core.utils.WxMiniConfig;
 import doumeemes.dao.business.model.Company;
 import doumeemes.dao.business.model.CompanyUser;
 import doumeemes.dao.business.model.Department;
@@ -32,6 +34,7 @@
 import doumeemes.service.system.SystemLoginLogService;
 import doumeemes.service.system.WxLoginService;
 import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.common.error.WxErrorException;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.shiro.SecurityUtils;
 import org.apache.shiro.authc.AuthenticationException;
@@ -57,7 +60,6 @@
 
     @Autowired
     private SystemDictDataBiz systemDictDataBiz;
-
 
     @Value("${project.version}")
     private String systemVersion;
@@ -85,42 +87,40 @@
      */
     public static final String GET_USER_INFO_URL = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
 
+    public static final String  url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
 
+    /**
+     * 娉細鍏紬鍙�-璁剧疆涓庡紑鍙�-鍩烘湰璁剧疆锛屽繀椤汇�愬凡缁戝畾鐨勫井淇″紑鏀惧钩鍙拌处鍙枫��
+     * @param code
+     * @param request
+     * @return
+     */
     @Override
     public WxLoginVO wxLogin(String code, HttpServletRequest request) {
-
         String appId = systemDictDataBiz.queryByCode(Constants.WX_CONFIG,Constants.APPID).getCode();
         String appSecret = systemDictDataBiz.queryByCode(Constants.WX_CONFIG,Constants.APPSECRET).getCode();
         String getTokenUrl = GET_ACCESS_TOKEN_URL.replace("CODE", code).replace("APPID", appId).replace("SECRET", appSecret);
         JSONObject tokenJson = JSONObject.parseObject(HttpsUtil.get(getTokenUrl,true));
-        if(Objects.isNull(tokenJson.get("access_token"))){
-            throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),tokenJson.getString("errmsg"));
-        }
-        String accessToken = tokenJson.getString("access_token");
         String openId = tokenJson.getString("openid");
-        String getUserInfoUrl = GET_USER_INFO_URL.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId);
-        JSONObject userInfoJson = JSONObject.parseObject(HttpsUtil.get(getUserInfoUrl,true));
-        return  loginByUnionIdAndReturn(userInfoJson.getString("unionid"),openId,request);
-
+        //娉細鍏紬鍙�-璁剧疆涓庡紑鍙�-鍩烘湰璁剧疆锛屽繀椤汇�愬凡缁戝畾鐨勫井淇″紑鏀惧钩鍙拌处鍙枫��
+        String unionId = tokenJson.getString("unionid");
+        return  loginByUnionIdAndReturn(unionId,Constants.OPENID_WX+openId,request);
     }
 
 
     @Override
     public WxLoginVO wxProgramLogin(String code, HttpServletRequest request) {
-        //TODO---------------浠诲悍
-        String appId = systemDictDataBiz.queryByCode(Constants.WX_CONFIG,Constants.APPID).getCode();
-        String appSecret = systemDictDataBiz.queryByCode(Constants.WX_CONFIG,Constants.APPSECRET).getCode();
-        String getTokenUrl = GET_ACCESS_TOKEN_URL.replace("CODE", code).replace("APPID", appId).replace("SECRET", appSecret);
-        JSONObject tokenJson = JSONObject.parseObject(HttpsUtil.get(getTokenUrl,true));
-        if(Objects.isNull(tokenJson.get("access_token"))){
-            throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),tokenJson.getString("errmsg"));
+        try {
+            WxMaJscode2SessionResult session =   WxMiniConfig.wxMaService.getUserService().getSessionInfo(code);
+            String unionid = session.getUnionid();
+            String openId = session.getOpenid();
+            return  loginByUnionIdAndReturn(unionid,Constants.OPENID_MINI+openId,request);
+        } catch (WxErrorException e) {
+            e.printStackTrace();
         }
-        String accessToken = tokenJson.getString("access_token");
-        String openId = tokenJson.getString("openid");
-        String getUserInfoUrl = GET_USER_INFO_URL.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId);
-        JSONObject userInfoJson = JSONObject.parseObject(HttpsUtil.get(getUserInfoUrl,true));
+        throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"寰俊鎺堟潈澶辫触锛岃鑱旂郴绠$悊鍛�");
 
-        return  loginByUnionIdAndReturn(userInfoJson.getString("unionid"),openId,request);
+
 
     }
     private WxLoginVO loginByUnionIdAndReturn(String unionid,String openId,HttpServletRequest request) {

--
Gitblit v1.9.3