123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- <?php
- /**
- * 用户基础信息
- * @author benzhan
- */
- class User extends Singleton {
- const REDIS_USER_ID_HASH = 'globals:user_id_hash';
- private static $userInfo = null;
- public static function checkLogin() {
- $userInfo = self::getInfo();
- return $userInfo;
- }
- public static function getUserId() {
- $userInfo = self::getInfo();
- return $userInfo['user_id'] ?: 0;
- }
- public static function getUserName() {
- $user_id = self::getUserId();
- $user_name = self::getUserNameById($user_id);
- return $user_name;
- }
- public static function getInfo() {
- if (self::$userInfo) {
- return self::$userInfo;
- }
- $flag = Account::checkToken();
- if ($flag) {
- // if ($_REQUEST['token'] && $_REQUEST['user_id']) {
- // $token = $_REQUEST['token'];
- // $user_id = $_REQUEST['user_id'];
- // } else {
- // $token = $_COOKIE['token'];
- // $user_id = $_COOKIE['user_id'];
- // }
- $token = $_REQUEST['token'];
- $user_id = $_REQUEST['user_id'];
- self::$userInfo = compact('user_id', 'token');
- // self::$userInfo['user_id'] = self::getUserIdByName(self::$userInfo['user_id']);
- return self::$userInfo;
- } else {
- return [];
- }
- }
- /**
- * 获取用户名
- * @author solu
- * @param $user_id
- * @return string
- */
- public static function getUserNameById($user_id) {
- $objRedis = dwRedis::init();
- $userName = $objRedis->hGet(self::REDIS_USER_ID_HASH, $user_id);
- if (!$userName) {
- $objUser = new TableHelper('user_info', 'dw_chat');
- $userName = $objUser->getOne(['user_id' => $user_id], ['_field' => 'user_name']);
- if ($userName) {
- $objRedis->hSet(self::REDIS_USER_ID_HASH, $user_id, $userName);
- }
- }
- return $userName;
- }
- /**
- * 获取用户信息
- * @param $user_id
- *
- * @return array
- */
- public static function getUserInfoById($user_id) {
- $_field = 'user_id, user_name, nick_name, cover_photo';
- $objUser = new TableHelper('user_info', 'dw_chat');
- $userInfo = $objUser->getRow(compact('user_id'), compact('_field'));
- return $userInfo;
- }
- /**
- * 用户登录(首次登录创建用户)
- * @author benzhan
- * @param $account
- * @param string $type
- */
- public static function login($account, $type = 'eos') {
- $where = compact('account');
- $objUserBindInfo = new TableHelper('user_bind_info', 'dw_chat');
- $row = $objUserBindInfo->getRow($where);
- $data = [
- 'last_login_time' => NOW,
- 'update_time' => NOW,
- ];
- $objUserInfo = new TableHelper('user_info', 'dw_chat');
- $user_id = $row['user_id'];
- if ($user_id) {
- $objUserInfo->updateObject($data, compact('user_id'));
- } else {
- $objUserInfo->autoCommit(false);
- // 添加用户信息
- $data['first_type'] = $type;
- $data['nick_name'] = self::getNewName($account);
- $data['user_name'] = $data['nick_name'] . '-' . strtoupper($type);
- $objUserInfo->addObject($data);
- $user_id = $objUserInfo->getInsertId();
- // 添加绑定关系
- $data2 = [
- 'user_id' => $user_id,
- 'type' => $type,
- 'account' => $account,
- 'create_time' => NOW,
- ];
- $objUserBindInfo->addObject($data2);
- $objUserInfo->tryCommit();
- try {
- (new GroupInfo())->joinGroup($user_id, GroupInfo::OFFICIAL_ID);
- } catch (Exception $e) {
- var_log("new user:{$user_id} add official group error:" . $e->getMessage());
- }
- }
- return $user_id;
- }
- /**
- * 绑定第三方账号
- * @param $user_id
- * @param $account
- * @param string $type
- *
- * @return mixed
- */
- public static function bind($user_id, $account, $type = 'eos') {
- $objUserBindInfo = new TableHelper('user_bind_info', 'dw_chat');
- $where = compact('account');
- $row = $objUserBindInfo->getRow($where);
- if ($row) {
- Response::error(CODE_NO_PERMITION, "{$type} account have been bind.");
- }
- $where = compact('user_id', 'type');
- $row = $objUserBindInfo->getRow($where);
- if ($row) {
- Response::error(CODE_NORMAL_ERROR, "you have bind {$type} account.");
- }
- $data = compact('user_id', 'type', 'account');
- $data['create_time'] = NOW;
- // 添加绑定关系
- $objUserBindInfo->addObject($data);
- return $user_id;
- }
- /**
- * 解绑操作
- * @author solu
- * @param $userId
- * @param string $type
- * @return bool
- * @throws Exception
- */
- public static function unbind($userId, $type = 'eos') {
- $objBind = new TableHelper('user_bind_info', 'dw_chat');
- $c = $objBind->getCount(['user_id' => $userId]);
- if ($c < 2) {
- throw new Exception('keep at least one account', CODE_NORMAL_ERROR);
- }
- if (!$objBind->delObject(['user_id' => $userId, 'type' => $type])) {
- throw new Exception('unbind error', CODE_NORMAL_ERROR);
- }
- return true;
- }
- private static function getNewName($account) {
- if (strlen($account) <= 12) {
- return $account;
- } else {
- $account = ltrim($account, '0x');
- $objUserInfo = new TableHelper('user_info', 'dw_chat');
- for ($i = 0; $i < 4; $i++) {
- $user_name = substr($account, 0, 12 + $i);
- $count = $objUserInfo->getCount(['user_name' => $user_name]);
- if ($count == 0) {
- return $user_name;
- }
- }
- for ($i = 0; $i < 10; $i++) {
- $user_name = substr($account, 0, 12) . rand(1, 999);
- $count = $objUserInfo->getCount(['user_name' => $user_name]);
- if ($count == 0) {
- return $user_name;
- }
- }
- throw new Exception('can not gen new user_name', CODE_UNKNOW_ERROT);
- }
- }
- /**
- * 保存用户信息
- * @param $user_id
- * @param $data
- */
- public static function saveInfo($user_id, $data) {
- $objUserInfo = new TableHelper('user_info', 'dw_chat');
- $objUserInfo->updateObject($data, compact('user_id'));
- }
- /**
- * 用户信息
- * @author solu
- * @param $userId
- * @param $myself
- * @return array
- */
- public static function getUserInfo($userId, $myself) {
- $objUserInfo = new TableHelper('user_info', 'dw_chat');
- $keyword = [];
- if (!$myself) {
- $keyword['_field'] = 'user_id, user_name, nick_name, cover_photo, is_block';
- }
- $userInfo = $objUserInfo->getRow(['user_id' => $userId], $keyword);
- $objBind = new TableHelper('user_bind_info', 'dw_chat');
- $keyword = [
- '_field' => 'type, account, is_visible, create_time',
- ];
- if (!$myself) {
- $keyword['_field'] = 'type, account, is_visible';
- }
- $items = $objBind->getAll(['user_id' => $userId], $keyword);
- $items = arrayFormatKey($items, 'type');
- $allType = array_keys(Account::getAllType());
- $binds = [];
- foreach ($allType as $type) {
- $item = $items[$type];
- if ($item) {
- $item['is_visible'] = intval($item['is_visible']);
- if (!$myself && !$item['is_visible']) {
- $item['account'] = ''; // 不可见
- }
- } else {
- $item = [
- 'type' => $type,
- 'account' => '',
- 'is_visible' => 1,
- ];
- }
- $binds[] = $item;
- }
- $userInfo['binds'] = $binds;
- return $userInfo ?: [];
- }
- }
|