欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 国际 > SpringCloud乐尚代驾学习笔记:司机端登录(四)

SpringCloud乐尚代驾学习笔记:司机端登录(四)

2025/2/13 16:44:13 来源:https://blog.csdn.net/weixin_53961667/article/details/141727871  浏览:    关键词:SpringCloud乐尚代驾学习笔记:司机端登录(四)

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、司机端微信小程序登录

image-20240830211935973

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 {@Autowiredprivate WxConfigProperties wxConfigProperties;@Beanpublic 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、接口开发

image-20240830212701707

1.2.1、service-driver模块

DriverInfoController

@Tag(name = "司机API接口管理")
@RestController
@RequestMapping(value="/driver/info")
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverInfoController {@Autowiredprivate 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 {@Autowiredprivate WxMaService wxMaService;@Autowiredprivate DriverInfoMapper driverInfoMapper;@Autowiredprivate DriverSetMapper driverSetMapper;@Autowiredprivate DriverAccountMapper driverAccountMapper;@Autowiredprivate DriverLoginLogMapper driverLoginLogMapper;//小程序授权登录@Overridepublic Long login(String code) {try {//根据code + 小程序id + 秘钥请求微信接口,返回openidWxMaJscode2SessionResult 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);//返回司机idreturn 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 {@Autowiredprivate 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 {@Autowiredprivate DriverInfoFeignClient driverInfoFeignClient;@Autowiredprivate RedisTemplate redisTemplate;//登录@Overridepublic String login(String code) {//远程调用,得到司机idResult<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、获取登录司机信息

image-20240830213002411

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 -- DriverLoginVoDriverLoginVo 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 获取用户idLong driverId = AuthContextHolder.getUserId();//2 远程调用获取司机信息Result<DriverLoginVo> loginVoResult = driverInfoFeignClient.getDriverLoginInfo(driverId);DriverLoginVo driverLoginVo = loginVoResult.getData();return Result.ok(driverLoginVo);
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com