package com.bonus.zlpt.system.config;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Description: 腾讯云短信配置
 *
 * @Author 阮世耀
 * @Create 2023/12/6 14:56
 * @Version 1.0
 */
@ToString
@Data
@Component
@Slf4j
public class SmsComponent {

    @Value("${sms.sdkAppId}")   // 注入参数值
    private String sdkAppId;

    @Value("${sms.secretId}")
    private String secretId;

    @Value("${sms.secretKey}")
    private String secretKey;

    @Value("${sms.signName}")
    private String signName;

    @Value("${sms.templateCodeId}")
    private String templateCodeId;

    @Value("${sms.timeout}")
    private Integer timeout;

    /**
     * 获取SmsClient客户端
     * @return
     */
    public SmsClient getClient() {

        // 实例化认证对象，入参需要传入腾讯云账户secretId，secretKey
        Credential cred = new Credential(secretId, secretKey);
        // 实例化http选项
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");
        // 实例化client选项
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        return new SmsClient(cred, "ap-beijing", clientProfile);
    }

    /**
     * 获取req请求 2个参数，可根据具体情况改变
     *
     * @param phone 手机号
     * @param code  参数1，这里是验证码，param2为参数2这里为分钟
     * @param templateId  短信模板id
     * @return 请求参数对象
     */
    public SendSmsRequest getReqTwo(String phone, String code, String param2, String templateId) {
        SendSmsRequest req = new SendSmsRequest();
        String[] phoneNumberSet = {"+86" + phone};
        req.setSmsSdkAppId(sdkAppId);     // 设置参数
        req.setPhoneNumberSet(phoneNumberSet);
        req.setSignName(signName);
        req.setTemplateId(templateId);
        //模板内容的参数有几个就设置几个，我这里是两个
        String[] templateParamSetOne = {code};
        String[] templateParamSetTwo = {code,param2};
        req.setTemplateParamSet(templateParamSetOne);
        return req;     // 返回请求参数内容
    }


    /**
     * 发送验证码
     *
     * @param phone 手机号码
     * @param code  验证码
     * @param param2  分钟参数（可为空）
     */
    public void sendCode(String phone, String code, String param2) {
        // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
        SendSmsResponse resp;
        try {
            resp = getClient().SendSms(getReqTwo(phone, code,param2 ,templateCodeId));  // 模板id是自己设置好的
            log.info(SendSmsResponse.toJsonString(resp));   // 把返回信息输入到日志中
        } catch (TencentCloudSDKException e) {
            log.error("腾讯云短信发送失败");
        }
    }



}
