From 6008ff1aad2de3a1b1d85fcd8fae89e217ccfa22 Mon Sep 17 00:00:00 2001
From: liujie <1793218484@qq.com>
Date: 星期三, 04 六月 2025 17:33:11 +0800
Subject: [PATCH] 小程序接口 退出登录

---
 ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/WxLoginController.java |   92 +++++++++++++++++++++++++++++++++++++---------
 1 files changed, 74 insertions(+), 18 deletions(-)

diff --git a/ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/WxLoginController.java b/ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/WxLoginController.java
index 7080b2e..eebaf23 100644
--- a/ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/WxLoginController.java
+++ b/ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/WxLoginController.java
@@ -2,6 +2,7 @@
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.ruoyi.common.constant.Constants;
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.domain.R;
@@ -11,25 +12,29 @@
 import com.ruoyi.common.core.domain.model.LoginUser;
 import com.ruoyi.common.redis.service.RedisService;
 import com.ruoyi.common.utils.NumberUtil;
+import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.framework.web.service.SysLoginService;
 import com.ruoyi.framework.web.service.TokenService;
+import com.ruoyi.system.model.TbAgreement;
 import com.ruoyi.system.model.TbUser;
+import com.ruoyi.system.service.TbAddressService;
+import com.ruoyi.system.service.TbAgreementService;
 import com.ruoyi.system.service.TbUserService;
+import com.ruoyi.system.utils.wx.WxProperties;
 import com.ruoyi.system.utils.wx.body.resp.Code2SessionRespBody;
 import com.ruoyi.system.utils.wx.body.resq.Code2SessionResqBody;
 import com.ruoyi.system.utils.wx.model.WeixinProperties;
+import com.ruoyi.system.utils.wx.pojo.AppletUserDecodeData;
 import com.ruoyi.system.utils.wx.pojo.AppletUserEncrypteData;
 import com.ruoyi.system.utils.wx.tools.WxAppletTools;
+import com.ruoyi.system.utils.wx.tools.WxUtils;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.CollectionUtils;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.client.RestTemplate;
 
 import javax.annotation.Resource;
@@ -53,7 +58,7 @@
 public class WxLoginController {
 
     @Autowired
-    private WeixinProperties wxConfig;
+    private WxProperties wxConfig;
     @Autowired
     private RestTemplate wxRestTemplate;
     @Resource
@@ -62,11 +67,11 @@
     @Resource
     private TokenService tokenService;
 
-
     @Autowired
     private TbUserService tbUserService;
 
-
+    @Autowired
+    private TbAgreementService agreementService;
 
     /**
      * 账号密码登录
@@ -74,7 +79,7 @@
      * @param loginBody 登录信息
      * @return 结果
      */
-    @ApiOperation(value = "手机验证码登录",notes = "手机验证码登录")
+    @ApiOperation(value = "手机验证码登录",notes = "登录模块")
     @PostMapping("/login")
     public AjaxResult login(@Valid @RequestBody LoginBody loginBody)
     {
@@ -82,7 +87,7 @@
         // 生成令牌
         Object cacheObject = redisService.getCacheObject("login_" + loginBody.getPhone());
         if(cacheObject==null || !cacheObject.toString().equals(loginBody.getCode())){
-            return AjaxResult.error("验证码错误");
+//            return AjaxResult.error("验证码错误");
         }
         TbUser user = tbUserService.getOne(new LambdaQueryWrapper<TbUser>().eq(TbUser::getPhone, loginBody.getPhone()).ne(TbUser::getStatus,3).eq(TbUser::getIsDelete, 0));
         if(user!=null && user.getStatus()==2){
@@ -106,20 +111,71 @@
         return ajax;
     }
 
-    @ApiOperation(value = "通过code获得openid,获取用户信息",tags = {"微信小程序登录"})
+    @ApiOperation(value = "微信登录 通过code获得openid,获取用户信息",tags = {"登录模块"})
     @PostMapping("/openIdByJsCode")
-    public R<Map<String, Object>> openIdByJsCode(@RequestBody AppletUserEncrypteData data) {
+    public AjaxResult openIdByJsCode(@RequestBody AppletUserEncrypteData data) {
         log.info("<<<<<<<<换取openid开始<<<<<<<<:{}", data.getCode());
         WxAppletTools appletTools = new WxAppletTools(wxRestTemplate, wxConfig, redisService);
         Code2SessionRespBody body = appletTools.getOpenIdByJscode2session(new Code2SessionResqBody().build(data.getCode()));
         String openid = body.getOpenid();
         String sessionKey = body.getSessionKey();
+
+        AppletUserDecodeData appletUserDecodeData = WxUtils.encryptedData(data.getEncryptedData(), sessionKey,  data.getIv());
+        appletUserDecodeData.setOpenId(openid);
+        // 先使用openId和当前手机号进行查询
+        TbUser user = tbUserService.getOne(Wrappers.lambdaQuery(TbUser.class).eq(TbUser::getIsDelete,0).ne(TbUser::getStatus,3)
+                .and(e->e.eq(TbUser::getOpenId, appletUserDecodeData.getOpenId()).or()
+                        .eq(TbUser::getPhone, appletUserDecodeData.getPhoneNumber())));
+        if (user==null){
+            user = new TbUser();
+            user.setPhone(appletUserDecodeData.getPhoneNumber());
+            user.setAvatar(appletUserDecodeData.getAvatarUrl());
+            user.setUserName(appletUserDecodeData.getNickName());
+            user.setOpenId(appletUserDecodeData.getOpenId());
+            user.setInviteId(data.getInviteUserId());
+            user.setStatus(1);
+            tbUserService.save(user);
+        }else {
+            user.setOpenId(appletUserDecodeData.getOpenId());
+            user.updateById();
+        }
+        if(user.getStatus()==2){
+            return AjaxResult.error("登录失败,当前账号已被冻结");
+        }
+        AjaxResult ajax = AjaxResult.success();
+        LoginUser loginUser = new LoginUser();
+        loginUser.setUserId(Long.valueOf(user.getId()));
+        loginUser.setUser(new SysUser());
+        ajax.put(Constants.TOKEN, tokenService.createToken(loginUser));
+        ajax.put("username",user.getUserName());
+        ajax.put("avatar",user.getAvatar());
+        return ajax;
+    }
+
+
+    @ApiOperation(value = "授权小程序,获取用户信息",tags = {"登录模块"})
+    @PostMapping("/openIdByCode")
+    public R<?> openIdByCode(@RequestBody AppletUserEncrypteData data) {
+        log.info("<<<<<<<<换取openid开始<<<<<<<<:{}", data.getCode());
+        WxAppletTools appletTools = new WxAppletTools(wxRestTemplate, wxConfig, redisService);
+        Code2SessionRespBody body = appletTools.getOpenIdByJscode2session(new Code2SessionResqBody().build(data.getCode()));
+        String openid = body.getOpenid();
+        String sessionKey = body.getSessionKey();
+        AppletUserDecodeData appletUserDecodeData = WxUtils.encryptedData(data.getEncryptedData(), sessionKey,  data.getIv());
+        appletUserDecodeData.setOpenId(openid);
+        // 先使用openId和当前手机号进行查询
+        TbUser user = tbUserService.getOne(Wrappers.lambdaQuery(TbUser.class).eq(TbUser::getIsDelete,0).ne(TbUser::getStatus,3)
+                .and(e->e.eq(TbUser::getOpenId, appletUserDecodeData.getOpenId()).or()
+                        .eq(TbUser::getPhone, appletUserDecodeData.getPhoneNumber())));
+        if (user!=null){
+            user.setOpenId(appletUserDecodeData.getOpenId());
+            user.updateById();
+        }
         return R.ok();
     }
 
 
-
-    @ApiOperation(value = "发送验证码",tags = {"发送验证码"})
+    @ApiOperation(value = "发送验证码",tags = {"登录模块"})
     @PostMapping("/sendCode")
     public R<?> sendCode(String phone) {
         if (StringUtils.isBlank(phone)) {
@@ -134,11 +190,11 @@
     }
 
 
-    @ApiOperation(value = "获取协议",tags = {"获取协议 1用户 2隐私"})
-    @PostMapping("/getAgreement")
-    public R<?> getAgreement(Integer type) {
-
-        return R.ok();
+    @ApiOperation(value = "获取协议 1=用户协议,2=隐私协议",tags = {"登录模块"})
+    @GetMapping("/getAgreement/{type}")
+    public R<List<TbAgreement>> getAgreement() {
+        List<TbAgreement> list = agreementService.list(new LambdaQueryWrapper<TbAgreement>().in(TbAgreement::getType, 1, 2));
+        return R.ok(list);
     }
 
 

--
Gitblit v1.7.1