<?php


namespace app\service;

use app\service\sms\SignatureHelper;
use Exception;

class Sms
{
    private static $accessKeyId = "bGRha2YwY2E5MzJlNjJiNDE5NmJmNWE4";
    private static $accessKeySecret = "bGRza2IxOTIyNjQ2MDM1MWM5NmZkMmJm";

    public const TEMPLATE_BEGIN     = '788';//活动开始通知
    public const TEMPLATE_SUCCESS   = '789';//竞拍成功通知
    public const TEMPLATE_SHIPPED   = '790';//发货通知
    public const TEMPLATE_NEW_ORDER = '791';//新订单通知

    /**
     * 发送短信
     */
    public static function send($tel, $code): bool
    {

        $params = array();

        // *** 需用户填写部分 ***
        // fixme 必填:是否启用https
        $security = false;

        // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
        $accessKeyId     = self::$accessKeyId;
        $accessKeySecret = self::$accessKeySecret;

        // fixme 必填: 短信接收号码
        $params["PhoneNumbers"] = $tel;

        // fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
        $params["SignName"] = "大头拍卖";

        // fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
        $params["TemplateCode"] = "130";//"code";

        // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
        $params['TemplateParam'] = array(
            "code" => $code
            //        "product" => "大头拍卖"
        );

        // fixme 可选: 设置发送短信流水号
        //    $params['OutId'] = "12345";

        // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
        //    $params['SmsUpExtendCode'] = "1234567";


        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }

        // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
        $helper = new SignatureHelper();

        // 此处可能会抛出异常,注意catch
        $res = $helper->request(
            $accessKeyId,
            $accessKeySecret,
            "apisms.landui.com",
            array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action"   => "SendSms",
                "Version"  => "2017-05-25",
            )),
            $security
        );

        if (isset($res->Code) && $res->Code == 'OK') {
            return true;
        }
        return false;
    }

    /**
     * 发送短信
     */
    public static function sendContent(string $tel, array $content, string $templateCode = '130'): bool
    {
        $params = array();

        // *** 需用户填写部分 ***
        // fixme 必填:是否启用https
        $security = false;

        // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
        $accessKeyId     = self::$accessKeyId;
        $accessKeySecret = self::$accessKeySecret;

        // fixme 必填: 短信接收号码
        $params["PhoneNumbers"] = $tel;

        // fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
        $params["SignName"] = "大头拍卖";

        // fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
        $params["TemplateCode"] = $templateCode;//"code";

        $params['TemplateParam'] = self::handleContent($templateCode, $content);

        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }

        // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
        $helper = new SignatureHelper();

        // 此处可能会抛出异常,注意catch
        $res = $helper->request(
            $accessKeyId,
            $accessKeySecret,
            "apisms.landui.com",
            array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action"   => "SendSms",
                "Version"  => "2017-05-25",
            )),
            $security
        );

        if (isset($res->Code) && $res->Code == 'OK') {
            return true;
        }
        return false;
    }

    /**
     * 处理内容
     *
     * @param  string  $templateCode
     * @param  array  $content
     * @return array|string[]
     * @throws Exception
     */
    private static function handleContent(string $templateCode, array $content): array
    {
        switch ($templateCode) {
            //788 活动开始通知
            case self::TEMPLATE_BEGIN:
                if (!isset($content['name']) || !isset($content['time'])) {
                    throw new Exception('参数错误');
                }
                $res = [
                    "name" => $content['name'] ?? '',
                    "time" => $content['time'] ?? ''
                ];
                break;
            //789 竞拍成功
            case self::TEMPLATE_SUCCESS:
                if (!isset($content['name'])) {
                    throw new Exception('参数错误');
                }
                $res = [
                    "name" => $content['name'] ?? '',
                ];
                break;
            //790 发货通知
            //791 新订单通知管理员
            case self::TEMPLATE_SHIPPED:
            case self::TEMPLATE_NEW_ORDER:
                if (!isset($content['order'])) {
                    throw new Exception('参数错误');
                }
                $res = [
                    "order" => $content['order'] ?? '',
                ];
                break;
            default:
                //默认130 短信验证码
                if (!isset($content['code'])) {
                    throw new Exception('参数错误');
                }
                $res = [
                    "code" => $content['code'] ?? ''
                ];
        }
        return $res;
    }
}