本章内容
- 完成司机端微信授权登录与获取司机登录信息
- 掌握腾讯云对象存储COS
- 掌握腾讯云OCR,识别身份证与驾驶证
- 完成司机认证功能
- 掌握司机腾讯云人脸识别技术
这章基本都是接口调用,复制就完事了!
司机登录
司机端登录与乘客端登录一直,都是微信授权登录,司机第一次登录需要初始化司机设置消息、司机账户信息等。
准备工作
在service -> servicec-driver中引入依赖,修改nacos配置文件中的信息,修改properties中的信息。
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
</dependency>
将上一章在乘客端创建的读取文件内容的类复制到司机端中。
接口开发
在service-sriver下的DriverInfoController创建接口
@Autowired
private DriverInfoService driverInfoService;
@Operation(summary = "小程序授权登录")
@GetMapping("/login/{code}")
public Result<Long> login(@PathVariable String code){
return Result.ok(driverInfoService.login(code));
}
service实现类
@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 RuntimeException(e);
}
}
service-client远程调用定义
/**
* 小程序授权登录
* @param code
* @return
*/
@GetMapping("/driver/info/login/{code}")
Result<Long> login(@PathVariable("code") String code);
在web-controller进行远程调用
@Autowired
private DriverService driverService;
@Operation(summary = "小程序授权登录")
@GetMapping("/login/{code}")
public Result<String> login(@PathVariable String code) {
return Result.ok(driverService.login(code));
}
service实现类
@Autowired
private DriverInfoFeignClient driverInfoFeignClient;
@Autowired
private RedisTemplate redisTemplate;
// 小程序授权登录
@Override
public String login(String code) {
// 远程调用,得到司机id
Result<Long> loginResult = driverInfoFeignClient.login(code);
// 判断
Integer codeResult = loginResult.getCode();
if(codeResult != 200){
throw new GuiguException(ResultCodeEnum.DATA_ERROR);
}
Long driverId = loginResult.getData();
if(driverId == null){
throw new GuiguException(ResultCodeEnum.DATA_ERROR);
}
// token字符串
String token = UUID.randomUUID().toString().replaceAll("-", "");
// 放到redis,设置过期时间
redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token,
driverId,
RedisConstant.USER_LOGIN_KEY_TIMEOUT,
TimeUnit.SECONDS);
return token;
}
获取登录司机信息
DriverInfoController
@Operation(summary = "获取司机登录信息")
@GetMapping("/getDriverLoginInfo/{driverId}")
public Result<DriverLoginVo> getDriverInfo(@PathVariable Long driverId){
DriverLoginVo driverLoginVo = driverInfoService.getDriverInfo(driverId);
return Result.ok(driverLoginVo);
}
DriverInfoServiceImpl
// 获取司机登录信息
@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;
}
DriverInfoFeignClient
/**
* 获取司机登录信息
* @param driverId
* @return
*/
@GetMapping("/driver/info/getDriverLoginInfo/{driverId}")
Result<DriverLoginVo> getDriverLoginInfo(@PathVariable("driverId") Long driverId);
DriverController
@Autowired
private DriverInfoFeignClient driverInfoFeignClient;
@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);
}
启动这三个服务,打开司机端微信小程序,进行测试。
司机认证
当司机点击开始接单的时候,会先判断该司机有没有通过认证,如果没有认证,会先跳转到认证界面进行认证。
司机认证模块需要集成腾讯云存储COS、腾讯云OCR证件识别及腾讯云人脸模型库创建。
开通腾讯云对象存储COS
进入官网直接搜索对象存储,进入之后点击立即使用,如果第一次使用的话需要认证。
认证完成之后开通COS服务
开通之后界面
点击创建存储桶,创建完成之后可以在存储桶列表查看创建的桶。
如果要使用java代码进行操作,需要获取腾讯云账号的id和key,点击右上角的账户 ->访问管理
在API秘钥管理的创建秘钥,记得保存好SecretId和SecretKey。
腾讯云对象存储上传接口
远程调用编写,web-driver下的CosController
@Autowired
private CosService cosService;
// 文件上传接口
@Operation(summary = "上传")
@GuiGuLogin
@PostMapping("/upload")
public Result<CosUploadVo> upload(@RequestPart("file")MultipartFile file,
@RequestParam(name = "path", defaultValue = "auth") String path){
CosUploadVo cosUploadVo = cosService.uploadFile(file, path);
return Result.ok(cosUploadVo);
}
CosServiceImpl
@Autowired
private CosFeignClient cosFeignClient;
// 文件上传接口
@Override
public CosUploadVo uploadFile(MultipartFile file, String path) {
// 远程调用
Result<CosUploadVo> cosUploadVoResult = cosFeignClient.upload(file, path);
CosUploadVo cosUploadVo = cosUploadVoResult.getData();
return cosUploadVo;
}
CosFeignClient,MediaType.MULTIPART_FORM_DATA_VALUE表示传的类型是文件
//文件上传
@PostMapping(value = "/cos/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Result<CosUploadVo> upload(@RequestPart("file") MultipartFile file, @RequestParam("path") String path);
在service-driver里引入依赖
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
</dependency>
把腾讯云需要的值放到相关配置文件中,在common-account.yaml中进行修改,修改其中的值
在service-driver的config包下创建类读取相关信息配置
@Data
@Component
@ConfigurationProperties(prefix = "tencent.cloud")
public class TencentCloudProperties {
private String secretId;
private String secretKey;
private String region;
private String bucketPrivate;
}
CosController
@Autowired
private CosService cosService;
@Operation(summary = "上传")
@PostMapping("/upload")
public Result<CosUploadVo> upload(@RequestPart("file") MultipartFile file,
@RequestParam("path") String path) {
CosUploadVo cosUploadVo = cosService.upload(file,path);
return Result.ok(cosUploadVo);
}
CosServiceImpl
@Autowired
private TencentCloudProperties tencentCloudProperties;
// 文件上传
@Override
public CosUploadVo upload(MultipartFile file, String path) {
// 1.初始化用户身份信息(secretId, secretKey)
String secretId = tencentCloudProperties.getSecretId();
String secretKey = tencentCloudProperties.getSecretKey();
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
// 2.设置bucket的地域,COS地域
Region region = new Region(tencentCloudProperties.getRegion());
ClientConfig clientConfig = new ClientConfig(region);
// 这里建议设置使用https协议
clientConfig.setHttpProtocol(HttpProtocol.https);
// 3.生成cos客户端
COSClient cosClient = new COSClient(cred, clientConfig);
// 文件上传
// 元数据信息
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(file.getSize());
meta.setContentEncoding("UTF-8");
meta.setContentType(file.getContentType());
// 向存储桶中保存文件
String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); //文件后缀名
String uploadPath = "/driver/" + path + "/" + UUID.randomUUID().toString().replaceAll("-", "") + fileType;
// 01.jpg
// /driver/auth/0o98754.jpg
PutObjectRequest putObjectRequest = null;
try {
//1 bucket名称
//2
putObjectRequest = new PutObjectRequest(tencentCloudProperties.getBucketPrivate(),
uploadPath,
file.getInputStream(),
meta);
} catch (IOException e) {
throw new RuntimeException(e);
}
putObjectRequest.setStorageClass(StorageClass.Standard);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest); //上传文件
cosClient.shutdown();
//返回vo对象
CosUploadVo cosUploadVo = new CosUploadVo();
cosUploadVo.setUrl(uploadPath);
//TODO 图片临时访问url,回显使用
cosUploadVo.setShowUrl("");
return cosUploadVo;
}
启动相关服务进行测试
在地址栏输入路径http://localhost:8602/doc.html#/home打开接口文档进行测试
注意:这里需要把CosController接口的@GuiguLogin注解注释了,不然会提示未登录
上传接口完善,回显图片,可以参考官方文档->生成预签名URL
对CosServiceImpl的upload方法中的代码进行封装
public COSClient getCosClient(){
// 1.初始化用户身份信息(secretId, secretKey)
String secretId = tencentCloudProperties.getSecretId();
String secretKey = tencentCloudProperties.getSecretKey();
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
// 2.设置bucket的地域,COS地域
Region region = new Region(tencentCloudProperties.getRegion());
ClientConfig clientConfig = new ClientConfig(region);
// 这里建议设置使用https协议
clientConfig.setHttpProtocol(HttpProtocol.https);
// 3.生成cos客户端
COSClient cosClient = new COSClient(cred, clientConfig);
return cosClient;
}
CosServiceImpl
//获取临时签名URL
@Override
public String getImageUrl(String path) {
if(!StringUtils.hasText(path)) return "";
//获取cosclient对象
COSClient cosClient = this.getCosClient();
//GeneratePresignedUrlRequest
GeneratePresignedUrlRequest request =
new GeneratePresignedUrlRequest(tencentCloudProperties.getBucketPrivate(),
path, HttpMethodName.GET);
//设置临时URL有效期为15分钟
Date date = new DateTime().plusMinutes(15).toDate();
request.setExpiration(date);
//调用方法获取
URL url = cosClient.generatePresignedUrl(request);
cosClient.shutdown();
return url.toString();
}
将upload的中TODO进行完善
腾讯云身份证认证接口
司机注册成功之后,应该引导他去做实名认证,这就需要用到腾讯云身份证识别和云存储功能了
在service-driver引入ocr依赖
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>${tencentcloud.version}</version>
</dependency>
OcrController
@Autowired
private OcrService ocrService;
@Operation(summary = "身份证识别")
@PostMapping("/idCardOcr")
public Result<IdCardOcrVo> idCardOcr(@RequestPart("file") MultipartFile file) {
IdCardOcrVo idCardOcrVo = ocrService.idCardOcr(file);
return Result.ok(idCardOcrVo);
}
OcrServiceImpl
@Autowired
private TencentCloudProperties tencentCloudProperties;
@Autowired
private CosService cosService;
//身份证识别
@Override
public IdCardOcrVo idCardOcr(MultipartFile file) {
try{
//图片转换base64格式字符串
byte[] base64 = Base64.encodeBase64(file.getBytes());
String fileBase64 = new String(base64);
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
Credential cred = new Credential(tencentCloudProperties.getSecretId(),
tencentCloudProperties.getSecretKey());
// 实例化一个http选项,可选的,没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("ocr.tencentcloudapi.com");
// 实例化一个client选项,可选的,没有特殊需求可以跳过
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
// 实例化要请求产品的client对象,clientProfile是可选的
OcrClient client = new OcrClient(cred,tencentCloudProperties.getRegion(), clientProfile);
// 实例化一个请求对象,每个接口都会对应一个request对象
IDCardOCRRequest req = new IDCardOCRRequest();
//设置文件
req.setImageBase64(fileBase64);
// 返回的resp是一个IDCardOCRResponse的实例,与请求对象对应
IDCardOCRResponse resp = client.IDCardOCR(req);
//转换为IdCardOcrVo对象
IdCardOcrVo idCardOcrVo = new IdCardOcrVo();
if (StringUtils.hasText(resp.getName())) {
//身份证正面
idCardOcrVo.setName(resp.getName());
idCardOcrVo.setGender("男".equals(resp.getSex()) ? "1" : "2");
idCardOcrVo.setBirthday(DateTimeFormat.forPattern("yyyy/MM/dd").parseDateTime(resp.getBirth()).toDate());
idCardOcrVo.setIdcardNo(resp.getIdNum());
idCardOcrVo.setIdcardAddress(resp.getAddress());
//上传身份证正面图片到腾讯云cos
CosUploadVo cosUploadVo = cosService.upload(file, "idCard");
idCardOcrVo.setIdcardFrontUrl(cosUploadVo.getUrl());
idCardOcrVo.setIdcardFrontShowUrl(cosUploadVo.getShowUrl());
} else {
//身份证反面
//证件有效期:"2010.07.21-2020.07.21"
String idcardExpireString = resp.getValidDate().split("-")[1];
idCardOcrVo.setIdcardExpire(DateTimeFormat.forPattern("yyyy.MM.dd").parseDateTime(idcardExpireString).toDate());
//上传身份证反面图片到腾讯云cos
CosUploadVo cosUploadVo = cosService.upload(file, "idCard");
idCardOcrVo.setIdcardBackUrl(cosUploadVo.getUrl());
idCardOcrVo.setIdcardBackShowUrl(cosUploadVo.getShowUrl());
}
return idCardOcrVo;
} catch (Exception e) {
throw new GuiguException(ResultCodeEnum.DATA_ERROR);
}
}
service-client定义接口
OcrFeignClient
/**
* 身份证识别
* @param file
* @return
*/
@PostMapping(value = "/ocr/idCardOcr", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Result<IdCardOcrVo> idCardOcr(@RequestPart("file") MultipartFile file);
在web-driver中
OcrController
@Autowired
private OcrService ocrService;
@Operation(summary = "身份证识别")
@GuiguLogin
@PostMapping("/idCardOcr")
public Result<IdCardOcrVo> uploadDriverLicenseOcr(@RequestPart("file") MultipartFile file) {
return Result.ok(ocrService.idCardOcr(file));
}
OcrServiceImpl
@Autowired
private OcrFeignClient ocrFeignClient;
//身份证识别
@Override
public IdCardOcrVo idCardOcr(MultipartFile file) {
Result<IdCardOcrVo> ocrVoResult = ocrFeignClient.idCardOcr(file);
IdCardOcrVo idCardOcrVo = ocrVoResult.getData();
return idCardOcrVo;
}
腾讯云驾驶证识别接口
在service-driver中
OcrController
@Operation(summary = "驾驶证识别")
@PostMapping("/driverLicenseOcr")
public Result<DriverLicenseOcrVo> driverLicenseOcr(@RequestPart("file") MultipartFile file) {
return Result.ok(ocrService.driverLicenseOcr(file));
}
OcrServiceImpl
// 驾驶证识别
@Override
public DriverLicenseOcrVo driverLicenseOcr(MultipartFile file) {
try{
//图片转换base64格式字符串
byte[] base64 = Base64.encodeBase64(file.getBytes());
String fileBase64 = new String(base64);
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
Credential cred = new Credential(tencentCloudProperties.getSecretId(),
tencentCloudProperties.getSecretKey());
// 实例化一个http选项,可选的,没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("ocr.tencentcloudapi.com");
// 实例化一个client选项,可选的,没有特殊需求可以跳过
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
// 实例化要请求产品的client对象,clientProfile是可选的
OcrClient client = new OcrClient(cred, tencentCloudProperties.getRegion(),
clientProfile);
// 实例化一个请求对象,每个接口都会对应一个request对象
DriverLicenseOCRRequest req = new DriverLicenseOCRRequest();
req.setImageBase64(fileBase64);
// 返回的resp是一个DriverLicenseOCRResponse的实例,与请求对象对应
DriverLicenseOCRResponse resp = client.DriverLicenseOCR(req);
//封装到vo对象里面
DriverLicenseOcrVo driverLicenseOcrVo = new DriverLicenseOcrVo();
if (StringUtils.hasText(resp.getName())) {
//驾驶证正面
//驾驶证名称要与身份证名称一致
driverLicenseOcrVo.setName(resp.getName());
driverLicenseOcrVo.setDriverLicenseClazz(resp.getClass_());
driverLicenseOcrVo.setDriverLicenseNo(resp.getCardCode());
driverLicenseOcrVo.setDriverLicenseIssueDate(DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(resp.getDateOfFirstIssue()).toDate());
driverLicenseOcrVo.setDriverLicenseExpire(DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(resp.getEndDate()).toDate());
//上传驾驶证反面图片到腾讯云cos
CosUploadVo cosUploadVo = cosService.upload(file, "driverLicense");
driverLicenseOcrVo.setDriverLicenseFrontUrl(cosUploadVo.getUrl());
driverLicenseOcrVo.setDriverLicenseFrontShowUrl(cosUploadVo.getShowUrl());
} else {
//驾驶证反面
//上传驾驶证反面图片到腾讯云cos
CosUploadVo cosUploadVo = cosService.upload(file, "driverLicense");
driverLicenseOcrVo.setDriverLicenseBackUrl(cosUploadVo.getUrl());
driverLicenseOcrVo.setDriverLicenseBackShowUrl(cosUploadVo.getShowUrl());
}
return driverLicenseOcrVo;
} catch (Exception e) {
e.printStackTrace();
throw new GuiguException(ResultCodeEnum.DATA_ERROR);
}
}
service-client中
OcrFeignClient
/**
* 驾驶证识别
* @param file
* @return
*/
@PostMapping(value = "/ocr/driverLicenseOcr", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Result<DriverLicenseOcrVo> driverLicenseOcr(@RequestPart("file") MultipartFile file);
web-driver中
OcrController
@Operation(summary = "驾驶证识别")
@GuiGuLogin
@PostMapping("/driverLicenseOcr")
public Result<DriverLicenseOcrVo> driverLicenseOcr(@RequestPart("file") MultipartFile file) {
return Result.ok(ocrService.driverLicenseOcr(file));
}
OcrServiceImpl
//驾驶证识别
@Override
public DriverLicenseOcrVo driverLicenseOcr(MultipartFile file) {
Result<DriverLicenseOcrVo> driverLicenseOcrVoResult = ocrFeignClient.driverLicenseOcr(file);
DriverLicenseOcrVo driverLicenseOcrVo = driverLicenseOcrVoResult.getData();
return driverLicenseOcrVo;
}
启动相关服务进行测试接口,点击开始接单会进入认证界面,这段我就不认证了,没有示例图片。
获取司机的认证信息
- 司机进行操作首先进行登录,登陆成功之后,进行认证,跳转到认证页面完成认证。
- 查看认证信息,进入到认证页面时候,回显证件信息
service-driver中
DriverInfoController
@Operation(summary = "获取司机认证信息")
@GetMapping("/getDriverAuthInfo/{driverId}")
public Result<DriverAuthInfoVo> getDriverAuthInfo(@PathVariable Long driverId) {
DriverAuthInfoVo driverAuthInfoVo = driverInfoService.getDriverAuthInfo(driverId);
return Result.ok(driverAuthInfoVo);
}
@Autowired
private CosService cosService;
//获取司机认证信息
@Override
public DriverAuthInfoVo getDriverAuthInfo(Long driverId) {
DriverInfo driverInfo = driverInfoMapper.selectById(driverId);
DriverAuthInfoVo driverAuthInfoVo = new DriverAuthInfoVo();
BeanUtils.copyProperties(driverInfo,driverAuthInfoVo);
driverAuthInfoVo.setIdcardBackShowUrl(cosService.getImageUrl(driverAuthInfoVo.getIdcardBackUrl()));
driverAuthInfoVo.setIdcardFrontShowUrl(cosService.getImageUrl(driverAuthInfoVo.getIdcardFrontUrl()));
driverAuthInfoVo.setIdcardHandShowUrl(cosService.getImageUrl(driverAuthInfoVo.getIdcardHandUrl()));
driverAuthInfoVo.setDriverLicenseFrontShowUrl(cosService.getImageUrl(driverAuthInfoVo.getDriverLicenseFrontUrl()));
driverAuthInfoVo.setDriverLicenseBackShowUrl(cosService.getImageUrl(driverAuthInfoVo.getDriverLicenseBackUrl()));
driverAuthInfoVo.setDriverLicenseHandShowUrl(cosService.getImageUrl(driverAuthInfoVo.getDriverLicenseHandUrl()));
return driverAuthInfoVo;
}
service-client中
DriverInfoFeignClient
/**
* 获取司机认证信息
* @param driverId
* @return
*/
@GetMapping("/driver/info/getDriverAuthInfo/{driverId}")
Result<DriverAuthInfoVo> getDriverAuthInfo(@PathVariable("driverId") Long driverId);
web-driver中
DriverController
@Operation(summary = "获取司机认证信息")
@GuiGuLogin
@GetMapping("/getDriverAuthInfo")
public Result<DriverAuthInfoVo> getDriverAuthInfo() {
//获取登录用户id,当前是司机id
Long driverId = AuthContextHolder.getUserId();
return Result.ok(driverService.getDriverAuthInfo(driverId));
}
DriverServiceImpl
//司机认证信息
@Override
public DriverAuthInfoVo getDriverAuthInfo(Long driverId) {
Result<DriverAuthInfoVo> authInfoVoResult = driverInfoFeignClient.getDriverAuthInfo(driverId);
DriverAuthInfoVo driverAuthInfoVo = authInfoVoResult.getData();
return driverAuthInfoVo;
}
修改司机认证信息
认证状态:
0:未认证 【刚注册完为未认证状态】
1:审核中 【提交了认证信息后变为审核中】
2:认证通过 【后台审核通过】
-1:认证未通过【后台审核不通过】
认证状态对应数据库中 daijia-driver -> driver_info -> auth_status字段
司机开启接单的条件:
1、认证通过
2、建立了腾讯云人员库人员
3、当日验证了人脸识别
接口实现
service-driver中
DriverInfoController
//更新司机认证信息
@Operation(summary = "更新司机认证信息")
@PostMapping("/updateDriverAuthInfo")
public Result<Boolean> updateDriverAuthInfo(@RequestBody UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
Boolean isSuccess = driverInfoService.updateDriverAuthInfo(updateDriverAuthInfoForm);
return Result.ok(isSuccess);
}
DriverInfoServiceImpl
//更新司机认证信息
@Override
public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
//获取司机id
Long driverId = updateDriverAuthInfoForm.getDriverId();
//修改操作
DriverInfo driverInfo = new DriverInfo();
driverInfo.setId(driverId);
BeanUtils.copyProperties(updateDriverAuthInfoForm,driverInfo);
// int i = driverInfoMapper.updateById(driverInfo);
boolean update = this.updateById(driverInfo);
return update;
}
service-client中
DriverInfoFeignClient
/**
* 更新司机认证信息
* @param updateDriverAuthInfoForm
* @return
*/
@PostMapping("/driver/info/updateDriverAuthInfo")
Result<Boolean> UpdateDriverAuthInfo(@RequestBody UpdateDriverAuthInfoForm updateDriverAuthInfoForm);
web-driver中
DriverController
@Operation(summary = "更新司机认证信息")
@GuiGuLogin
@PostMapping("/updateDriverAuthInfo")
public Result<Boolean> updateDriverAuthInfo(@RequestBody UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
updateDriverAuthInfoForm.setDriverId(AuthContextHolder.getUserId());
return Result.ok(driverService.updateDriverAuthInfo(updateDriverAuthInfoForm));
}
DriverServiceImpl
//更新司机认证信息
@Override
public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
Result<Boolean> booleanResult = driverInfoFeignClient.UpdateDriverAuthInfo(updateDriverAuthInfoForm);
Boolean data = booleanResult.getData();
return data;
}
开通人脸识别
新注册的司机进行人脸信息采集,类似于在公司里面进行人脸录入,每天进行人脸打卡
地址:https://cloud.tencent.com/product/facerecognition
修改TencentCloudProperties
@Data
@Component
@ConfigurationProperties(prefix = "tencent.cloud")
public class TencentCloudProperties {
private String secretId;
private String secretKey;
private String region;
private String bucketPrivate;
private String persionGroupId;
}
service-driver中
DriverInfoController
//创建司机人脸模型
@Operation(summary = "创建司机人脸模型")
@PostMapping("/creatDriverFaceModel")
public Result<Boolean> creatDriverFaceModel(@RequestBody DriverFaceModelForm driverFaceModelForm) {
Boolean isSuccess = driverInfoService.creatDriverFaceModel(driverFaceModelForm);
return Result.ok(isSuccess);
}
DriverInfoServiceImpl
@Autowired
private TencentCloudProperties tencentCloudProperties;
//创建司机人脸模型
@Override
public Boolean creatDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
//根据司机id获取司机信息
DriverInfo driverInfo =
driverInfoMapper.selectById(driverFaceModelForm.getDriverId());
try{
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
Credential cred = new Credential(tencentCloudProperties.getSecretId(),
tencentCloudProperties.getSecretKey());
// 实例化一个http选项,可选的,没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("iai.tencentcloudapi.com");
// 实例化一个client选项,可选的,没有特殊需求可以跳过
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
// 实例化要请求产品的client对象,clientProfile是可选的
IaiClient client = new IaiClient(cred, tencentCloudProperties.getRegion(),
clientProfile);
// 实例化一个请求对象,每个接口都会对应一个request对象
CreatePersonRequest req = new CreatePersonRequest();
//设置相关值
req.setGroupId(tencentCloudProperties.getPersionGroupId());
//基本信息
req.setPersonId(String.valueOf(driverInfo.getId()));
req.setGender(Long.parseLong(driverInfo.getGender()));
req.setQualityControl(4L);
req.setUniquePersonControl(4L);
req.setPersonName(driverInfo.getName());
req.setImage(driverFaceModelForm.getImageBase64());
// 返回的resp是一个CreatePersonResponse的实例,与请求对象对应
CreatePersonResponse resp = client.CreatePerson(req);
// 输出json格式的字符串回包
System.out.println(AbstractModel.toJsonString(resp));
String faceId = resp.getFaceId();
if(StringUtils.hasText(faceId)) {
driverInfo.setFaceModelId(faceId);
driverInfoMapper.updateById(driverInfo);
}
} catch (TencentCloudSDKException e) {
e.printStackTrace();
return false;
}
return true;
}
DriverInfoFeignClient
/**
* 创建司机人脸模型
* @param driverFaceModelForm
* @return
*/
@PostMapping("/driver/info/creatDriverFaceModel")
Result<Boolean> creatDriverFaceModel(@RequestBody DriverFaceModelForm driverFaceModelForm);
web-driver中
DriverController
@Operation(summary = "创建司机人脸模型")
@GuiGuLogin
@PostMapping("/creatDriverFaceModel")
public Result<Boolean> creatDriverFaceModel(@RequestBody DriverFaceModelForm driverFaceModelForm) {
driverFaceModelForm.setDriverId(AuthContextHolder.getUserId());
return Result.ok(driverService.creatDriverFaceModel(driverFaceModelForm));
}
DriverServiceImpl
//创建司机人脸模型
@Override
public Boolean creatDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
Result<Boolean> booleanResult = driverInfoFeignClient.creatDriverFaceModel(driverFaceModelForm);
return booleanResult.getData();
}
测试
测试的时候发现出现了404,这个接口老师没有在视频里写,笔记里面也没有。大家可以到项目完整代码里面找,里面写出来了。