Base.php 23 KB

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