Base.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Sms\Lib;
  3. use Dever;
  4. class Base
  5. {
  6. protected $config;
  7. private function config($skin)
  8. {
  9. if (!$this->config) {
  10. $this->config['skin'] = Dever::db('sms/skin')->one(array('key' => $skin));
  11. if ($this->config['skin']) {
  12. $this->config += Dever::db('sms/config')->one($this->config['skin']['config_id']);
  13. } else {
  14. Dever::alert('错误的短信模板');
  15. }
  16. if (!$this->config) {
  17. Dever::alert('错误的短信配置');
  18. }
  19. }
  20. return $this;
  21. }
  22. private function mobile($mobile)
  23. {
  24. # 验证手机号是否有效
  25. $rule = Dever::rule('mobile');
  26. $state = preg_match($rule, $mobile);
  27. if (!$state) {
  28. Dever::alert('手机号无效');
  29. }
  30. }
  31. public function send($skin = false, $mobile = false, $param = array())
  32. {
  33. $skin = Dever::input('skin', $skin);
  34. $mobile = Dever::input('mobile', $mobile);
  35. $param = Dever::input('param', $param);
  36. if (!is_array($param)) {
  37. $param = Dever::json_decode($param);
  38. }
  39. $this->config($skin)->mobile($mobile);
  40. $content = $this->config['body'];
  41. $skin = $this->config['skin']['content'];
  42. $param['skin'] = $skin;
  43. $param['mobile'] = $mobile;
  44. $param['sign'] = $this->config['sign'];
  45. if ($content) {
  46. $content = $this->replace($content, $param);
  47. parse_str($content, $param);
  48. }
  49. if ($this->config['sdk'] != 'url') {
  50. $result = Dever::load('sms/lib/' . $this->config['sdk'] . '.send', $param, $this->config);
  51. } else {
  52. if (!$this->config['url']) {
  53. $result = 'url error';
  54. } else {
  55. $result = Dever::curl($this->config['url'], $param, $this->config['method'], $this->config['json'], $this->config['header']);
  56. }
  57. }
  58. # 记录当前的短信信息
  59. $insert = array();
  60. $insert['mobile'] = $mobile;
  61. $insert['param'] = Dever::json_encode($this->config + $param);
  62. $insert['result'] = Dever::json_encode($result);
  63. Dever::db('sms/info')->insert($insert);
  64. return $result;
  65. }
  66. public function sendCode($skin = false, $mobile = false)
  67. {
  68. $skin = Dever::input('skin', $skin);
  69. $mobile = Dever::input('mobile', $mobile);
  70. $this->config($skin)->mobile($mobile);
  71. $day = date('Ymd', time());
  72. # 检测当前手机号最新一次发送时间,不允许一分钟之内发送
  73. $param['day'] = $day;
  74. $param['mobile'] = $mobile;
  75. # 检测当前手机号今天已经发送多少验证码了
  76. $info = Dever::db('sms/code')->total($param);
  77. if ($info >= 1) {
  78. $check = Dever::db('sms/code')->one($param);
  79. if ($check) {
  80. if (time() - $check['cdate'] < $this->config['code_time']) {
  81. Dever::alert('请不要在一分钟之内申请多次验证码,请您稍后再试');
  82. }
  83. }
  84. }
  85. $total = $this->config['code_total'];
  86. if ($info >= $total) {
  87. Dever::alert('很抱歉,您已经申请获取验证码超过' . $total . '次,今天您已经无法获取验证码了,请您明天再来');
  88. }
  89. $code = $this->createCode();
  90. # 启动发送
  91. $result = $this->send($skin, $mobile, array('code' => $code));
  92. # 记录当前的验证码
  93. $insert = array();
  94. $insert['mobile'] = $mobile;
  95. $insert['day'] = $day;
  96. $insert['code'] = $code;
  97. $insert['status'] = 1;
  98. $insert['result'] = Dever::json_encode($result);
  99. $id = Dever::db('sms/code')->insert($insert);
  100. //Dever::session($this->config['key'], $mobile . '_' . $code, $this->config['code_timeout'], 'session');
  101. return $code;
  102. }
  103. public function checkCode($skin = false, $mobile = false, $code = false)
  104. {
  105. $skin = Dever::input('skin', $skin);
  106. $mobile = Dever::input('mobile', $mobile);
  107. $code = Dever::input('code', $code);
  108. if ($code && $skin && $mobile) {
  109. //$save = Dever::session($this->config['key'], false, 3600, 'session');
  110. //return $mobile . '_' . $code == $save;
  111. $save = Dever::db('sms/code')->getNew(array('mobile' => $mobile));
  112. if ($save && $save['status'] == 1 && $code == $save['code']) {
  113. Dever::db('sms/code')->update(array('where_id' => $save['id'], 'status' => 2));
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119. private function replace($content, $param)
  120. {
  121. foreach ($param as $k => $v) {
  122. $string = '{'.$k.'}';
  123. if (strstr($content, $string)) {
  124. $content = str_replace($string, $v, $content);
  125. }
  126. }
  127. return $content;
  128. }
  129. private function createCode()
  130. {
  131. $len = isset($this->config['code_length']) ? $this->config['code_length'] : 4;
  132. $type = isset($this->config['code_type']) ? $this->config['code_type'] : 4;
  133. return Dever::rand($len, $type);
  134. }
  135. }