当前位置: 首页 > news >正文

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);
}

http://www.mrgr.cn/news/15876.html

相关文章:

  • 【化学方程式配平 / 3】
  • 笔记:应用Visual Studio Profiler分析CPU使用情况
  • Python数据分析的数据导入和导出
  • 「数组」二分查找模版|二段性分析|恢复二段性/ LeetCode 35|33|81(C++)
  • Python中的“break”与“continue”:控制循环的艺术
  • DDR test Tool for imx9
  • 【Python篇】Python 类和对象:详细讲解(上篇)
  • 生产环境中变态开启devtools(强制)
  • classA cla= ...; if(cla == nullptr) 这种写法是否安全
  • 远程教学必备神器:热门远程控制软件大盘点
  • Vue3.0教程001:Vue3简介
  • 一些零碎的关于合约测试,ERC20调用的知识
  • python基础(14内置函数介绍)
  • 第22周:调用Gensim库训练Word2Vec模型
  • 掌握SQL数据分割技巧:垂直与水平分割全解析
  • flume系列之:批量并行启动、停止、重启flume agent组
  • 【Linux】在 bash shell 环境下,当一命令正在执行时,按下 control-Z 会?
  • 【干货分享!】十五届蓝桥杯单片机国一经验分享
  • 【数学建模经验贴】数模的意义,并不只在于获奖,而在于历练!
  • union 的正确食用方法