feat: 获取乘客用户信息

This commit is contained in:
Bunny 2024-12-03 19:48:05 +08:00
parent f4c204380a
commit f2bc1c8e21
6 changed files with 38 additions and 99 deletions

View File

@ -5,16 +5,16 @@ package com.atguigu.daijia.common.util;
*/
public class AuthContextHolder {
private static ThreadLocal<Long> userId = new ThreadLocal<Long>();
public static void setUserId(Long _userId) {
userId.set(_userId);
}
private static final ThreadLocal<Long> userId = new ThreadLocal<Long>();
public static Long getUserId() {
return userId.get();
}
public static void setUserId(Long _userId) {
userId.set(_userId);
}
public static void removeUserId() {
userId.remove();
}

View File

@ -12,10 +12,11 @@ import org.springframework.web.bind.annotation.RequestBody;
@FeignClient(value = "service-customer")
public interface CustomerInfoFeignClient {
// 登录接口
// 登录客户端接口
@GetMapping("/customer/info/login/{code}")
Result<Long> login(@PathVariable String code);
// 获取乘客用户信息
@GetMapping("/customer/info/getCustomerLoginInfo/{customerId}")
Result<CustomerLoginVo> getCustomerLoginInfo(@PathVariable("customerId") Long customerId);

View File

@ -40,8 +40,7 @@ public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, Cus
// 1 获取code值使用微信工具包对象获取微信唯一标识openid
String openid;
try {
WxMaJscode2SessionResult sessionInfo =
wxMaService.getUserService().getSessionInfo(code);
WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
openid = sessionInfo.getOpenid();
} catch (WxErrorException e) {
throw new RuntimeException(e);
@ -80,14 +79,10 @@ public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, Cus
// 2 封装到CustomerLoginVo
CustomerLoginVo customerLoginVo = new CustomerLoginVo();
// customerLoginVo.setNickname(customerInfo.getNickname());
BeanUtils.copyProperties(customerInfo, customerLoginVo);
//@Schema(description = "是否绑定手机号码")
// private Boolean isBindPhone;
String phone = customerInfo.getPhone();
boolean isBindPhone = StringUtils.hasText(phone);
customerLoginVo.setIsBindPhone(isBindPhone);
customerLoginVo.setIsBindPhone(StringUtils.hasText(phone));
// 3 CustomerLoginVo返回
return customerLoginVo;

View File

@ -25,7 +25,6 @@ public class CustomerController {
@GuiguLogin
@GetMapping("/getCustomerLoginInfo")
public Result<CustomerLoginVo> getCustomerLoginInfo() {
// 1 从ThreadLocal获取用户id
Long customerId = AuthContextHolder.getUserId();
@ -35,25 +34,11 @@ public class CustomerController {
return Result.ok(customerLoginVo);
}
// @Operation(summary = "获取客户登录信息")
// @GetMapping("/getCustomerLoginInfo")
// public Result<CustomerLoginVo>
// getCustomerLoginInfo(@RequestHeader(value = "token") String token) {
//
// //1 从请求头获取token字符串
//// HttpServletRequest request
//// String token = request.getHeader("token");
//
// //调用service
// CustomerLoginVo customerLoginVo = customerInfoService.getCustomerLoginInfo(token);
//
// return Result.ok(customerLoginVo);
// }
@Operation(summary = "小程序授权登录")
@GetMapping("/login/{code}")
public Result<String> wxLogin(@PathVariable String code) {
return Result.ok(customerInfoService.login(code));
String login = customerInfoService.login(code);
return Result.ok(login);
}
@Operation(summary = "更新用户微信手机号")

View File

@ -5,15 +5,12 @@ import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
public interface CustomerService {
//微信登录
// 微信登录
String login(String code);
//获取用户信息
CustomerLoginVo getCustomerLoginInfo(String token);
//获取用户信息
// 获取用户信息
CustomerLoginVo getCustomerInfo(Long customerId);
//更新用户微信手机号
// 更新用户微信手机号
Boolean updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm);
}

View File

@ -12,7 +12,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@ -22,7 +21,7 @@ import java.util.concurrent.TimeUnit;
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {
//注入远程调用接口
// 注入远程调用接口
@Autowired
private CustomerInfoFeignClient client;
@ -34,96 +33,58 @@ public class CustomerServiceImpl implements CustomerService {
@Override
public String login(String code) {
//1 拿着code进行远程调用返回用户id
// 1 拿着code进行远程调用返回用户id
Result<Long> loginResult = client.login(code);
//2 判断如果返回失败了返回错误提示
// 2 判断如果返回失败了返回错误提示
Integer codeResult = loginResult.getCode();
if(codeResult != 200) {
if (codeResult != 200) {
throw new GuiguException(ResultCodeEnum.DATA_ERROR);
}
//3 获取远程调用返回用户id
// 3 获取远程调用返回用户id
Long customerId = loginResult.getData();
//4 判断返回用户id是否为空如果为空返回错误提示
if(customerId == null) {
// 4 判断返回用户id是否为空如果为空返回错误提示
if (customerId == null) {
throw new GuiguException(ResultCodeEnum.DATA_ERROR);
}
//5 生成token字符串
String token = UUID.randomUUID().toString().replaceAll("-","");
// 5 生成token字符串
String token = UUID.randomUUID().toString().replaceAll("-", "");
//6 把用户id放到Redis设置过期时间
// 6 把用户id放到Redis设置过期时间
// key:token value:customerId
//redisTemplate.opsForValue().set(token,customerId.toString(),30, TimeUnit.MINUTES);
redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token,
// redisTemplate.opsForValue().set(token,customerId.toString(),30, TimeUnit.MINUTES);
redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token,
customerId.toString(),
RedisConstant.USER_LOGIN_KEY_TIMEOUT,
TimeUnit.SECONDS);
//7 返回token
// 7 返回token
return token;
}
@Override
public CustomerLoginVo getCustomerLoginInfo(String token) {
//2 根据token查询redis
//3 查询token在redis里面对应用户id
String customerId =
(String)redisTemplate.opsForValue()
.get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);
if(StringUtils.isEmpty(customerId)) {
throw new GuiguException(ResultCodeEnum.DATA_ERROR);
}
// if(!StringUtils.hasText(customerId)) {
// throw new GuiguException(ResultCodeEnum.DATA_ERROR);
// }
//4 根据用户id进行远程调用 得到用户信息
Result<CustomerLoginVo> customerLoginVoResult =
customerInfoFeignClient.getCustomerLoginInfo(Long.parseLong(customerId));
Integer code = customerLoginVoResult.getCode();
if(code != 200) {
throw new GuiguException(ResultCodeEnum.DATA_ERROR);
}
CustomerLoginVo customerLoginVo = customerLoginVoResult.getData();
if(customerLoginVo == null) {
throw new GuiguException(ResultCodeEnum.DATA_ERROR);
}
//5 返回用户信息
return customerLoginVo;
}
//获取用户信息
// 获取用户信息
@Override
public CustomerLoginVo getCustomerInfo(Long customerId) {
//根据用户id进行远程调用 得到用户信息
Result<CustomerLoginVo> customerLoginVoResult =
customerInfoFeignClient.getCustomerLoginInfo(customerId);
// 根据用户id进行远程调用 得到用户信息
Result<CustomerLoginVo> customerLoginVoResult = customerInfoFeignClient.getCustomerLoginInfo(customerId);
Integer code = customerLoginVoResult.getCode();
if(code != 200) {
throw new GuiguException(ResultCodeEnum.DATA_ERROR);
}
if (code != 200) throw new GuiguException(ResultCodeEnum.DATA_ERROR);
CustomerLoginVo customerLoginVo = customerLoginVoResult.getData();
if(customerLoginVo == null) {
throw new GuiguException(ResultCodeEnum.DATA_ERROR);
}
//5 返回用户信息
if (customerLoginVo == null) throw new GuiguException(ResultCodeEnum.DATA_ERROR);
// 5 返回用户信息
return customerLoginVo;
}
//更新用户微信手机号
// 更新用户微信手机号
@Override
public Boolean updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm) {
Result<Boolean> booleanResult = customerInfoFeignClient.updateWxPhoneNumber(updateWxPhoneForm);
return true;
}
}