<?php


namespace common\util;


use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
use Aliyun\Core\Config;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Core\Profile\DefaultProfile;
use common\models\LwtPhoneMessageList;

class PhoneMessage
{
    public static $phoneList = [15106211853];

    public static function saveMsg($company_name, $code)
    {
        $LwtPhoneMessage = new LwtPhoneMessageList();
        $LwtPhoneMessage->company_name = $company_name;
        $LwtPhoneMessage->code = $code;
        $LwtPhoneMessage->is_send = 0;
        $LwtPhoneMessage->create_time = time();
        $LwtPhoneMessage->update_time = time();
        $LwtPhoneMessage->save();
    }

    public static function sendNoticeMsg($to, $name, $status)
    {
        $templateParam = [
            "name" => $name,
            "status" => $status,
        ];
        $template_code = "SMS_273680618";
        return self::sendAliyunMsg($to, $templateParam, $template_code);
    }

    public static function sendVerifyCode($to, $code)
    {
        $templateParam = [
            "code" => $code
        ];
        $template_code = "SMS_271540041";
        return self::sendAliyunMsg($to, $templateParam, $template_code);
    }

    /**
     * 发送阿里云短信
     * @param $to
     * @param $templateParam
     * @param $template_code
     * @return mixed
     */
    protected static function sendAliyunMsg($to, $templateParam, $template_code)
    {
        require_once __DIR__ . '/../thirdparty/aliyun/vendor/autoload.php';
        $accessKeyId = 'LTAI5tDemxqCH7dpgTwWBPGt'; //创建的accessKey
        $accessKeySecret = '0IDsiApCaU4GU1Myp2eXBfTAFT4krO'; //创建的accessKeySecret
        $sign_name = '郅安科技';
        Config::load();//加载区域结点配置
        //短信API产品名（短信产品名固定，无需修改）
        $product = "Dysmsapi";
        //短信API产品域名（接口地址固定，无需修改）
        $domain = "dysmsapi.aliyuncs.com";
        //暂时不支持多Region（目前仅支持cn-hangzhou请勿修改）
        $region = "cn-hangzhou";
        // 初始化用户Profile实例
        $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
        // 增加服务结点
        DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", $product, $domain);
        // 初始化AcsClient用于发起请求
        $acsClient = new DefaultAcsClient($profile);
        // 初始化SendSmsRequest实例用于设置发送短信的参数
        $request = new SendSmsRequest();
        // 必填，设置雉短信接收号码
        $request->setPhoneNumbers($to);
        // 必填，设置签名名称
        $request->setSignName($sign_name);
        // 必填，设置模板CODE
        $request->setTemplateCode($template_code);
        // 可选，设置模板参数
        if ($templateParam) {
            $request->setTemplateParam(json_encode($templateParam)); //由于我的模板里需要传递我的短信验证码
        }
        //发起访问请求
        $acsResponse = $acsClient->getAcsResponse($request);
        //返回请求结果
        $result = json_decode(json_encode($acsResponse), true);
        // 具体返回值参考文档：https://help.aliyun.com/document_detail/55451.html?spm=a2c4g.11186623.6.563.YSe8FK
        return $result;
    }
}