User.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. self::$userInfo = compact('user_id', 'token');
  36. // self::$userInfo['user_id'] = self::getUserIdByName(self::$userInfo['user_id']);
  37. return self::$userInfo;
  38. } else {
  39. return [];
  40. }
  41. }
  42. /**
  43. * 获取用户名
  44. * @author solu
  45. * @param $user_id
  46. * @return string
  47. */
  48. public static function getUserNameById($user_id) {
  49. $objRedis = dwRedis::init();
  50. $userName = $objRedis->hGet(self::REDIS_USER_ID_HASH, $user_id);
  51. if (!$userName) {
  52. $objUser = new TableHelper('user_info', 'dw_chat');
  53. $userName = $objUser->getOne(['user_id' => $user_id], ['_field' => 'user_name']);
  54. if ($userName) {
  55. $objRedis->hSet(self::REDIS_USER_ID_HASH, $user_id, $userName);
  56. }
  57. }
  58. return $userName;
  59. }
  60. /**
  61. * 获取用户信息
  62. * @param $user_id
  63. *
  64. * @return array
  65. */
  66. public static function getUserInfoById($user_id) {
  67. $_field = 'user_id, user_name, nick_name, cover_photo';
  68. $objUser = new TableHelper('user_info', 'dw_chat');
  69. $userInfo = $objUser->getRow(compact('user_id'), compact('_field'));
  70. return $userInfo;
  71. }
  72. /**
  73. * 用户登录(首次登录创建用户)
  74. * @author benzhan
  75. * @param $account
  76. * @param string $type
  77. */
  78. public static function login($account, $type = 'eos') {
  79. $where = compact('account');
  80. $objUserBindInfo = new TableHelper('user_bind_info', 'dw_chat');
  81. $row = $objUserBindInfo->getRow($where);
  82. $data = [
  83. 'last_login_time' => NOW,
  84. 'update_time' => NOW,
  85. ];
  86. $objUserInfo = new TableHelper('user_info', 'dw_chat');
  87. $user_id = $row['user_id'];
  88. if ($user_id) {
  89. $objUserInfo->updateObject($data, compact('user_id'));
  90. } else {
  91. $objUserInfo->autoCommit(false);
  92. // 添加用户信息
  93. $data['first_type'] = $type;
  94. $data['nick_name'] = self::getNewName($account);
  95. $data['user_name'] = $data['nick_name'] . '-' . strtoupper($type);
  96. $objUserInfo->addObject($data);
  97. $user_id = $objUserInfo->getInsertId();
  98. // 添加绑定关系
  99. $data2 = [
  100. 'user_id' => $user_id,
  101. 'type' => $type,
  102. 'account' => $account,
  103. 'create_time' => NOW,
  104. ];
  105. $objUserBindInfo->addObject($data2);
  106. $objUserInfo->tryCommit();
  107. try {
  108. (new GroupInfo())->joinGroup($user_id, GroupInfo::OFFICIAL_ID);
  109. } catch (Exception $e) {
  110. var_log("new user:{$user_id} add official group error:" . $e->getMessage());
  111. }
  112. }
  113. return $user_id;
  114. }
  115. /**
  116. * 绑定第三方账号
  117. * @param $user_id
  118. * @param $account
  119. * @param string $type
  120. *
  121. * @return mixed
  122. */
  123. public static function bind($user_id, $account, $type = 'eos') {
  124. $objUserBindInfo = new TableHelper('user_bind_info', 'dw_chat');
  125. $where = compact('account');
  126. $row = $objUserBindInfo->getRow($where);
  127. if ($row) {
  128. Response::error(CODE_NO_PERMITION, "{$type} account have been bind.");
  129. }
  130. $where = compact('user_id', 'type');
  131. $row = $objUserBindInfo->getRow($where);
  132. if ($row) {
  133. Response::error(CODE_NORMAL_ERROR, "you have bind {$type} account.");
  134. }
  135. $data = compact('user_id', 'type', 'account');
  136. $data['create_time'] = NOW;
  137. // 添加绑定关系
  138. $objUserBindInfo->addObject($data);
  139. return $user_id;
  140. }
  141. /**
  142. * 解绑操作
  143. * @author solu
  144. * @param $userId
  145. * @param string $type
  146. * @return bool
  147. * @throws Exception
  148. */
  149. public static function unbind($userId, $type = 'eos') {
  150. $objBind = new TableHelper('user_bind_info', 'dw_chat');
  151. $c = $objBind->getCount(['user_id' => $userId]);
  152. if ($c < 2) {
  153. throw new Exception('keep at least one account', CODE_NORMAL_ERROR);
  154. }
  155. if (!$objBind->delObject(['user_id' => $userId, 'type' => $type])) {
  156. throw new Exception('unbind error', CODE_NORMAL_ERROR);
  157. }
  158. return true;
  159. }
  160. private static function getNewName($account) {
  161. if (strlen($account) <= 12) {
  162. return $account;
  163. } else {
  164. $account = ltrim($account, '0x');
  165. $objUserInfo = new TableHelper('user_info', 'dw_chat');
  166. for ($i = 0; $i < 4; $i++) {
  167. $user_name = substr($account, 0, 12 + $i);
  168. $count = $objUserInfo->getCount(['user_name' => $user_name]);
  169. if ($count == 0) {
  170. return $user_name;
  171. }
  172. }
  173. for ($i = 0; $i < 10; $i++) {
  174. $user_name = substr($account, 0, 12) . rand(1, 999);
  175. $count = $objUserInfo->getCount(['user_name' => $user_name]);
  176. if ($count == 0) {
  177. return $user_name;
  178. }
  179. }
  180. throw new Exception('can not gen new user_name', CODE_UNKNOW_ERROT);
  181. }
  182. }
  183. /**
  184. * 保存用户信息
  185. * @param $user_id
  186. * @param $data
  187. */
  188. public static function saveInfo($user_id, $data) {
  189. $objUserInfo = new TableHelper('user_info', 'dw_chat');
  190. $objUserInfo->updateObject($data, compact('user_id'));
  191. }
  192. /**
  193. * 用户信息
  194. * @author solu
  195. * @param $userId
  196. * @param $myself
  197. * @return array
  198. */
  199. public static function getUserInfo($userId, $myself) {
  200. $objUserInfo = new TableHelper('user_info', 'dw_chat');
  201. $keyword = [];
  202. if (!$myself) {
  203. $keyword['_field'] = 'user_id, user_name, nick_name, cover_photo, is_block';
  204. }
  205. $userInfo = $objUserInfo->getRow(['user_id' => $userId], $keyword);
  206. $objBind = new TableHelper('user_bind_info', 'dw_chat');
  207. $keyword = [
  208. '_field' => 'type, account, is_visible, create_time',
  209. ];
  210. if (!$myself) {
  211. $keyword['_field'] = 'type, account, is_visible';
  212. }
  213. $items = $objBind->getAll(['user_id' => $userId], $keyword);
  214. $items = arrayFormatKey($items, 'type');
  215. $allType = array_keys(Account::getAllType());
  216. $binds = [];
  217. foreach ($allType as $type) {
  218. $item = $items[$type];
  219. if ($item) {
  220. $item['is_visible'] = intval($item['is_visible']);
  221. if (!$myself && !$item['is_visible']) {
  222. $item['account'] = ''; // 不可见
  223. }
  224. } else {
  225. $item = [
  226. 'type' => $type,
  227. 'account' => '',
  228. 'is_visible' => 1,
  229. ];
  230. }
  231. $binds[] = $item;
  232. }
  233. $userInfo['binds'] = $binds;
  234. return $userInfo ?: [];
  235. }
  236. }