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 { public class AuthContextHolder {
private static ThreadLocal<Long> userId = new ThreadLocal<Long>(); private static final ThreadLocal<Long> userId = new ThreadLocal<Long>();
public static void setUserId(Long _userId) {
userId.set(_userId);
}
public static Long getUserId() { public static Long getUserId() {
return userId.get(); return userId.get();
} }
public static void setUserId(Long _userId) {
userId.set(_userId);
}
public static void removeUserId() { public static void removeUserId() {
userId.remove(); userId.remove();
} }

View File

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

View File

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

View File

@ -25,7 +25,6 @@ public class CustomerController {
@GuiguLogin @GuiguLogin
@GetMapping("/getCustomerLoginInfo") @GetMapping("/getCustomerLoginInfo")
public Result<CustomerLoginVo> getCustomerLoginInfo() { public Result<CustomerLoginVo> getCustomerLoginInfo() {
// 1 从ThreadLocal获取用户id // 1 从ThreadLocal获取用户id
Long customerId = AuthContextHolder.getUserId(); Long customerId = AuthContextHolder.getUserId();
@ -35,25 +34,11 @@ public class CustomerController {
return Result.ok(customerLoginVo); 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 = "小程序授权登录") @Operation(summary = "小程序授权登录")
@GetMapping("/login/{code}") @GetMapping("/login/{code}")
public Result<String> wxLogin(@PathVariable String 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 = "更新用户微信手机号") @Operation(summary = "更新用户微信手机号")

View File

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