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

namespace Common\Vendor\Sms\Cocsms;
class Cocsms{    
    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];
        $phone=$data['phone'];
        $content=$data['content'];
        if(empty($phone) || empty($content))return false;
        $url='http://api.smsbao.com/sms?u='.$this->config['username'].'&p='.md5($this->config['password']).'&m='.$phone.'&c='.urlencode($content);
        $result['body']=curlget($url);
        $result['status']=$result['body']==0?1:0;
        $result['msg']=$this->errcode($result['body']);
        return $result;
    }
    public function balance() {
        $url='http://www.smsbao.com/query?u='.$this->config['username'].'&p='.md5($this->config['password']);
        $result['body']=curlget($url);
        $result['body']=str_replace("\n",PHP_EOL,$result['body']);
        $arr=explode(PHP_EOL,$result['body']);
        if($arr[0]==0){
            $result['status']=1;
            $count=explode(',',$arr[1]);
            $result['msg']="发送条数:".$count[0]."条,剩余条数:".$count[1]."条";
        }else{
            $result['status']=0;
            $result['msg']=$this->errcode($arr[0]);
        }return $result;
    }
    private function errcode($code=''){
        $error=array(
        '0' =>'短信发送成功',
        '30'=>'密码错误',
        '40'=>'账号不存在',
        '41'=>'余额不足',
        '42'=>'帐号过期',
        '43'=>'IP地址限制',
        '50'=>'内容含有敏感词',
        '51'=>'手机号码不正确',
        );
        return $error[$code];
    }
}