package com.YuyeTech.HeartRate.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.YuyeTech.HeartRate.config.AliyunConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author:slambb
 * @date:2020/1/8
 */
@Service
public class AliyunSMSUtil {

    @Autowired
    private AliyunConfig aliyunConfig;

    public String sendSms(String phoneNumber,String key){
        DefaultProfile profile = DefaultProfile.getProfile(
                aliyunConfig.getRegionId(),
                aliyunConfig.getAccessKeyId(),
                aliyunConfig.getAccessKeySecret());

        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", aliyunConfig.getRegionId());
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", aliyunConfig.getSignName());
        request.putQueryParameter("TemplateCode", aliyunConfig.getTemplateCode());
        request.putQueryParameter("TemplateParam", "{code:"+key+"}");

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return null;
    }
}
