PkApiController.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hanle
  5. * Date: 2018/8/16
  6. * Time: 16:30
  7. */
  8. namespace App\Http\Controllers;
  9. use App\Models\Department;
  10. use App\Services\BrainstormingService;
  11. use App\Services\DepartmentService;
  12. use App\Services\PkService;
  13. use App\Services\PushMessageService;
  14. use App\Services\TopService;
  15. use App\Services\UserService;
  16. use Laravel\Lumen\Routing\Controller as BaseController;
  17. use Illuminate\Http\Request;
  18. use App\Models\PkRoom;
  19. use App\Models\User;
  20. use Illuminate\Support\Facades\Redis;
  21. use Illuminate\Support\Facades\DB;
  22. //class PkApiController extends BaseController
  23. class PkApiController extends Controller
  24. {
  25. const ONLINE_KEY = 'bs_online_list:';
  26. const USER_STATUS_KEY = 'bs_user_current_work:';
  27. const PK_READY = 'bs_pk_ready:';
  28. // protected $userId;
  29. // protected $activityId;
  30. // protected $user;
  31. //
  32. // public function __construct()
  33. // {
  34. // $this->userId = 723613;
  35. // $this->activityId = 521;
  36. // $this->user = UserService::getInstance()->getUser($this->userId);
  37. // }
  38. /**
  39. * 获取活动在线人员信息及状态
  40. * @param Request $request
  41. * @return \Illuminate\Http\JsonResponse
  42. */
  43. public function getOnlineUsers(Request $request){
  44. $params = $request->all();
  45. $activityId = $this->activityId;
  46. $keyword = empty($params['keyword'])?'':$params['keyword'];
  47. $user_id = $this->userId;
  48. $onlineUsers= Redis::hgetAll(self::ONLINE_KEY.$activityId); //获取活动在线人员
  49. //获取段位配置
  50. $activityInfo = BrainstormingService::getInstance()->get($activityId);
  51. $level_start_config = json_decode($activityInfo['level_star_config'], true);
  52. if(!empty($onlineUsers)) {
  53. $userIds = array_keys($onlineUsers);
  54. $onlineUsersInfo = TopService::getInstance()->getUsers($activityId, $userIds);
  55. //数据组装
  56. $data = [];
  57. foreach ($onlineUsersInfo as $k => $v) {
  58. //模糊搜索判断
  59. if (!empty($keyword)) {
  60. $is_search_word = strstr($v['name'], $keyword);
  61. if (!$is_search_word) {
  62. continue;
  63. }
  64. }
  65. //如果为null则跳过
  66. if(empty($v)){
  67. continue;
  68. }
  69. //剔除自己并获取自己的排名信息
  70. if ($user_id == $v['user_id']) {
  71. continue;
  72. }
  73. $user = $v;
  74. $curWork = Redis::hGetAll(self::USER_STATUS_KEY . $v['user_id']); //获取用户状态 存在则为正忙,不存在则为正闲
  75. if ($curWork) {
  76. switch ($curWork["channel"]) {
  77. case "pk":
  78. $user["is_busy"] = 1;
  79. $user["busy_message"] = "正在PK";
  80. break;
  81. case "blockade":
  82. $user["is_busy"] = 1;
  83. $user["busy_message"] = "正在答题";
  84. break;
  85. }
  86. } else {
  87. //用户的被邀请状态
  88. $invite_status = Redis::get(self::PK_READY . $this->userId . ":" . $v['user_id']);
  89. if ($invite_status) {
  90. $user['is_invite'] = 1;
  91. }
  92. $user["is_busy"] = 0;
  93. }
  94. $user['user_level_name'] = $level_start_config[$v['user_level']]['name'];
  95. $user['user_name'] = $user['name'];
  96. $data[] = $user;
  97. }
  98. //只返回一百条
  99. $result_status = collect($data)->sortBy('is_busy')->values()->all();
  100. } else {
  101. //只返回一百条
  102. $result_status = [];
  103. }
  104. //获取当前用户的排名
  105. $user_self['user_level_name'] = isset($level_start_config[$this->user['user_level']]['name']) ? $level_start_config[$this->user['user_level']]['name'] : '无';
  106. $user_self['level_need_star'] = intval($level_start_config[$this->user['user_level']]['star']);
  107. $user_self['user_level'] = intval($this->user['user_level']);
  108. $user_self['user_star'] = intval($this->user['user_star']);
  109. $user_self['avatar'] = $this->user['avatar'];
  110. $user_self['name'] = $this->user['name'];
  111. $user_self['department_name'] = $this->user['department_name'];
  112. $user_self['index'] = intval(TopService::getInstance()->getPkRank($this->activityId, $this->userId));
  113. return response()->json([
  114. "error" =>0,
  115. "message" =>"OK",
  116. "data" =>[
  117. 'online_users' =>collect($result_status)->take(100),
  118. 'user_self' =>$user_self
  119. ]
  120. ]);
  121. }
  122. /**
  123. * 获取排行榜信息与当前用户信息
  124. * @param Request $request
  125. * @return \Illuminate\Http\JsonResponse
  126. */
  127. public function getTop(Request $request){
  128. $activity_id = $this->activityId;
  129. //获取排行信息
  130. $topUsers = TopService::getInstance()->getPkTop($activity_id);
  131. //获取段位配置信息
  132. $activityInfo = BrainstormingService::getInstance()->get($activity_id);
  133. $levelStartConfig = json_decode($activityInfo['level_star_config'], true);
  134. //获取当前用户的排行等信息
  135. $userSelfInfoAll = UserService::getInstance()->getUser($this->userId);
  136. $userSelfInfo['index'] = TopService::getInstance()->getPkRank($activity_id, $this->userId);
  137. $userSelfInfo['user_level_name'] = isset($levelStartConfig[$userSelfInfoAll['user_level']]['name'])?$levelStartConfig[$userSelfInfoAll['user_level']]['name']:'无';
  138. $userSelfInfo['level_need_star'] = intval($levelStartConfig[$userSelfInfoAll['user_level']]['star']);
  139. //排行榜用户信息组装
  140. foreach ($topUsers as $k=>$v) {
  141. $topUsers[$k]['user_level_name'] = isset($levelStartConfig[$v['user_level']]['name'])?$levelStartConfig[$v['user_level']]['name']:'无';
  142. }
  143. return response()->json([
  144. "error" =>0,
  145. "message" =>"OK",
  146. "data" =>[
  147. 'top'=>collect($topUsers)->take(100),
  148. 'user_self'=>$userSelfInfo
  149. ]
  150. ]);
  151. }
  152. /**
  153. * 发起PK
  154. * @param Request $request
  155. * @return \Illuminate\Http\JsonResponse
  156. */
  157. public function postChallenge(Request $request){
  158. $params = $request->all();
  159. //参数过滤
  160. if(empty($params['invite_user_id'])){
  161. return response()->json([
  162. "error" =>1,
  163. "message" =>"参数不全,无法发起PK"
  164. ]);
  165. }
  166. $sponsor_user_id = $this->userId; //挑战者ID
  167. $invite_user_id = $params['invite_user_id']; //被挑战者ID
  168. Redis::del(self::USER_STATUS_KEY.$sponsor_user_id); //清除用户状态缓存(默认可操作邀请,则状态为空闲状态)
  169. $invite_status = Redis::hGetAll(self::USER_STATUS_KEY.$invite_user_id);
  170. $sponsor_user_info = UserService::getInstance()->getUser($sponsor_user_id);
  171. $invite_user_info = UserService::getInstance()->getUser($invite_user_id);
  172. if($invite_status){
  173. return response()->json([
  174. "error" =>1,
  175. "message" =>"对方正忙,无法发起PK"
  176. ]);
  177. }
  178. if($sponsor_user_info['status'] == 0 || $invite_user_info['status'] == 0){
  179. return response()->json([
  180. "error" =>1,
  181. "message" =>"用户不存在,无法发起PK"
  182. ]);
  183. }
  184. $data = [
  185. 'sponsor_user_id' =>$sponsor_user_id, //发起挑战者
  186. 'invite_user_id' =>$invite_user_id, //被挑战者
  187. 'name' =>$sponsor_user_info['name']
  188. ];
  189. //挑战消息推送
  190. PushMessageService::getInstance()->pushByUserId('pk','inviteNotify',$data, $invite_user_id);
  191. //建立被邀请关系
  192. Redis::setex(self::PK_READY.$sponsor_user_id.":".$invite_user_id, 10, $invite_user_id);
  193. return response()->json([
  194. "error" =>0,
  195. "message" =>"OK",
  196. ]);
  197. }
  198. /**
  199. * PK响应接口
  200. * @param Request $request
  201. * @return \Illuminate\Http\JsonResponse
  202. */
  203. public function postPkResponse(Request $request){
  204. $params = $request->all();
  205. //参数过滤
  206. if(empty($params['sponsor_user_id'])){
  207. return response()->json([
  208. "error" =>1,
  209. "message" =>"参数不全,无法接受PK"
  210. ]);
  211. }
  212. $answer = empty($params['answer'])?0:$params['answer'];
  213. if($answer == 1){ //同意进行PK
  214. $res = PkService::getInstance()->startPk($this->activityId, $params['sponsor_user_id'], $this->userId);
  215. if(!empty($res)){
  216. return response()->json([
  217. 'error'=>0,
  218. 'message'=>'OK',
  219. "data"=>[
  220. 'roomId'=>$res
  221. ]
  222. ]);
  223. } else {
  224. return response()->json([
  225. 'error'=>1,
  226. 'message'=>'PK开启失败'
  227. ]);
  228. }
  229. } elseif($answer == 2) { //响应超时
  230. $data = [
  231. 'sponsor_user_id' =>$params['sponsor_user_id'], //发起挑战者
  232. 'name' =>$this->user['name'],
  233. 'message' =>'对方无应答'
  234. ];
  235. //挑战消息推送
  236. PushMessageService::getInstance()->pushByUserId('pk','timeoutNotify',$data, $params['sponsor_user_id']);
  237. return response()->json([
  238. 'error'=>1,
  239. 'message'=>'对方无应答,请稍后邀请!!'
  240. ]);
  241. } else { //拒绝PK
  242. $sponsor_info = $this->user;
  243. $data = [
  244. 'sponsor_user_id' =>$params['sponsor_user_id'], //发起挑战者
  245. 'name' =>$sponsor_info['name'],
  246. 'message' =>'邀请被拒绝'
  247. ];
  248. //挑战消息推送
  249. PushMessageService::getInstance()->pushByUserId('pk','refuseNotify',$data, $params['sponsor_user_id']);
  250. return response()->json([
  251. 'error'=>1,
  252. 'message'=>'邀请已拒绝!!'
  253. ]);
  254. }
  255. }
  256. }
  257. ?>