SpringCloud乐尚代驾学习笔记:司机端登录(四)
文章目录
- SpringCloud乐尚代驾学习笔记:司机端登录(四)
- 1、司机端微信小程序登录
- 1.1、准备工作
- 1.2、接口开发
- 1.2.1、service-driver模块
- 1.2.2、openFeign远程调用定义
- 1.2.3、web-driver模块
- 2、获取登录司机信息
- 2.1、service-driver模块
- 2.2、openFeign远程调用定义
- 2.3、web-driver模块
1、司机端微信小程序登录
1.1、准备工作
引入微信工具包依赖
<dependencies>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
</dependency>
</dependencies>
-
修改项目配置文件和Nacos里面配置文件内
-
创建类,读取配置文件内容,微信小程序id和秘钥
– 因为司机端和乘客端相同的,从乘客端直接复制相关类就可以了
@Component
@Data
@ConfigurationProperties(prefix = "wx.miniapp")
public class WxConfigProperties {
private String appId;
private String secret;
}
创建类,初始化微信工具包相关对象
@Component
public class WxConfigOperator {
@Autowired
private WxConfigProperties wxConfigProperties;
@Bean
public WxMaService wxMaService() {
//微信小程序id和秘钥
WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
wxMaConfig.setAppid(wxConfigProperties.getAppId());
wxMaConfig.setSecret(wxConfigProperties.getSecret());
WxMaService service = new WxMaServiceImpl();
service.setWxMaConfig(wxMaConfig);
return service;
}
}
1.2、接口开发
1.2.1、service-driver模块
DriverInfoController
@Tag(name = "司机API接口管理")
@RestController
@RequestMapping(value="/driver/info")
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverInfoController {
@Autowired
private DriverInfoService driverInfoService;
@Operation(summary = "小程序授权登录")
@GetMapping("/login/{code}")
public Result<Long> login(@PathVariable String code) {
return Result.ok(driverInfoService.login(code));
}
}
service
@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverInfoServiceImpl extends ServiceImpl<DriverInfoMapper, DriverInfo> implements DriverInfoService {
@Autowired
private WxMaService wxMaService;
@Autowired
private DriverInfoMapper driverInfoMapper;
@Autowired
private DriverSetMapper driverSetMapper;
@Autowired
private DriverAccountMapper driverAccountMapper;
@Autowired
private DriverLoginLogMapper driverLoginLogMapper;
//小程序授权登录
@Override
public Long login(String code) {
try {
//根据code + 小程序id + 秘钥请求微信接口,返回openid
WxMaJscode2SessionResult sessionInfo =
wxMaService.getUserService().getSessionInfo(code);
String openid = sessionInfo.getOpenid();
//根据openid查询是否第一次登录
LambdaQueryWrapper<DriverInfo> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(DriverInfo::getWxOpenId,openid);
DriverInfo driverInfo = driverInfoMapper.selectOne(wrapper);
if(driverInfo == null) {
//添加司机基本信息
driverInfo = new DriverInfo();
driverInfo.setNickname(String.valueOf(System.currentTimeMillis()));
driverInfo.setAvatarUrl("https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
driverInfo.setWxOpenId(openid);
driverInfoMapper.insert(driverInfo);
//初始化司机设置
DriverSet driverSet = new DriverSet();
driverSet.setDriverId(driverInfo.getId());
driverSet.setOrderDistance(new BigDecimal(0));//0:无限制
driverSet.setAcceptDistance(new BigDecimal(SystemConstant.ACCEPT_DISTANCE));//默认接单范围:5公里
driverSet.setIsAutoAccept(0);//0:否 1:是
driverSetMapper.insert(driverSet);
//初始化司机账户信息
DriverAccount driverAccount = new DriverAccount();
driverAccount.setDriverId(driverInfo.getId());
driverAccountMapper.insert(driverAccount);
}
//记录司机登录信息
DriverLoginLog driverLoginLog = new DriverLoginLog();
driverLoginLog.setDriverId(driverInfo.getId());
driverLoginLog.setMsg("小程序登录");
driverLoginLogMapper.insert(driverLoginLog);
//返回司机id
return driverInfo.getId();
} catch (WxErrorException e) {
throw new GuiguException(ResultCodeEnum.DATA_ERROR);
}
}
}
1.2.2、openFeign远程调用定义
@FeignClient(value = "service-driver")
public interface DriverInfoFeignClient {
/**
* 小程序授权登录
* @param code
* @return
*/
@GetMapping("/driver/info/login/{code}")
Result<Long> login(@PathVariable("code") String code);
}
1.2.3、web-driver模块
controller
@Slf4j
@Tag(name = "司机API接口管理")
@RestController
@RequestMapping(value="/driver")
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverController {
@Autowired
private DriverService driverService;
@Operation(summary = "小程序授权登录")
@GetMapping("/login/{code}")
public Result<String> login(@PathVariable String code) {
return Result.ok(driverService.login(code));
}
}
service
@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverServiceImpl implements DriverService {
@Autowired
private DriverInfoFeignClient driverInfoFeignClient;
@Autowired
private RedisTemplate redisTemplate;
//登录
@Override
public String login(String code) {
//远程调用,得到司机id
Result<Long> longResult = driverInfoFeignClient.login(code);
//TODO 判断
Long driverId = longResult.getData();
//token字符串
String token = UUID.randomUUID().toString().replaceAll("-","");
//放到redis,设置过期时间
redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token,
driverId.toString(),
RedisConstant.USER_LOGIN_KEY_TIMEOUT,
TimeUnit.SECONDS);
return token;
}
}
2、获取登录司机信息
2.1、service-driver模块
controller
@Operation(summary = "获取司机登录信息")
@GetMapping("/getDriverLoginInfo/{driverId}")
public Result<DriverLoginVo> getDriverInfo(@PathVariable Long driverId) {
DriverLoginVo driverLoginVo = driverInfoService.getDriverInfo(driverId);
return Result.ok(driverLoginVo);
}
service
//获取司机登录信息
@Override
public DriverLoginVo getDriverInfo(Long driverId) {
//根据司机id获取司机信息
DriverInfo driverInfo = driverInfoMapper.selectById(driverId);
//driverInfo -- DriverLoginVo
DriverLoginVo driverLoginVo = new DriverLoginVo();
BeanUtils.copyProperties(driverInfo,driverLoginVo);
//是否建档人脸识别
String faceModelId = driverInfo.getFaceModelId();
boolean isArchiveFace = StringUtils.hasText(faceModelId);
driverLoginVo.setIsArchiveFace(isArchiveFace);
return driverLoginVo;
}
2.2、openFeign远程调用定义
@FeignClient(value = "service-driver")
public interface DriverInfoFeignClient {
@GetMapping("/driver/info/login/{code}")
public Result<Long> login(@PathVariable String code);
@GetMapping("/driver/info/getDriverLoginInfo/{driverId}")
public Result<DriverLoginVo> getDriverInfo(@PathVariable Long driverId);
}
2.3、web-driver模块
@Operation(summary = "获取司机登录信息")
@GuiguLogin
@GetMapping("/getDriverLoginInfo")
public Result<DriverLoginVo> getDriverLoginInfo() {
//1 获取用户id
Long driverId = AuthContextHolder.getUserId();
//2 远程调用获取司机信息
Result<DriverLoginVo> loginVoResult = driverInfoFeignClient.getDriverLoginInfo(driverId);
DriverLoginVo driverLoginVo = loginVoResult.getData();
return Result.ok(driverLoginVo);
}