Jelajahi Sumber

feature 代码提交

xiahan 5 bulan lalu
induk
melakukan
a20cb41c10

+ 22 - 0
zhsw-common/src/main/java/com/rongwei/zhsw/config/WeChatLoginApiPara.java

@@ -0,0 +1,22 @@
+package com.rongwei.zhsw.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+/**
+ * WeChatLoginApiPara class
+ *
+ * @author XH
+ * @date 2025/03/08
+ */
+@Component
+@ConfigurationProperties(prefix="wechat")
+@Data
+public class WeChatLoginApiPara {
+    private String appid;
+
+    private String secret;
+
+    private String grantType;
+}

+ 2 - 1
zhsw-common/src/main/java/com/rongwei/zhsw/sys/service/LoginLogicService.java

@@ -1,5 +1,6 @@
 package com.rongwei.zhsw.sys.service;
 
+import com.rongwe.zhsw.vo.WeChatLoginVo;
 import com.rongwei.rwadmincommon.system.domain.UserLoginVo;
 import com.rongwei.rwcommon.base.R;
 
@@ -17,5 +18,5 @@ public interface LoginLogicService {
      */
     R accountLogin(UserLoginVo userLogin);
 
-    R wechatLogin();
+    R wechatLogin(WeChatLoginVo weChatLoginVo);
 }

+ 7 - 13
zhsw-common/src/main/java/com/rongwei/zhsw/sys/service/impl/LoginLogicServiceImpl.java

@@ -1,6 +1,8 @@
 package com.rongwei.zhsw.sys.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.rongwe.zhsw.vo.WeChatLoginReturnVo;
+import com.rongwe.zhsw.vo.WeChatLoginVo;
 import com.rongwei.commonservice.service.RedisService;
 
 import com.rongwei.rwadmincommon.system.domain.SysUserDo;
@@ -17,6 +19,7 @@ import com.rongwei.rwcommonentity.commonservers.domain.TenantDo;
 import com.rongwei.zhsw.sys.dao.CommonBusinessDao;
 import com.rongwei.zhsw.fegin.LoginAuth;
 import com.rongwei.zhsw.sys.service.LoginLogicService;
+import com.rongwei.zhsw.utils.WeChatUtils;
 import lombok.SneakyThrows;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
@@ -48,20 +51,9 @@ public class LoginLogicServiceImpl implements LoginLogicService {
     @Autowired
     private SysUserService sysUserService;
     @Autowired
-    private CommonBusinessDao CommonBusinessDao;
-    @Autowired
     private LoginAuth loginAuth;
 
-    @Value("${login.checkPhoneNumErrorMsg:手机号码未注册!}")
-    private String checkPhoneNumErrorMsg;
-    @Value("${login.primarySchemaId:1}")
-    private String primarySchemaId;
 
-    /**
-     * 移动端登录标识
-     */
-    private static final String MOBILE_CLIENT_TYPE = "YD";
-    private static final String MOBILE_LOGING_DEFAULT_ERROR = "没有找到该手机号绑定的账号";
 
     /**
      * 账号密码登录逻辑
@@ -141,8 +133,10 @@ public class LoginLogicServiceImpl implements LoginLogicService {
     }
 
     @Override
-    public R wechatLogin() {
-        return null;
+    public R wechatLogin(WeChatLoginVo weChatLoginVo) {
+        log.info("微信用户登录");
+        WeChatLoginReturnVo login = WeChatUtils.login(weChatLoginVo.getWxCode());
+        return R.ok(login);
     }
 
 }

+ 76 - 0
zhsw-common/src/main/java/com/rongwei/zhsw/utils/WeChatUtils.java

@@ -0,0 +1,76 @@
+package com.rongwei.zhsw.utils;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.rongwe.zhsw.vo.WeChatLoginReturnVo;
+import com.rongwei.commonservice.service.RedisService;
+import com.rongwei.rwcommon.base.exception.CustomException;
+import com.rongwei.zhsw.config.WeChatLoginApiPara;
+import lombok.SneakyThrows;
+import org.apache.http.HttpEntity;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.io.IOException;
+
+/**
+ * WeChatUtils class
+ *
+ * @author XH
+ * @date 2025/03/08
+ */
+
+@Component
+public class WeChatUtils {
+
+    @Autowired
+    private WeChatLoginApiPara autoWeChatLoginApiPara;
+
+    private static WeChatLoginApiPara weChatLoginApiPara;
+    private static final Logger log = LoggerFactory.getLogger(WeChatUtils.class);
+    @PostConstruct
+    public void init() {
+        weChatLoginApiPara = autoWeChatLoginApiPara;
+    }
+    public static final String WECHAT_LOGIN_URL="https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
+
+
+
+    public static WeChatLoginReturnVo login(String jsCode){
+        String url = String.format(WECHAT_LOGIN_URL, weChatLoginApiPara.getAppid(), weChatLoginApiPara.getSecret(), jsCode);
+
+        RequestConfig config = RequestConfig.custom().setConnectTimeout(35000).setSocketTimeout(35000).build();
+        CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
+        HttpGet httpGet = new HttpGet(url);
+        CloseableHttpResponse response = null;
+        WeChatLoginReturnVo returnVo=null;
+        try {
+            response = httpclient.execute(httpGet);
+            int statusCode = response.getStatusLine().getStatusCode();
+            if(statusCode !=200){
+                log.error("接口状态异常:{}", statusCode);
+                throw new CustomException("微信登录失败");
+            }
+            HttpEntity entity = response.getEntity();
+            if (entity != null) {
+                ObjectMapper objectMapper = new ObjectMapper();
+                returnVo  = objectMapper.readValue(EntityUtils.toString(entity, "utf-8"), WeChatLoginReturnVo.class);
+            }else{
+                return returnVo;
+            }
+            response.close();
+        } catch (IOException e) {
+            log.error("获取session_key出现异常:{}",response);
+            throw new CustomException("微信登录异常");
+        }
+        return returnVo;
+    }
+}

+ 38 - 0
zhsw-entity/src/main/java/com/rongwe/zhsw/vo/WeChatLoginReturnVo.java

@@ -0,0 +1,38 @@
+package com.rongwe.zhsw.vo;
+
+import lombok.Data;
+
+/**
+ * WeChatLoginReturnVo class
+ *
+ * @author XH
+ * @date 2025/03/08
+ */
+@Data
+public class WeChatLoginReturnVo {
+    /**
+     * 会话密钥
+     */
+    private String session_key;
+    /**
+     * 用户在开放平台的唯一标识符,若当前小程序已绑定到微信开放平台账号下会返回,详见 UnionID 机制说明。
+     */
+    private String unionid;
+    /**
+     * 错误信息
+     */
+    private String errmsg;
+    /**
+     * 用户唯一标识
+     */
+    private String openid;
+    /**
+     * 错误码
+     * 40029	code 无效	js_code无效
+     * 45011	api minute-quota reach limit  mustslower  retry next minute	API 调用太频繁,请稍候再试
+     * 40226	code blocked	高风险等级用户,小程序登录拦截 。风险等级详见用户安全解方案
+     * -1	system error	系统繁忙,此时请开发者稍候再试
+     *
+     */
+    private String errcode;
+}

+ 20 - 0
zhsw-entity/src/main/java/com/rongwe/zhsw/vo/WeChatLoginVo.java

@@ -0,0 +1,20 @@
+package com.rongwe.zhsw.vo;
+
+import lombok.Data;
+
+/**
+ * WeChatLoginVo class
+ *
+ * @author XH
+ * @date 2025/03/08
+ */
+@Data
+public class WeChatLoginVo {
+    private String wxCode;
+
+    private String accountNumber;
+
+    private String accountName;
+
+    private String dsKey;
+}

+ 5 - 4
zhsw-server/src/main/java/com/rongwei/zhsw/controller/LoginLogicController.java

@@ -1,6 +1,7 @@
 package com.rongwei.zhsw.controller;
 
 
+import com.rongwe.zhsw.vo.WeChatLoginVo;
 import com.rongwei.rwadmincommon.system.domain.UserLoginVo;
 import com.rongwei.rwcommon.base.R;
 import com.rongwei.zhsw.sys.service.LoginLogicService;
@@ -39,13 +40,13 @@ public class LoginLogicController {
     }
 
     /**
-     * Core+ 通过账号密码登录和校验方法
+     * 小程序登录
      *
-     * @param userLogin
+     * @param weChatLoginVo
      * @return
      */
     @PostMapping("/wechat")
-    private R wechatLogin(@RequestBody UserLoginVo userLogin) {
-        return loginLogicService.accountLogin(userLogin);
+    private R wechatLogin(@RequestBody WeChatLoginVo weChatLoginVo) {
+        return loginLogicService.wechatLogin(weChatLoginVo);
     }
 }