User.php 8.1 KB

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