feat: 微信登录获取用户信息,客户列表按最后登录排序,修复Lombok配置
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
16
pom.xml
16
pom.xml
@@ -105,6 +105,22 @@
|
||||
|
||||
<build>
|
||||
<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>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
|
||||
@@ -38,10 +38,16 @@ public class AuthController {
|
||||
|
||||
/**
|
||||
* 微信登录
|
||||
* @param code 微信授权码
|
||||
* @param nickname 微信昵称(可选)
|
||||
* @param avatar 微信头像URL(可选)
|
||||
*/
|
||||
@PostMapping("/wechat")
|
||||
public Result<Map<String, Object>> wechatLogin(@RequestParam String code) {
|
||||
Map<String, Object> result = authService.wechatLogin(code);
|
||||
public Result<Map<String, Object>> wechatLogin(
|
||||
@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);
|
||||
}
|
||||
|
||||
|
||||
@@ -31,6 +31,16 @@ public class Customer {
|
||||
*/
|
||||
private String wechatOpenid;
|
||||
|
||||
/**
|
||||
* 微信昵称
|
||||
*/
|
||||
private String wechatNickname;
|
||||
|
||||
/**
|
||||
* 微信头像
|
||||
*/
|
||||
private String wechatAvatar;
|
||||
|
||||
/**
|
||||
* 地址
|
||||
*/
|
||||
@@ -51,6 +61,11 @@ public class Customer {
|
||||
*/
|
||||
private String createdBy;
|
||||
|
||||
/**
|
||||
* 最后登录时间(用于订单客户排序)
|
||||
*/
|
||||
private LocalDateTime lastLoginAt;
|
||||
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
/**
|
||||
* 支付宝扫码登录
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package com.example.building.service.impl;
|
||||
|
||||
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.entity.Customer;
|
||||
import com.example.building.entity.User;
|
||||
import com.example.building.mapper.CustomerMapper;
|
||||
import com.example.building.mapper.UserMapper;
|
||||
import com.example.building.service.AuthService;
|
||||
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.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
@@ -25,6 +29,9 @@ public class AuthServiceImpl implements AuthService {
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Autowired
|
||||
private CustomerMapper customerMapper;
|
||||
|
||||
@Autowired
|
||||
private JwtUtil jwtUtil;
|
||||
|
||||
@@ -81,25 +88,49 @@ public class AuthServiceImpl implements AuthService {
|
||||
* 实际生产中需要调用微信API获取openid
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> wechatLogin(String code) {
|
||||
public Map<String, Object> wechatLogin(String code, String nickname, String avatar) {
|
||||
// TODO: 调用微信API获取openid
|
||||
// String openid = wechatService.getOpenId(code);
|
||||
String openid = "wechat_" + code;
|
||||
|
||||
// 查询用户,不存在则创建
|
||||
User user = userMapper.selectOne(new LambdaQueryWrapper<User>()
|
||||
.eq(User::getWechatOpenid, openid));
|
||||
if (user == null) {
|
||||
user = new User();
|
||||
user.setUserId(UUID.randomUUID().toString());
|
||||
user.setWechatOpenid(openid);
|
||||
user.setUsername("微信用户");
|
||||
user.setRole("sales");
|
||||
user.setStatus(1);
|
||||
userMapper.insert(user);
|
||||
// 查询或创建顾客
|
||||
Customer customer = customerMapper.selectOne(new LambdaQueryWrapper<Customer>()
|
||||
.eq(Customer::getWechatOpenid, openid));
|
||||
|
||||
if (customer == null) {
|
||||
// 新顾客
|
||||
customer = new Customer();
|
||||
customer.setCustomerId(UUID.randomUUID().toString());
|
||||
customer.setWechatOpenid(openid);
|
||||
customer.setName(nickname != null ? nickname : "微信用户");
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -33,6 +33,7 @@ public class CustomerServiceImpl implements CustomerService {
|
||||
.or()
|
||||
.like(Customer::getPhone, keyword);
|
||||
}
|
||||
wrapper.orderByDesc(Customer::getLastLoginAt);
|
||||
wrapper.orderByDesc(Customer::getCreatedAt);
|
||||
return customerMapper.selectPage(pageParam, wrapper);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user