<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/5/22
 * Time: 上午9:39
 */

namespace common\libs\api\sms;

use common\helpers\Cache;
use common\helpers\ocr\baidu\AipOcr;
use common\helpers\Tool;
use common\libs\api\sms\meilian\SmsSend;
use yii\base\BaseObject;
use yii\base\StaticInstanceTrait;

class SmsApi extends BaseObject {
    use StaticInstanceTrait;

    const TYPE_MEILIAN = 'meilian';
    const MEILIAN_CONFIGS = [
        'username' => 'string',
        'password' => 'string',
        'apikey' => 'string',
    ];

    private $_type;
    private $_typeClass;
    private $_smsApiObj;
    private $_errorCode;
    private $_error = [];

    /**
     * 初始化
     * @throws \yii\base\InvalidConfigException
     */
    public function init() {
        parent::init();
        // 获取当前使用的ocr识别接口
        $smsApiConfig = Cache::getSmsApiConfig();
        $this->_type = $smsApiConfig['API_NAME'];
        $this->_typeClass = null;
        $apiConfig = [];
        if ($smsApiConfig['API_NAME'] == self::TYPE_MEILIAN) {
            $this->_typeClass = SmsSend::class;
            foreach ($smsApiConfig['CONFIG'] as $value) {
                $apiConfig[] = $value;
            }
        }
        $this->_errorCode = require \Yii::getAlias('@common/libs/api/sms/meilian/') . 'errorCode.php';
        // 创建短信接口对象
        $this->_smsApiObj = \Yii::createObject($this->_typeClass, $apiConfig);
    }

    /**
     * 发送短信
     * @param array $params
     * [
     *      'mobiles' => '13903351111,13903352222',
     *      'content' => '测试短息，您好',
     * ]
     * @return bool
     */
    public function send(array $params = []) {
        if(!$this->_smsApiObj->send($params)){
            $this->_error[] = $this->_smsApiObj->getError();
            return false;
        }
        return true;
    }

    /**
     * 异步协程发送
     * @param array $params
     */
    public function goSend(array $params = []) {
        go(function() use($params){
            $this->send($params);
        });
    }

    /**
     * 获取错误
     * @return mixed
     */
    public function getError(){
        return $this->_error;
    }

    /**
     * 清空错误
     */
    public function clearError(){
        $this->_error = [];
    }

    /**
     * 获取api设置的字段
     * @param $apiName
     * @return array
     */
    public static function apiConfigs($apiName) {
        $allApiModelData = \common\models\SmsApi::getAllData();
        $result = [];
        if ($apiName == self::TYPE_MEILIAN) {
            $result = [
                'username' => ['TITLE' => 'username', 'CONFIG_NAME' => 'username', 'INPUT_TYPE' => 1, 'OPTIONS' => null, 'VALUE' => $allApiModelData[self::TYPE_MEILIAN]['CONFIG']['username'] ?? null],
                'password' => ['TITLE' => 'password', 'CONFIG_NAME' => 'password', 'INPUT_TYPE' => 1, 'OPTIONS' => null, 'VALUE' => $allApiModelData[self::TYPE_MEILIAN]['CONFIG']['password'] ?? null],
                'apikey' => ['TITLE' => 'apikey', 'CONFIG_NAME' => 'apikey', 'INPUT_TYPE' => 1, 'OPTIONS' => null, 'VALUE' => $allApiModelData[self::TYPE_MEILIAN]['CONFIG']['apikey'] ?? null],
            ];
        }
        return $result;
    }
}