|
@@ -5,100 +5,134 @@ use Dever;
|
|
|
|
|
|
class Base
|
|
|
{
|
|
|
- const NAME = 'dever_passport';
|
|
|
+ protected $config;
|
|
|
|
|
|
- const CODE = 'dever_code';
|
|
|
+ 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']['id']);
|
|
|
+ } else {
|
|
|
+ Dever::alert('错误的短信模板');
|
|
|
+ }
|
|
|
+ if (!$this->config) {
|
|
|
+ Dever::alert('错误的短信配置');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
|
|
|
- const MCODE = 'dever_mcode';
|
|
|
+ private function mobile($mobile)
|
|
|
+ {
|
|
|
+
|
|
|
|
|
|
- protected $config;
|
|
|
+ }
|
|
|
|
|
|
- public function __construct()
|
|
|
+ public function send($skin, $mobile, $param = array())
|
|
|
{
|
|
|
- $this->config = Dever::config('base', 'project')->sms;
|
|
|
+ $this->config($skin)->mobile($mobile);
|
|
|
+
|
|
|
+ $content = $this->config['body'];
|
|
|
+ $skin = $this->config['skin']['content'];
|
|
|
+
|
|
|
+ $param['skin'] = $skin;
|
|
|
+ $param['mobile'] = $mobile;
|
|
|
+ $param['sign'] = $this->config['sign'];
|
|
|
+
|
|
|
+ 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 {
|
|
|
+ $result = Dever::curl($this->config['url'], $param, $this->config['method'], $this->config['json'], $this->config['header']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ $insert = array();
|
|
|
+ $insert['mobile'] = $mobile;
|
|
|
+ $insert['param'] = Dever::json_encode($this->config + $param);
|
|
|
+ $insert['result'] = Dever::json_encode($result);
|
|
|
+ Dever::db('sms/info')->insert($insert);
|
|
|
+
|
|
|
+ return $result;
|
|
|
}
|
|
|
|
|
|
- protected function code($mobile, $code = false)
|
|
|
+ public function sendCode($skin, $mobile, $code = false)
|
|
|
{
|
|
|
+ $this->config($skin)->mobile($mobile);
|
|
|
+
|
|
|
if ($code) {
|
|
|
- $save = $this->save->get($this->config['code']);
|
|
|
+ $save = Dever::session($this->config['key']);
|
|
|
return $mobile . '_' . $code == $save;
|
|
|
}
|
|
|
|
|
|
$day = date('Ymd', time());
|
|
|
|
|
|
|
|
|
- $param['option_day'] = $day;
|
|
|
- $param['option_mobile'] = $mobile;
|
|
|
+ $param['day'] = $day;
|
|
|
+ $param['mobile'] = $mobile;
|
|
|
|
|
|
|
|
|
- $info = Dever::load('sms/code-total', $param);
|
|
|
+ $info = Dever::db('sms/code')->total($param);
|
|
|
|
|
|
if ($info >= 1) {
|
|
|
- $check = Dever::load('sms/code-one', $param);
|
|
|
+ $check = Dever::db('sms/code')->one($param);
|
|
|
|
|
|
if ($check) {
|
|
|
- if (time() - $check['cdate'] < $this->config['time']) {
|
|
|
+ if (time() - $check['cdate'] < $this->config['code_time']) {
|
|
|
Dever::alert('请不要在一分钟之内申请多次验证码,请您稍后再试');
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- $total = $this->config['total'];
|
|
|
+ $total = $this->config['code_total'];
|
|
|
if ($info >= $total) {
|
|
|
Dever::alert('很抱歉,您已经申请获取验证码超过' . $total . '次,今天您已经无法获取验证码了,请您明天再来');
|
|
|
}
|
|
|
|
|
|
- $code = new Code();
|
|
|
- $code->createM();
|
|
|
-
|
|
|
-
|
|
|
- $insert['add_mobile'] = $mobile;
|
|
|
- $insert['add_day'] = $day;
|
|
|
- $insert['add_code'] = $code->mcode;
|
|
|
- $id = Dever::load('passport/code-insert', $insert);
|
|
|
+ $code = $this->createCode();
|
|
|
|
|
|
|
|
|
- $this->send($mobile, $insert['add_code'], $id);
|
|
|
+ $result = $this->send($skin, $mobile, array('code' => $code));
|
|
|
|
|
|
- $this->save->add($this->config['code'], $mobile . '_' . $code->mcode, $this->config['timeout']);
|
|
|
+
|
|
|
+ $insert = array();
|
|
|
+ $insert['mobile'] = $mobile;
|
|
|
+ $insert['day'] = $day;
|
|
|
+ $insert['code'] = $code;
|
|
|
+ $insert['result'] = Dever::json_encode($result);
|
|
|
+ $id = Dever::db('sms/code')->insert($insert);
|
|
|
+
|
|
|
+ Dever::session($this->config['key'], $mobile . '_' . $code, $this->config['code_timeout']);
|
|
|
|
|
|
- return $code->mcode;
|
|
|
+ return $code;
|
|
|
}
|
|
|
+
|
|
|
|
|
|
- protected function send($mobile, $code, $id = false)
|
|
|
+ private function replace($content, $param)
|
|
|
{
|
|
|
- $url = $this->config['url'];
|
|
|
-
|
|
|
- if (!$url) {
|
|
|
- return;
|
|
|
+ foreach ($param as $k => $v) {
|
|
|
+ $string = '{'.$k.'}';
|
|
|
+ if (strstr($content, $string)) {
|
|
|
+ $content = str_replace($string, $v, $content);
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- $content = $this->config['body'];
|
|
|
-
|
|
|
- $content = $this->replace($content, $mobile, $code);
|
|
|
-
|
|
|
- parse_str($content, $param);
|
|
|
-
|
|
|
- $type = $this->config['method'];
|
|
|
- $json = $this->config['json'];
|
|
|
- $header = $this->config['header'];
|
|
|
-
|
|
|
- return Dever::curl($url, $param, $type, $json, $header);
|
|
|
+ return $content;
|
|
|
}
|
|
|
|
|
|
- private function replace($content, $mobile = '', $code = '')
|
|
|
+ private function createCode()
|
|
|
{
|
|
|
- $skin = $this->config['skin'];
|
|
|
- $skin_key = Dever::input('skin', 1);
|
|
|
- if (isset($skin[$skin_key])) {
|
|
|
- $skin = $skin[$skin_key];
|
|
|
- } else {
|
|
|
- $skin = array_shift($skin);
|
|
|
- }
|
|
|
-
|
|
|
- $config = array('{code}', '{mobile}', '{sign}', '{skin}', '{param}');
|
|
|
- $replace = array($code, $mobile, $this->config['sign'], $skin);
|
|
|
- return str_replace($config, $replace, $content);
|
|
|
+ $len = isset($this->config['code_length']) ? $this->config['code_length'] : 4;
|
|
|
+ $type = isset($this->config['code_type']) ? $this->config['code_type'] : 4;
|
|
|
+ return Dever::rand($len, $type);
|
|
|
}
|
|
|
}
|