Base.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Sms\Lib;
  3. use Dever;
  4. class Base
  5. {
  6. const NAME = 'dever_passport';
  7. const CODE = 'dever_code';
  8. const MCODE = 'dever_mcode';
  9. protected $config;
  10. public function __construct()
  11. {
  12. $this->config = Dever::config('base', 'project')->sms;
  13. }
  14. protected function code($mobile, $code = false)
  15. {
  16. if ($code) {
  17. $save = $this->save->get($this->config['code']);
  18. return $mobile . '_' . $code == $save;
  19. }
  20. $day = date('Ymd', time());
  21. # 检测当前手机号最新一次发送时间,不允许一分钟之内发送
  22. $param['option_day'] = $day;
  23. $param['option_mobile'] = $mobile;
  24. # 检测当前手机号今天已经发送多少验证码了
  25. $info = Dever::load('sms/code-total', $param);
  26. if ($info >= 1) {
  27. $check = Dever::load('sms/code-one', $param);
  28. if ($check) {
  29. if (time() - $check['cdate'] < $this->config['time']) {
  30. Dever::alert('请不要在一分钟之内申请多次验证码,请您稍后再试');
  31. }
  32. }
  33. }
  34. $total = $this->config['total'];
  35. if ($info >= $total) {
  36. Dever::alert('很抱歉,您已经申请获取验证码超过' . $total . '次,今天您已经无法获取验证码了,请您明天再来');
  37. }
  38. $code = new Code();
  39. $code->createM();
  40. # 记录当前的验证码
  41. $insert['add_mobile'] = $mobile;
  42. $insert['add_day'] = $day;
  43. $insert['add_code'] = $code->mcode;
  44. $id = Dever::load('passport/code-insert', $insert);
  45. # 启动发送
  46. $this->send($mobile, $insert['add_code'], $id);
  47. $this->save->add($this->config['code'], $mobile . '_' . $code->mcode, $this->config['timeout']);
  48. return $code->mcode;
  49. }
  50. protected function send($mobile, $code, $id = false)
  51. {
  52. $url = $this->config['url'];
  53. if (!$url) {
  54. return;
  55. }
  56. $content = $this->config['body'];
  57. $content = $this->replace($content, $mobile, $code);
  58. parse_str($content, $param);
  59. $type = $this->config['method'];
  60. $json = $this->config['json'];
  61. $header = $this->config['header'];
  62. return Dever::curl($url, $param, $type, $json, $header);
  63. }
  64. private function replace($content, $mobile = '', $code = '')
  65. {
  66. $skin = $this->config['skin'];
  67. $skin_key = Dever::input('skin', 1);
  68. if (isset($skin[$skin_key])) {
  69. $skin = $skin[$skin_key];
  70. } else {
  71. $skin = array_shift($skin);
  72. }
  73. $config = array('{code}', '{mobile}', '{sign}', '{skin}', '{param}');
  74. $replace = array($code, $mobile, $this->config['sign'], $skin);
  75. return str_replace($config, $replace, $content);
  76. }
  77. }