交友项目【自动装配模块】封装阿里云发送短信&Oss对象存储&百度云人脸识别

目录

自动装配模块:tanhua-autoconfig

1:SpringBoot封装短信

1.1:配置类

1.2:发送短信模板对象

1.3:自动装配类

1.4:自动装配配置

2:SpringBoot封装OSS对象

2.1:配置类

2.2:发送短信模板对象

2.3:自动装配类

2.4:自动装配配置

3:SpringBoot封装百度云人脸识别

3.1:配置类

3.2:发送短信模板对象

3.3:自动装配类

3.4:自动装配配置


自动装配模块:tanhua-autoconfig

properties用来进行读取yml文件的参数进行封装参数,

template模板用来封装短信,对象存储,人脸识别模板或者说是方法用来进行调用

 配置类:将其封装的模板注入Bean

@EnableConfigurationProperties(value = {SmsProperties.class, OssProperties.class, FaceProperties.class})
public class TanhuaAutoConfiguration {
    @Bean
    private SmsTemplate smsTemplate(SmsProperties smsProperties){
        return new SmsTemplate(smsProperties);
    }
    @Bean
    private OssTemplate ossTemplate(OssProperties ossProperties){
        return new OssTemplate(ossProperties);
    }
    @Bean
    private FaceTemplate faceTemplate(FaceProperties faceProperties){
        return new FaceTemplate(faceProperties);
    }
}

 

在resources目录下创建spring.factories配置文件,加载配置类

1:SpringBoot封装短信

1.1:配置类

tanhua-autoconfig模块创建配置信息类SmsProperties

@Data
@ConfigurationProperties(prefix = "tanhua.sms")
public class SmsProperties {
    private String signName;
    private String templateCode;
    private String accessKey;
    private String secret;
}

1.2:发送短信模板对象

tanhua-autoconfig模块创建模板对象发送信息

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/29 10:59
 */
@Data
public class SmsTemplate {
    private SmsProperties properties;

    public SmsTemplate(SmsProperties properties) {
        this.properties = properties;
    }

    public void sendSms(String mobile, String username ,String code) {
        try {
            //配置阿里云
            Config config = new Config()
                    // 您的AccessKey ID
                    .setAccessKeyId(properties.getAccessKey())
                    // 您的AccessKey Secret
                    .setAccessKeySecret(properties.getSecret());
            // 访问的域名
            config.endpoint = "dysmsapi.aliyuncs.com";

            com.aliyun.dysmsapi20170525.Client client =  new com.aliyun.dysmsapi20170525.Client(config);

            String address = "默认地址";
            String phone = "13612345678";

            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(mobile)
                    .setSignName(properties.getSignName())
                    .setTemplateCode(properties.getTemplateCode())
                    .setTemplateParam("{\"name\":\""+username+"\",\"code\":\""+code+"\",\"address\":\""+address+"\",\"phone\":\""+phone+"\"}");
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse response = client.sendSms(sendSmsRequest);

            SendSmsResponseBody body = response.getBody();

            System.out.println(body.getMessage());

        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

1.3:自动装配类

tanhua-autoconfig模块创建自动装配的配置类

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/29 11:03
 */
@EnableConfigurationProperties(value = {SmsProperties.class, OssProperties.class, FaceProperties.class})
public class TanhuaAutoConfiguration {
    @Bean
    private SmsTemplate smsTemplate(SmsProperties smsProperties){
        return new SmsTemplate(smsProperties);
    }
    @Bean
    private OssTemplate ossTemplate(OssProperties ossProperties){
        return new OssTemplate(ossProperties);
    }
    @Bean
    private FaceTemplate faceTemplate(FaceProperties faceProperties){
        return new FaceTemplate(faceProperties);
    }
}

1.4:自动装配配置

  • 根据自动装配原则,在tanhua-autoconfig模块创建/META-INF/spring.factories文件

 

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.czxy.tanhua.autoconfig.TanhuaAutoConfiguration

2:SpringBoot封装OSS对象

2.1:配置类

tanhua-autoconfig模块创建配置信息类OssProperties

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/31 9:44
 */
@Data
@ConfigurationProperties(prefix = "tanhua.oss")
public class OssProperties {
    private String pathProtocol;    //路径协议

    private String endpoint;        //地域

    private String keyId;           //账号

    private String keySecret;       //密码

    private String dirName;         //上传目录

    private String bucketName;      //Bucket 名称
}

2.2:发送OSS对象模板

tanhua-autoconfig模块创建模板对象保存信息

OssTemplate对象模板

package com.czxy.tanhua.autoconfig.template;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.czxy.tanhua.autoconfig.properties.OssProperties;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/31 9:49
 */
@Component
public class OssTemplate {
    private OssProperties ossProperties;

    public OssTemplate(OssProperties properties) {
        this.ossProperties = properties;
    }

    public String upload(File file) {
        if(file == null) {
            throw new RuntimeException("上传文件为空");
        }
        try {
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getKeyId(), ossProperties.getKeySecret());
            String path = ossProperties.getDirName() + "/" + System.currentTimeMillis() + ".png";
            // 数据流
            FileInputStream fileInputStream = new FileInputStream(file);
            // 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。
            ossClient.putObject(ossProperties.getBucketName(), path, fileInputStream);
            // 关闭OSSClient。
            ossClient.shutdown();
            String url = ossProperties.getPathProtocol() + "://"+ossProperties.getBucketName()+"."+ossProperties.getEndpoint()+"/" + path;
            return url;
        } catch (IOException e) {
            throw new RuntimeException("文件上传有误");
        }

    }
}

2.3:自动装配类

 tanhua-autoconfig模块创建自动装配的配置类

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/29 11:03
 */
@EnableConfigurationProperties(value = {SmsProperties.class, OssProperties.class, FaceProperties.class})
public class TanhuaAutoConfiguration {
    @Bean
    private SmsTemplate smsTemplate(SmsProperties smsProperties){
        return new SmsTemplate(smsProperties);
    }
    @Bean
    private OssTemplate ossTemplate(OssProperties ossProperties){
        return new OssTemplate(ossProperties);
    }
    @Bean
    private FaceTemplate faceTemplate(FaceProperties faceProperties){
        return new FaceTemplate(faceProperties);
    }
}

2.4:自动装配配置

  •  根据自动装配原则,在tanhua-autoconfig模块创建/META-INF/spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.czxy.tanhua.autoconfig.TanhuaAutoConfiguration

3:SpringBoot封装百度云人脸识别

3.1:配置类

tanhua-autoconfig模块创建配置信息类FaceProperties

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/31 10:24
 */
@Data
@ConfigurationProperties(prefix = "tanhua.aip")
public class FaceProperties {
    private String appId;
    private String apiKey;
    private String secretKey;
    @Bean
    public AipFace aipFace(){
        AipFace client = new AipFace(appId, apiKey, secretKey);
        // 可选:设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        return client;
    }
}

3.2:发送百度人脸图像识别模板

tanhua-autoconfig模块创建配置人脸识别类:检测识别结果

package com.czxy.tanhua.autoconfig.template;

import com.baidu.aip.face.AipFace;
import com.czxy.tanhua.autoconfig.properties.FaceProperties;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/31 10:27
 */
@Component
public class FaceTemplate {
    @Resource
    private AipFace aipFace;
    private FaceProperties faceProperties;
    public FaceTemplate(FaceProperties faceProperties){
        this.faceProperties = faceProperties;
    }

    public boolean FaceUtils(String imageurl){
        
        String imageType = "URL";


            // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
//        options.put("liveness_control", "LOW");

        // 人脸检测
        JSONObject res = aipFace.detect(imageurl, imageType, options);
        Integer error_code = (Integer) res.get("error_code");
        return error_code == 0;
    }
}

3.3:自动装配类

 tanhua-autoconfig模块创建自动装配的配置类

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/29 11:03
 */
@EnableConfigurationProperties(value = {SmsProperties.class, OssProperties.class, FaceProperties.class})
public class TanhuaAutoConfiguration {
    @Bean
    private SmsTemplate smsTemplate(SmsProperties smsProperties){
        return new SmsTemplate(smsProperties);
    }
    @Bean
    private OssTemplate ossTemplate(OssProperties ossProperties){
        return new OssTemplate(ossProperties);
    }
    @Bean
    private FaceTemplate faceTemplate(FaceProperties faceProperties){
        return new FaceTemplate(faceProperties);
    }
}

3.4:自动装配配置

  •   根据自动装配原则,在tanhua-autoconfig模块创建/META-INF/spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.czxy.tanhua.autoconfig.TanhuaAutoConfiguration

猜你喜欢

转载自blog.csdn.net/m0_64550837/article/details/129882885