Base.php 5.5 KB

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