<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/3/12
 * Time: 下午4:48
 */

require_once(BASE_CORE_PATH . '/framework/libraries/sms.php');

class sms_helper
{
    const repeat_mintime = 60;
    const code_expire = 5 * 60;

    static public function send($mobile,$type)
    {
        static $types = array('register', 'resetpass', 'getbonus','getwxbind','getfcode');
        if (!in_array($type, $types)) {
            return array('code' => errcode::ErrParamter, 'msg' => "错误的类型参数: type={$type}");
        }

        if ($type == 'register') {
            $itype = Sms::register_code;
        }
        elseif ($type == 'resetpass') { //重置密码需要在登录的状态下才可以执行。
            $itype = Sms::resetpass_code;
        }
        elseif ($type == 'getbonus') {
            $itype = Sms::getbonus_code;
        }
        elseif($type == 'getwxbind') {
            $itype = Sms::getbind_code;
        }
        elseif($type == 'getfcode') {
            $itype = Sms::getfcode_code;
        }

        $cur_time = time();
        if(isset($_SESSION['sms_code'][$itype]))
        {
            $last_time = $_SESSION['sms_code'][$itype]['last_time'];
            if($cur_time - $last_time < self::repeat_mintime) {
                return array('code' => errcode::ErrSmsLessRepeateMinTime, 'msg' => errcode::msg(errcode::ErrSmsLessRepeateMinTime));
            }
        }

        //$sms = new Sms();
        $code = makeSmscode();
        Log::record("sms code = {$code}",Log::DEBUG);
        //Logic('queue')->sendSMS(array('mobile'=>$mobile,'type'=>$itype,'datas' => array('code'=>$code,'time'=>'5')));
        QueueClient::push('sendSMS',array('mobile'=>$mobile,'type'=>$itype,'datas' => array('code'=>$code,'time'=>'5')));
        $status = 0;
        if ($status == 0)
        {
            if(isset($_SESSION['sms_code'][$itype])) {
                $count = $_SESSION['sms_code'][$itype]['count'];
            } else {
                $count = 0;
            }
            $_SESSION['sms_code'][$itype]['count'] = $count + 1;
            $_SESSION['sms_code'][$itype]['code'] = $code;
            $_SESSION['sms_code'][$itype]['last_time'] = time();
            $_SESSION['sms_code'][$itype]['mobile'] = $mobile;

            return array('code' => 200);
        } else {
            $msg = self::get_errmsg($status,$itype);
            return array('code' => errcode::ErrSms, 'msg' => $msg);
        }
        return array('code' => 200);
    }

    static public function send_nostore_message($mobile,$type,$datas)
    {
        QueueClient::push('sendSMS', array('mobile' => $mobile,'type' => $type, 'datas' => $datas));
    }

    static public function check_code($itype,$code,$mobile)
    {
        if(isset($_SESSION['sms_code'][$itype]))
        {
            if($_SESSION['sms_code'][$itype]['mobile'] != $mobile) {
                return array('code' => errcode::ErrSmscode, 'msg' => '验证手机号和发送验证码手机号不一致.');
            }

            $tmout = time() - $_SESSION['sms_code'][$itype]['last_time'];
            if($tmout <= self::code_expire)
            {
                if($code == $_SESSION['sms_code'][$itype]['code']) {
                    return true;
                } else {
                    return array('code' => errcode::ErrSmscode, 'msg' => '验证码错误.');
                }
            } else {
                return array('code' => errcode::ErrSmscodeExpire, 'msg' => errcode::msg(errcode::ErrSmscodeExpire));
            }
        } else {
            return array('code' => errcode::ErrSmscode, 'msg' => '验证码错误.');
        }
    }

    static private function  get_errmsg($status,$itype)
    {
        $iCode = intval($status);
        if($iCode == 160038) {
            return '短信验证码发送过频繁';
        }
        else if($iCode == 160040)
        {
            if($itype == Sms::register_code) {
                $stype = '注册验证码';
            } elseif ($itype == Sms::getbonus_code) {
                $stype = '红包验证码';
            } elseif ($itype == Sms::resetpass_code) {
                $stype = '重置密码验证码';
            }

            $msg = sprintf("%s,超过当天发送上限.",$stype);
            return $msg;
        }
        else if($iCode == 160042) {
            return "号码格式不正确.";
        }
        else if($iCode == 160000) {
            return "第三方短信系统错误.";
        }
        else if($iCode == 160034) {
            return "号码在第三方短信系统黑名单中.";
        }
        else {
            return "发送短信错误 error={$status}.";
        }
    }
}