<?php

namespace App\Http\Controllers;

use App\Models\DwbsUser;
use App\Models\Admin;
use App\Models\User;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;

class LoginController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:users', ['except' => ['login']]);
    }

    //用户微信登陆
    public function login(Request $request){
        $user=User::where('phone',15238617333)->first();

        $token = Auth::guard('api')->fromUser($user);
        return $this->success($token);


        $code=$request->input('code');
        $info=$this->getUserOpenid($code);
        if(isset($info['openid']) && isset($info['unionid'])){
            $user=User::where('unionId',$info['unionid'])->where('openId',$info['openid'])->first();
            if(empty($user)){   //用户不存在
                $userInfo=$this->getWxUserInfo($info['openid'],$info['access_token']);
                if(isset($userInfo['openid']) && !empty($userInfo['openid'])){
                    $user = User::create([
                        'openId'=>$userInfo['openid'],
                        'unionId'=>$userInfo['unionid'],
                        'nickname'=>$userInfo['nickname'],
                        'avatar'=>$userInfo['headimgurl'],
                    ]);
                }else{
                    return $this->error('450001','您未关注微信公众号1');
                }
            }
            $token = Auth::guard('api')->fromUser($user);
            return $this->success($token);
        }else{
            return $this->error('450001','','您未关注微信公众号2');
        }
    }


    // 获取openid
    public function getUserOpenid($Code): array
    {
        $client=new Client();
        $AppId=config('wechat.official_account.daweiboshi.app_id');
        $Secret=config('wechat.official_account.daweiboshi.secret');
        $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$AppId.'&secret='.$Secret.'&code='.$Code.'&grant_type=authorization_code';
        $data=$client->request('get',$url);
        $da=$data->getBody()->getContents();
        $das=json_decode($da,true);
        return (array)$das;
    }

    // 获取微信用户信息
    public function getWxUserInfo($openid,$access_token){
        $client=new Client();
        $url="https://api.weixin.qq.com/sns/userinfo";
        $array=[
            'query' => [
                'access_token' =>$access_token,
                'openid'=>$openid
            ]
        ];
        $data=$client->request('GET',$url,$array);
        $userInfo = json_decode($data->getBody()->getContents(),true);
        return $userInfo;
    }
    //用户退出
    public function logout(Request $request): \Illuminate\Http\JsonResponse
    {
        try {
            JWTAuth::parseToken()->invalidate();

            return $this->success([]);
        } catch (JWTException $exception) {

            return $this->error();
        }
    }
    //获取用户信息
    public function getAuthUser(Request $request): \Illuminate\Http\JsonResponse
    {
//        return $this->success(Auth::guard('api')->user());
        return $this->success(['user' => JWTAuth::parseToken()->touser()]);
    }

}
