WebController.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: guanxl
  5. * Date: 2018/3/18
  6. * Time: 23:25
  7. */
  8. namespace App\Http\Controllers;
  9. use App\Libs\AppClient;
  10. use App\Services\AccessToken;
  11. use App\Services\BrainstormingService;
  12. use App\Services\CookieService;
  13. use App\Services\ErrorMessageService;
  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 Illuminate\Support\Facades\Log;
  19. use Illuminate\Support\Facades\Cookie;
  20. class WebController extends BaseController
  21. {
  22. protected $user;
  23. protected $activityId;
  24. protected $oauth;
  25. public function __construct(Request $request)
  26. {
  27. Log::info("1");
  28. $this->activityId = $request->get("activity_id");
  29. $this->oauth = CookieService::getOauth();
  30. Log::info($this -> oauth);
  31. //判断是否已经登录
  32. $userData = CookieService::getBsAuthUser();
  33. Log::info($userData);
  34. if ($userData) {
  35. $user = UserService::getInstance()->getUser($userData["user_id"]);
  36. if ($user) {
  37. if ($this->activityId == $userData["activity_id"]) {
  38. if (AccessToken::getInstance()->getAccessToken($user["user_id"]) != CookieService::getBsAccessToken()) {
  39. CookieService::unsetBsAuth();
  40. }
  41. }
  42. }
  43. }
  44. try {
  45. //获取活动信息
  46. $activity = BrainstormingService::getInstance()->get($this->activityId);
  47. if(isset($activity["qy_corp_id"])){
  48. CookieService::setQyCorpId($activity["qy_corp_id"]);
  49. }
  50. if (empty($activity)) {
  51. ErrorMessageService::redirect("您要参加的活动不存在!");
  52. die;
  53. }
  54. $browser = AppClient::browserType();
  55. switch ($activity["client_type"]) {
  56. case "wechat":
  57. if ($browser != AppClient::BROWSER_TYPE_WECHAT) {
  58. ErrorMessageService::redirect("请在微信客户端打开链接!");
  59. die;
  60. }
  61. break;
  62. case "wesuit":
  63. if ($browser != AppClient::BROWSER_TYPE_WESUITAPP) {
  64. ErrorMessageService::redirect("请在企业微信客户端打开链接!");
  65. die;
  66. }
  67. break;
  68. case "cmb_mobile_oa":
  69. /*if($browser!=AppClient::BROWSER_TYPE_CMB_MOBILE_OA){
  70. ErrorMessageService::redirect("请在招行移事通打开链接!");
  71. die;
  72. }*/
  73. break;
  74. case "pingan":
  75. break;
  76. case "pingan_zhiniao":
  77. if ($browser != AppClient::BROWSER_TYPE_PINGAN_ZHINIAO) {
  78. header("location:http://a.app.qq.com/o/simple.jsp?pkgname=com.pingan.xueyuan");
  79. die;
  80. }
  81. break;
  82. case "yunzhijia":
  83. if ($browser != AppClient::BROWSER_TYPE_YUNZHIJIA || !AppClient::isMobile()) {
  84. ErrorMessageService::redirect("请在云之家移动端打开链接!");
  85. }
  86. break;
  87. case "kara":
  88. /*if ($browser != AppClient::BROWSER_TYPE_KARA || !AppClient::isMobile()) {
  89. ErrorMessageService::redirect("请在信部落移动端打开链接!");
  90. }*/
  91. break;
  92. case "dingtalk":
  93. if ($browser != AppClient::BROWSER_TYPE_DINGTALK || !AppClient::isMobile()) {
  94. ErrorMessageService::redirect("请在钉钉移动端打开链接!");
  95. }
  96. break;
  97. /*default:
  98. if (!AppClient::isWxBrowser()) {
  99. ErrorMessageService::redirect("请在企业微信或微信客户端打开链接!");
  100. die;
  101. }
  102. break;*/
  103. }
  104. $openId = $this->oauth["oauth_open_id"];
  105. $avatar = $this->oauth["oauth_avatar"];
  106. $nickname = $this->oauth["oauth_nickname"];
  107. $platform = $this->oauth["oauth_platform"];
  108. $department = $this->oauth["oauth_department"];
  109. $guid = $this->oauth["oauth_guid"];
  110. $gCorpId = $this->oauth["oauth_gcorp_id"];
  111. $userData = CookieService::getBsAuthUser();
  112. if ($userData) {
  113. if ($this->activityId != $userData["activity_id"]) {
  114. CookieService::unsetBsAuth();
  115. }
  116. }
  117. if (!empty($this->oauth["oauth_open_id"])) {
  118. Log::info("oauth_open_id:" . $openId);
  119. //校验活动进行方式 wechat 纯微信(导入人员名单) agh_import 微信+企业微信(导人员入名单) agh 微信+企业微信(不导入人员名单) agh_corps 多企业使用
  120. //根据认证方式去处理是否需要新增人员或者单位信息。
  121. switch ($activity['auth_type']) {
  122. case "wechat_auto_add":
  123. UserService::getInstance()->addUserByOpenId($this->activityId, $openId, $avatar, $nickname, $platform);
  124. $user = UserService::getInstance()->auth2($this->activityId, $openId, $avatar, $nickname, $platform);
  125. break;
  126. case "cmb_mobile_oa_add":
  127. UserService::getInstance()->addUserByOpenId($this->activityId, $openId, $avatar, $nickname, $platform);
  128. $user = UserService::getInstance()->auth2($this->activityId, $openId, $avatar, $nickname, $platform);
  129. break;
  130. case "cmb_mobile_oa":
  131. //UserService::getInstance()->addUserByGuid($this->activityId, $guid, $gCorpId);
  132. $user = UserService::getInstance()->authByGuid($this->activityId, $openId, $avatar, $nickname, $platform, $guid);
  133. break;
  134. case "yunzhijia_add":
  135. UserService::getInstance()->addUserByOpenId($this->activityId, $openId, $avatar, $nickname, $platform);
  136. $user = UserService::getInstance()->auth2($this->activityId, $openId, $avatar, $nickname, $platform);
  137. break;
  138. case "yunzhijia":
  139. $user = UserService::getInstance()->authByGuid($this->activityId, $openId, $avatar, $nickname, $platform, $guid);
  140. break;
  141. case "dingtalk_add":
  142. UserService::getInstance()->addUserByOpenId($this->activityId, $openId, $avatar, $nickname, $platform);
  143. $user = UserService::getInstance()->auth2($this->activityId, $openId, $avatar, $nickname, $platform);
  144. break;
  145. case "dingtalk":
  146. $user = UserService::getInstance()->authByGuid($this->activityId, $openId, $avatar, $nickname, $platform, $guid);
  147. break;
  148. case "kara_sbu_add":
  149. UserService::getInstance()->addUserByOpenId($this->activityId, $openId, $avatar, $nickname, $platform,$department);
  150. $user = UserService::getInstance()->auth2($this->activityId, $openId, $avatar, $nickname, $platform);
  151. break;
  152. case "kara_add":
  153. UserService::getInstance()->addUserByOpenId($this->activityId, $openId, $avatar, $nickname, $platform);
  154. $user = UserService::getInstance()->auth2($this->activityId, $openId, $avatar, $nickname, $platform);
  155. break;
  156. case "kara":
  157. $user = UserService::getInstance()->authByGuid($this->activityId, $openId, $avatar, $nickname, $platform, $guid);
  158. break;
  159. case "pingan_zhiniao_add":
  160. UserService::getInstance()->addUserByOpenId($this->activityId, $openId, $avatar, $nickname, $platform);
  161. $user = UserService::getInstance()->auth2($this->activityId, $openId, $avatar, $nickname, $platform);
  162. break;
  163. case "pingan_zhiniao":
  164. $user = UserService::getInstance()->authByGuid($this->activityId, $openId, $avatar, $nickname, $platform, $guid);
  165. break;
  166. case "agh":
  167. $ret = UserService::getInstance()->addUserByGuid($this->activityId, $guid, $gCorpId);
  168. if($ret["error"]>0){
  169. CookieService::unsetOauth();
  170. Log::info($_COOKIE);
  171. ErrorMessageService::redirect("抱歉,你不在(".$ret["corp_short_name"].")活动名单中");
  172. }
  173. $user = UserService::getInstance()->authByGuid($this->activityId, $openId, $avatar, $nickname, $platform, $guid);
  174. break;
  175. case "agh_corps":
  176. UserService::getInstance()->addUserByGuid($this->activityId, $guid, $gCorpId, true);
  177. $user = UserService::getInstance()->authByGuid($this->activityId, $openId, $avatar, $nickname, $platform, $guid);
  178. break;
  179. default:
  180. $user = UserService::getInstance()->auth2($this->activityId, $openId, $avatar, $nickname, $platform);
  181. break;
  182. }
  183. //如果认证为纯微信认证
  184. if ($activity['auth_type'] == "agh"||$activity["client_type"]=="yunzhijia") {
  185. //若$user为空,则清除原有cookie信息,重新设置cookie
  186. if (empty($user)) {
  187. if (CookieService::getLoginTrialNum() < 3) {
  188. //清除Oauth的cookie
  189. CookieService::unsetOauth();
  190. CookieService::incrLoginTrialNum();
  191. $this->oauth["oauth_open_id"] = null;
  192. } else {
  193. CookieService::unsetLoginTrialNum();
  194. ErrorMessageService::redirect("抱歉,你不在活动名单中!");
  195. }
  196. }
  197. }
  198. if ($user) {
  199. $this->user = $user;
  200. $accessToken = AccessToken::getInstance()->create($user);
  201. //生成认证后的登录cookie
  202. CookieService::setBsAuth($user, $accessToken);
  203. Log::info($user);
  204. }
  205. }
  206. } catch (\Exception $exception) {
  207. Log::info("NOT found activity_id" . $exception->getMessage());
  208. Log::error($exception->getTraceAsString());
  209. ErrorMessageService::redirect("您要参加的活动不存在!");
  210. exit;
  211. }
  212. }
  213. public function room($id)
  214. {
  215. $getData = $_GET;
  216. unset($getData["_url_"]);
  217. $redirectUrl = env("WEB_URL") . "/pk/room/{$id}?".http_build_query($getData);
  218. $frontendRedirectUrl = env("WEB_URL") . "/static/v1/#/beatdown?room_id=" . $id . "&activity_id={$this->activityId}&time=" . time();
  219. $this->sso($redirectUrl, $frontendRedirectUrl);
  220. }
  221. public function index(Request $request)
  222. {
  223. if(!empty($request->get("token"))){
  224. //return $request->get("token");
  225. }
  226. $getData = $_GET;
  227. unset($getData["_url_"]);
  228. $redirectUrl = env("WEB_URL") . "?".http_build_query($getData);
  229. $frontendRedirectUrl = env("WEB_URL") . "/static/#/?useOpenApi=1&activity_id={$this->activityId}&time=" . time();
  230. Log::info($frontendRedirectUrl);
  231. $this->sso($redirectUrl, $frontendRedirectUrl);
  232. }
  233. public function frontend(Request $request)
  234. {
  235. $redirectUrl = env("WEB_URL") . "/frontend?activity_id=" . $this->activityId . "&redirect=" . urlencode($request->get("redirect"));
  236. $frontendRedirectUrl = $request->get("redirect");
  237. $this->sso($redirectUrl, $frontendRedirectUrl);
  238. }
  239. /**
  240. * 单点登录,生成cookie信息
  241. * @param $redirectUrl
  242. * @param $frontRedirectUrl
  243. */
  244. protected function sso($redirectUrl, $frontRedirectUrl)
  245. {
  246. if (empty($this->oauth["oauth_open_id"])) {
  247. $queryData = $_GET;
  248. unset($queryData["_url_"]);
  249. //如果是知鸟APP
  250. if(AppClient::browserType()==AppClient::BROWSER_TYPE_PINGAN_ZHINIAO){
  251. $queryData["useOpenApi"] = 1;
  252. }
  253. $queryData["action"] = "login";
  254. $queryData["redirect"] = $redirectUrl;
  255. $brainstroming = BrainstormingService::getInstance()->get($this->activityId);
  256. if (isset($brainstroming["gcorp_id"])) {
  257. $queryData["gcorp_id"] = $brainstroming["gcorp_id"];
  258. }
  259. $url = env("SSO_URL") . "?" . http_build_query($queryData);
  260. Log::info($url);
  261. header("location: " . $url);
  262. } else {
  263. header("location:" . $frontRedirectUrl);
  264. }
  265. }
  266. }