Base.php 5.4 KB

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