123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace Sms\Lib;
- use Dever;
- class Base
- {
- const NAME = 'dever_passport';
- const CODE = 'dever_code';
- const MCODE = 'dever_mcode';
- protected $config;
- public function __construct()
- {
- $this->config = Dever::config('base', 'project')->sms;
- }
- protected function code($mobile, $code = false)
- {
- if ($code) {
- $save = $this->save->get($this->config['code']);
- return $mobile . '_' . $code == $save;
- }
- $day = date('Ymd', time());
- # 检测当前手机号最新一次发送时间,不允许一分钟之内发送
- $param['option_day'] = $day;
- $param['option_mobile'] = $mobile;
- # 检测当前手机号今天已经发送多少验证码了
- $info = Dever::load('sms/code-total', $param);
- if ($info >= 1) {
- $check = Dever::load('sms/code-one', $param);
- if ($check) {
- if (time() - $check['cdate'] < $this->config['time']) {
- Dever::alert('请不要在一分钟之内申请多次验证码,请您稍后再试');
- }
- }
- }
- $total = $this->config['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);
- # 启动发送
- $this->send($mobile, $insert['add_code'], $id);
- $this->save->add($this->config['code'], $mobile . '_' . $code->mcode, $this->config['timeout']);
- return $code->mcode;
- }
- protected function send($mobile, $code, $id = false)
- {
- $url = $this->config['url'];
- if (!$url) {
- return;
- }
- $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);
- }
- private function replace($content, $mobile = '', $code = '')
- {
- $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);
- }
- }
|