User.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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['nick_name'] = $name ?: self::getNewName($account);
  106. $data['user_name'] = $data['nick_name'] . '-' . strtoupper($type);
  107. $objUserInfo->addObject($data);
  108. $user_id = $objUserInfo->getInsertId();
  109. }
  110. // 添加绑定关系
  111. $data2 = [
  112. 'user_id' => $user_id,
  113. 'type' => $type,
  114. 'account' => $account,
  115. 'create_time' => NOW,
  116. ];
  117. $objUserBindInfo->addObject($data2);
  118. $objUserInfo->tryCommit();
  119. try {
  120. (new GroupInfo())->joinGroup($user_id, GroupInfo::OFFICIAL_ID);
  121. } catch (Exception $e) {
  122. var_log("new user:{$user_id} add official group error:" . $e->getMessage());
  123. }
  124. }
  125. return $user_id;
  126. }
  127. /**
  128. * 绑定第三方账号
  129. * @param $user_id
  130. * @param $account
  131. * @param string $type
  132. *
  133. * @return mixed
  134. */
  135. public static function bind($user_id, $account, $type = 'eos') {
  136. $objUser = new TableHelper('user_info', 'dw_chat');
  137. $objUserBindInfo = new TableHelper('user_bind_info', 'dw_chat');
  138. $user = $objUser->getRow(['user_id' => $user_id]);
  139. if ($user['first_type'] == $type && $user['first_account'] != $account) {
  140. Response::error(CODE_NORMAL_ERROR, "this account {$type} must be bind {$user['first_account']}");
  141. }
  142. $where = compact('account');
  143. $row = $objUserBindInfo->getRow($where);
  144. if ($row) {
  145. Response::error(CODE_NO_PERMITION, "{$type} account have been bind.");
  146. }
  147. $where = compact('user_id', 'type');
  148. $row = $objUserBindInfo->getRow($where);
  149. if ($row) {
  150. Response::error(CODE_NORMAL_ERROR, "you have bind {$type} account.");
  151. }
  152. $data = compact('user_id', 'type', 'account');
  153. $data['create_time'] = NOW;
  154. // 添加绑定关系
  155. $objUserBindInfo->addObject($data);
  156. return $user_id;
  157. }
  158. /**
  159. * 解绑操作
  160. * @author solu
  161. * @param $userId
  162. * @param string $type
  163. * @return bool
  164. * @throws Exception
  165. */
  166. public static function unbind($userId, $type = Account::TYPE_EOS) {
  167. $objBind = new TableHelper('user_bind_info', 'dw_chat');
  168. $rows = $objBind->getAll(['user_id' => $userId]);
  169. if (!$objBind->delObject(['user_id' => $userId, 'type' => $type])) {
  170. throw new Exception('unbind error', CODE_NORMAL_ERROR);
  171. }
  172. $bind = [];
  173. foreach ($rows as $row) {
  174. if ($row['type'] == $type) {
  175. $bind = $row;
  176. break;
  177. }
  178. }
  179. if ($bind['type'] == Account::TYPE_TG) {
  180. Telegram::delUserByTG($bind['account']);
  181. }
  182. return true;
  183. }
  184. public static function getNewName($account) {
  185. if (strlen($account) <= 12) {
  186. return $account;
  187. } else {
  188. $account = ltrim($account, '0x');
  189. $objUserInfo = new TableHelper('user_info', 'dw_chat');
  190. for ($i = 0; $i < 4; $i++) {
  191. $user_name = substr($account, 0, 12 + $i);
  192. $count = $objUserInfo->getCount(['user_name' => $user_name]);
  193. if ($count == 0) {
  194. return $user_name;
  195. }
  196. }
  197. for ($i = 0; $i < 10; $i++) {
  198. $user_name = substr($account, 0, 12) . rand(1, 999);
  199. $count = $objUserInfo->getCount(['user_name' => $user_name]);
  200. if ($count == 0) {
  201. return $user_name;
  202. }
  203. }
  204. throw new Exception('can not gen new user_name', CODE_UNKNOW_ERROT);
  205. }
  206. }
  207. /**
  208. * 保存用户信息
  209. * @param $user_id
  210. * @param $data
  211. */
  212. public static function saveInfo($user_id, $data) {
  213. $objUserInfo = new TableHelper('user_info', 'dw_chat');
  214. $objUserInfo->updateObject($data, compact('user_id'));
  215. }
  216. /**
  217. * 用户信息
  218. * @author solu
  219. * @param $userId
  220. * @param $self
  221. * @param $groupId
  222. * @return array
  223. */
  224. public static function getUserInfo($userId, $self, $groupId) {
  225. $myself = $userId == $self;
  226. $objUserInfo = new TableHelper('user_info', 'dw_chat');
  227. $keyword = [];
  228. if (!$myself) {
  229. $keyword['_field'] = 'user_id, user_name, nick_name, cover_photo, is_block';
  230. }
  231. $userInfo = $objUserInfo->getRow(['user_id' => $userId], $keyword);
  232. $objBind = new TableHelper('user_bind_info', 'dw_chat');
  233. $keyword = [
  234. '_field' => 'type, account, is_visible, create_time',
  235. ];
  236. if (!$myself) {
  237. $keyword['_field'] = 'type, account, is_visible';
  238. }
  239. $items = $objBind->getAll(['user_id' => $userId], $keyword);
  240. $items = arrayFormatKey($items, 'type');
  241. $allType = array_keys(Account::getAllType());
  242. // $isAdmin = false;
  243. // if ($groupId) {
  244. // $isAdmin = (new UserGroup())->isAdmin($groupId, $self);
  245. // }
  246. $binds = [];
  247. foreach ($allType as $type) {
  248. $item = $items[$type];
  249. if ($item) {
  250. $item['is_visible'] = intval($item['is_visible']);
  251. // if (!$myself && !$item['is_visible'] && !$isAdmin) {
  252. if (!$myself && !$item['is_visible']) {
  253. $item['account'] = '******'; // 不可见
  254. }
  255. } else {
  256. $item = [
  257. 'type' => $type,
  258. 'account' => '',
  259. 'is_visible' => 1,
  260. ];
  261. }
  262. $binds[] = $item;
  263. }
  264. $userInfo['binds'] = $binds;
  265. return $userInfo ?: [];
  266. }
  267. }