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(), $send = false) { $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 (!$send && 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); } $debug = false; $mobileCode = Dever::config('base', 'project')->mobileCode; if ($mobileCode && isset($mobileCode['debug'])) { $debug = $mobileCode['debug']; } if (!$debug) { 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']); } } } else { $result = 'debug true'; } # 记录当前的短信信息 $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, $state = false, $key = 'code') { $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($key => $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'); $msg = '验证码已发送至您的手机,请注意查收,十分钟之内有效'; if ($state) { return $code; } return $msg . (!$result ? '::' . $code : ''); } public function checkCode($skin = false, $mobile = false, $code = false, $update = 1) { //$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']) { if ($update == 1) { 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); } public function type(){ $type = Dever::input('search_option_type'); $where = array(); if ($type) { $where['key'] = $type; } $data = Dever::db('sms/skin')->getAll($where); return $data; } public function templateUpdate($id,$name,$data){ $update = array(); $content = Dever::param('content', $data); $name = Dever::param('name', $data); $type = Dever::param('type', $data); $info = Dever::db('sms/template')->one($id); if($info){ if (Dever::project('sms')) { $skin = Dever::db('sms/skin')->find($type[0]); Dever::load('sms/api')->send($skin['key'], $info['mobile'], array('name' => $name , 'content' => $content),true); } } } public function getTable($table=false){ // $table = Dever::input('table',$table); if(!$table){ $table = 'template'; } return 'project/database/update?project=sms&table='.$table; } }