User.php 8.5 KB

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