Base.php 4.6 KB

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