Base.php 4.0 KB

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