工作中常用php函數

    /**
     * 判斷是否是微信瀏覽器
     *
     * @return bool
     */
    protected function isWeiXinBrowser()
    {
        $userAgent = $this->request->getUserAgent();
        return (false !== stripos($userAgent, 'MicroMessenger'));
    }


    /**
     * 判斷是否是微信小程序
     *
     * @return bool
     */
    protected function isWeiXinMiniProgram()
    {
        $userAgent = $this->request->getUserAgent();
        return (false !== stripos($userAgent, 'miniProgram'));
    }
     
   /**
     * 比較兩個日期的大小
     *
     * @return bool
     */
    private function dateBDate($date1, $date2)
    {
        // 日期1是否大於等於日期2
        $month1 = date("m", strtotime($date1));
        $month2 = date("m", strtotime($date2));
        $day1 = date("d", strtotime($date1));
        $day2 = date("d", strtotime($date2));
        $year1 = date("Y", strtotime($date1));
        $year2 = date("Y", strtotime($date2));
        $from = mktime(0, 0, 0, $month1, $day1, $year1);
        $to = mktime(0, 0, 0, $month2, $day2, $year2);
        if ($from >= $to) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 檢查用戶每天是否是第一次訪問
     */
    protected function checkIsFirstAccess()
    {
        $uuid = $this->cookies->has('21cake_uuid') ? $this->cookies->get('21cake_uuid')->getValue() : '';
        if(!$uuid){
            $uuid = '21cake_uuid'.md5('21cake_uuid_' . time() . mt_rand(1000, 9999));
            $this->cookies->set('21cake_uuid', $uuid, time() + 31536000, '/', '', '.21cake.com'); // 365 * 24 * 60 * 60
        }

        $currentDate = date('Y-m-d',time());
        $source = $this->channel;
        if($this->isWeiXinBrowser() || $this->isWeiXinMiniProgram()){
            $source = 'wx';
        }

        //如果redis中沒有該uuid,說明今天該用戶沒訪問過
        if ($this->redis->setnx($uuid, $currentDate)) {
            $this->redis->expire($uuid, 24*3600);
            $writeData = [
                'user_source' => $source,
                'access_date' => $currentDate,
                'ip' => $this->request->getClientAddress(true),
                'uuid' => $uuid
            ];
            $userAccessMdl = new UserAccess();
            $userAccessMdl->save($writeData);
        }
    }



    /**
     * 檢查用戶每天是否是第一次登陸
     */
    protected function checkIsFirstLogin($memberId)
    {
        $currentDate = date('Y-m-d',time());
        $source = $this->channel;
        if($this->isWeiXinBrowser() || $this->isWeiXinMiniProgram()){
            $source = 'wx';
        }
        $cookieKey = md5('userEveryDayFirstLogin'.$source.$memberId);

        if(!$this->cookies->has($cookieKey) || $this->cookies->get($cookieKey)->getValue() != $currentDate){
            $userLoginDate = UserLoginNums::findFirst([
                'conditions' => 'user_source = :userSource: and login_date = :loginDate:',
                'bind' => [
                    'userSource' => $source,
                    'loginDate' => $currentDate
                ]
            ]);

            $writeCookie = false;
            if(empty($userLoginDate)){
                $writeData = [
                    'user_source' => $source,
                    'login_date' => $currentDate,
                    'login_nums' => 1,
                ];
                $userAccessMdl = new UserLoginNums();
                if($userAccessMdl->save($writeData)){
                    $writeCookie = true;
                }
            }else{
                $writeData = [
                    'login_nums' => $userLoginDate->login_nums + 1,
                    'update_at' => date('Y-m-d H:i:s',time())
                ];
                if($userLoginDate->save($writeData)){
                    $writeCookie = true;
                }
            }

            if($writeCookie){
                $this->cookies->set($cookieKey, $currentDate, time() + 24*3600, '/', '', '.21cake.com');
            }
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章