feat: 微信登录获取用户信息,客户列表按最后登录排序,修复Lombok配置
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Agent
2026-03-24 03:17:27 +00:00
parent f0ce1fe825
commit 4fb18fc40e
6 changed files with 88 additions and 16 deletions

16
pom.xml
View File

@@ -105,6 +105,22 @@
<build> <build>
<plugins> <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>

View File

@@ -38,10 +38,16 @@ public class AuthController {
/** /**
* 微信登录 * 微信登录
* @param code 微信授权码
* @param nickname 微信昵称(可选)
* @param avatar 微信头像URL可选
*/ */
@PostMapping("/wechat") @PostMapping("/wechat")
public Result<Map<String, Object>> wechatLogin(@RequestParam String code) { public Result<Map<String, Object>> wechatLogin(
Map<String, Object> result = authService.wechatLogin(code); @RequestParam String code,
@RequestParam(required = false) String nickname,
@RequestParam(required = false) String avatar) {
Map<String, Object> result = authService.wechatLogin(code, nickname, avatar);
return Result.success(result); return Result.success(result);
} }

View File

@@ -31,6 +31,16 @@ public class Customer {
*/ */
private String wechatOpenid; private String wechatOpenid;
/**
* 微信昵称
*/
private String wechatNickname;
/**
* 微信头像
*/
private String wechatAvatar;
/** /**
* 地址 * 地址
*/ */
@@ -51,6 +61,11 @@ public class Customer {
*/ */
private String createdBy; private String createdBy;
/**
* 最后登录时间(用于订单客户排序)
*/
private LocalDateTime lastLoginAt;
@TableField(fill = FieldFill.INSERT) @TableField(fill = FieldFill.INSERT)
private LocalDateTime createdAt; private LocalDateTime createdAt;

View File

@@ -19,8 +19,11 @@ public interface AuthService {
/** /**
* 微信扫码登录 * 微信扫码登录
* @param code 微信授权码
* @param nickname 微信昵称
* @param avatar 微信头像
*/ */
Map<String, Object> wechatLogin(String code); Map<String, Object> wechatLogin(String code, String nickname, String avatar);
/** /**
* 支付宝扫码登录 * 支付宝扫码登录

View File

@@ -1,8 +1,11 @@
package com.example.building.service.impl; package com.example.building.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.building.common.JwtUtil; import com.example.building.common.JwtUtil;
import com.example.building.entity.Customer;
import com.example.building.entity.User; import com.example.building.entity.User;
import com.example.building.mapper.CustomerMapper;
import com.example.building.mapper.UserMapper; import com.example.building.mapper.UserMapper;
import com.example.building.service.AuthService; import com.example.building.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -10,6 +13,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@@ -25,6 +29,9 @@ public class AuthServiceImpl implements AuthService {
@Autowired @Autowired
private UserMapper userMapper; private UserMapper userMapper;
@Autowired
private CustomerMapper customerMapper;
@Autowired @Autowired
private JwtUtil jwtUtil; private JwtUtil jwtUtil;
@@ -81,25 +88,49 @@ public class AuthServiceImpl implements AuthService {
* 实际生产中需要调用微信API获取openid * 实际生产中需要调用微信API获取openid
*/ */
@Override @Override
public Map<String, Object> wechatLogin(String code) { public Map<String, Object> wechatLogin(String code, String nickname, String avatar) {
// TODO: 调用微信API获取openid // TODO: 调用微信API获取openid
// String openid = wechatService.getOpenId(code); // String openid = wechatService.getOpenId(code);
String openid = "wechat_" + code; String openid = "wechat_" + code;
// 查询用户,不存在则创建 // 查询或创建顾客
User user = userMapper.selectOne(new LambdaQueryWrapper<User>() Customer customer = customerMapper.selectOne(new LambdaQueryWrapper<Customer>()
.eq(User::getWechatOpenid, openid)); .eq(Customer::getWechatOpenid, openid));
if (user == null) {
user = new User(); if (customer == null) {
user.setUserId(UUID.randomUUID().toString()); // 新顾客
user.setWechatOpenid(openid); customer = new Customer();
user.setUsername("微信用户"); customer.setCustomerId(UUID.randomUUID().toString());
user.setRole("sales"); customer.setWechatOpenid(openid);
user.setStatus(1); customer.setName(nickname != null ? nickname : "微信用户");
userMapper.insert(user); customer.setWechatNickname(nickname);
customer.setWechatAvatar(avatar);
customer.setTotalAmount(new java.math.BigDecimal("0"));
customer.setLastLoginAt(LocalDateTime.now());
customerMapper.insert(customer);
} else {
// 更新已有顾客信息
LambdaUpdateWrapper<Customer> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(Customer::getCustomerId, customer.getCustomerId());
if (nickname != null) {
updateWrapper.set(Customer::getWechatNickname, nickname);
}
if (avatar != null) {
updateWrapper.set(Customer::getWechatAvatar, avatar);
}
updateWrapper.set(Customer::getLastLoginAt, LocalDateTime.now());
customerMapper.update(null, updateWrapper);
customer.setLastLoginAt(LocalDateTime.now());
} }
return generateTokens(user); // 返回顾客信息
Map<String, Object> result = new HashMap<>();
result.put("token", "customer_token_" + customer.getCustomerId());
result.put("customerId", customer.getCustomerId());
result.put("name", customer.getName());
result.put("role", "customer");
return result;
} }
/** /**

View File

@@ -33,6 +33,7 @@ public class CustomerServiceImpl implements CustomerService {
.or() .or()
.like(Customer::getPhone, keyword); .like(Customer::getPhone, keyword);
} }
wrapper.orderByDesc(Customer::getLastLoginAt);
wrapper.orderByDesc(Customer::getCreatedAt); wrapper.orderByDesc(Customer::getCreatedAt);
return customerMapper.selectPage(pageParam, wrapper); return customerMapper.selectPage(pageParam, wrapper);
} }