<?php

namespace App\Http\Controllers\API\mH5;

use App\Http\Controllers\Controller;
use App\Services\mH5\PersonService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use JWT;
use function PHPUnit\Framework\isEmpty;


class LoginController extends Controller
{
    //微信登录授权获取code ,重新引导页面返回前端
    public function wxGetCode(Request $request) {
        $code = $request->query('code');

        $env= $request->query('state');
        if($env=='production'){
            return redirect(config('app.globals.WxReturnCodeUrl_server').$code);
        }else{
            return redirect(config('app.globals.WxReturnCodeUrl_local').$code);
        }


    }

    //微信登录授权获取openid
    public function wxGetOpenid(){
        $result=array();
        $code =request('code');
        $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".config('app.globals.WxAppid')."&secret=".config('app.globals.WxAppSecret')."&code=".$code."&grant_type=authorization_code";
        $response = Http::get($url);
        if ($response->successful()) {
            $data = $response->json(); // 获取响应的 JSON 数据
            //dd($data['openid']);
           $openid=$data['openid'];

            $accessTimeout = JWT::GetGetSecretTimeOut();
            $refreshTimeout = JWT::GetRefreshTokenTimeOut();
            $access_token = JWT::BuildJWT('yz','access',$openid,'mH5user',$accessTimeout);
            $refresh_token = JWT::BuildJWT('yz','refresh',$openid,'',$refreshTimeout);
            $s=app()->make(PersonService::class);
            $c= $s->checkRegister(['openid'=>$openid]);
            if($c){  //判断是否注册过
                DB::table('persons')->where(['openid'=>$openid,'status'=>1])->update(['token'=>md5($refresh_token)]);
                $result['isRegister']=true;
            }else{
                $result['isRegister']=false;
            }
            $result['token']=$access_token;
            $result['refresh_token']=$refresh_token;
            $result['status']='ok';


        } else {
            // 请求失败处理...
            $result['msg']='获取openid失败';
            $result['status']='ok';
        }
        return $result;
    }

    //APP嵌套h5登录，参数APP内用户唯一id
    public function Applogin(){
        $openid =request('openid');
        if(isset($openid)){
            $accessTimeout = JWT::GetGetSecretTimeOut();
            $refreshTimeout = JWT::GetRefreshTokenTimeOut();
            $access_token = JWT::BuildJWT('yz','access',$openid,'mH5user',$accessTimeout);
            $refresh_token = JWT::BuildJWT('yz','refresh',$openid,'',$refreshTimeout);
            $s=app()->make(PersonService::class);
            $c= $s->checkRegister(['openid'=>$openid]);
            if($c){  //判断是否注册过
                DB::table('persons')->where(['openid'=>$openid,'status'=>1])->update(['token'=>md5($refresh_token)]);
                $result['isRegister']=true;
            }else{
                $result['isRegister']=false;
            }
            $result['token']=$access_token;
            $result['refresh_token']=$refresh_token;
            $result['status']='ok';
        }else{
            $result['msg']='获取openid失败';
            $result['status']='no';
        }
        return $result;
    }

}
