尚硅谷———-乐(智)尚代驾~~--------Day5----司机认证篇~

尚硅谷———-乐(智)尚代驾~~--------Day5----司机认证篇~

前言:

Hello亲爱的uu们,在读过了一个愉快的周末后(摸鱼了一会),我又回来更新啦,感谢uu们的阅读,话不多说~

司机认证

当司机点击开始接单的时候,会先判断该司机有没有通过认证,如果没有认证,会先跳转到认证界面进行认证。 司机认证模块需要集成腾讯云存储COS、腾讯云OCR证件识别及腾讯云人脸模型库创建。

开通腾讯云对象存储COS

进入官网直接搜索对象存储,进入之后点击立即使用,如果第一次使用的话需要认证

认证完成之后开通COS服务

开通之后界面 点击创建存储桶,创建完成之后可以在存储桶列表查看创建的桶。

如果要使用java代码进行操作,需要获取腾讯云账号的id和key,点击右上角的账户 ->访问管理

在API秘钥管理的创建秘钥,记得保存好SecretId和SecretKey(很重要,后续代码编写都用到他)

远程调用编写,web-driver下的CosController

@Autowired

private CosService cosService;

// 文件上传接口

@Operation(summary = "上传")

@GuiGuLogin

@PostMapping("/upload")

public Result 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 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 upload(@RequestPart("file") MultipartFile file, @RequestParam("path") String path);

在service-driver里引入依赖

com.qcloud

cos_api

把腾讯云需要的值放到相关配置文件中,在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;

}

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依赖

com.tencentcloudapi

tencentcloud-sdk-java

${tencentcloud.version}

OcrController

@Autowired

private OcrService ocrService;

@Operation(summary = "身份证识别")

@PostMapping("/idCardOcr")

public Result 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 idCardOcr(@RequestPart("file") MultipartFile file);

在web-driver中 OcrController

@Autowired

private OcrService ocrService;

@Operation(summary = "身份证识别")

@GuiguLogin

@PostMapping("/idCardOcr")

public Result uploadDriverLicenseOcr(@RequestPart("file") MultipartFile file) {

return Result.ok(ocrService.idCardOcr(file));

}

OcrServiceImpl

@Autowired

private OcrFeignClient ocrFeignClient;

//身份证识别

@Override

public IdCardOcrVo idCardOcr(MultipartFile file) {

Result ocrVoResult = ocrFeignClient.idCardOcr(file);

IdCardOcrVo idCardOcrVo = ocrVoResult.getData();

return idCardOcrVo;

}

腾讯云驾驶证识别接口

在service-driver中 OcrController

@Operation(summary = "驾驶证识别")

@PostMapping("/driverLicenseOcr")

public Result 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 driverLicenseOcr(@RequestPart("file") MultipartFile file);

web-driver中 OcrController

@Operation(summary = "驾驶证识别")

@GuiGuLogin

@PostMapping("/driverLicenseOcr")

public Result driverLicenseOcr(@RequestPart("file") MultipartFile file) {

return Result.ok(ocrService.driverLicenseOcr(file));

}

OcrServiceImpl

//驾驶证识别

@Override

public DriverLicenseOcrVo driverLicenseOcr(MultipartFile file) {

Result driverLicenseOcrVoResult = ocrFeignClient.driverLicenseOcr(file);

DriverLicenseOcrVo driverLicenseOcrVo = driverLicenseOcrVoResult.getData();

return driverLicenseOcrVo;

}

启动相关服务进行测试接口,点击开始接单会进入认证界面,这段我就不认证了,没有示例图片。

先到这里哈~接下来的下篇继续,有点多了看着审美疲劳,谢谢你的观赏~~~

清芳推荐

WLK怀旧服堪比BIS装备 寒冰纹章兑换装备盘点
体育投注365下载

WLK怀旧服堪比BIS装备 寒冰纹章兑换装备盘点

📅 07-29 👀 5234
穆勒领衔德国队告别2022世界杯最后一战的荣耀与遗憾
NAMM 2015 展会:Hotone 革命性的蓝牙单块效果器 Xtomp 第一时间上手