Base.php 22 KB

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