Base.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. $debug = false;
  56. $mobileCode = Dever::config('base', 'project')->mobileCode;
  57. if ($mobileCode && isset($mobileCode['debug'])) {
  58. $debug = $mobileCode['debug'];
  59. }
  60. if (!$debug) {
  61. if ($this->config['sdk'] != 'url') {
  62. $result = Dever::load('sms/lib/' . $this->config['sdk'] . '.send', $param, $this->config);
  63. } else {
  64. if (!$this->config['url']) {
  65. $result = 'url error';
  66. } else {
  67. $json = false;
  68. if ($this->config['json'] == 1) {
  69. $json = true;
  70. } else {
  71. $json = false;
  72. }
  73. $result = Dever::curl($this->config['url'], $param, $this->config['method'], $json, $this->config['header']);
  74. }
  75. }
  76. } else {
  77. $result = 'debug true';
  78. }
  79. # 记录当前的短信信息
  80. $insert = array();
  81. $insert['mobile'] = $mobile;
  82. $insert['param'] = Dever::json_encode(array('config' => $this->config, 'param' => $param));
  83. $insert['result'] = Dever::json_encode($result);
  84. Dever::db('sms/info')->insert($insert);
  85. return $result;
  86. }
  87. public function sendCode($skin = false, $mobile = false, $state = false, $key = 'code')
  88. {
  89. $skin = Dever::input('skin', $skin);
  90. $mobile = Dever::input('mobile', $mobile);
  91. $this->config($skin)->mobile($mobile);
  92. $day = date('Ymd', time());
  93. # 检测当前手机号最新一次发送时间,不允许一分钟之内发送
  94. $param['day'] = $day;
  95. $param['mobile'] = $mobile;
  96. # 检测当前手机号今天已经发送多少验证码了
  97. $info = Dever::db('sms/code')->total($param);
  98. if ($info >= 1) {
  99. $check = Dever::db('sms/code')->one($param);
  100. if ($check) {
  101. if (time() - $check['cdate'] < $this->config['code_time']) {
  102. Dever::alert('请不要在一分钟之内申请多次验证码,请您稍后再试');
  103. }
  104. }
  105. }
  106. $total = $this->config['code_total'];
  107. if ($info >= $total) {
  108. Dever::alert('很抱歉,您已经申请获取验证码超过' . $total . '次,今天您已经无法获取验证码了,请您明天再来');
  109. }
  110. $code = $this->createCode();
  111. # 启动发送
  112. $result = $this->send($skin, $mobile, array($key => $code));
  113. # 记录当前的验证码
  114. $insert = array();
  115. $insert['mobile'] = $mobile;
  116. $insert['day'] = $day;
  117. $insert['code'] = $code;
  118. $insert['status'] = 1;
  119. $insert['result'] = Dever::json_encode($result);
  120. $id = Dever::db('sms/code')->insert($insert);
  121. //Dever::session($this->config['key'], $mobile . '_' . $code, $this->config['code_timeout'], 'session');
  122. $msg = '验证码已发送至您的手机,请注意查收,十分钟之内有效';
  123. if ($state) {
  124. return $code;
  125. }
  126. return $msg . (!$result ? '::' . $code : '');
  127. }
  128. public function checkCode($skin = false, $mobile = false, $code = false, $update = 1)
  129. {
  130. //$skin = Dever::input('skin', $skin);
  131. //$mobile = Dever::input('mobile', $mobile);
  132. //$code = Dever::input('code', $code);
  133. if ($code && $skin && $mobile) {
  134. //$save = Dever::session($this->config['key'], false, 3600, 'session');
  135. //return $mobile . '_' . $code == $save;
  136. $save = Dever::db('sms/code')->getNew(array('mobile' => $mobile));
  137. if ($save && $save['status'] == 1 && $code == $save['code']) {
  138. if ($update == 1) {
  139. Dever::db('sms/code')->update(array('where_id' => $save['id'], 'status' => 2));
  140. }
  141. return true;
  142. }
  143. }
  144. return false;
  145. }
  146. private function replace($content, $param)
  147. {
  148. foreach ($param as $k => $v) {
  149. $string = '{'.$k.'}';
  150. if (strstr($content, $string)) {
  151. $content = str_replace($string, $v, $content);
  152. }
  153. }
  154. return $content;
  155. }
  156. private function createCode()
  157. {
  158. $len = isset($this->config['code_length']) ? $this->config['code_length'] : 4;
  159. $type = isset($this->config['code_type']) ? $this->config['code_type'] : 1;
  160. return Dever::rand($len, $type - 1);
  161. }
  162. public function type(){
  163. $type = Dever::input('search_option_type');
  164. $where = array();
  165. if ($type) {
  166. $where['key'] = $type;
  167. }
  168. $data = Dever::db('sms/skin')->getAll($where);
  169. return $data;
  170. }
  171. public function templateUpdate($id,$name,$data){
  172. $update = array();
  173. $content = Dever::param('content', $data);
  174. $name = Dever::param('name', $data);
  175. $type = Dever::param('type', $data);
  176. $info = Dever::db('sms/template')->one($id);
  177. if($info){
  178. if (Dever::project('sms')) {
  179. $skin = Dever::db('sms/skin')->find($type[0]);
  180. Dever::load('sms/api')->send($skin['key'], $info['mobile'], array('name' => $name , 'content' => $content),true);
  181. }
  182. }
  183. }
  184. public function getTable($table=false){
  185. // $table = Dever::input('table',$table);
  186. if(!$table){
  187. $table = 'template';
  188. }
  189. return 'project/database/update?project=sms&table='.$table;
  190. }
  191. }