<?php

namespace app\controller;

use support\Request;
use support\Db;
use yzh52521\EasyHttp\Http;
use yzh52521\EasyHttp\Response;
use yzh52521\EasyHttp\RequestException;

class LayAuth
{
    public function index(Request $request,$appid)
    {
        $appquery= Db::table('App')->where('ID', $appid);
        if($appquery->doesntExist()){
            return view('404');
        }
        $app=$appquery->first();
        $provider= Db::table('Provider')->where('ID', $app->provider)->first();
        $redirecturl='https://'.getenv('weburl').'/auth/lay/'.$appid.'/callback';
        return view('auth', ['app'=>$app,'provider'=>$provider,'redirecturl'=>$redirecturl]);

    }

    public function callback(Request $request,$appid,$gateway)
    {
        $appquery= Db::table('App')->where('ID', $appid);
        if($appquery->doesntExist()){
            return view('404');
        }
        $app=$appquery->first();
        $provider= Db::table('Provider')->where('ID', $app->provider)->first();
        $redirecturl='https://'.getenv('weburl').'/auth/lay/'.$appid.'/callback';
        switch ($gateway) {
            case "qywx":
                $code = $request->input('code','null');
                if($code=='null'){
                    return view('auth', ['app'=>$app,'provider'=>$provider,'special'=>'登陆信息无效','redirecturl'=>$redirecturl]);
                }
                $tokenfile=base_path().'/token/qywx/innerQYWX.token';
                if(file_exists($tokenfile)){
                    $tokencontent=json_decode(file_get_contents($tokenfile));
                    $fulltoken=$tokencontent->token;
                    $ddl=$tokencontent->ddl;
                    if($ddl-time()<180){
                        $reapply=true;
                    }else{
                        $reapply=false;
                    }
                }else{
                    $reapply=true;
                }
                if($reapply==true){
                    $apply=$response = Http::get('https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='.getenv('appid').'&corpsecret='.getenv('token'))->json();
                    $fulltoken=$apply->access_token;
                        $ddl=time()+$apply->expires_in;
                        $file=fopen($tokenfile,"w");
                        fwrite($file, json_encode(array('token'=>$fulltoken,'ddl'=>$ddl)));
                        fclose($file);
                }
                $lookup= Http::get('https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token='.$fulltoken.'&code='.$code)->json();
                if($lookup->errcode!=0){
                    return view('auth', ['app'=>$app,'provider'=>$provider,'special'=>'登陆信息无效','redirecturl'=>$redirecturl]);
                }else{
                $userid=$lookup->userid;
                $userinfo=Http::get('https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token='.$fulltoken.'&userid='.$userid)->json();
                $username=$userinfo->name;
                $userposition=$userinfo->position;
                $WT=json_encode(['id'=>$userid,'name'=>$username,'position'=>$userposition,'time'=>time()]);
                $key = getenv('aeskey');
                $iv = getenv('aesiv');
                $WT = encryptAES($WT, $key, $iv);  
                $dest=$app->redirect;
                return view('success', ['app'=>$app,'provider'=>$provider,'dest'=>$dest,'userinfo'=>$userinfo])->cookie('WT', $WT,time()+9600,'/','.laysense.cn');
                }
                break;
            default:
                return view('auth', ['app'=>$app,'provider'=>$provider,'special'=>'验证方式无效或不存在','redirecturl'=>$redirecturl]);
        }

        return view('auth', ['app'=>$app,'provider'=>$provider,'special'=>'验证方式无效或不存在','redirecturl'=>$redirecturl]);
    }

    public function check(Request $request,$appid)
    {
        return json(['code' => 0, 'msg' => 'ok']);
    }

}
