User.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * 用户基础信息
  4. * @author benzhan
  5. */
  6. class User extends Singleton {
  7. const REDIS_USER_ID_HASH = 'globals:user_id_hash';
  8. private static $userInfo = null;
  9. public static function checkLogin() {
  10. $userInfo = self::getInfo();
  11. return $userInfo;
  12. }
  13. public static function getUserId() {
  14. $userInfo = self::getInfo();
  15. return $userInfo['user_id'] ?: 0;
  16. }
  17. public static function getUserName() {
  18. $user_id = self::getUserId();
  19. $user_name = self::getUserNameById($user_id);
  20. return $user_name;
  21. }
  22. public static function getInfo() {
  23. if (self::$userInfo) {
  24. return self::$userInfo;
  25. }
  26. $flag = Account::checkToken();
  27. if ($flag) {
  28. // if ($_REQUEST['token'] && $_REQUEST['user_id']) {
  29. // $token = $_REQUEST['token'];
  30. // $user_id = $_REQUEST['user_id'];
  31. // } else {
  32. // $token = $_COOKIE['token'];
  33. // $user_id = $_COOKIE['user_id'];
  34. // }
  35. $token = $_REQUEST['token'];
  36. $user_id = $_REQUEST['user_id'];
  37. self::$userInfo = compact('user_id', 'token');
  38. // self::$userInfo['user_id'] = self::getUserIdByName(self::$userInfo['user_id']);
  39. return self::$userInfo;
  40. } else {
  41. return [];
  42. }
  43. }
  44. /**
  45. * 获取用户名
  46. * @author solu
  47. * @param $user_id
  48. * @return string
  49. */
  50. public static function getUserNameById($user_id) {
  51. $objRedis = dwRedis::init();
  52. $userName = $objRedis->hGet(self::REDIS_USER_ID_HASH, $user_id);
  53. if (!$userName) {
  54. $objUser = new TableHelper('user_info', 'dw_chat');
  55. $userName = $objUser->getOne(['user_id' => $user_id], ['_field' => 'nick_name']);
  56. if ($userName) {
  57. self::setUserNameById($user_id, $userName, $objRedis);
  58. }
  59. }
  60. return $userName;
  61. }
  62. public static function setUserNameById($user_id, $user_name, $objRedis = null) {
  63. !$objRedis && $objRedis = dwRedis::init();
  64. $objRedis->hSet(self::REDIS_USER_ID_HASH, $user_id, $user_name);
  65. }
  66. /**
  67. * 获取用户信息
  68. * @param $user_id
  69. *
  70. * @return array
  71. */
  72. public static function getUserInfoById($user_id) {
  73. $_field = 'user_id, user_name, nick_name, cover_photo';
  74. $objUser = new TableHelper('user_info', 'dw_chat');
  75. $userInfo = $objUser->getRow(compact('user_id'), compact('_field'));
  76. return $userInfo;
  77. }
  78. /**
  79. * 用户登录(首次登录创建用户)
  80. * @author benzhan
  81. * @param $account
  82. * @param string $type
  83. * @param $name
  84. */
  85. public static function login($account, $type = 'eos', $name = '') {
  86. $where = compact('account');
  87. $objUserBindInfo = new TableHelper('user_bind_info', 'dw_chat');
  88. $row = $objUserBindInfo->getRow($where);
  89. $data = [
  90. 'last_login_time' => NOW,
  91. 'update_time' => NOW,
  92. ];
  93. $objUserInfo = new TableHelper('user_info', 'dw_chat');
  94. $user_id = $row['user_id'];
  95. if ($user_id) {
  96. $objUserInfo->updateObject($data, compact('user_id'));
  97. } else {
  98. $objUserInfo->autoCommit(false);
  99. $oldUser = $objUserInfo->getRow(['first_account' => $account, 'first_type' => $type]);
  100. if ($oldUser) { // 有老账号
  101. $user_id = $oldUser['user_id'];
  102. } else {
  103. // 添加用户信息
  104. $data['first_type'] = $type;
  105. $data['first_account'] = $account;
  106. $data['nick_name'] = $name ?: self::getNewName($account);
  107. $data['user_name'] = $data['nick_name'] . '-' . strtoupper($type);
  108. $objUserInfo->addObject($data);
  109. $user_id = $objUserInfo->getInsertId();
  110. }
  111. // 添加绑定关系
  112. $data2 = [
  113. 'user_id' => $user_id,
  114. 'type' => $type,
  115. 'account' => $account,
  116. 'create_time' => NOW,
  117. ];
  118. $objUserBindInfo->addObject($data2);
  119. $objUserInfo->tryCommit();
  120. try {
  121. (new GroupInfo())->joinGroup($user_id, GroupInfo::OFFICIAL_ID);
  122. } catch (Exception $e) {
  123. var_log("new user:{$user_id} add official group error:" . $e->getMessage());
  124. }
  125. }
  126. return $user_id;
  127. }
  128. /**
  129. * 绑定第三方账号
  130. * @param $user_id
  131. * @param $account
  132. * @param string $type
  133. *
  134. * @return mixed
  135. */
  136. public static function bind($user_id, $account, $type = 'eos') {
  137. $objUser = new TableHelper('user_info', 'dw_chat');
  138. $objUserBindInfo = new TableHelper('user_bind_info', 'dw_chat');
  139. $user = $objUser->getRow(['user_id' => $user_id]);
  140. if ($user['first_type'] == $type && $user['first_account'] != $account) {
  141. Response::error(CODE_NORMAL_ERROR, "this account {$type} must be bind {$user['first_account']}");
  142. }
  143. $where = compact('account', 'type');
  144. $row = $objUserBindInfo->getRow($where);
  145. if ($row) {
  146. Response::error(CODE_NO_PERMITION, "{$type} account have been bind.");
  147. }
  148. $where = compact('user_id', 'type');
  149. $row = $objUserBindInfo->getRow($where);
  150. if ($row) {
  151. Response::error(CODE_NORMAL_ERROR, "you have bind {$type} account.");
  152. }
  153. $data = compact('user_id', 'type', 'account');
  154. $data['create_time'] = NOW;
  155. // 添加绑定关系
  156. $objUserBindInfo->addObject($data);
  157. return $user_id;
  158. }
  159. /**
  160. * 解绑操作
  161. * @author solu
  162. * @param $userId
  163. * @param string $type
  164. * @return bool
  165. * @throws Exception
  166. */
  167. public static function unbind($userId, $type = Account::TYPE_EOS) {
  168. $objBind = new TableHelper('user_bind_info', 'dw_chat');
  169. $rows = $objBind->getAll(['user_id' => $userId]);
  170. if (!$objBind->delObject(['user_id' => $userId, 'type' => $type])) {
  171. throw new Exception('unbind error', CODE_NORMAL_ERROR);
  172. }
  173. $bind = [];
  174. foreach ($rows as $row) {
  175. if ($row['type'] == $type) {
  176. $bind = $row;
  177. break;
  178. }
  179. }
  180. if ($bind['type'] == Account::TYPE_TG) {
  181. Telegram::delUserByTG($bind['account']);
  182. }
  183. return true;
  184. }
  185. public static function getNewName($account) {
  186. if (strlen($account) <= 12) {
  187. return $account;
  188. } else {
  189. $account = ltrim($account, '0x');
  190. $objUserInfo = new TableHelper('user_info', 'dw_chat');
  191. for ($i = 0; $i < 4; $i++) {
  192. $user_name = substr($account, 0, 12 + $i);
  193. $count = $objUserInfo->getCount(['user_name' => $user_name]);
  194. if ($count == 0) {
  195. return $user_name;
  196. }
  197. }
  198. for ($i = 0; $i < 10; $i++) {
  199. $user_name = substr($account, 0, 12) . rand(1, 999);
  200. $count = $objUserInfo->getCount(['user_name' => $user_name]);
  201. if ($count == 0) {
  202. return $user_name;
  203. }
  204. }
  205. throw new Exception('can not gen new user_name', CODE_UNKNOW_ERROT);
  206. }
  207. }
  208. /**
  209. * 保存用户信息
  210. * @param $user_id
  211. * @param $data
  212. */
  213. public static function saveInfo($user_id, $data) {
  214. $objUserInfo = new TableHelper('user_info', 'dw_chat');
  215. $objUserInfo->updateObject($data, compact('user_id'));
  216. }
  217. /**
  218. * 用户信息
  219. * @author solu
  220. * @param $userId
  221. * @param $self
  222. * @param $groupId
  223. * @return array
  224. */
  225. public static function getUserInfo($userId, $self, $groupId) {
  226. $myself = $userId == $self;
  227. $objUserInfo = new TableHelper('user_info', 'dw_chat');
  228. $keyword = [];
  229. if (!$myself) {
  230. $keyword['_field'] = 'user_id, user_name, nick_name, cover_photo, is_block';
  231. }
  232. $userInfo = $objUserInfo->getRow(['user_id' => $userId], $keyword);
  233. $objBind = new TableHelper('user_bind_info', 'dw_chat');
  234. $keyword = [
  235. '_field' => 'type, account, is_visible, create_time',
  236. ];
  237. if (!$myself) {
  238. $keyword['_field'] = 'type, account, is_visible';
  239. }
  240. $items = $objBind->getAll(['user_id' => $userId], $keyword);
  241. $items = arrayFormatKey($items, 'type');
  242. $allType = array_keys(Account::getAllType());
  243. // $isAdmin = false;
  244. // if ($groupId) {
  245. // $isAdmin = (new UserGroup())->isAdmin($groupId, $self);
  246. // }
  247. $binds = [];
  248. foreach ($allType as $type) {
  249. $item = $items[$type];
  250. if ($item) {
  251. $item['is_visible'] = intval($item['is_visible']);
  252. // if (!$myself && !$item['is_visible'] && !$isAdmin) {
  253. if (!$myself && !$item['is_visible']) {
  254. $item['account'] = '******'; // 不可见
  255. }
  256. } else {
  257. $item = [
  258. 'type' => $type,
  259. 'account' => '',
  260. 'is_visible' => 1,
  261. ];
  262. }
  263. $binds[] = $item;
  264. }
  265. $userInfo['binds'] = $binds;
  266. $userInfo['cover_photo'] = awsReplaceImg($userInfo['cover_photo']);
  267. return $userInfo ?: [];
  268. }
  269. }