package com.cool.store.service.impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.cool.store.exception.ApiException;
import com.cool.store.service.SmsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @Author: young.yu
 * @Date: 2023-06-21  10:55
 * @Description:
 */
@Service
public class SmsServiceImpl implements SmsService {

    @Value("${hs.sms.accessKeyId:null}")
    private String accessKeyId ;

    @Value("${hs.sms.accessKeySecret:null}")
    private String accessKeySecret;

    /**
     * 发送短信
     * @param params Json格式的参数
     * @param mobile
     * @return
     */
    @Override
    @Async
    public SendSmsResponse sendSms(String params,String templateCode, String mobile) throws ApiException {
        try {
            Config config = new Config()
                    // 您的AccessKey ID
                    .setAccessKeyId(accessKeyId)
                    // 您的AccessKey Secret
                    .setAccessKeySecret(accessKeySecret);
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(mobile)
                    .setSignName("沪上阿姨")
                    .setTemplateCode(templateCode)
                    .setTemplateParam(params);
            Client client = new Client(config);
            return client.sendSms(sendSmsRequest);
        }catch (ApiException e){
            throw new ApiException(e.getMessage());
        } catch (Exception exception) {
            throw new ApiException(exception.getMessage());
        }
    }
}
