<?php
/**
 * Created by PhpStorm.
 * User: guanxl
 * Date: 2018/6/5
 * Time: 11:29
 */

namespace App\Services;
use Illuminate\Support\Facades\Cookie;

class CookieService
{
    /**
     * 获得Oauth的COOKIE
     * @return array
     */

    public static function getOauth(){
        $data = [
            "oauth_open_id" => Cookie::get("oauth_open_id"),
            "oauth_avatar" => Cookie::get("oauth_avatar"),
            "oauth_nickname" => Cookie::get("oauth_nickname"),
            "oauth_platform" => Cookie::get("oauth_platform"),
            "oauth_guid" => Cookie::get("oauth_guid"),
            "oauth_department" => Cookie::get("oauth_department"),
            "oauth_gcorp_id" => Cookie::get("oauth_gcorp_id"),
        ];

        return $data;
    }

    /**
     * 设置oAuth cookies
     * @param $data
     */
    public static function setOauth($data){
        setcookie("oauth_open_id", $data["oauth_open_id"], 0,"/",env("COOKIE_DOMAIN"));
        setcookie("oauth_avatar", $data["oauth_avatar"], 0,"/",env("COOKIE_DOMAIN"));
        setcookie("oauth_nickname", $data["oauth_nickname"], 0,"/",env("COOKIE_DOMAIN"));
        setcookie("oauth_platform", $data["oauth_platform"], 0,"/",env("COOKIE_DOMAIN"));
        setcookie("oauth_guid", $data["oauth_guid"], 0,"/",env("COOKIE_DOMAIN"));
    }

    /**
     * 清除Oauth cookies信息
     */
    public static function unsetOauth(){
        setcookie("oauth_open_id", '', null,"/",env("COOKIE_DOMAIN"));
        setcookie("oauth_avatar", '', null,"/",env("COOKIE_DOMAIN"));
        setcookie("oauth_nickname", '', null,"/",env("COOKIE_DOMAIN"));
        setcookie("oauth_platform", '', null,"/",env("COOKIE_DOMAIN"));
        setcookie("oauth_guid", '', null,"/",env("COOKIE_DOMAIN"));
        setcookie("oauth_department", '', null,"/",env("COOKIE_DOMAIN"));
        setcookie("oauth_gcorp_id", '', null,"/",env("COOKIE_DOMAIN"));
    }

    public static function setQyCorpId($qyCorpId){
        setcookie("qyCorpId", $qyCorpId,null,"/",env("COOKIE_DOMAIN"));
    }

    /**
     * 获得用户信息
     * @return mixed|null
     */
    public static function getBsAuthUser(){
        $userData = Cookie::get("bs_access_user_data");
        $user = empty($userData)? null:json_decode(base64_decode($userData),true);
        return $user;
    }

    /**
     * 获得accessToken的cookie信息
     * @return string
     */
    public static function getBsAccessToken(){
        return Cookie::get("bs_access_token");
    }

    /**
     * 设置认证后的cookie信息
     * @param $user
     * @param $accessToken
     */

    public static function setBsAuth($user,$accessToken){
        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"));
        setcookie("bs_access_token", $accessToken,null,"/",env("COOKIE_DOMAIN"));
    }

    /**
     * 清除认证后的auth Cookie信息
     */
    public static function unsetBsAuth(){
        setcookie("bs_access_user_data", null,0,"/",env("COOKIE_DOMAIN"));
        setcookie("bs_access_token", null,0,"/",env("COOKIE_DOMAIN"));
    }

    /**
     * 增加登录尝试次数
     */
    public static function incrLoginTrialNum(){
        $loginNum = intval(self::getLoginTrialNum())+1;
        setcookie("bs_login_trials",$loginNum,null,"/",env("COOKIE_DOMAIN"));
    }

    /**
     * 获得登录尝试的次数
     * @return string
     */
    public static function getLoginTrialNum(){
        return intval(Cookie::get("bs_login_trials"));
    }

    /**
     * 清除登录尝试次数
     */
    public static function unsetLoginTrialNum(){
        setcookie("bs_login_trials", '', null,"/",env("COOKIE_DOMAIN"));
    }
}