<?php
/**
 * 凌凯短信类
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */

namespace Common\Vendor\Sms\Lingkai;
class Lingkai{    
    private $config;
    public function __construct($config) {
        $this->config = !empty($this->config) && !empty($config)? array_merge($this->config, $config):!empty($config)?$config:$this->config;
        $this->member_info=session('member_info');
    }
    public function send($data) {		
        $data=$data[0];$result=array('status'=>0);
        $phone=$data['phone'];
        $content=$data['content'];
        $check=substr($content,-3,3);
        if($check!='】'){$result['msg']=$this->errcode('-7');return $result;}
        $content = rawurlencode(mb_convert_encoding($content, "gb2312", "utf-8"));
        if(empty($phone) || empty($content))return false;
        $url='https://sdk2.028lk.com/sdk2/BatchSend2.aspx?CorpID='.$this->config['username'].'&Pwd='.$this->config['password'].'&Mobile='.$phone.'&Content='.$content.'&Cell=&SendTime=';
        $result['body']=file_get_contents($url);
        $result['status']=$result['body']>0?1:0;
        $result['msg']=$this->errcode($result['body']);
        return $result;
    }
    public function balance() {
        $url='https://sdk2.028lk.com/sdk2/SelSum.aspx?CorpID='.$this->config['username'].'&Pwd='.$this->config['password'];
        $result['body']=file_get_contents($url);
        if($result['body']>-1){
            $result['status']=1;
            $result['msg']="剩余条数:".$result['body']."条";
        }else{
            $result['status']=0;
            $result['msg']=$this->errcode($result['body']);
        }return $result;
    }
    private function errcode($code=''){
        $error=array(
            '-1'=>'账号未注册',
            '-2'=>'其他错误',
            '-3'=>'帐号或密码错误',
            '-5'=>'余额不足，请充值',
            '-7'=>'提交信息末尾未加签名，请添加中文的企业签名【】',
            '-6'=>'定时发送时间不是有效的时间格式',
            '-8'=>'发送内容需在1到300字之间',
            '-9'=>'发送号码为空',
            '-10'=>'定时时间不能小于系统当前时间',
            '-11'=>'屏蔽手机号码',
            '-100'=>'IP黑名单',
            '-101'=>'调用接口速度太快（大于30s）',
            '-102'=>'账号黑名单',
            '-103'=>'IP未导白',
        );
        return $error[$code]?$error[$code]:$code;
    }
}