UserController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. 'ethBind',
  12. 'tronBind',
  13. 'unbind',
  14. 'changeUserName',
  15. 'changeNickName',
  16. 'changePhoto',
  17. 'setVisible',
  18. ];
  19. public function __construct() {
  20. parent::__construct(false);
  21. }
  22. /**
  23. * 获取随机数【不需要登录】
  24. * @author benzhan
  25. */
  26. public function actionRandom($args) {
  27. $rules = [
  28. 'account' => ['string', 'reg' => '/^([a-z0-9\.]{1,13})$|^(0x[a-z0-9]{40})$|^[a-zA-Z0-9]{34}$/i', 'desc' => 'eos账号|eth账号|tron账号'],
  29. ];
  30. Param::checkParam2($rules, $args);
  31. $random = Account::getRandom($args['account']);
  32. Response::success($random);
  33. }
  34. /**
  35. * 获取随机数【不需要登录】
  36. * @author benzhan
  37. */
  38. public function actionRandom2($args) {
  39. $rules = [
  40. 'account' => ['string', 'reg' => '/^([a-z0-9\.]{1,13})$|^(0x[a-z0-9]{40})$|^[a-zA-Z0-9]{34}$/i', 'desc' => 'eos账号|eth账号'],
  41. ];
  42. Param::checkParam2($rules, $args);
  43. $random = Account::getRandom($args['account'], true);
  44. Response::success($random);
  45. }
  46. private function _getUserBaseInfo($user_id) {
  47. $objUserInfo = new TableHelper('user_info', 'dw_chat');
  48. $row = $objUserInfo->getRow(['user_id' => $user_id], ['_field' => 'user_id, user_name, nick_name, cover_photo']);
  49. return $row;
  50. }
  51. /**
  52. * Eos账号登录【不需要登录】
  53. * @author benzhan
  54. */
  55. public function actionEosLogin($args) {
  56. $rules = [
  57. 'account' => ['string', 'reg' => '/^[a-z0-9\.]{1,13}$/', 'desc' => 'eos账号'],
  58. 'pubkey' => ['string', 'nullable' => true, '公钥'],
  59. 'authority' => ['string', 'nullable' => true, 'enum' => ['owner', 'active'], '权限'],
  60. 'sign' => ['string', 'desc' => '私钥加密后的签名']
  61. ];
  62. Param::checkParam2($rules, $args);
  63. $info = User::getInfo();
  64. if ($info) {
  65. $info['info'] = $this->_getUserBaseInfo($info['user_id']);
  66. return $info;
  67. }
  68. if (!$args['pubkey'] && $args['authority']) {
  69. $args['pubkey'] = $this->_getPublicKey($args['account'], $args['authority']);
  70. }
  71. if (!$args['pubkey']) {
  72. Response::error(CODE_PARAM_ERROR, 'pubkey is empty.');
  73. }
  74. $account = $args['account'];
  75. $data = Account::getRandom($account);
  76. if (!$data) {
  77. Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
  78. }
  79. $flag = false;
  80. try {
  81. $flag = Account::verifyMsg($args['pubkey'], $account, $data, $args['sign']);
  82. } catch (Exception $e) {
  83. Response::error($e->getCode(), $e->getMessage());
  84. }
  85. if ($flag) {
  86. $user_id = User::login($args['account'], Account::TYPE_EOS);
  87. $info = Account::setCookie($user_id);
  88. $info['info'] = $this->_getUserBaseInfo($info['user_id']);
  89. return $info;
  90. } else {
  91. Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
  92. }
  93. }
  94. /**
  95. * 校验登录 (合约方式
  96. * @author solu
  97. * @param $args
  98. * @return array
  99. */
  100. public function actionEosLogin2($args) {
  101. $rules = [
  102. 'account' => ['string', 'reg' => '/^[a-z0-9\.]{1,13}$/', 'desc' => 'eos账号'],
  103. 'access_token' => ['string', 'len' => 36, 'desc' => 'access_token'],
  104. 'trx_id' => ['string', 'desc' => '交易id'],
  105. 'group_id' => ['int', 'nullable' => true, 'desc' => '群id'],
  106. ];
  107. Param::checkParam2($rules, $args);
  108. $objSync = new Sync_LoginLog();
  109. $objLoginLog = new TableHelper('login_log', 'dw_chat');
  110. $_where = ['trx_id' => $args['trx_id']];
  111. $row = $objLoginLog->getRow($_where);
  112. $maxTry = 3;
  113. $i = 0;
  114. // 没有数据请求同步再尝试
  115. while ($i < $maxTry && !$row) {
  116. sleep($i + 1);
  117. $objSync->pubSubscribe();
  118. $row = $objLoginLog->getRow($_where);
  119. $i++;
  120. }
  121. if (!$row) {
  122. Response::error(CODE_NORMAL_ERROR, 'trx_id not in log!');
  123. }
  124. $account = $args['account'];
  125. $random = Account::getRandom($account);
  126. if (!$random) {
  127. Response::error(CODE_NORMAL_ERROR, 'can not find random data, please refresh.');
  128. }
  129. if ($row['account'] != $account) {
  130. Response::error(CODE_NORMAL_ERROR, 'account unmatch');
  131. }
  132. if ($random != $row['memo']) {
  133. Response::error(CODE_NORMAL_ERROR, 'random unmatch');
  134. }
  135. $data = [];
  136. try {
  137. $data = Account::verifyPlayer($row['account'], $random, $args['access_token']);
  138. } catch (Exception $e) {
  139. Response::error($e->getCode(), $e->getMessage());
  140. }
  141. // 带群id检测是否加入群
  142. if ($args['group_id']) {
  143. try {
  144. (new GroupInfo())->joinGroup($data['user_id'], $args['group_id']);
  145. } catch (Exception $e) {
  146. var_log($e->getMessage());
  147. }
  148. }
  149. return $data;
  150. }
  151. private function _getPublicKey($account, $authority) {
  152. $accountInfo = EosBase::getAccount($account);
  153. $accountInfo = json_decode($accountInfo, true);
  154. foreach ($accountInfo['permissions'] as $p) {
  155. if ($p['perm_name'] == $authority) {
  156. return $p['required_auth']['keys'][0]['key'];
  157. }
  158. }
  159. return null;
  160. }
  161. /**
  162. * Eos账号绑定
  163. * @author benzhan
  164. */
  165. public function actionEosBind($args) {
  166. $rules = [
  167. 'account' => ['string', 'reg' => '/^[a-z0-9\.]{1,13}$/', 'desc' => 'eos账号'],
  168. 'pubkey' => ['string', '公钥'],
  169. 'sign' => ['string', 'desc' => '私钥加密后的签名'],
  170. ];
  171. Param::checkParam2($rules, $args);
  172. $account = $args['account'];
  173. $data = Account::getRandom($account);
  174. if (!$data) {
  175. Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
  176. }
  177. $flag = Account::verifyMsg($args['pubkey'], $account, $data, $args['sign']);
  178. if ($flag) {
  179. $user_id = User::getUserId();
  180. User::bind($user_id, $args['account'], Account::TYPE_EOS);
  181. } else {
  182. Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
  183. }
  184. }
  185. /**
  186. * Eth账号登录【不需要登录】
  187. * @author solu
  188. */
  189. public function actionEthLogin($args) {
  190. $rules = [
  191. 'account' => ['string', 'reg' => '/^0x[a-z0-9]{40}$/i', 'desc' => 'eos账号'],
  192. 'sign' => ['string', 'desc' => '私钥加密后的签名']
  193. ];
  194. Param::checkParam2($rules, $args);
  195. $info = User::getInfo();
  196. if ($info) {
  197. return $info;
  198. }
  199. $account = $args['account'];
  200. $data = Account::getRandom($account);
  201. if (!$data) {
  202. Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
  203. }
  204. $flag = false;
  205. try {
  206. $flag = Account::verifyEth($args['account'], $data, $args['sign']);
  207. } catch (Exception $e) {}
  208. if (!$flag) {
  209. Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
  210. }
  211. $user_id = User::login($args['account'], Account::TYPE_ETH);
  212. return Account::setCookie($user_id);
  213. }
  214. /**
  215. * ETH账号绑定
  216. * @author solu
  217. * @param $args
  218. */
  219. public function actionEthBind($args) {
  220. $rules = [
  221. 'account' => ['string', 'reg' => '/^0x[a-z0-9]{40}$/i', 'desc' => 'eos账号'],
  222. 'sign' => ['string', 'desc' => '私钥加密后的签名']
  223. ];
  224. Param::checkParam2($rules, $args);
  225. $account = $args['account'];
  226. $data = Account::getRandom($account);
  227. if (!$data) {
  228. Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
  229. }
  230. $flag = false;
  231. try {
  232. $flag = Account::verifyEth($args['account'], $data, $args['sign']);
  233. } catch (Exception $e) {}
  234. if (!$flag) {
  235. Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
  236. }
  237. $user_id = User::getUserId();
  238. User::bind($user_id, $account, Account::TYPE_ETH);
  239. }
  240. /**
  241. * Tron账号登录【不需要登录】
  242. * @param $args
  243. * @return array
  244. */
  245. public function actionTronLogin($args) {
  246. $rules = [
  247. 'account' => ['string', 'reg' => '/^[a-zA-Z0-9]{34}$/', 'desc' => 'tron账号'],
  248. 'sign' => ['string', 'desc' => '私钥加密后的签名']
  249. ];
  250. Param::checkParam2($rules, $args);
  251. $info = User::getInfo();
  252. if ($info) {
  253. return $info;
  254. }
  255. $account = $args['account'];
  256. $data = Account::getRandom($account);
  257. if (!$data) {
  258. Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
  259. }
  260. $flag = false;
  261. try {
  262. $flag = Account::verifyTron($args['account'], $data, $args['sign']);
  263. } catch (Exception $e) {}
  264. if (!$flag) {
  265. Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
  266. }
  267. $user_id = User::login($args['account'], Account::TYPE_TRON);
  268. return Account::setCookie($user_id);
  269. }
  270. /**
  271. * Tron账号绑定
  272. * @author solu
  273. * @param $args
  274. */
  275. public function actionTronBind($args) {
  276. $rules = [
  277. 'account' => ['string', 'reg' => '/^[a-zA-Z0-9]{34}$/', 'desc' => 'tron账号'],
  278. 'sign' => ['string', 'desc' => '私钥加密后的签名']
  279. ];
  280. Param::checkParam2($rules, $args);
  281. $account = $args['account'];
  282. $data = Account::getRandom($account);
  283. if (!$data) {
  284. Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
  285. }
  286. $flag = false;
  287. try {
  288. $flag = Account::verifyTron($args['account'], $data, $args['sign']);
  289. } catch (Exception $e) {}
  290. if (!$flag) {
  291. Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
  292. }
  293. $user_id = User::getUserId();
  294. User::bind($user_id, $account, Account::TYPE_TRON);
  295. }
  296. /**
  297. * 解绑账号
  298. * @author solu
  299. * @param $args
  300. */
  301. public function actionUnbind($args) {
  302. $typeRule = array_keys(Account::getAllType());
  303. $rules = [
  304. 'type' => ['string', 'enum' => $typeRule, 'desc' => '类型 eos,eth, tron'],
  305. ];
  306. Param::checkParam2($rules, $args);
  307. $userId = User::getUserId();
  308. try {
  309. User::unbind($userId, $args['type']);
  310. } catch (Exception $e) {
  311. Response::error($e->getCode(), $e->getMessage());
  312. }
  313. }
  314. /**
  315. * 修改用户名【只能修改一次】
  316. * @author benzhan
  317. */
  318. public function actionChangeUserName($args) {
  319. $rules = [
  320. 'user_name' => ['string', 'reg' => '/^[a-zA-Z_0-9]{5,20}$/i', 'desc' => '用户名'],
  321. ];
  322. Param::checkParam2($rules, $args);
  323. $user_id = User::getUserId();
  324. User::saveInfo($user_id, $args);
  325. }
  326. /**
  327. * 修改昵称
  328. * @author benzhan
  329. */
  330. public function actionChangeNickName($args) {
  331. $rules = [
  332. 'nick_name' => ['string', 'len' => [1, 20], 'desc' => '用户名'],
  333. ];
  334. Param::checkParam2($rules, $args);
  335. $user_id = User::getUserId();
  336. User::saveInfo($user_id, $args);
  337. User::setUserNameById($user_id, $args['nick_name']);
  338. }
  339. /**
  340. * 修改头像
  341. * @author benzhan
  342. */
  343. public function actionChangePhoto($args) {
  344. $args = array_merge($args, $_FILES);
  345. $rules = [
  346. 'cover_photo' => ['array', 'desc' => '头像文件'],
  347. ];
  348. Param::checkParam2($rules, $args);
  349. $file = $args['cover_photo'];
  350. $cover_photo = '';
  351. try {
  352. $cover_photo = (new FileUrl())->getFileUrl($file['tmp_name'], $file['name'], $file['type'], true);
  353. } catch (Exception $e) {
  354. Response::error($e->getCode(), $e->getMessage());
  355. }
  356. $user_id = User::getUserId();
  357. User::saveInfo($user_id, compact('cover_photo'));
  358. }
  359. /**
  360. * 第三方账号的是否可见
  361. * @author benzhan
  362. */
  363. public function actionSetVisible($args) {
  364. $rules = [
  365. 'type' => ['string', 'enum' => ['eos', 'eth', 'tron'], 'desc' => '类型'],
  366. 'is_visible' => ['int', 'desc' => '是否可见'],
  367. ];
  368. Param::checkParam2($rules, $args);
  369. $user_id = User::getUserId();
  370. $type = $args['type'];
  371. $is_visible = (int) $args['is_visible'];
  372. $objUserBindInfo = new TableHelper('user_bind_info', 'dw_chat');
  373. $objUserBindInfo->updateObject(compact('is_visible'), compact('user_id', 'type'));
  374. }
  375. /**
  376. * 用户信息接口
  377. * @author solu
  378. * @param $args
  379. * @return array
  380. */
  381. public function actionInfo($args) {
  382. $rules = [
  383. 'target_id' => ['int', 'desc' => '用户id'],
  384. 'group_id' => ['int', 'nullable' => true, '群id'],
  385. ];
  386. Param::checkParam2($rules, $args);
  387. $self = User::getUserId();
  388. $userId = (int)$args['target_id'];
  389. $groupId = (int)$args['group_id'];
  390. return User::getUserInfo($userId, $self, $groupId);
  391. }
  392. }