['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); } }