123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- <?php
- /**
- * 用户登录相关
- * User: benzhan
- * Date: 2018/10/18
- * Time: 10:41
- */
- class UserController extends BaseController {
- protected $ajaxLoginActions = [
- 'eosBind',
- 'ethBind',
- 'tronBind',
- 'unbind',
- 'changeUserName',
- 'changeNickName',
- 'changePhoto',
- 'setVisible',
- ];
- public function __construct() {
- parent::__construct(false);
- }
- /**
- * 获取随机数【不需要登录】
- * @author benzhan
- */
- public function actionRandom($args) {
- $rules = [
- 'account' => ['string', 'reg' => '/^([a-z0-9\.]{1,13})$|^(0x[a-z0-9]{40})$|^[a-zA-Z0-9]{34}$/i', 'desc' => 'eos账号|eth账号|tron账号'],
- ];
- Param::checkParam2($rules, $args);
- $random = Account::getRandom($args['account']);
- Response::success($random);
- }
- /**
- * 获取随机数【不需要登录】
- * @author benzhan
- */
- public function actionRandom2($args) {
- $rules = [
- 'account' => ['string', 'reg' => '/^([a-z0-9\.]{1,13})$|^(0x[a-z0-9]{40})$|^[a-zA-Z0-9]{34}$/i', 'desc' => 'eos账号|eth账号'],
- ];
- Param::checkParam2($rules, $args);
- $random = Account::getRandom($args['account'], true);
- Response::success($random);
- }
- private function _getUserBaseInfo($user_id) {
- $objUserInfo = new TableHelper('user_info', 'dw_chat');
- $row = $objUserInfo->getRow(['user_id' => $user_id], ['_field' => 'user_id, user_name, nick_name, cover_photo']);
- return $row;
- }
- /**
- * Eos账号登录【不需要登录】
- * @author benzhan
- */
- public function actionEosLogin($args) {
- $rules = [
- 'account' => ['string', 'reg' => '/^[a-z0-9\.]{1,13}$/', 'desc' => 'eos账号'],
- 'pubkey' => ['string', 'nullable' => true, '公钥'],
- 'authority' => ['string', 'nullable' => true, 'enum' => ['owner', 'active'], '权限'],
- 'sign' => ['string', 'desc' => '私钥加密后的签名']
- ];
- Param::checkParam2($rules, $args);
- $info = User::getInfo();
- if ($info) {
- $info['info'] = $this->_getUserBaseInfo($info['user_id']);
- return $info;
- }
- if (!$args['pubkey'] && $args['authority']) {
- $args['pubkey'] = $this->_getPublicKey($args['account'], $args['authority']);
- }
- if (!$args['pubkey']) {
- Response::error(CODE_PARAM_ERROR, 'pubkey is empty.');
- }
- $account = $args['account'];
- $data = Account::getRandom($account);
- if (!$data) {
- Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
- }
- $flag = false;
- try {
- $flag = Account::verifyMsg($args['pubkey'], $account, $data, $args['sign']);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- if ($flag) {
- $user_id = User::login($args['account'], Account::TYPE_EOS);
- $info = Account::setCookie($user_id);
- $info['info'] = $this->_getUserBaseInfo($info['user_id']);
- return $info;
- } else {
- Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
- }
- }
- /**
- * 校验登录 (合约方式
- * @author solu
- * @param $args
- * @return array
- */
- public function actionEosLogin2($args) {
- $rules = [
- 'account' => ['string', 'reg' => '/^[a-z0-9\.]{1,13}$/', 'desc' => 'eos账号'],
- 'access_token' => ['string', 'len' => 36, 'desc' => 'access_token'],
- 'trx_id' => ['string', 'desc' => '交易id'],
- 'group_id' => ['int', 'nullable' => true, 'desc' => '群id'],
- ];
- Param::checkParam2($rules, $args);
- $objSync = new Sync_LoginLog();
- $objLoginLog = new TableHelper('login_log', 'dw_chat');
- $_where = ['trx_id' => $args['trx_id']];
- $row = $objLoginLog->getRow($_where);
- $maxTry = 3;
- $i = 0;
- // 没有数据请求同步再尝试
- while ($i < $maxTry && !$row) {
- sleep($i + 1);
- $objSync->pubSubscribe();
- $row = $objLoginLog->getRow($_where);
- $i++;
- }
- if (!$row) {
- Response::error(CODE_NORMAL_ERROR, 'trx_id not in log!');
- }
- $account = $args['account'];
- $random = Account::getRandom($account);
- if (!$random) {
- Response::error(CODE_NORMAL_ERROR, 'can not find random data, please refresh.');
- }
- if ($row['account'] != $account) {
- Response::error(CODE_NORMAL_ERROR, 'account unmatch');
- }
- if ($random != $row['memo']) {
- Response::error(CODE_NORMAL_ERROR, 'random unmatch');
- }
- $data = [];
- try {
- $data = Account::verifyPlayer($row['account'], $random, $args['access_token']);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- // 带群id检测是否加入群
- if ($args['group_id']) {
- try {
- (new GroupInfo())->joinGroup($data['user_id'], $args['group_id']);
- } catch (Exception $e) {
- var_log($e->getMessage());
- }
- }
- return $data;
- }
- private function _getPublicKey($account, $authority) {
- $accountInfo = EosBase::getAccount($account);
- $accountInfo = json_decode($accountInfo, true);
- foreach ($accountInfo['permissions'] as $p) {
- if ($p['perm_name'] == $authority) {
- return $p['required_auth']['keys'][0]['key'];
- }
- }
- return null;
- }
- /**
- * Eos账号绑定
- * @author benzhan
- */
- public function actionEosBind($args) {
- $rules = [
- 'account' => ['string', 'reg' => '/^[a-z0-9\.]{1,13}$/', 'desc' => 'eos账号'],
- 'pubkey' => ['string', '公钥'],
- 'sign' => ['string', 'desc' => '私钥加密后的签名'],
- ];
- Param::checkParam2($rules, $args);
- $account = $args['account'];
- $data = Account::getRandom($account);
- if (!$data) {
- Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
- }
- $flag = Account::verifyMsg($args['pubkey'], $account, $data, $args['sign']);
- if ($flag) {
- $user_id = User::getUserId();
- User::bind($user_id, $args['account'], Account::TYPE_EOS);
- } else {
- Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
- }
- }
- /**
- * Eth账号登录【不需要登录】
- * @author solu
- */
- public function actionEthLogin($args) {
- $rules = [
- 'account' => ['string', 'reg' => '/^0x[a-z0-9]{40}$/i', 'desc' => 'eos账号'],
- 'sign' => ['string', 'desc' => '私钥加密后的签名']
- ];
- Param::checkParam2($rules, $args);
- $info = User::getInfo();
- if ($info) {
- return $info;
- }
- $account = $args['account'];
- $data = Account::getRandom($account);
- if (!$data) {
- Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
- }
- $flag = false;
- try {
- $flag = Account::verifyEth($args['account'], $data, $args['sign']);
- } catch (Exception $e) {}
- if (!$flag) {
- Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
- }
- $user_id = User::login($args['account'], Account::TYPE_ETH);
- return Account::setCookie($user_id);
- }
- /**
- * ETH账号绑定
- * @author solu
- * @param $args
- */
- public function actionEthBind($args) {
- $rules = [
- 'account' => ['string', 'reg' => '/^0x[a-z0-9]{40}$/i', 'desc' => 'eos账号'],
- 'sign' => ['string', 'desc' => '私钥加密后的签名']
- ];
- Param::checkParam2($rules, $args);
- $account = $args['account'];
- $data = Account::getRandom($account);
- if (!$data) {
- Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
- }
- $flag = false;
- try {
- $flag = Account::verifyEth($args['account'], $data, $args['sign']);
- } catch (Exception $e) {}
- if (!$flag) {
- Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
- }
- $user_id = User::getUserId();
- User::bind($user_id, $account, Account::TYPE_ETH);
- }
- /**
- * Tron账号登录【不需要登录】
- * @param $args
- * @return array
- */
- public function actionTronLogin($args) {
- $rules = [
- 'account' => ['string', 'reg' => '/^[a-zA-Z0-9]{34}$/', 'desc' => 'tron账号'],
- 'sign' => ['string', 'desc' => '私钥加密后的签名']
- ];
- Param::checkParam2($rules, $args);
- $info = User::getInfo();
- if ($info) {
- return $info;
- }
- $account = $args['account'];
- $data = Account::getRandom($account);
- if (!$data) {
- Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
- }
- $flag = false;
- try {
- $flag = Account::verifyTron($args['account'], $data, $args['sign']);
- } catch (Exception $e) {}
- if (!$flag) {
- Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
- }
- $user_id = User::login($args['account'], Account::TYPE_TRON);
- return Account::setCookie($user_id);
- }
- /**
- * Tron账号绑定
- * @author solu
- * @param $args
- */
- public function actionTronBind($args) {
- $rules = [
- 'account' => ['string', 'reg' => '/^[a-zA-Z0-9]{34}$/', 'desc' => 'tron账号'],
- 'sign' => ['string', 'desc' => '私钥加密后的签名']
- ];
- Param::checkParam2($rules, $args);
- $account = $args['account'];
- $data = Account::getRandom($account);
- if (!$data) {
- Response::error(CODE_NO_PERMITION, 'can not find random data, please refresh.');
- }
- $flag = false;
- try {
- $flag = Account::verifyTron($args['account'], $data, $args['sign']);
- } catch (Exception $e) {}
- if (!$flag) {
- Response::error(CODE_NORMAL_ERROR, 'Verify failure, please retry.');
- }
- $user_id = User::getUserId();
- User::bind($user_id, $account, Account::TYPE_TRON);
- }
- /**
- * 解绑账号
- * @author solu
- * @param $args
- */
- public function actionUnbind($args) {
- $typeRule = array_keys(Account::getAllType());
- $rules = [
- 'type' => ['string', 'enum' => $typeRule, 'desc' => '类型 eos,eth, tron'],
- ];
- Param::checkParam2($rules, $args);
- $userId = User::getUserId();
- try {
- User::unbind($userId, $args['type']);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- }
- /**
- * 修改用户名【只能修改一次】
- * @author benzhan
- */
- public function actionChangeUserName($args) {
- $rules = [
- 'user_name' => ['string', 'reg' => '/^[a-zA-Z_0-9]{5,20}$/i', 'desc' => '用户名'],
- ];
- Param::checkParam2($rules, $args);
- $user_id = User::getUserId();
- User::saveInfo($user_id, $args);
- }
- /**
- * 修改昵称
- * @author benzhan
- */
- public function actionChangeNickName($args) {
- $rules = [
- 'nick_name' => ['string', 'len' => [1, 20], 'desc' => '用户名'],
- ];
- Param::checkParam2($rules, $args);
- $user_id = User::getUserId();
- User::saveInfo($user_id, $args);
- }
- /**
- * 修改头像
- * @author benzhan
- */
- public function actionChangePhoto($args) {
- $args = array_merge($args, $_FILES);
- $rules = [
- 'cover_photo' => ['array', 'desc' => '头像文件'],
- ];
- Param::checkParam2($rules, $args);
- $file = $args['cover_photo'];
- $cover_photo = '';
- try {
- $cover_photo = (new FileUrl())->getFileUrl($file['tmp_name'], $file['name'], $file['type']);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- $user_id = User::getUserId();
- User::saveInfo($user_id, compact('cover_photo'));
- }
- /**
- * 第三方账号的是否可见
- * @author benzhan
- */
- public function actionSetVisible($args) {
- $rules = [
- 'type' => ['string', 'enum' => ['eos', 'eth', 'tron'], 'desc' => '类型'],
- 'is_visible' => ['int', 'desc' => '是否可见'],
- ];
- Param::checkParam2($rules, $args);
- $user_id = User::getUserId();
- $type = $args['type'];
- $is_visible = (int) $args['is_visible'];
- $objUserBindInfo = new TableHelper('user_bind_info', 'dw_chat');
- $objUserBindInfo->updateObject(compact('is_visible'), compact('user_id', 'type'));
- }
- /**
- * 用户信息接口
- * @author solu
- * @param $args
- * @return array
- */
- public function actionInfo($args) {
- $rules = [
- 'user_id' => ['int', 'desc' => '用户id'],
- ];
- Param::checkParam2($rules, $args);
- $self = User::getUserId();
- $userId = (int)$args['user_id'];
- $myself = $userId == $self;
- return User::getUserInfo($userId, $myself);
- }
- }
|