<?php
/**
 * Created by PhpStorm.
 * User: lldream
 * Date: 2018/11/14
 * Time: 17:52
 */



const lingkai_code=[
    "-1"=>"账号未注册",
    "-7"=>"提交信息末尾未加签名",
];

class Sms{



    /**

    * 凌凯验证码短信方案

    * @date: 2018年11月15日 下午4:02:40

    * @author: 龚华尧

    * @param: variable

    * @return:

    */
    public function lingkai($tel,$msg,$type="验证码"){
        $uid=$GLOBALS['config_account']['LK_ACCOUNT'];
        $pwd=$GLOBALS['config_account']['LK_PASSWORD'];
        $sign=$GLOBALS['config_account']['LK_SIGN'];
        $re_data["status"]=false;
        $re_data["msg"]="发送失败";

        $smslogModel = new smslogModel();

        header("Content-type: text/html; charset=gbk2312");
        date_default_timezone_set('PRC');

        $message="";
        if($type=="验证码"){
            $code=rand(1000,9999);
            $message=$code;               //验证码
            $_SESSION['yzm'] = $message;            //存session
            $message = "亲爱的用户，您的手机验证码{$code} 。温馨提示：如非本人使用，请停止操作。";     //编辑短信内容
            //             $msg="【{$sign}】".$message;
            $msg=$message;
            $message=$code;//添加签名
        }else{
            $message="【{$sign}】".$msg;         //添加签名
        }


        $msg = rawurlencode(mb_convert_encoding($msg, "gb2312", "utf-8"));
        $gateway = "http://mb345.com:999/ws/BatchSend2.aspx?CorpID={$uid}&Pwd={$pwd}&Mobile={$tel}&Content={$msg}&SendTime=&cell=";

        $result = file_get_contents($gateway);

        //echo $gateway;die();

        if ($result>0){
            $re_data["status"]=true;
            $re_data["msg"]="信息发送成功";
            if($type=="验证码"){
                $smslogModel->addlog($tel, $message, $type);
            }else{
                $smslogModel->addlog($tel, $re_data["msg"]." 内容：".$message, $type);
            }
        }else{
            $re_data["msg"]="发送失败:{lingkai_code[$result]}";
            $smslogModel->addlog($tel, $re_data["msg"]." 内容：".$message, $type);
        }

        return $re_data;

    }

    /**

    * 阿里短信方案

    * @date: 2018年11月15日 下午4:00:36

    * @author: 龚华尧

    * @param: variable

    * @return:

    */

    public function ali($tel,$msg,$type="验证码"){
        $uid=$GLOBALS['config_account']['LK_ACCOUNT'];
        $pwd=$GLOBALS['config_account']['LK_PASSWORD'];
        $sign=$GLOBALS['config_account']['LK_SIGN'];
        $re_data["status"]=false;
        $re_data["msg"]="发送失败";

        $smslogModel = new smslogModel();

        header("Content-type: text/html; charset=gbk2312");
        date_default_timezone_set('PRC');

        $message="";
        if($type=="验证码"){
            $message=rand(1000,9999);               //验证码
            $_SESSION['yzm'] = $message;            //存session
            $message = "您的验证码为".$message;     //编辑短信内容
            $msg="【{$sign}】".$message;         //添加签名
        }else{
            $msg="【{$sign}】".$msg;         //添加签名
        }


        $msg = rawurlencode(mb_convert_encoding($msg, "gb2312", "utf-8"));
        $gateway = "http://mb345.com:999/ws/BatchSend2.aspx?CorpID={$uid}&Pwd={$pwd}&Mobile={$tel}&Content={$msg}&SendTime=&cell=";

//        $result = file_get_contents($gateway);

        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch, CURLOPT_URL, $gateway);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $result = curl_exec($ch);
        curl_close($ch);


        if ($result>0){
            $re_data["status"]=true;
            $re_data["msg"]="信息发送成功";
            $smslogModel->addlog($tel, $re_data["msg"]." 内容：".$message, $type);
        }else{
            $re_data["msg"]="发送失败:{lingkai_code[$result]}";
            $smslogModel->addlog($tel, $re_data["msg"]." 内容：".$message, $type);
        }

        return $re_data;

    }

    /**

    * 验证验证码

    * @date: 2019年2月21日 下午4:39:20

    * @author: 龚华尧

    * @param: variable

    * @return:

    */
    public function checkyzm($shoujihao,$yzm){
        $data['status']=false;
        $data['msg']="失败";
        $smslogModel = new smslogModel();

        $_data=  $smslogModel->where("type='验证码' and user='{$shoujihao}' and log='{$yzm}' and dtime between date_add(now(), interval - 5 minute) and now() ")->one();
        if($_data){
            $data['status']=true;
            $data['msg']="验证码正确";
            $_data['log'] = $_data['log']."已使用";
            $smslogModel->update($_data);

        }else{
            $data['msg']="验证码错误或失效";
        }
        return $data;

    }

    /**
     * Name:查询当前账户的短信余额
     * Notes:
     * User: gonghuayao
     * Date: 2020-01-06
     * Time: 17:32
     * @param string $type:短信供应商类型
     */
    public function getDuanxinYuE($type="lingkai"){
        $uid=$GLOBALS['config_account']['LK_ACCOUNT'];
        $pwd=$GLOBALS['config_account']['LK_PASSWORD'];
        if($type=="lingkai"){
            $duanxing_api_url = "https://mb345.com/ws/SelSum.aspx?CorpID={$uid}&Pwd={$pwd}";
            $re_string =file_get_contents($duanxing_api_url);

        }else{
            $re_string = "暂无对于短信供应商的查询方法";
        }

        return $re_string;

    }


}
