<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Sdk\Model;

use Think\Model;

class SmsLogModel extends Model
{

    public function __construct($name = '', $tablePrefix = '', $connection = '')
    {
        /* 设置默认的表前缀 */
        $this->tablePrefix = 'tab_';
        /* 执行构造方法 */
        parent::__construct($name, $tablePrefix, $connection);
    }

    public static $msg = [
        'register' => '执行注册操作',
        'login'    => '执行登录操作',
        'forget'   => '执行修改密码操作',
    ];

    public static $sms_sign = [
    	'001' => '【聚游】',
    ];

    public static $msgContent = [
        // '30974' => '手机验证码:{1}。您正在{2}。该验证码在{3}分钟内有效。如非本人操作,请忽略此短信。',
        '30974' => '{1}您的验证码为{2},10分钟内有效,为保证账户安全,请勿向他人泄露验证信息。',
    ];

    public function sendCode($strPhone, $type = '')
    {
    	$params[0]  = self::$sms_sign['001'];
        $params[1]  = rand(100000, 999999);
        $str        = self::getContent(self::$msgContent['30974'], $params);
        $msg        = $this->sms($str, $strPhone, $params[1]);
        return $msg;
    }

    /**
     * 获取短信模板
     * @param  [type] $temp   [description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public static function getContent($temp, $params)
    {
        foreach ($params as $key => $value) {
            $temp = str_replace('{' . ($key + 1) . '}', $value, $temp);
        }
        return $temp;
    }

    public function create($arData)
    {
        $arData['ip']          = get_client_ip();
        $arData['create_time'] = time();
        $this->add($arData);
    }

    public function sms($content, $phone, $code)
    {
        $data = [
            'apName'       => '',
            'apPassword'   => '',
            'calledNumber' => $phone,
            'content'      => $content,
        ];

        $resp = $this->curlPost("http://139.224.36.226:382/wgws/BatchSubmit", $data);
        $resp = self::XmlToArray($resp);
       	$resp = $resp['submitResp'];
        $arr = [
            'code'   => $code,
            'phone'   => $phone,
            'content' => $content,
            'status'  => $resp['error'],
            'msg'     => $resp['message'],
        ];
        $this->create($arr);
        $resp['code'] = $code;
        return $resp;
    }

    public function curlPost($url, $postFields)
    {
        $postFields = http_build_query($postFields);
        $ch         = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }


    /**
     * xml转数组
     * @param type $xmlstring
     * @return type array
     */
    public static function XmlToArray($xmlstring)
    {
        $tmp       = explode("\n", $xmlstring);
        $xmlstring = "";
        foreach ($tmp as $val) {
            $xmlstring .= trim($val);
        }
        $xml_parser = xml_parser_create();
        if (!xml_parse($xml_parser, $xmlstring, true)) {
            xml_parser_free($xml_parser);
            return false;
        } else {
            return json_decode(json_encode(simplexml_load_string($xmlstring, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        }
    }

}