User.php 8.6 KB

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