CookieService.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: guanxl
  5. * Date: 2018/6/5
  6. * Time: 11:29
  7. */
  8. namespace App\Services;
  9. use Illuminate\Support\Facades\Cookie;
  10. class CookieService
  11. {
  12. /**
  13. * 获得Oauth的COOKIE
  14. * @return array
  15. */
  16. public static function getOauth(){
  17. $data = [
  18. "oauth_open_id" => Cookie::get("oauth_open_id"),
  19. "oauth_avatar" => Cookie::get("oauth_avatar"),
  20. "oauth_nickname" => Cookie::get("oauth_nickname"),
  21. "oauth_platform" => Cookie::get("oauth_platform"),
  22. "oauth_guid" => Cookie::get("oauth_guid"),
  23. "oauth_department" => Cookie::get("oauth_department"),
  24. "oauth_gcorp_id" => Cookie::get("oauth_gcorp_id"),
  25. ];
  26. return $data;
  27. }
  28. /**
  29. * 设置oAuth cookies
  30. * @param $data
  31. */
  32. public static function setOauth($data){
  33. setcookie("oauth_open_id", $data["oauth_open_id"], 0,"/",env("COOKIE_DOMAIN"));
  34. setcookie("oauth_avatar", $data["oauth_avatar"], 0,"/",env("COOKIE_DOMAIN"));
  35. setcookie("oauth_nickname", $data["oauth_nickname"], 0,"/",env("COOKIE_DOMAIN"));
  36. setcookie("oauth_platform", $data["oauth_platform"], 0,"/",env("COOKIE_DOMAIN"));
  37. setcookie("oauth_guid", $data["oauth_guid"], 0,"/",env("COOKIE_DOMAIN"));
  38. }
  39. /**
  40. * 清除Oauth cookies信息
  41. */
  42. public static function unsetOauth(){
  43. setcookie("oauth_open_id", '', null,"/",env("COOKIE_DOMAIN"));
  44. setcookie("oauth_avatar", '', null,"/",env("COOKIE_DOMAIN"));
  45. setcookie("oauth_nickname", '', null,"/",env("COOKIE_DOMAIN"));
  46. setcookie("oauth_platform", '', null,"/",env("COOKIE_DOMAIN"));
  47. setcookie("oauth_guid", '', null,"/",env("COOKIE_DOMAIN"));
  48. setcookie("oauth_department", '', null,"/",env("COOKIE_DOMAIN"));
  49. setcookie("oauth_gcorp_id", '', null,"/",env("COOKIE_DOMAIN"));
  50. }
  51. public static function setQyCorpId($qyCorpId){
  52. setcookie("qyCorpId", $qyCorpId,null,"/",env("COOKIE_DOMAIN"));
  53. }
  54. /**
  55. * 获得用户信息
  56. * @return mixed|null
  57. */
  58. public static function getBsAuthUser(){
  59. $userData = Cookie::get("bs_access_user_data");
  60. $user = empty($userData)? null:json_decode(base64_decode($userData),true);
  61. return $user;
  62. }
  63. /**
  64. * 获得accessToken的cookie信息
  65. * @return string
  66. */
  67. public static function getBsAccessToken(){
  68. return Cookie::get("bs_access_token");
  69. }
  70. /**
  71. * 设置认证后的cookie信息
  72. * @param $user
  73. * @param $accessToken
  74. */
  75. public static function setBsAuth($user,$accessToken){
  76. setcookie("bs_access_user_data", base64_encode(json_encode(["user_id" => $user["user_id"],"corp_id"=>$user["corp_id"],"activity_id"=>$user["activity_id"]])),null,"/",env("COOKIE_DOMAIN"));
  77. setcookie("bs_access_token", $accessToken,null,"/",env("COOKIE_DOMAIN"));
  78. }
  79. /**
  80. * 清除认证后的auth Cookie信息
  81. */
  82. public static function unsetBsAuth(){
  83. setcookie("bs_access_user_data", null,0,"/",env("COOKIE_DOMAIN"));
  84. setcookie("bs_access_token", null,0,"/",env("COOKIE_DOMAIN"));
  85. }
  86. /**
  87. * 增加登录尝试次数
  88. */
  89. public static function incrLoginTrialNum(){
  90. $loginNum = intval(self::getLoginTrialNum())+1;
  91. setcookie("bs_login_trials",$loginNum,null,"/",env("COOKIE_DOMAIN"));
  92. }
  93. /**
  94. * 获得登录尝试的次数
  95. * @return string
  96. */
  97. public static function getLoginTrialNum(){
  98. return intval(Cookie::get("bs_login_trials"));
  99. }
  100. /**
  101. * 清除登录尝试次数
  102. */
  103. public static function unsetLoginTrialNum(){
  104. setcookie("bs_login_trials", '', null,"/",env("COOKIE_DOMAIN"));
  105. }
  106. }