Base.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. <?php
  2. namespace Passport\Lib;
  3. use Dever;
  4. use Upload\Src\Lib\Img;
  5. use Dever\Routing\Uri;
  6. use Dever\Session\Oper as Save;
  7. use Dever\String\Encrypt;
  8. class Base
  9. {
  10. const NAME = 'passportv1';
  11. const CODE = 'code';
  12. const MCODE = 'mcode';
  13. protected $save;
  14. public function __construct()
  15. {
  16. //$this->save = new Save(false, 'cookie');
  17. $this->save = new Save(false, 'session');
  18. }
  19. # 注册用户
  20. public function reg($type, $data)
  21. {
  22. $user = $this->getUserExtInfo($data);
  23. $user['source_type'] = $type;
  24. $user['system_id'] = isset($data['system']) ? $data['system'] : 1;
  25. if (isset($data['username'])) {
  26. $user['temp'] = 2;
  27. $user['username'] = $data['username'];
  28. } else {
  29. $user['temp'] = 1;
  30. $user['username'] = '临时用户';
  31. }
  32. if (isset($data['mobile'])) {
  33. $user['bind'] = 1;
  34. $user['mobile'] = $data['mobile'];
  35. } else {
  36. $user['bind'] = 2;
  37. $user['mobile'] = '';
  38. }
  39. $uid = Dever::db('passport/user')->insert($user);
  40. $this->createUsername($uid, $user['username'], true);
  41. return $uid;
  42. }
  43. # 更新用户
  44. public function updateUser($uid, $data)
  45. {
  46. $user = $this->getUserExtInfo($data);
  47. $user['username'] = $this->createUsername($uid, $data['username']);
  48. if (isset($user) && $user) {
  49. $update['where_id'] = $uid;
  50. Dever::db('passport/user')->update($update);
  51. }
  52. return $uid;
  53. }
  54. private function getUserExtInfo($data)
  55. {
  56. $user = array();
  57. if (isset($data['sex'])) {
  58. $user['sex'] = $data['sex'];
  59. }
  60. if (isset($data['avatar'])) {
  61. //$update['avatar'] = $this->saveAvatar($pic);
  62. $user['avatar'] = $data['avatar'];
  63. }
  64. if (isset($data['city'])) {
  65. $user['city'] = $data['city'];
  66. }
  67. if (isset($data['province'])) {
  68. $user['province'] = $data['province'];
  69. }
  70. if (isset($data['country'])) {
  71. $user['country'] = $data['country'];
  72. }
  73. if (isset($user['country']) && isset($user['province']) && isset($user['city'])) {
  74. $user['set_area'] = $user['country'] .','. $user['province'] .','. $user['city'];
  75. }
  76. return $user;
  77. }
  78. public function wechat($data, $user = array(), $account, $system, $source_type, $source = false)
  79. {
  80. $uid = false;
  81. if (!$data['openid']) {
  82. Dever::alert('错误的openid');
  83. }
  84. if (!isset($user['username'])) {
  85. Dever::alert('用户名错误');
  86. }
  87. if (isset($user['sex'])) {
  88. if ($user['sex'] != 1 && $user['sex'] != 2) {
  89. $user['sex'] = 3;
  90. }
  91. }
  92. if (isset($user['country']) && $user['country'] && isset($user['province']) && $user['province'] && isset($user['city']) && $user['city']) {
  93. $user['area'] = $user['country'] .','. $user['province'] .','. $user['city'];
  94. }
  95. $info = Dever::db('passport/wechat')->one(array('openid' => $data['openid']));
  96. if (!$info) {
  97. if (isset($data['unionid']) && $data['unionid']) {
  98. $info = Dever::db('passport/wechat')->one(array('unionid' => $data['unionid']));
  99. if (!$info) {
  100. $uid = false;
  101. } else {
  102. $uid = $info['uid'];
  103. }
  104. $wechat['unionid'] = $data['unionid'];
  105. }
  106. if (!$uid) {
  107. $user['source_type'] = 'service';
  108. if ($system) {
  109. $user['system_id'] = $system;
  110. }
  111. if ($source_type) {
  112. $user['source_type'] = $source_type;
  113. }
  114. $uid = Dever::db('passport/user')->insert($user);
  115. } else {
  116. $user['where_id'] = $uid;
  117. Dever::db('passport/user')->update($user);
  118. }
  119. $wechat['access_token'] = $data['access_token'];
  120. $wechat['openid'] = $data['openid'];
  121. $wechat['expires_in'] = $data['expires_in'];
  122. $wechat['refresh_token'] = $data['refresh_token'];
  123. $wechat['account_id'] = $account;
  124. if ($system) {
  125. $wechat['system_id'] = $system;
  126. }
  127. $wechat['uid'] = $uid;
  128. $wechat['type'] = 2;
  129. if ($source_type == 'applet') {
  130. $wechat['type'] = 1;
  131. } elseif ($source_type == 'ios') {
  132. $wechat['type'] = 3;
  133. } elseif ($source_type == 'android') {
  134. $wechat['type'] = 4;
  135. }
  136. $id = Dever::db('passport/wechat')->insert($wechat);
  137. if (Dever::project('source') && isset($source) && $source && $source > 0) {
  138. Dever::load('source/lib/core')->saveUser($id, $uid, $source, 'oauth', $account);
  139. }
  140. } else {
  141. $uid = $info['uid'];
  142. if (isset($data['unionid']) && $data['unionid']) {
  143. $wechat['unionid'] = $data['unionid'];
  144. # 判断用户是否存在,是否需要合并
  145. //$wechat['uid'] = $this->combine($uid, $data['unionid']);
  146. }
  147. $wechat['access_token'] = $data['access_token'];
  148. $wechat['openid'] = $data['openid'];
  149. $wechat['expires_in'] = $data['expires_in'];
  150. $wechat['refresh_token'] = $data['refresh_token'];
  151. $wechat['where_id'] = $info['id'];
  152. $wechat['account_id'] = $account;
  153. if ($system) {
  154. $wechat['system_id'] = $system;
  155. }
  156. $id = $info['id'];
  157. //Dever::db('passport/wechat')->update($wechat);
  158. $user['where_id'] = $uid;
  159. //Dever::load('passport/user-update', $user);
  160. }
  161. $this->createUsername($uid, $user['username']);
  162. $user = Dever::load('passport/user-one', $uid);
  163. $this->save($user);
  164. $user['uid'] = $user['id'];
  165. $user['signature'] = Dever::login($user['id']);
  166. return $user;
  167. }
  168. /**
  169. * 更新用户信息 绑定用户手机号
  170. *
  171. * @return mixed
  172. */
  173. public function bind_mobile()
  174. {
  175. $uid = $this->check();
  176. //$code = Dever::input('mcode');
  177. $mobile = Dever::load('passport/reg')->checkMobileExists();
  178. //$mobile = Dever::input('mobile');
  179. if ($mobile && $uid) {
  180. $info = Dever::load('passport/user-one', array('mobile' => $mobile));
  181. if ($info && $info['bind'] == 1) {
  182. Dever::alert('该手机号已绑定');
  183. }
  184. $uid = $this->combine($uid, $mobile, 'mobile');
  185. $info = Dever::load('passport/user-one', $uid);
  186. $result['mobile'] = $mobile;
  187. if ($info) {
  188. $update['set_mobile'] = $mobile;
  189. $update['set_bind'] = 1;
  190. $update['where_id'] = $uid;
  191. Dever::load('passport/user-update', $update);
  192. $state = Dever::config('base', 'project')->regSendSms;
  193. if ($state) {
  194. Dever::setInput('skin', $state);
  195. $this->send($mobile, $uid);
  196. }
  197. } else {
  198. Dever::alert('无效的用户id,请重新登录');
  199. }
  200. }
  201. if (!$info['mobile']) {
  202. Dever::score($uid, 'bind_mobile', '绑定手机号');
  203. }
  204. $result['uid'] = $uid;
  205. $result['signature'] = Dever::login($uid);
  206. return $result;
  207. }
  208. public function createUsername($uid, $username, $update = false)
  209. {
  210. if (!$username || $username == '临时用户') {
  211. $username = $uid + 100000;
  212. $username = 'JM' . $username;
  213. if ($update) {
  214. Dever::db('passport/user')->update(array('where_id' => $uid, 'username' => $username));
  215. }
  216. }
  217. return $username;
  218. }
  219. # 合并用户
  220. public function combine($uid, $unionid, $col = 'unionid')
  221. {
  222. $cur = $uid;
  223. if ($col == 'mobile' || $col == 'id') {
  224. $user_wechat = Dever::load('passport/user-all', array($col => $unionid));
  225. } else {
  226. $user_wechat = Dever::load('passport/wechat-getByUnionid', array($col => $unionid));
  227. }
  228. if ($user_wechat) {
  229. # 合并去
  230. $drop = array();
  231. $total = count($user_wechat);
  232. if ($total <= 1) {
  233. return $cur;
  234. }
  235. $new = false;
  236. foreach ($user_wechat as $k => $v) {
  237. if (!isset($v['uid'])) {
  238. $v['uid'] = $v['id'];
  239. $user = $v;
  240. } else {
  241. $user = Dever::db('passport/user')->one($v['uid']);
  242. $user['uid'] = $user['id'];
  243. }
  244. if (!$new) {
  245. if ($user['bind'] == 1 && !strstr($user['username'], '****')) {
  246. $new = $user;
  247. } elseif ($user['temp'] == 2 && $user['source_type'] == 'applet') {
  248. $new = $user;
  249. } elseif ($user['avatar']) {
  250. $new = $user;
  251. } elseif ($user['mobile']) {
  252. $new = $user;
  253. } else {
  254. $drop[$user['uid']] = $user;
  255. }
  256. } else {
  257. $drop[$user['uid']] = $user;
  258. }
  259. }
  260. $cur = $new;
  261. if (Dever::input('test') == 1) {
  262. print_r($user_wechat);
  263. print_r($cur);
  264. print_r($drop);die;
  265. }
  266. # 异步处理
  267. $this->updateCombine($cur, $drop);
  268. $cur = $new['uid'];
  269. }
  270. return $cur;
  271. }
  272. private function updateCombine($new, $drop)
  273. {
  274. # 队列
  275. //Dever::queue();
  276. if (isset($drop[$new['uid']])) {
  277. unset($drop[$new['uid']]);
  278. }
  279. $drops = array();
  280. if ($drop) {
  281. foreach ($drop as $k => $v) {
  282. $drops[] = $k;
  283. $update = array();
  284. if (!$new['username'] && $v['username']) {
  285. $update['username'] = $v['username'];
  286. } elseif (strstr($new['username'], '****') && $v['username']) {
  287. $update['username'] = $v['username'];
  288. }
  289. if (!$new['avatar'] && $v['avatar']) {
  290. $update['avatar'] = $v['avatar'];
  291. }
  292. if (!$new['mobile'] && $v['mobile']) {
  293. $update['mobile'] = $v['mobile'];
  294. }
  295. if (!$new['area'] && $v['area']) {
  296. $update['area'] = $v['area'];
  297. }
  298. if (isset($update) && $update) {
  299. $update['where_id'] = $new['uid'];
  300. Dever::load('passport/user-update', $update);
  301. }
  302. }
  303. if ($drops) {
  304. asort($drops);
  305. $drop_uid = implode(',', $drops);
  306. //Dever::load('passport/lib/base.handleCombine?new_uid=' . $new['uid'] . '&old_uid=' . $drop_uid);
  307. Dever::daemon('lib/base.handleCombine?new_uid=' . $new['uid'] . '&old_uid=' . $drop_uid, 'passport');
  308. }
  309. }
  310. }
  311. public function handleCombine()
  312. {
  313. $combine = array();
  314. $combine['status'] = 1;
  315. $combine['new_uid'] = Dever::input('new_uid');
  316. $combine['old_uid'] = Dever::input('old_uid');
  317. $info = Dever::db('passport/combine')->one($combine);
  318. if (!$info) {
  319. Dever::db('passport/combine')->insert($combine);
  320. }
  321. Dever::load('passport/user-updates', array('set_state' => 2, 'where_id' => $combine['old_uid']));
  322. Dever::load('passport/wechat-updates', array('set_uid' => $combine['new_uid'], 'where_uid' => $combine['old_uid']));
  323. }
  324. /**
  325. * 检测用户有效性
  326. *
  327. * @return mixed
  328. */
  329. public function check($state = true)
  330. {
  331. $signature = Dever::input('signature');
  332. $user = Dever::checkLogin($signature, $state);
  333. if ($state && !isset($user['uid'])) {
  334. Dever::alert('user error');
  335. }
  336. if (isset($user['uid']) && $user['uid']) {
  337. return $user['uid'];
  338. }
  339. return -1;
  340. }
  341. public function getToken($request = array())
  342. {
  343. return http_build_query(Dever::token($request));
  344. }
  345. public function getUrl($method, $request = array())
  346. {
  347. return Dever::proxy($method, $this->getToken($request));
  348. }
  349. protected function info()
  350. {
  351. $data = $this->save->get(self::NAME);
  352. return $data;
  353. }
  354. public function code($code = false, $image = true)
  355. {
  356. if ($code) {
  357. $save = $this->save->get(self::CODE);
  358. $state = $code == $save;
  359. if ($state) {
  360. //$this->save->un(self::CODE);
  361. }
  362. return $state;
  363. }
  364. if ($image) {
  365. $code = new Code();
  366. $code->create();
  367. $code = $code->code;
  368. } else {
  369. $code = Dever::code();
  370. }
  371. $this->save->add(self::CODE, $code, 86400);
  372. return $code;
  373. }
  374. protected function mcode($mobile, $code = false)
  375. {
  376. if ($code) {
  377. $save = $this->save->get(self::MCODE);
  378. return $mobile . '_' . $code == $save;
  379. }
  380. $day = date('Ymd', time());
  381. # 检测当前手机号最新一次发送时间,不允许一分钟之内发送
  382. $param['option_day'] = $day;
  383. $param['option_mobile'] = $mobile;
  384. # 检测当前手机号今天已经发送多少验证码了
  385. $info = Dever::load('passport/code-total', $param);
  386. if ($info >= 1) {
  387. $check = Dever::load('passport/code-one', $param);
  388. if ($check) {
  389. if (time() - $check['cdate'] < Dever::config('base', 'project')->mobileCode['time']) {
  390. Dever::alert('请不要在一分钟之内申请多次验证码,请您稍后再试');
  391. }
  392. }
  393. }
  394. $total = Dever::config('base', 'project')->mobileCode['total'];
  395. if ($info >= $total) {
  396. Dever::alert('很抱歉,您已经申请获取验证码超过' . $total . '次,今天您已经无法获取验证码了,请您明天再来');
  397. }
  398. $code = new Code();
  399. $code->createM();
  400. # 记录当前的验证码
  401. $insert['add_mobile'] = $mobile;
  402. $insert['add_day'] = $day;
  403. $insert['add_code'] = $code->mcode;
  404. $id = Dever::load('passport/code-insert', $insert);
  405. # 启动发送
  406. $this->send($mobile, $insert['add_code'], $id);
  407. $this->save->add(self::MCODE, $mobile . '_' . $code->mcode, Dever::config('base', 'project')->mobileCode['timeout']);
  408. return $code->mcode;
  409. }
  410. public function send($mobile, $code, $id = false)
  411. {
  412. if (Dever::project('sms')) {
  413. $send['skin'] = 'code';
  414. $send['mobile'] = $mobile;
  415. $send['param'] = array
  416. (
  417. 'code' => $code
  418. );
  419. $send['param'] = Dever::json_encode($send['param']);
  420. return Dever::load('sms/api.send', $send);
  421. }
  422. $url = Dever::config('base', 'project')->mobileCode['url'];
  423. if (!$url) {
  424. return;
  425. }
  426. $content = Dever::config('base', 'project')->mobileCode['body'];
  427. $content = $this->replace($content, $mobile, $code);
  428. parse_str($content, $param);
  429. $type = Dever::config('base', 'project')->mobileCode['method'];
  430. $json = Dever::config('base', 'project')->mobileCode['json'];
  431. $header = Dever::config('base', 'project')->mobileCode['header'];
  432. return Dever::curl($url, $param, $type, $json, $header);
  433. }
  434. private function replace($content, $mobile = '', $code = '')
  435. {
  436. $skin = Dever::config('base', 'project')->mobileCode['skin'];
  437. $skin_key = Dever::input('skin', 'code');
  438. if (isset($skin[$skin_key])) {
  439. $skin = $skin[$skin_key];
  440. } else {
  441. $skin = array_shift($skin);
  442. }
  443. $config = array('{code}', '{mobile}', '{sign}', '{skin}', '{param}');
  444. $replace = array($code, $mobile, Dever::config('base', 'project')->mobileCode['sign'], $skin);
  445. return str_replace($config, $replace, $content);
  446. }
  447. protected function refer($state = false)
  448. {
  449. $refer = Dever::input('refer');
  450. $project = 'main';
  451. if ($refer) {
  452. if (!strstr($refer, 'http://')) {
  453. $refer = Encrypt::decode($refer);
  454. }
  455. /*
  456. $url = parse_url(Encrypt::decode($refer));
  457. $url['path'] = preg_replace('/^\//', '', $url['path']);
  458. if (!isset($url['query'])) {
  459. $url['query'] = '';
  460. }
  461. if (Uri::$type == '?') {
  462. $refer = Dever::url(str_replace($url['path'] . Uri::$type, '', $url['query']), $project);
  463. } else {
  464. $refer = Dever::url($url['path'] . '?' . $url['query'], $project);
  465. }
  466. */
  467. } else {
  468. $refer = Dever::url('home', $project);
  469. }
  470. $param = Dever::input('param');
  471. if ($param) {
  472. $refer .= '&' . $param;
  473. }
  474. if ($state) {
  475. return $refer;
  476. }
  477. Dever::out($refer);
  478. }
  479. protected function save($user)
  480. {
  481. if (is_numeric($user)) {
  482. $user = Dever::load('passport/user-one', $user);
  483. }
  484. if ($user && is_array($user)) {
  485. if (Dever::mobile()) {
  486. $time = 30;
  487. } else {
  488. $time = 7;
  489. }
  490. $this->save->add(self::NAME, $user, 3600 * 24 * $time);
  491. $user['signature'] = Dever::login($user['id']);
  492. if (Dever::config('base', 'project')->regAction) {
  493. Dever::load(Dever::config('base', 'project')->regAction, $user);
  494. }
  495. return $user;
  496. }
  497. }
  498. public function createRefer()
  499. {
  500. return 'refer=' . Encrypt::encode(Dever::url(false, 'main'));
  501. }
  502. protected function saveSex($sex)
  503. {
  504. if ($sex || $sex == 0) {
  505. $config_sex = Dever::config('base', 'project')->sex;
  506. if (isset($config_sex[$sex])) {
  507. $sex = $config_sex[$sex];
  508. } else {
  509. if ($sex == '男') {
  510. $sex = 1;
  511. } elseif ($sex == '女') {
  512. $sex = 2;
  513. } elseif ($sex == '未知') {
  514. $sex = 3;
  515. }
  516. }
  517. }
  518. return $sex;
  519. }
  520. protected function saveAvatar($value)
  521. {
  522. if (!$value) {
  523. return '';
  524. }
  525. $value = Dever::pic($value);
  526. $base = Dever::data();
  527. $date = explode('-', date("Y-m-d"));
  528. if (is_array($value)) {
  529. $user = $value[1];
  530. $value = $value[0];
  531. if (is_array($value)) {
  532. $name = md5($value['tmp_name'] . rand(0, 100) . microtime());
  533. $path = 'avatar/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
  534. $ext = '.jpg';
  535. $temp = Dever::path($base, $path . $name . $ext);
  536. move_uploaded_file($value['tmp_name'], $temp);
  537. $value = $temp;
  538. }
  539. }
  540. //$result = Dever::$global['host']['img'] . 'chead.jpg';
  541. if ($value) {
  542. # 裁图
  543. $handle = new Img();
  544. $size = '200_200_2';
  545. $user = isset($user) && $user ? $user : $this->init();
  546. if ($user && isset($user['id']) && $user['id']) {
  547. $name = md5($user['id']);
  548. } else {
  549. $name = md5($value . rand(0, 100) . microtime());
  550. }
  551. $path = 'avatar/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
  552. $ext = '.jpg';
  553. $file = Dever::path($base, $path . $name . $ext);
  554. $temp = $file . '.temp.jpg';
  555. //$value = Dever::curl($value);
  556. //file_put_contents($temp, file_get_contents($value));
  557. $file = $handle->thumb($value, $size, true, $file);
  558. if ($file) {
  559. $result = str_replace('/upload/', '/', Dever::config('host')->uploadRes . $path . $name . $ext);
  560. } else {
  561. $result = $value;
  562. }
  563. }
  564. return $result;
  565. }
  566. }