| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 | 
							- <?php
 
- namespace Sms\Lib;
 
- use Dever;
 
- class Base
 
- {
 
-     protected $config;
 
-     private function config($skin)
 
-     {
 
-         if (!$this->config) {
 
-             $this->config['skin'] = Dever::db('sms/skin')->one(array('key' => $skin));
 
-             if ($this->config['skin']) {
 
-                 $this->config += Dever::db('sms/config')->one($this->config['skin']['config_id']);
 
-             } else {
 
-                 Dever::alert('错误的短信模板');
 
-             }
 
-             if (!$this->config) {
 
-                 Dever::alert('错误的短信配置');
 
-             }
 
-         }
 
-         
 
-         return $this;
 
-     }
 
-     private function mobile($mobile)
 
-     {
 
-         # 验证手机号是否有效
 
-         $rule = Dever::rule('mobile');
 
-         $state = preg_match($rule, $mobile);
 
-         if (!$state) {
 
-             Dever::alert('手机号无效');
 
-         }
 
-     }
 
-     public function send($skin = false, $mobile = false, $param = array())
 
-     {
 
-         $skin = Dever::input('skin', $skin);
 
-         $mobile = Dever::input('mobile', $mobile);
 
-         $param = Dever::input('param', $param);
 
-         if (!is_array($param)) {
 
-             $param = Dever::json_decode($param);
 
-         }
 
-         $this->config($skin)->mobile($mobile);
 
-         if (isset($this->config['skin']['send']) && $this->config['skin']['send'] == 2) {
 
-             return false;
 
-         }
 
-         $content = $this->config['body'];
 
-         $skin = $this->config['skin']['content'];
 
-         $param['skin'] = $skin;
 
-         $param['mobile'] = $mobile;
 
-         $param['sign'] = $this->config['sign'];
 
-         if (strstr($skin, '{')) {
 
-             $param['skin'] = $this->replace($skin, $param);
 
-         }
 
-         if ($content) {
 
-             $content = $this->replace($content, $param);
 
-             parse_str($content, $param);
 
-         }
 
-         if ($this->config['sdk'] != 'url') {
 
-             $result = Dever::load('sms/lib/' . $this->config['sdk'] . '.send', $param, $this->config);
 
-         } else {
 
-             if (!$this->config['url']) {
 
-                 $result = 'url error';
 
-             } else {
 
-                 $json = false;
 
-                 if ($this->config['json'] == 1) {
 
-                     $json = true;
 
-                 } else {
 
-                     $json = false;
 
-                 }
 
-                 $result = Dever::curl($this->config['url'], $param, $this->config['method'], $json, $this->config['header']);
 
-             }
 
-         }
 
-         # 记录当前的短信信息
 
-         $insert = array();
 
-         $insert['mobile'] = $mobile;
 
-         $insert['param'] = Dever::json_encode(array('config' => $this->config, 'param' => $param));
 
-         $insert['result'] = Dever::json_encode($result);
 
-         Dever::db('sms/info')->insert($insert);
 
-         return $result;
 
-     }
 
-     public function sendCode($skin = false, $mobile = false)
 
-     {
 
-         $skin = Dever::input('skin', $skin);
 
-         $mobile = Dever::input('mobile', $mobile);
 
-         $this->config($skin)->mobile($mobile);
 
-         $day = date('Ymd', time());
 
-         # 检测当前手机号最新一次发送时间,不允许一分钟之内发送
 
-         $param['day'] = $day;
 
-         $param['mobile'] = $mobile;
 
-         # 检测当前手机号今天已经发送多少验证码了
 
-         $info = Dever::db('sms/code')->total($param);
 
-         if ($info >= 1) {
 
-             $check = Dever::db('sms/code')->one($param);
 
-             if ($check) {
 
-                 if (time() - $check['cdate'] < $this->config['code_time']) {
 
-                     Dever::alert('请不要在一分钟之内申请多次验证码,请您稍后再试');
 
-                 }
 
-             }
 
-         }
 
-         $total = $this->config['code_total'];
 
-         if ($info >= $total) {
 
-             Dever::alert('很抱歉,您已经申请获取验证码超过' . $total . '次,今天您已经无法获取验证码了,请您明天再来');
 
-         }
 
-         $code = $this->createCode();
 
-         # 启动发送
 
-         $result = $this->send($skin, $mobile, array('code' => $code));
 
-         # 记录当前的验证码
 
-         $insert = array();
 
-         $insert['mobile'] = $mobile;
 
-         $insert['day'] = $day;
 
-         $insert['code'] = $code;
 
-         $insert['status'] = 1;
 
-         $insert['result'] = Dever::json_encode($result);
 
-         $id = Dever::db('sms/code')->insert($insert);
 
-         //Dever::session($this->config['key'], $mobile . '_' . $code, $this->config['code_timeout'], 'session');
 
-         return $code;
 
-     }
 
-     public function checkCode($skin = false, $mobile = false, $code = false)
 
-     {
 
-         $skin = Dever::input('skin', $skin);
 
-         $mobile = Dever::input('mobile', $mobile);
 
-         $code = Dever::input('code', $code);
 
-         if ($code && $skin && $mobile) {
 
-             //$save = Dever::session($this->config['key'], false, 3600, 'session');
 
-             //return $mobile . '_' . $code == $save;
 
-             $save = Dever::db('sms/code')->getNew(array('mobile' => $mobile));
 
-             if ($save && $save['status'] == 1 && $code == $save['code']) {
 
-                 Dever::db('sms/code')->update(array('where_id' => $save['id'], 'status' => 2));
 
-                 return true;
 
-             }
 
-         }
 
-         return false;
 
-     }
 
-    
 
-     private function replace($content, $param)
 
-     {
 
-         foreach ($param as $k => $v) {
 
-             $string = '{'.$k.'}';
 
-             if (strstr($content, $string)) {
 
-                 $content = str_replace($string, $v, $content);
 
-             }
 
-         }
 
-         return $content;
 
-     }
 
-     private function createCode()
 
-     {
 
-         $len = isset($this->config['code_length']) ? $this->config['code_length'] : 4;
 
-         $type = isset($this->config['code_type']) ? $this->config['code_type'] : 1;
 
-         return Dever::rand($len, $type - 1);
 
-     }
 
- }
 
 
  |