Base.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace Passport\Src\Lib;
  3. use Dever;
  4. use Dever\Img\Handle;
  5. use Dever\Routing\Uri;
  6. use Dever\Session\Oper as Save;
  7. use Dever\String\Encrypt;
  8. class Base
  9. {
  10. const NAME = 'dever';
  11. const CODE = 'dever_code';
  12. const MCODE = 'dever_mcode';
  13. protected $save;
  14. public function __construct()
  15. {
  16. $this->save = new Save(false, 'cookie');
  17. }
  18. protected function info()
  19. {
  20. return $this->save->get(self::NAME);
  21. }
  22. protected function code($code = false)
  23. {
  24. if ($code) {
  25. $save = $this->save->get(self::CODE);
  26. return $code == $save;
  27. }
  28. $code = new Code();
  29. $code->create();
  30. $this->save->add(self::CODE, $code->code, 600);
  31. }
  32. protected function mcode($mobile, $code = false)
  33. {
  34. if ($code) {
  35. $save = $this->save->get(self::MCODE);
  36. return $mobile . '_' . $code == $save;
  37. }
  38. $day = date('Ymd', time());
  39. # 检测当前手机号最新一次发送时间,不允许一分钟之内发送
  40. $param['option_day'] = $day;
  41. $param['option_mobile'] = $mobile;
  42. # 检测当前手机号今天已经发送多少验证码了
  43. $info = Dever::load('passport/code-total', $param);
  44. if ($info >= 1) {
  45. $check = Dever::load('passport/code-check', $param);
  46. if ($check) {
  47. if (time() - $check['cdate'] < Dever::config('base', 'passport')->mobileCode['time']) {
  48. Dever::alert('请不要在一分钟之内申请多次验证码,请您稍后再试');
  49. }
  50. }
  51. }
  52. $total = Dever::config('base', 'passport')->mobileCode['total'];
  53. if ($info >= $total) {
  54. Dever::alert('很抱歉,您已经申请获取验证码超过' . $total . '次,今天您已经无法获取验证码了,请您明天再来');
  55. }
  56. $code = new Code();
  57. $code->createM();
  58. # 记录当前的验证码
  59. $insert['add_mobile'] = $mobile;
  60. $insert['add_day'] = $day;
  61. $insert['add_code'] = $code->mcode;
  62. $id = Dever::load('passport/code-insert', $insert);
  63. # 发送验证码到手机上
  64. //$text = '您好,您的验证码为' . $code->mcode . ',十分钟之内有效。';
  65. $text = str_replace('{code}', $code->mcode, Dever::config('base', 'passport')->mobileCode['msg']);
  66. # 启动发送
  67. $this->send($mobile, $text, $id);
  68. $this->save->add(self::MCODE, $mobile . '_' . $code->mcode, 600);
  69. return $code->mcode;
  70. }
  71. private function send($mobile, $content, $id)
  72. {
  73. $url = Dever::config('base', 'passport')->mobileCode['url'];
  74. if (Dever::config('base', 'passport')->mobileCode['signType'] == 1) {
  75. $content = Dever::config('base', 'passport')->mobileCode['sign'] . $content;
  76. } else {
  77. $content .= Dever::config('base', 'passport')->mobileCode['sign'];
  78. }
  79. $param['phone'] = $mobile;
  80. $param['message'] = $content;
  81. $param['seqid'] = $id + 1000;
  82. $param['smspriority'] = 1;
  83. $url .= http_build_query($param);
  84. Dever::curl($url);
  85. }
  86. protected function refer()
  87. {
  88. $refer = Dever::input('refer');
  89. $project = 'main';
  90. if ($refer) {
  91. $url = parse_url(Encrypt::decode($refer));
  92. $url['path'] = preg_replace('/^\//', '', $url['path']);
  93. if (!isset($url['query'])) {
  94. $url['query'] = '';
  95. }
  96. if (Uri::$type == '?') {
  97. $refer = Dever::url(str_replace($url['path'] . Uri::$type, '', $url['query']), $project);
  98. } else {
  99. $refer = Dever::url($url['path'] . '?' . $url['query'], $project);
  100. }
  101. Dever::out($refer);
  102. } else {
  103. Dever::out(Dever::url('home', $project));
  104. }
  105. }
  106. protected function save($user)
  107. {
  108. if (is_numeric($user)) {
  109. $user = Dever::load('service/company-one', $user);
  110. }
  111. if ($user && is_array($user)) {
  112. if (Dever::mobile()) {
  113. $time = 30;
  114. } else {
  115. $time = 7;
  116. }
  117. $this->save->add(self::NAME, $user, 3600 * 24 * $time);
  118. $user['signature'] = Dever::login($user['id']);
  119. return $user;
  120. }
  121. }
  122. public function createRefer()
  123. {
  124. return 'refer=' . Encrypt::encode(Dever::url());
  125. }
  126. protected function saveAvatar($value)
  127. {
  128. $base = DEVER_PATH . 'data/upload/';
  129. $date = explode('-', date("Y-m-d"));
  130. if (is_array($value)) {
  131. $user = $value[1];
  132. $value = $value[0];
  133. if (is_array($value)) {
  134. $name = md5($value['tmp_name'] . rand(0, 100) . microtime());
  135. $path = 'avatar/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
  136. $ext = '.jpg';
  137. $temp = Dever::path($base, $path . $name . $ext);
  138. move_uploaded_file($value['tmp_name'], $temp);
  139. $value = $temp;
  140. }
  141. }
  142. $result = Dever::$global['host']['img'] . 'chead.jpg';
  143. if ($value) {
  144. # 裁图
  145. $handle = new Handle();
  146. $size = '200_200_2';
  147. $user = isset($user) && $user ? $user : $this->init();
  148. if ($user && isset($user['id']) && $user['id']) {
  149. $name = md5($user['id']);
  150. } else {
  151. $name = md5($value . rand(0, 100) . microtime());
  152. }
  153. $path = 'avatar/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
  154. $ext = '.jpg';
  155. $file = Dever::path($base, $path . $name . $ext);
  156. $temp = $file . '.temp.jpg';
  157. //$value = Dever::curl($value);
  158. //file_put_contents($temp, file_get_contents($value));
  159. $file = $handle->thumb($value, $size, true, $file);
  160. //print_r($file);die;
  161. if ($file) {
  162. $result = Dever::$global['host']['image'] . $path . $name . $ext;
  163. } else {
  164. $result = $value;
  165. }
  166. }
  167. return $result;
  168. }
  169. }