UserController.php 23 KB

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