CorpController.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: guanxl
  5. * Date: 2018/9/28
  6. * Time: 11:41
  7. */
  8. namespace App\Http\Controllers;
  9. use App\Models\Corp;
  10. use App\Services\BrainstormingService;
  11. use App\Services\UserService;
  12. use Illuminate\Support\Facades\Cookie;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Http\Request;
  15. use App\Models\User;
  16. use Illuminate\Support\Facades\Log;
  17. use Illuminate\Support\Facades\Redis;
  18. class CorpController extends BaseController
  19. {
  20. /**
  21. * 企业活动列表
  22. * @param Request $request
  23. * @return \Illuminate\Http\JsonResponse
  24. */
  25. public function activityList(Request $request)
  26. {
  27. try{
  28. $ddCorpId = $request->get("dd_corp_id");
  29. $corpId = 0;
  30. if(!empty($ddCorpId)){
  31. $corp = Corp::where("dd_corp_id",$ddCorpId)->first();
  32. if($corp){
  33. $corpId = $corp->corp_id;
  34. }
  35. }else{
  36. $corpId = $request->get("corp_id");
  37. }
  38. if(empty($corpId)){
  39. $corpId = empty(Cookie::get("oauth_corp_id"))? 0:Cookie::get("oauth_corp_id");
  40. }
  41. $status = $request->get("status","running");
  42. $redisKey = "bs_corp:".$status.":".$corpId;
  43. //$redisValue = Redis::get($redisKey);
  44. $redisValue = "";
  45. if(empty($redisValue)){
  46. $curTime = time();
  47. $where = " and A.corp_id={$corpId} and A.status=1 and is_complete_set=1";
  48. switch ($status) {
  49. case "upcoming":
  50. $where .= " and A.start_time>".$curTime;
  51. break;
  52. case "finish":
  53. $where .= " and A.end_time<".$curTime;
  54. break;
  55. default:
  56. $where .= " and (A.start_time<$curTime and A.end_time>$curTime)";
  57. break;
  58. }
  59. $data = [];
  60. $activitys = DB::select("select A.activity_id,A.title,B.share_home_desc,A.start_time,A.end_time,B.top_banner from agh_activitys as A,bs_configs as B where A.activity_id=B.activity_id ".$where." limit 0,100");
  61. foreach ($activitys as $activity){
  62. $userCount = User::where("activity_id",$activity->activity_id)->where("status",1)->count();
  63. $data[] = [
  64. "activity_id" => $activity->activity_id,
  65. "title" => $activity->title,
  66. "desc" => $activity->share_home_desc,
  67. "start_time" => $activity->start_time*1000,
  68. "end_time" => $activity->end_time*1000,
  69. "top_banner" => $activity->top_banner,
  70. "url" => env("WEB_URL")."?activity_id=".$activity->activity_id,
  71. "user_count" => $userCount
  72. ];
  73. }
  74. Redis::setex($redisKey,300,json_encode($data));
  75. }else{
  76. $data = json_decode($redisValue,true);
  77. }
  78. return response()->json([
  79. "error" => 0,
  80. "message" => "OK",
  81. "data" => $data
  82. ]);
  83. }catch (\Exception $exception){
  84. Log::info($exception->getTraceAsString());
  85. return response()->json([
  86. "error" => 0,
  87. "message" => "OK",
  88. "data" => []
  89. ]);
  90. }
  91. }
  92. /**
  93. * 我加入的活动
  94. * @param Request $request
  95. * @return \Illuminate\Http\JsonResponse
  96. */
  97. public function joinActivity(Request $request){
  98. try{
  99. $openId = $request->get("openid");
  100. $ddCorpId = $request->get("dd_corp_id");
  101. $corpId = 0;
  102. if(!empty($ddCorpId)){
  103. $corp = Corp::where("dd_corp_id",$ddCorpId)->first();
  104. if($corp){
  105. $corpId = $corp->corp_id;
  106. }
  107. }else{
  108. $corpId = $request->get("corp_id");
  109. }
  110. $data = [];
  111. if($openId){
  112. $redisKey = "bs_corp:activitys:".$openId;
  113. //$redisValue = Redis::get($redisKey);
  114. $redisValue = "";
  115. if(empty($redisValue)){
  116. $where = " and B.open_id='$openId' and A.corp_id='$corpId' and A.status=1";
  117. $activitys = DB::select("select A.activity_id,A.title,C.share_home_desc,A.start_time,A.end_time from agh_activitys as A,bs_users as B,bs_configs as C where A.activity_id=C.activity_id and A.activity_id=B.activity_id ".$where." limit 0,100");
  118. foreach ($activitys as $activity){
  119. $activityRedis = BrainstormingService::getInstance()->get($activity->activity_id);
  120. $userCount = User::where("activity_id",$activity->activity_id)->where("status",1)->count();
  121. $data[] = [
  122. "activity_id" => $activity->activity_id,
  123. "title" => $activity->title,
  124. "desc" => $activity->share_home_desc,
  125. "start_time" => $activity->start_time*1000,
  126. "end_time" => $activity->end_time*1000,
  127. "top_banner" => $activityRedis["top_banner"],
  128. "url" => env("WEB_URL")."?activity_id=".$activity->activity_id,
  129. "user_count" => $userCount
  130. ];
  131. }
  132. Redis::setex($redisKey,300,json_encode($data));
  133. }else{
  134. $data = json_decode($redisValue,true);
  135. }
  136. }
  137. return response()->json([
  138. "error" => 0,
  139. "message" => "OK",
  140. "data" => $data
  141. ]);
  142. }catch (\Exception $exception){
  143. Log::info($exception->getTraceAsString());
  144. return response()->json([
  145. "error" => 0,
  146. "message" => "OK",
  147. "data" => []
  148. ]);
  149. }
  150. }
  151. }