<?php

namespace app\apicom\home;

use think\Config;
use think\Exception;
use think\Log;

date_default_timezone_set('PRC'); //设置时区

class ZthySms
{
    public function sendSmsCode($mobile = '')
    {
        try {
            $code = rand(10000, 99999);
            $content = "您的验证码：" . $code . "，您正进行身份验证，打死不告诉别人！";
            //模板信息发送demo
            $url     = "https://api.mix2.zthysms.com/v2/sendSms";

            $tKey     = time();
            $password = md5(md5('8053uAI7') . $tKey);
            $date     = array(
                'username'  => 'sdcx888hy', //用户名
                'password'  => $password, //密码
                'tKey'      => $tKey, //tKey
                'mobile' => $mobile, //手机号码
                'content'   => '【青石证券】' . $content
            );
            $ret = self::httpPost($url, $date);
            $res = json_decode($ret, true);
            if($res['code'] != 200){
                return false;
            }
            $code = $code;
            $arr['code'] = md5($code);
            $arr['time'] = time();
            setRegSmsCache($mobile, $arr);
        } catch (Exception $error) {
            Log::record('短信记录异常：' . $error->getMessage(), 'error');
        }
        return true;
    }

    public static function httpPost($url, $date)
    { // 模拟提交数据函数
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($date)); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
        curl_setopt($curl, CURLOPT_HEADER, false); //开启header
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8'
        )); //类型为json
        $result = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Error POST' . curl_error($curl);
        }
        curl_close($curl); // 关键CURL会话
        return $result; // 返回数据
    }
}
