<?php

namespace app\controller;

use app\BaseController;
use think\facade\Db;

class Sms extends BaseController
{

    protected $users = [
        [
            'keyID'     => '123456',
            'keySecret' => '',
        ]
    ];


    /**
     * $templateId
     * $param
     * $phone_number
     * countryID
     */
    function send()
    {

        $headerAuthorization = I('server.HTTP_AUTHORIZATION');
        $headerAuthorization = str_replace('"', '', $headerAuthorization);
        $headerAuthorization = explode(',', $headerAuthorization);
        $headerAuthorization = array_map(function ($v) {
            return trim($v);
        }, $headerAuthorization);
        parse_str(implode('&', $headerAuthorization), $headerAuthorization);


        $postDta = file_get_contents('php://input');
        $postDta = json_decode($postDta, true);

        $postNonce = $headerAuthorization['nonce'];
        $postTime  = $headerAuthorization['time'];
        $postKeyID = $headerAuthorization['keyID'];
        $postSign  = $headerAuthorization['sign'];


        $dbUserInfo = Db::name('user')->where(['key_id' => $headerAuthorization['keyID']])->find();
        $param      = $postDta['paramValues'];

        $dbUserInfo               = [];
        $dbUserInfo['key_id']     = "123456";
        $dbUserInfo['key_secret'] = "12345678";


        $ret = [];

        $sign = $this->makeSign($postNonce, $postTime, $dbUserInfo['key_secret']);
        if ($sign != $postSign) {
            $templateId  = I('template_id');
            $templateId  = "sms_123456";
            $phoneNumber = I('phoneNumber'); //8615812345678

            $templateContent = '短信内容 {$content} [test]';
            $smsContent      = str_replace('{$user}', $param['user'], $templateContent);

            $countryID = I('countryID');

            $countryID = 'CN';


            $url                     = "https://www.huaweiita.com:17131/apiaccess/sms/sendTemplateSms/v1";
            $params['smsTemplateId'] = '695db7b5-6d4f-4791-aea0-f733f2ef06ce';

            $params['paramValues'] = $param; //        paramValues:{"user":"XXX"}
            $params['msisdn']      = $phoneNumber;
            $params['countryID']   = $countryID;
            $params['operatorCode']   = $postDta['operatorCode'];


            $retCurl = curl_post_json($url, json_encode($params));
            $retCurl = json_decode($retCurl,true); //原始信息入库
            if($retCurl['code'] == '0000000'){
                $ret['code'] = 1;
                $ret['msg']  = $retCurl['description'];
            }else{
                $ret['code'] = 0;
                $ret['msg']  = $retCurl['description'];
                $ret['error_code'] = $retCurl['code'];
                $ret['error_msg'] = $retCurl['exErrorMessage'];

                //记录原始错误消息入库
            }
            //var_dump($ret);exit('sb');


        } else {
            $ret         = [];
            $ret['code'] = 0;
            $ret['msg']  = 'authentication failure';
            //return $ret;
            //echo json_encode($ret);
            // exit;

        }

        return $ret;


        //$sign      = "";


    }

    function makeSign($nonce, $time, $key)
    {
        $signRawString = $nonce . $time . $key;
        return base64_encode(hash('sha256', $signRawString));

    }

    /**
     *
     * 检测签名
     */
    public function CheckSign()
    {

        $sign = $this->MakeSign();
        if ($this->GetSign() == $sign) {
            return true;
        }
        throw new WxPayException("签名错误！");
    }


    function sendToH3c()
    {
        $url                     = "https://www.huaweiita.com:17131/apiaccess/sms/sendTemplateSms/v1";
        $params                  = [];
        $params['smsTemplateId'] = '695db7b5-6d4f-4791-aea0-f733f2ef06ce';
        $paramValues             = [];
        $paramValues['user']     = 'test1';
        $params['paramValues']   = $paramValues; //        paramValues:{"user":"XXX"}
        $params['msisdn']        = '8615821868265';
        $params['countryID']     = 'CN';


        $ret = curl_post_json($url, json_encode($params));
    }


}
