UserService.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /**
  3. * User Service
  4. * Created by PhpStorm.
  5. * User: guanxl
  6. * Date: 2018/3/23
  7. * Time: 19:20
  8. */
  9. namespace App\Services;
  10. use App\Models\Corp;
  11. use App\Models\Department;
  12. use App\Models\User;
  13. use App\Traits\Singleton;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. use Illuminate\Support\Facades\Redis;
  17. class UserService
  18. {
  19. use Singleton;
  20. const REDIS_USERS = "bs_users";
  21. const REDIS_USERS_OPEN_ID = "bs_users_open_id";
  22. const REDIS_AGH_GCORP = "agh_gcorp";
  23. const WHITE_LIST = "bs_white_list";
  24. const REDIS_USERS_GUID = "bs_user_guid";
  25. const REDIS_AGH_GUID = "agh_guid";
  26. const USER_CURRENT_WORK = "bs_user_current_work";
  27. const WEBSOCKET_USERS = "bs_websocket_users";
  28. public function auth2($activityId, $openid, $avatar, $nickname, $platform)
  29. {
  30. try {
  31. $key = self::REDIS_USERS_OPEN_ID . ":" . $activityId . ":" . $openid;
  32. $userId = Redis::get($key);
  33. if (empty($userId)) {
  34. $user = User::where("activity_id", $activityId)->where("open_id", $openid)->where("status", 1)->first();
  35. if (empty($user)) {
  36. //此微信之前是否有绑定过手机号码
  37. $phones = DB::table("agh_wechats")->where("open_id", $openid)->pluck("phone")->toArray();
  38. if (!empty($phones)) {
  39. $user = User::where("activity_id", $activityId)->whereIn("phone", $phones)->first();
  40. }
  41. }
  42. if (!empty($user)) {
  43. if (!empty($openid)) $user->open_id = $openid;
  44. if (!empty($avatar)) $user->avatar = $avatar;
  45. if (!empty($nickname)) $user->nickname = $nickname;
  46. $user->platform = $platform;
  47. $user->save();
  48. $user = collect($user)->toArray();
  49. $user["department_name"] = DepartmentService::getInstance()->getDepartmentName($user["department_id"]);
  50. Redis::hMset(self::REDIS_USERS . ":" . $user["user_id"], $user);
  51. Redis::expire(self::REDIS_USERS . ":" . $user["user_id"], 600);
  52. Redis::set($key, $user["user_id"]);
  53. Redis::expire($key, 600);
  54. return $user;
  55. }
  56. } else {
  57. return $this->getUser($userId);
  58. }
  59. } catch (\Exception $exception) {
  60. Log::info($exception->getMessage());
  61. }
  62. return null;
  63. }
  64. /**
  65. * 判断是否在白名单里面
  66. */
  67. public function isWhiteList($ystId)
  68. {
  69. return Redis::sismember(self::WHITE_LIST, $ystId);
  70. }
  71. /**
  72. * 获得用户信息
  73. * @param $userId
  74. * @param string $hashKey
  75. * @param bool $force 强制从数据库里面获取
  76. * @return mixed
  77. */
  78. public function getUser($userId, $hashKey = "", $force = false)
  79. {
  80. $key = self::REDIS_USERS . ":" . $userId;
  81. if (!Redis::exists($key) || $force) {
  82. $user = User::find($userId);
  83. if ($user) {
  84. $user = collect($user)->toArray();
  85. $user["department_name"] = DepartmentService::getInstance()->getDepartmentName($user["department_id"]);
  86. Redis::hMset($key, $user);
  87. Redis::expire($key, 600);
  88. } else {
  89. return null;
  90. }
  91. }
  92. if (empty($hashKey)) {
  93. return Redis::hGetAll(self::REDIS_USERS . ":" . $userId);
  94. } else {
  95. return Redis::hGet(self::REDIS_USERS . ":" . $userId, $hashKey);
  96. }
  97. }
  98. /**
  99. * 根据openId增加用户
  100. * @param $activityId
  101. * @param $openid
  102. * @param $avatar
  103. * @param $nickname
  104. * @param $platform
  105. * @param string $departmentName
  106. * @return bool
  107. */
  108. public function addUserByOpenId($activityId, $openid, $avatar, $nickname, $platform, $departmentName = "")
  109. {
  110. $activity = BrainstormingService::getInstance()->get($activityId);
  111. $departmentId = 0;
  112. if (!empty($departmentName)) {
  113. $department = Department::where("activity_id", $activityId)->where("department_name", $departmentName)->first();
  114. if (empty($department)) {
  115. $data = [];
  116. $data['activity_id'] = $activityId;
  117. $data["corp_id"] = $activity["corp_id"];
  118. $data["department_name"] = $departmentName;
  119. $department = Department::create($data);
  120. } else {
  121. if ($department->status == 0) {
  122. $department->status = 1;
  123. $department->save();
  124. }
  125. }
  126. $departmentId = empty($department) ? 0 : $department->department_id;
  127. }
  128. $user = User::where("activity_id", $activityId)
  129. ->where("open_id", $openid)->where("status", 1)->first();
  130. if (empty($user)) {
  131. $query = [];
  132. $query['activity_id'] = $activityId;
  133. $query["corp_id"] = $activity["corp_id"];
  134. $query['open_id'] = $openid;
  135. $query['guid'] = $openid;
  136. $query['name'] = $nickname;
  137. $query['nickname'] = "";
  138. $query['department_id'] = $departmentId;
  139. $query['email'] = "";
  140. $query['phone'] = "";
  141. $query['avatar'] = $avatar;
  142. $query['platform'] = $platform;
  143. User::create($query);
  144. } else {
  145. if ($departmentId != $user->department_id) {
  146. $user->department_id = $departmentId;
  147. $user->save();
  148. }
  149. }
  150. return true;
  151. }
  152. /**
  153. * 使用Guid的用户信息,新增用户
  154. * @param $activityId int 活动ID
  155. * @param $guid int 爱关怀的用户ID
  156. * @param $gCorpId
  157. * @param bool $hasDepartment 是否需要新增部门
  158. * @return mixed
  159. */
  160. public function addUserByGuid($activityId, $guid, $gCorpId, $hasDepartment = false)
  161. {
  162. $gUser = Redis::get(self::REDIS_AGH_GUID . ":" . $guid);
  163. $brainstorming = BrainstormingService::getInstance()->get($activityId);
  164. $gCorp = $this->getCorpByGcorpId($gCorpId);
  165. $corpId = isset($gCorp["corp_id"]) ? $gCorp["corp_id"] : 0;
  166. if ($brainstorming["corp_id"] != $corpId) {
  167. return ["error" => 1000, "corp_short_name" => $brainstorming["corp_short_name"]];
  168. }
  169. if ($gUser) {
  170. $gUser = json_decode($gUser, true);
  171. $departmentId = 0;
  172. if ($hasDepartment) {
  173. if (!empty($gCorp)) {
  174. $department = Department::where("activity_id", $activityId)
  175. ->where("corp_id", $gCorp["corp_id"])->first();
  176. if (empty($department)) {
  177. $department = Department::create([
  178. "activity_id" => $activityId,
  179. "corp_id" => $gCorp["corp_id"],
  180. "department_name" => $gCorp["corp_short_name"]
  181. ]);
  182. }
  183. if ($department) {
  184. $departmentId = $department->department_id;
  185. }
  186. }
  187. }
  188. $user = User::where("activity_id", $activityId)
  189. ->where("guid", $guid)->where("status", 1)->first();
  190. if (empty($user)) {
  191. foreach ($gUser["subUids"] as $subUser) {
  192. if ($subUser["corpId"] == $gCorpId) {
  193. $query = [];
  194. $query['corp_id'] = $gCorp["corp_id"];
  195. $query['activity_id'] = $activityId;
  196. $query['guid'] = $guid;
  197. $query['name'] = $subUser['realName'];
  198. $query['nickname'] = "";
  199. $query['department_id'] = $departmentId;
  200. $query['email'] = $subUser['email'];
  201. $query['phone'] = $subUser['mobile'];
  202. $query['avatar'] = "";
  203. $query['platform'] = "";
  204. User::create($query);
  205. break;
  206. }
  207. }
  208. }
  209. }
  210. return ["error" => 0, "corp_name" => $brainstorming["corp_name"]];
  211. }
  212. /**
  213. * @param $gCorpId
  214. * @return mixed
  215. */
  216. public function getCorpByGcorpId($gCorpId)
  217. {
  218. $redisKey = self::REDIS_AGH_GCORP . ":" . $gCorpId;
  219. $gcorp = Redis::get($redisKey);
  220. if (empty($gcorp)) {
  221. $gcorp = Corp::where("source_id", $gCorpId)->where("source_type", "agh")->first();
  222. if ($gcorp) {
  223. $gcorp = collect($gcorp)->toArray();
  224. Redis::set($redisKey, json_encode($gcorp));
  225. Redis::expire($redisKey, 300);
  226. }
  227. } else {
  228. $gcorp = json_decode($gcorp, true);
  229. }
  230. return $gcorp;
  231. }
  232. /**
  233. * 使用guid认证
  234. * @param $activityId
  235. * @param $openid
  236. * @param $avatar
  237. * @param $nickname
  238. * @param $platform
  239. * @param $guid
  240. * @return array|mixed|null
  241. */
  242. public function authByGuid($activityId, $openid, $avatar, $nickname, $platform, $guid)
  243. {
  244. try {
  245. $key = self::REDIS_USERS_GUID . ":" . $activityId . ":" . $guid;
  246. $userId = Redis::get($key);
  247. if (empty($userId)) {
  248. $user = User::where("activity_id", $activityId)->where("guid", $guid)->where("status", 1)->first();
  249. if (!empty($user)) {
  250. if (!empty($avatar)) $user->avatar = $avatar;
  251. if (!empty($nickname)) $user->nickname = $nickname;
  252. if ($user->name == "***") $user->name = $nickname;
  253. $user->open_id = $openid;
  254. $user->platform = $platform;
  255. $user->save();
  256. $user = collect($user)->toArray();
  257. $user["department_name"] = DepartmentService::getInstance()->getDepartmentName($user["department_id"]);
  258. Redis::hMset(self::REDIS_USERS . ":" . $user["user_id"], $user);
  259. Redis::expire(self::REDIS_USERS . ":" . $user["user_id"], 600);
  260. Redis::set($key, $user["user_id"]);
  261. Redis::expire($key, 600);
  262. return $user;
  263. }
  264. } else {
  265. return $this->getUser($userId);
  266. }
  267. } catch (\Exception $exception) {
  268. Log::info($exception->getMessage());
  269. }
  270. return null;
  271. }
  272. /**
  273. * 获得用户在线信息
  274. * @param $userId
  275. * @return mixed
  276. */
  277. public function getOnlineInfo($userId)
  278. {
  279. return Redis::hGetAll(self::WEBSOCKET_USERS . ":" . $userId);
  280. }
  281. /**
  282. * 设置现在用户正在进行的事项,如答题,PK
  283. * @param $userId
  284. * @param $channel
  285. * @param $id
  286. * @param int $timeout
  287. */
  288. public function setUserCurrentWork($userId, $channel, $id, $timeout = 600)
  289. {
  290. Redis::hMset(self::USER_CURRENT_WORK . ":" . $userId, [
  291. "channel" => $channel,
  292. "id" => $id
  293. ]);
  294. Redis::expire(self::USER_CURRENT_WORK . ":" . $userId, $timeout);
  295. }
  296. /**
  297. * 清除用户的工作项
  298. * @param $userId
  299. */
  300. public function unsetUserCurrentWork($userId)
  301. {
  302. Redis::del(self::USER_CURRENT_WORK . ":" . $userId);
  303. }
  304. /**
  305. * 获取用户当前正在工作项
  306. * @param $userId
  307. * @return mixed
  308. */
  309. public function getUserCurrentWork($userId)
  310. {
  311. return Redis::hGetAll(self::USER_CURRENT_WORK . ":" . $userId);
  312. }
  313. }