微信登录修改

This commit is contained in:
2025-12-22 13:37:22 +08:00
parent f0a6e03989
commit e09817015e
6 changed files with 200 additions and 3 deletions

View File

@@ -29,6 +29,10 @@
<groupId>org.xyzh.apis</groupId>
<artifactId>api-system</artifactId>
</dependency>
<dependency>
<groupId>org.xyzh.apis</groupId>
<artifactId>api-auth</artifactId>
</dependency>
<!-- Spring Boot Actuator父 pom 未包含) -->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@@ -2,6 +2,7 @@ package org.xyzh.system.controller;
import java.util.Arrays;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -9,14 +10,23 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xyzh.api.auth.service.AuthService;
import org.xyzh.api.system.service.GuestService;
import org.xyzh.common.core.domain.LoginDomain;
import org.xyzh.common.core.domain.LoginParam;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.dto.sys.TbGuestDTO;
import org.xyzh.common.dto.sys.TbSysUserDTO;
import org.xyzh.common.dto.sys.TbSysUserInfoDTO;
import org.xyzh.common.utils.id.IdUtil;
import org.xyzh.common.utils.validation.ValidationUtils;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
@@ -35,6 +45,9 @@ public class GuestController {
@Autowired
private GuestService guestService;
@DubboReference(version = "1.0.0", group = "auth", timeout = 5000, check = false, retries = 0)
private AuthService authService;
@PostMapping
public ResultDomain<TbGuestDTO> createGuest(TbGuestDTO guest) {
@@ -81,5 +94,105 @@ public class GuestController {
return guestService.selectGuestPage(pageRequest);
}
// ========================= 微信小程序用户识别登录 =========================
@Operation(summary = "微信小程序用户识别登录")
@PostMapping("/identify")
public ResultDomain<LoginDomain> identifyUser(@RequestBody LoginParam loginParam, HttpServletRequest request) {
// 验证参数必须有wechatId或phone
if ((loginParam.getWechatId() == null || loginParam.getWechatId().trim().isEmpty())
&& (loginParam.getPhone() == null || loginParam.getPhone().trim().isEmpty())) {
return ResultDomain.failure("微信ID或手机号不能为空");
}
// 设置登录类型为微信小程序
loginParam.setLoginType("wechat_miniprogram");
// 1. 尝试通过AuthService登录员工
ResultDomain<LoginDomain> loginResult = authService.login(loginParam, request);
if (loginResult.getSuccess() && loginResult.getData() != null) {
// 登录成功,是系统员工
return loginResult;
}
// 2. 登录失败,查询/注册来客
return handleGuestLogin(loginParam);
}
/**
* 处理来客登录查询或注册来客构造LoginDomain
*/
private ResultDomain<LoginDomain> handleGuestLogin(LoginParam loginParam) {
TbGuestDTO guest = null;
// 优先用wechatId查询
if (loginParam.getWechatId() != null && !loginParam.getWechatId().trim().isEmpty()) {
ResultDomain<TbGuestDTO> guestResult = guestService.selectGuestByWechatId(loginParam.getWechatId());
if (guestResult.getSuccess() && guestResult.getData() != null) {
guest = guestResult.getData();
}
}
// wechatId未找到尝试用phone查询
if (guest == null && loginParam.getPhone() != null && !loginParam.getPhone().trim().isEmpty()) {
TbGuestDTO filter = new TbGuestDTO();
filter.setPhone(loginParam.getPhone());
ResultDomain<TbGuestDTO> guestResult = guestService.selectGuestOne(filter);
if (guestResult.getSuccess() && guestResult.getData() != null) {
guest = guestResult.getData();
// 如果来客存在但wechatId为空更新wechatId
if (guest.getWechatId() == null && loginParam.getWechatId() != null) {
TbGuestDTO updateGuest = new TbGuestDTO();
updateGuest.setUserId(guest.getUserId());
updateGuest.setWechatId(loginParam.getWechatId());
guestService.updateGuest(updateGuest);
guest.setWechatId(loginParam.getWechatId());
}
}
}
// 3. 来客不存在,创建新来客
if (guest == null) {
TbGuestDTO newGuest = new TbGuestDTO();
newGuest.setUserId(IdUtil.generateID());
newGuest.setWechatId(loginParam.getWechatId());
newGuest.setPhone(loginParam.getPhone());
newGuest.setName(loginParam.getUsername() != null ? loginParam.getUsername() : "来客");
ResultDomain<TbGuestDTO> createResult = guestService.createGuest(newGuest);
if (!createResult.getSuccess()) {
return ResultDomain.failure("创建来客失败: " + createResult.getMessage());
}
guest = createResult.getData();
}
// 4. 构造来客的LoginDomain
return ResultDomain.success("来客登录成功", buildGuestLoginDomain(guest));
}
/**
* 从来客信息构造LoginDomain
*/
private LoginDomain buildGuestLoginDomain(TbGuestDTO guest) {
LoginDomain loginDomain = new LoginDomain();
// 构造TbSysUserDTOstatus设为guest
TbSysUserDTO userDTO = new TbSysUserDTO();
userDTO.setUserId(guest.getUserId());
userDTO.setPhone(guest.getPhone());
userDTO.setEmail(guest.getEmail());
userDTO.setWechatId(guest.getWechatId());
userDTO.setStatus("guest"); // 来客特殊状态
loginDomain.setUser(userDTO);
// 构造TbSysUserInfoDTO
TbSysUserInfoDTO userInfoDTO = new TbSysUserInfoDTO();
userInfoDTO.setUserId(guest.getUserId());
userInfoDTO.setUsername(guest.getName());
loginDomain.setUserInfo(userInfoDTO);
loginDomain.setLoginType("wechat_miniprogram");
return loginDomain;
}
}