UserController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. <?php
  2. /**
  3. * 用户登录相关
  4. * User: benzhan
  5. * Date: 2018/10/18
  6. * Time: 10:41
  7. */
  8. class UserController extends BaseController {
  9. protected $ajaxLoginActions = [
  10. 'eosBind',
  11. 'eosBind2',
  12. 'ethBind',
  13. 'tronBind',
  14. 'tgBind',
  15. 'tgBind2',
  16. 'unbind',
  17. 'changeUserName',
  18. 'changeNickName',
  19. 'changePhoto',
  20. 'setVisible',
  21. ];
  22. public function __construct() {
  23. parent::__construct(false);
  24. }
  25. /**
  26. * 获取随机数【不需要登录】
  27. * @author benzhan
  28. */
  29. public function actionRandom($args) {
  30. $rules = [
  31. 'account' => ['string', 'reg' => '/^([a-z0-9\.]{1,13})$|^(0x[a-z0-9]{40})$|^[a-zA-Z0-9]{34}$/i', 'desc' => 'eos账号|eth账号|tron账号'],
  32. ];
  33. Param::checkParam2($rules, $args);
  34. $random = Account::getRandom($args['account']);
  35. Response::success($random);
  36. }
  37. /**
  38. * 获取随机数【不需要登录】
  39. * @author benzhan
  40. */
  41. public function actionRandom2($args) {
  42. $rules = [
  43. 'account' => ['string', 'reg' => '/^([a-z0-9\.]{1,13})$|^(0x[a-z0-9]{40})$|^[a-zA-Z0-9]{34}$/i', 'desc' => 'eos账号|eth账号'],
  44. ];
  45. Param::checkParam2($rules, $args);
  46. $random = Account::getRandom($args['account'], true);
  47. Response::success($random);
  48. }
  49. private function _getUserBaseInfo($user_id) {
  50. $objUserInfo = new TableHelper('user_info', 'dw_chat');
  51. $row = $objUserInfo->getRow(['user_id' => $user_id], ['_field' => 'user_id, user_name, nick_name, cover_photo']);
  52. return $row;
  53. }
  54. /**
  55. * Eos账号登录【不需要登录】
  56. * @author benzhan
  57. */
  58. public function actionEosLogin($args, $orginStr = null) {
  59. $rules = [
  60. 'account' => ['string', 'reg' => '/^[a-z0-9\.]{1,13}$/', 'desc' => 'eos账号'],
  61. 'pubkey' => ['string', 'nullable' => true, '公钥'],
  62. 'authority' => ['string', 'nullable' => true, 'enum' => ['owner', 'active'], '权限'],
  63. 'sign' => ['string', 'desc' => '私钥加密后的签名']
  64. ];
  65. Param::checkParam2($rules, $args);
  66. $info = User::getInfo();
  67. if ($info) {
  68. $info['info'] = $this->_getUserBaseInfo($info['user_id']);
  69. return $info;
  70. }
  71. if (!$args['pubkey'] && $args['authority']) {
  72. $args['pubkey'] = $this->_getPublicKey($args['account'], $args['authority']);
  73. }
  74. if (!$args['pubkey']) {
  75. Response::error(CODE_PARAM_ERROR, 'pubkey is empty.');
  76. }
  77. $account = $args['account'];
  78. if ($orginStr) {
  79. $data = $orginStr;
  80. } else {
  81. $data = Account::getRandom($account);
  82. if (!$data) {
  83. Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
  84. }
  85. }
  86. $flag = false;
  87. try {
  88. $flag = Account::verifyMsg($args['pubkey'], $account, $data, $args['sign']);
  89. } catch (Exception $e) {
  90. Response::error($e->getCode(), $e->getMessage());
  91. }
  92. if ($flag) {
  93. $user_id = User::login($args['account'], Account::TYPE_EOS);
  94. $info = Account::setCookie($user_id);
  95. $info['info'] = $this->_getUserBaseInfo($info['user_id']);
  96. return $info;
  97. } else {
  98. if ($orginStr) {
  99. return false;
  100. } else {
  101. Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
  102. }
  103. }
  104. }
  105. /**
  106. * 校验登录 (合约方式
  107. * @author solu
  108. * @param $args
  109. * @return array
  110. */
  111. public function actionEosLogin2($args) {
  112. $rules = [
  113. 'account' => ['string', 'reg' => '/^[a-z0-9\.]{1,13}$/', 'desc' => 'eos账号'],
  114. 'access_token' => ['string', 'len' => 36, 'desc' => 'access_token'],
  115. 'trx_id' => ['string', 'desc' => '交易id'],
  116. 'group_id' => ['int', 'nullable' => true, 'desc' => '群id'],
  117. ];
  118. Param::checkParam2($rules, $args);
  119. $objSync = new Sync_LoginLog();
  120. $objLoginLog = new TableHelper('login_log', 'dw_chat');
  121. $_where = ['trx_id' => $args['trx_id']];
  122. $row = $objLoginLog->getRow($_where);
  123. $maxTry = 3;
  124. $i = 0;
  125. // 没有数据请求同步再尝试
  126. while ($i < $maxTry && !$row) {
  127. sleep($i + 1);
  128. $objSync->pubSubscribe();
  129. $row = $objLoginLog->getRow($_where);
  130. $i++;
  131. }
  132. if (!$row) {
  133. Response::error(CODE_NORMAL_ERROR, 'trx_id not in log!');
  134. }
  135. $account = $args['account'];
  136. $random = Account::getRandom($account);
  137. if (!$random) {
  138. Response::error(CODE_NORMAL_ERROR, 'can not find random data, please refresh.');
  139. }
  140. if ($row['account'] != $account) {
  141. Response::error(CODE_NORMAL_ERROR, 'account unmatch');
  142. }
  143. if ($random != $row['memo']) {
  144. Response::error(CODE_NORMAL_ERROR, 'random unmatch');
  145. }
  146. $data = [];
  147. try {
  148. $data = Account::verifyPlayer($row['account'], $random, $args['access_token']);
  149. } catch (Exception $e) {
  150. Response::error($e->getCode(), $e->getMessage());
  151. }
  152. // 带群id检测是否加入群
  153. if ($args['group_id']) {
  154. try {
  155. (new GroupInfo())->joinGroup($data['user_id'], $args['group_id']);
  156. } catch (Exception $e) {
  157. var_log($e->getMessage());
  158. }
  159. }
  160. return $data;
  161. }
  162. private function _getPublicKey($account, $authority) {
  163. $accountInfo = EosBase::getAccount($account);
  164. $accountInfo = json_decode($accountInfo, true);
  165. foreach ($accountInfo['permissions'] as $p) {
  166. if ($p['perm_name'] == $authority) {
  167. return $p['required_auth']['keys'][0]['key'];
  168. }
  169. }
  170. return null;
  171. }
  172. /**
  173. * Eos账号绑定
  174. * @author benzhan
  175. */
  176. public function actionEosBind($args) {
  177. $rules = [
  178. 'account' => ['string', 'reg' => '/^[a-z0-9\.]{1,13}$/', 'desc' => 'eos账号'],
  179. 'pubkey' => ['string', '公钥'],
  180. 'sign' => ['string', 'desc' => '私钥加密后的签名'],
  181. ];
  182. Param::checkParam2($rules, $args);
  183. $account = $args['account'];
  184. $data = Account::getRandom($account);
  185. if (!$data) {
  186. Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
  187. }
  188. $flag = Account::verifyMsg($args['pubkey'], $account, $data, $args['sign']);
  189. if ($flag) {
  190. $user_id = User::getUserId();
  191. User::bind($user_id, $args['account'], Account::TYPE_EOS);
  192. } else {
  193. Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
  194. }
  195. }
  196. /**
  197. * Eos账号绑定(合约方式
  198. * @author solu
  199. * @author benzhan
  200. */
  201. public function actionEosBind2($args) {
  202. $rules = [
  203. 'account' => ['string', 'reg' => '/^[a-z0-9\.]{1,13}$/', 'desc' => 'eos账号'],
  204. 'access_token' => ['string', 'len' => 36, 'desc' => 'access_token'],
  205. 'trx_id' => ['string', 'desc' => '交易id'],
  206. ];
  207. Param::checkParam2($rules, $args);
  208. $objSync = new Sync_LoginLog();
  209. $objLoginLog = new TableHelper('login_log', 'dw_chat');
  210. $_where = ['trx_id' => $args['trx_id']];
  211. $row = $objLoginLog->getRow($_where);
  212. $maxTry = 3;
  213. $i = 0;
  214. // 没有数据请求同步再尝试
  215. while ($i < $maxTry && !$row) {
  216. sleep($i + 1);
  217. $objSync->pubSubscribe();
  218. $row = $objLoginLog->getRow($_where);
  219. $i++;
  220. }
  221. if (!$row) {
  222. Response::error(CODE_NORMAL_ERROR, 'trx_id not in log!');
  223. }
  224. $account = $args['account'];
  225. $random = Account::getRandom($account);
  226. if (!$random) {
  227. Response::error(CODE_NORMAL_ERROR, 'can not find random data, please refresh.');
  228. }
  229. if ($row['account'] != $account) {
  230. Response::error(CODE_NORMAL_ERROR, 'account unmatch');
  231. }
  232. if ($random != $row['memo']) {
  233. Response::error(CODE_NORMAL_ERROR, 'random unmatch');
  234. }
  235. try {
  236. Account::verifyEosOnly($account, $random, $args['access_token']);
  237. } catch (Exception $e) {
  238. Response::error($e->getCode(), $e->getMessage());
  239. }
  240. $user_id = User::getUserId();
  241. User::bind($user_id, $account, Account::TYPE_EOS);
  242. }
  243. /**
  244. * Eth账号登录【不需要登录】
  245. * @author solu
  246. */
  247. public function actionEthLogin($args) {
  248. $rules = [
  249. 'account' => ['string', 'reg' => '/^0x[a-z0-9]{40}$/i', 'desc' => 'eos账号'],
  250. 'sign' => ['string', 'desc' => '私钥加密后的签名']
  251. ];
  252. Param::checkParam2($rules, $args);
  253. $info = User::getInfo();
  254. if ($info) {
  255. return $info;
  256. }
  257. $account = $args['account'];
  258. $data = Account::getRandom($account);
  259. if (!$data) {
  260. Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
  261. }
  262. $flag = false;
  263. try {
  264. $flag = Account::verifyEth($args['account'], $data, $args['sign']);
  265. } catch (Exception $e) {}
  266. if (!$flag) {
  267. Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
  268. }
  269. $user_id = User::login($args['account'], Account::TYPE_ETH);
  270. return Account::setCookie($user_id);
  271. }
  272. /**
  273. * ETH账号绑定
  274. * @author solu
  275. * @param $args
  276. */
  277. public function actionEthBind($args) {
  278. $rules = [
  279. 'account' => ['string', 'reg' => '/^0x[a-z0-9]{40}$/i', 'desc' => 'eos账号'],
  280. 'sign' => ['string', 'desc' => '私钥加密后的签名']
  281. ];
  282. Param::checkParam2($rules, $args);
  283. $account = $args['account'];
  284. $data = Account::getRandom($account);
  285. if (!$data) {
  286. Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
  287. }
  288. $flag = false;
  289. try {
  290. $flag = Account::verifyEth($args['account'], $data, $args['sign']);
  291. } catch (Exception $e) {}
  292. if (!$flag) {
  293. Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
  294. }
  295. $user_id = User::getUserId();
  296. User::bind($user_id, $account, Account::TYPE_ETH);
  297. }
  298. /**
  299. * Tron账号登录【不需要登录】
  300. * @param $args
  301. * @return array
  302. */
  303. public function actionTronLogin($args) {
  304. $rules = [
  305. 'account' => ['string', 'reg' => '/^[a-zA-Z0-9]{34}$/', 'desc' => 'tron账号'],
  306. 'sign' => ['string', 'desc' => '私钥加密后的签名']
  307. ];
  308. Param::checkParam2($rules, $args);
  309. $info = User::getInfo();
  310. if ($info) {
  311. return $info;
  312. }
  313. $account = $args['account'];
  314. $data = Account::getRandom($account);
  315. if (!$data) {
  316. Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
  317. }
  318. $flag = false;
  319. try {
  320. $flag = Account::verifyTron($args['account'], $data, $args['sign']);
  321. } catch (Exception $e) {}
  322. if (!$flag) {
  323. Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
  324. }
  325. $user_id = User::login($args['account'], Account::TYPE_TRON);
  326. return Account::setCookie($user_id);
  327. }
  328. /**
  329. * Tron账号绑定
  330. * @author solu
  331. * @param $args
  332. */
  333. public function actionTronBind($args) {
  334. $rules = [
  335. 'account' => ['string', 'reg' => '/^[a-zA-Z0-9]{34}$/', 'desc' => 'tron账号'],
  336. 'sign' => ['string', 'desc' => '私钥加密后的签名']
  337. ];
  338. Param::checkParam2($rules, $args);
  339. $account = $args['account'];
  340. $data = Account::getRandom($account);
  341. if (!$data) {
  342. Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
  343. }
  344. $flag = false;
  345. try {
  346. $flag = Account::verifyTron($args['account'], $data, $args['sign']);
  347. } catch (Exception $e) {}
  348. if (!$flag) {
  349. Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
  350. }
  351. $user_id = User::getUserId();
  352. User::bind($user_id, $account, Account::TYPE_TRON);
  353. }
  354. /**
  355. * 解绑账号
  356. * @author solu
  357. * @param $args
  358. */
  359. public function actionUnbind($args) {
  360. $typeRule = array_keys(Account::getAllType());
  361. $rules = [
  362. 'type' => ['string', 'enum' => $typeRule, 'desc' => '类型 eos,eth, tron'],
  363. ];
  364. Param::checkParam2($rules, $args);
  365. $userId = User::getUserId();
  366. try {
  367. User::unbind($userId, $args['type']);
  368. } catch (Exception $e) {
  369. Response::error($e->getCode(), $e->getMessage());
  370. }
  371. }
  372. /**
  373. * 修改用户名【只能修改一次】
  374. * @author benzhan
  375. */
  376. public function actionChangeUserName($args) {
  377. $rules = [
  378. 'user_name' => ['string', 'reg' => '/^[a-zA-Z_0-9]{5,20}$/i', 'desc' => '用户名'],
  379. ];
  380. Param::checkParam2($rules, $args);
  381. $user_id = User::getUserId();
  382. User::saveInfo($user_id, $args);
  383. }
  384. /**
  385. * 修改昵称
  386. * @author benzhan
  387. */
  388. public function actionChangeNickName($args) {
  389. $rules = [
  390. 'nick_name' => ['string', 'len' => [1, 20], 'desc' => '用户名'],
  391. ];
  392. Param::checkParam2($rules, $args);
  393. $user_id = User::getUserId();
  394. User::saveInfo($user_id, $args);
  395. User::setUserNameById($user_id, $args['nick_name']);
  396. }
  397. /**
  398. * 修改头像
  399. * @author benzhan
  400. */
  401. public function actionChangePhoto($args) {
  402. $args = array_merge($args, $_FILES);
  403. $rules = [
  404. 'cover_photo' => ['array', 'desc' => '头像文件'],
  405. ];
  406. Param::checkParam2($rules, $args);
  407. $file = $args['cover_photo'];
  408. $cover_photo = '';
  409. try {
  410. $cover_photo = (new FileUrl())->getFileUrl($file['tmp_name'], $file['name'], $file['type'], true);
  411. } catch (Exception $e) {
  412. Response::error($e->getCode(), $e->getMessage());
  413. }
  414. $user_id = User::getUserId();
  415. User::saveInfo($user_id, compact('cover_photo'));
  416. }
  417. /**
  418. * 第三方账号的是否可见
  419. * @author benzhan
  420. */
  421. public function actionSetVisible($args) {
  422. $types = array_keys(Account::getAllType());
  423. $rules = [
  424. 'type' => ['string', 'enum' => $types, 'desc' => '类型'],
  425. 'is_visible' => ['int', 'desc' => '是否可见'],
  426. ];
  427. Param::checkParam2($rules, $args);
  428. $user_id = User::getUserId();
  429. $type = $args['type'];
  430. $is_visible = (int) $args['is_visible'];
  431. $objUserBindInfo = new TableHelper('user_bind_info', 'dw_chat');
  432. $objUserBindInfo->updateObject(compact('is_visible'), compact('user_id', 'type'));
  433. }
  434. /**
  435. * 用户信息接口
  436. * @author solu
  437. * @param $args
  438. * @return array
  439. */
  440. public function actionInfo($args) {
  441. $rules = [
  442. 'target_id' => ['int', 'desc' => '用户id'],
  443. 'group_id' => ['int', 'nullable' => true, '群id'],
  444. ];
  445. Param::checkParam2($rules, $args);
  446. $self = User::getUserId();
  447. $userId = (int)$args['target_id'];
  448. $groupId = (int)$args['group_id'];
  449. // 如果是自己调用,则刷新token的有效期
  450. if ($userId == $self) {
  451. Account::refreshToken();
  452. }
  453. return User::getUserInfo($userId, $self, $groupId);
  454. }
  455. /**
  456. * Simplewallet 协议登录
  457. * @param $args
  458. * @param string $authority
  459. *
  460. * @return array|bool|null
  461. */
  462. public function actionSimplewallet($args, $authority = 'active') {
  463. if (!$args) {
  464. $json = file_get_contents('php://input');
  465. $data = json_decode($json, true);
  466. $args += $data;
  467. }
  468. $rules = [
  469. 'account' => ['string', 'desc' => 'Eos账号'],
  470. 'chainId' => ['string', 'desc' => '链id'],
  471. 'protocol' => ['string', 'desc' => '协议', 'enum' => ['SimpleWallet']],
  472. 'ref' => ['string', 'desc' => '来源'],
  473. 'sign' => ['string', 'desc' => '签名'],
  474. 'timestamp' => ['string', 'desc' => '时间戳'],
  475. 'uuID' => ['string', 'desc' => '随机数'],
  476. 'version' => ['string', 'desc' => '版本号'],
  477. ];
  478. Param::checkParam($rules, $args);
  479. $pubkey = $this->_getPublicKey($args['account'], $authority);
  480. $newArgs = [
  481. 'account' => $args['account'],
  482. 'pubkey' => $pubkey,
  483. 'authority' => $authority,
  484. 'sign' => $args['sign'],
  485. ];
  486. $orginStr = "{$args['timestamp']}{$args['account']}{$args['uuID']}{$args['ref']}";
  487. $info = $this->actionEosLogin($newArgs, $orginStr);
  488. if ($info) {
  489. $objRedis = dwRedis::init();
  490. $info['account'] = $args['account'];
  491. $uuid = $this->_getUuid($args['uuID']);
  492. $objRedis->setex($uuid, 60, json_encode($info));
  493. } else if ($authority == 'active') {
  494. // 再尝试一次 owner
  495. $this->actionSimplewallet($args, 'owner');
  496. }
  497. }
  498. /**
  499. * Simplewallet 协议登录
  500. * @param $args
  501. * @param string $authority
  502. *
  503. * @return array|bool|null
  504. */
  505. public function actionSimplewalletCheck($args) {
  506. $rules = [
  507. 'uuID' => ['string', 'desc' => '随机数']
  508. ];
  509. Param::checkParam($rules, $args);
  510. $objRedis = dwRedis::init();
  511. $uuid = $this->_getUuid($args['uuID']);
  512. $json = $objRedis->get($uuid);
  513. if ($json) {
  514. $info = json_decode($json, true);
  515. return $info;
  516. } else {
  517. Response::error(CODE_NORMAL_ERROR, 'no found');
  518. }
  519. }
  520. private function _getUuid($uuid) {
  521. return "globals:simple_wallet:{$uuid}";
  522. }
  523. /**
  524. * Telegram登录
  525. * @author solu
  526. * @param $args
  527. * @return array
  528. * @ignore
  529. */
  530. public function actionTgLogin($args) {
  531. $rules = [
  532. 'id' => ['int', 'desc' => 'telegram user id'],
  533. 'first_name' => 'string',
  534. 'last_name' => ['string', 'nullable' => true],
  535. 'auth_date' => ['int', 'desc' => 'timestamp'],
  536. 'hash' => ['string', 'desc' => 'verify hash'],
  537. ];
  538. Param::checkParam2($rules, $args);
  539. $data = [];
  540. try {
  541. $data = ThirdApi::checkTelegramAuthorization($args);
  542. } catch (Exception $e) {
  543. Response::error(CODE_SIGN_ERROR, $e->getMessage());
  544. }
  545. $name = $data['first_name'];
  546. $args['last_name'] && $name .= "_{$args['last_name']}";
  547. $user_id = User::login($data['id'], Account::TYPE_TG, $name);
  548. return Account::setCookie($user_id);
  549. }
  550. /**
  551. * Telegram绑定
  552. * @author solu
  553. * @param $args
  554. * @ignore
  555. */
  556. public function actionTgBind($args) {
  557. $rules = [
  558. 'id' => ['int', 'desc' => 'telegram user id'],
  559. 'first_name' => 'string',
  560. 'last_name' => ['string', 'nullable' => true],
  561. 'auth_date' => ['int', 'desc' => 'timestamp'],
  562. 'hash' => ['string', 'desc' => 'verify hash'],
  563. ];
  564. Param::checkParam2($rules, $args);
  565. $data = [];
  566. try {
  567. $data = ThirdApi::checkTelegramAuthorization($args);
  568. } catch (Exception $e) {
  569. Response::error(CODE_SIGN_ERROR, $e->getMessage());
  570. }
  571. $user_id = User::getUserId();
  572. User::bind($user_id, $data['id'], Account::TYPE_TG);
  573. }
  574. /**
  575. * Telegram登录token
  576. * @author solu
  577. * @param $args
  578. * @return array
  579. */
  580. public function actionTgCSRF($args) {
  581. $rules = [
  582. 'type' => ['string', 'enum' => ['login', 'bind'], 'desc' => '类型'],
  583. ];
  584. Param::checkParam2($rules, $args);
  585. $csrf_token = $args['type'] . '-' . uuid();
  586. Telegram::initCSRF($csrf_token);
  587. $url = BOT_CHAT_URL . "?start={$csrf_token}";
  588. return compact('csrf_token', 'url');
  589. }
  590. /**
  591. * Telegram登录(窗口start模式
  592. * @param $args
  593. * @return array
  594. */
  595. public function actionTgLogin2($args) {
  596. $rules = [
  597. 'csrf_token' => 'string',
  598. ];
  599. Param::checkParam2($rules, $args);
  600. $status = Telegram::getCSRFStatus($args['csrf_token']);
  601. $user_id = 0;
  602. $token = '';
  603. if ($status > 0) { // 用户已在Telegram确定
  604. $userData = Account::setCookie($status);
  605. $user_id = $userData['user_id'];
  606. $token = $userData['token'];
  607. }
  608. return compact('status', 'user_id', 'token');
  609. }
  610. /**
  611. * Telegram绑定 (窗口start模式
  612. * @param $args
  613. * @return array
  614. */
  615. public function actionTgBind2($args) {
  616. $rules = [
  617. 'csrf_token' => 'string',
  618. ];
  619. Param::checkParam2($rules, $args);
  620. $user_id = User::getUserId();
  621. $status = Telegram::getCSRFStatus($args['csrf_token']);
  622. if ($status > 0) { // 用户已在Telegram确定
  623. User::bind($user_id, $status, Account::TYPE_TG);
  624. Telegram::setUserByTG($status, $user_id);
  625. }
  626. return compact('status');
  627. }
  628. /**
  629. * 校验登录态
  630. * @author solu
  631. * @param $args
  632. * @return array
  633. */
  634. public function actionCheckLogin($args) {
  635. $rules = [
  636. 'user_id' => 'int',
  637. 'token' => 'string',
  638. ];
  639. Param::checkParam2($rules, $args);
  640. $user_id = User::getUserId();
  641. $is_login = $user_id > 0;
  642. return compact('is_login');
  643. }
  644. }