Bladeren bron

20190716微信小程序登录

suochencheng 6 jaren geleden
bovenliggende
commit
ba5e09cfa7

+ 1 - 1
src/main/java/com/izouma/awesomeadmin/container/WeixinConfig.java

@@ -11,7 +11,7 @@ import org.springframework.context.annotation.Configuration;
 public class WeixinConfig {
 public class WeixinConfig {
 
 
     @Bean
     @Bean
-    public WxMaService wxMaService() {
+    public static WxMaService wxMaService() {
         WxMaInMemoryConfig config = new WxMaInMemoryConfig();
         WxMaInMemoryConfig config = new WxMaInMemoryConfig();
         config.setAppid(PropertiesFileLoader.getProperties("weixinappid"));
         config.setAppid(PropertiesFileLoader.getProperties("weixinappid"));
         config.setSecret(PropertiesFileLoader.getProperties("weixinsecret"));
         config.setSecret(PropertiesFileLoader.getProperties("weixinsecret"));

+ 6 - 6
src/main/java/com/izouma/awesomeadmin/service/impl/UserInfoServiceImpl.java

@@ -1,5 +1,6 @@
 package com.izouma.awesomeadmin.service.impl;
 package com.izouma.awesomeadmin.service.impl;
 
 
+import cn.binarywang.wx.miniapp.api.WxMaService;
 import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
 import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
 import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
 import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
 import com.izouma.awesomeadmin.constant.AppConstant;
 import com.izouma.awesomeadmin.constant.AppConstant;
@@ -27,7 +28,6 @@ import org.apache.log4j.Logger;
 import org.json.JSONException;
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.json.JSONObject;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
 import javax.crypto.SecretKey;
 import javax.crypto.SecretKey;
@@ -47,6 +47,8 @@ public class UserInfoServiceImpl implements UserInfoService {
     private static Logger logger = Logger.getLogger(UserInfoServiceImpl.class);
     private static Logger logger = Logger.getLogger(UserInfoServiceImpl.class);
     private RongCloud rongCloud = RongCloud.getInstance(PropertiesFileLoader.getProperties("rongyunappkey"), PropertiesFileLoader.getProperties("rongyunappsecret"));
     private RongCloud rongCloud = RongCloud.getInstance(PropertiesFileLoader.getProperties("rongyunappkey"), PropertiesFileLoader.getProperties("rongyunappsecret"));
 
 
+    private WxMaService wxMaService = WeixinConfig.wxMaService();
+
     @Autowired
     @Autowired
     private UserInfoMapper userInfoMapper;
     private UserInfoMapper userInfoMapper;
     @Autowired
     @Autowired
@@ -65,8 +67,6 @@ public class UserInfoServiceImpl implements UserInfoService {
     private RecommenderLogMapper recommenderLogMapper;
     private RecommenderLogMapper recommenderLogMapper;
 
 
 
 
-    private WeixinConfig weixinConfig;
-
     @Override
     @Override
     public List<UserInfo> getUserInfoList(UserInfo record) {
     public List<UserInfo> getUserInfoList(UserInfo record) {
 
 
@@ -602,7 +602,7 @@ public class UserInfoServiceImpl implements UserInfoService {
     @Override
     @Override
     public UserInfo loginMiniApp(String code) {
     public UserInfo loginMiniApp(String code) {
         try {
         try {
-            WxMaJscode2SessionResult result = weixinConfig.wxMaService().jsCode2SessionInfo(code);
+            WxMaJscode2SessionResult result = wxMaService.jsCode2SessionInfo(code);
             String openId = result.getOpenid();
             String openId = result.getOpenid();
             String sessionKey = result.getSessionKey();
             String sessionKey = result.getSessionKey();
 
 
@@ -635,12 +635,12 @@ public class UserInfoServiceImpl implements UserInfoService {
     public Result getMiniAppUserInfo(String sessionKey, String rawData, String signature,
     public Result getMiniAppUserInfo(String sessionKey, String rawData, String signature,
                                      String encryptedData, String iv) {
                                      String encryptedData, String iv) {
         // 用户信息校验
         // 用户信息校验
-        if (!weixinConfig.wxMaService().getUserService().checkUserInfo(sessionKey, rawData, signature)) {
+        if (!wxMaService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
             return new Result(false, "获取用户信息失败");
             return new Result(false, "获取用户信息失败");
         }
         }
 
 
         // 解密用户信息
         // 解密用户信息
-        WxMaUserInfo wxUserInfo = weixinConfig.wxMaService().getUserService().getUserInfo(sessionKey, encryptedData, iv);
+        WxMaUserInfo wxUserInfo = wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
 
 
         UserInfo userInfo = new UserInfo();
         UserInfo userInfo = new UserInfo();
         userInfo.setOpenId(wxUserInfo.getOpenId());
         userInfo.setOpenId(wxUserInfo.getOpenId());