Base.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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(), $send = false)
  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. if (!$send && isset($this->config['skin']['send']) && $this->config['skin']['send'] == 2) {
  41. return false;
  42. }
  43. $content = $this->config['body'];
  44. $skin = $this->config['skin']['content'];
  45. $param['skin'] = $skin;
  46. $param['mobile'] = $mobile;
  47. $param['sign'] = $this->config['sign'];
  48. if (strstr($skin, '{')) {
  49. $param['skin'] = $this->replace($skin, $param);
  50. }
  51. if ($content) {
  52. $content = $this->replace($content, $param);
  53. parse_str($content, $param);
  54. }
  55. if ($this->config['sdk'] != 'url') {
  56. $result = Dever::load('sms/lib/' . $this->config['sdk'] . '.send', $param, $this->config);
  57. } else {
  58. if (!$this->config['url']) {
  59. $result = 'url error';
  60. } else {
  61. $json = false;
  62. if ($this->config['json'] == 1) {
  63. $json = true;
  64. } else {
  65. $json = false;
  66. }
  67. $result = Dever::curl($this->config['url'], $param, $this->config['method'], $json, $this->config['header']);
  68. }
  69. }
  70. # 记录当前的短信信息
  71. $insert = array();
  72. $insert['mobile'] = $mobile;
  73. $insert['param'] = Dever::json_encode(array('config' => $this->config, 'param' => $param));
  74. $insert['result'] = Dever::json_encode($result);
  75. Dever::db('sms/info')->insert($insert);
  76. return $result;
  77. }
  78. public function sendCode($skin = false, $mobile = false)
  79. {
  80. $skin = Dever::input('skin', $skin);
  81. $mobile = Dever::input('mobile', $mobile);
  82. $this->config($skin)->mobile($mobile);
  83. $day = date('Ymd', time());
  84. # 检测当前手机号最新一次发送时间,不允许一分钟之内发送
  85. $param['day'] = $day;
  86. $param['mobile'] = $mobile;
  87. # 检测当前手机号今天已经发送多少验证码了
  88. $info = Dever::db('sms/code')->total($param);
  89. if ($info >= 1) {
  90. $check = Dever::db('sms/code')->one($param);
  91. if ($check) {
  92. if (time() - $check['cdate'] < $this->config['code_time']) {
  93. Dever::alert('请不要在一分钟之内申请多次验证码,请您稍后再试');
  94. }
  95. }
  96. }
  97. $total = $this->config['code_total'];
  98. if ($info >= $total) {
  99. Dever::alert('很抱歉,您已经申请获取验证码超过' . $total . '次,今天您已经无法获取验证码了,请您明天再来');
  100. }
  101. $code = $this->createCode();
  102. # 启动发送
  103. $result = $this->send($skin, $mobile, array('code' => $code));
  104. # 记录当前的验证码
  105. $insert = array();
  106. $insert['mobile'] = $mobile;
  107. $insert['day'] = $day;
  108. $insert['code'] = $code;
  109. $insert['status'] = 1;
  110. $insert['result'] = Dever::json_encode($result);
  111. $id = Dever::db('sms/code')->insert($insert);
  112. //Dever::session($this->config['key'], $mobile . '_' . $code, $this->config['code_timeout'], 'session');
  113. return $code;
  114. }
  115. public function checkCode($skin = false, $mobile = false, $code = false, $update = 1)
  116. {
  117. //$skin = Dever::input('skin', $skin);
  118. //$mobile = Dever::input('mobile', $mobile);
  119. //$code = Dever::input('code', $code);
  120. if ($code && $skin && $mobile) {
  121. //$save = Dever::session($this->config['key'], false, 3600, 'session');
  122. //return $mobile . '_' . $code == $save;
  123. $save = Dever::db('sms/code')->getNew(array('mobile' => $mobile));
  124. if ($save && $save['status'] == 1 && $code == $save['code']) {
  125. if ($update == 1) {
  126. Dever::db('sms/code')->update(array('where_id' => $save['id'], 'status' => 2));
  127. }
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. private function replace($content, $param)
  134. {
  135. foreach ($param as $k => $v) {
  136. $string = '{'.$k.'}';
  137. if (strstr($content, $string)) {
  138. $content = str_replace($string, $v, $content);
  139. }
  140. }
  141. return $content;
  142. }
  143. private function createCode()
  144. {
  145. $len = isset($this->config['code_length']) ? $this->config['code_length'] : 4;
  146. $type = isset($this->config['code_type']) ? $this->config['code_type'] : 1;
  147. return Dever::rand($len, $type - 1);
  148. }
  149. }