<?php

namespace Modules\User\Http\Controllers\Api\Auth;

use App\Api\Controllers\Controller;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use EasyWeChat\Kernel\Exceptions\RuntimeException;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class WechatController extends Controller
{

    /**
     * Notes: 获取授权的跳转地址
     *
     * @Author: 玄尘
     * @Date  : 2021/6/25 13:21
     * @param  Request  $request
     * @return mixed
     */
    public function getAuthUrl(Request $request)
    {
        $app = app('wechat.official_account');

        $pages  = $request->url ?? '';
        $scopes = $request->scopes ?? ['snsapi_userinfo'];

        $web_url  = config('user.web.base');
        $redirect = $web_url.$pages;
        if (! is_array($scopes)) {
            $scopes = [$scopes];
        }
        $response = $app->oauth->scopes($scopes)->redirect($redirect);

        return $this->success($response);
    }

    /**
     * Notes: 获取jssdk
     *
     * @Author: 玄尘
     * @Date  : 2021/7/1 11:08
     * @param  Request  $request
     * @return JsonResponse
     */
    public function getJsSdk(Request $request)
    {
        $url         = $request->url;
        $jsApiList   = $request->jsApiList ?? [];
        $openTagList = $request->openTagList ?? [];
        $app         = app('wechat.official_account');

        if ($url) {
            $app->jssdk->setUrl($url);
        }

        if (! is_array($jsApiList)) {
            $jsApiList = [$jsApiList];
        }

        if (! is_array($openTagList)) {
            $openTagList = [$openTagList];
        }

        return $this->success($app->jssdk->buildConfig($jsApiList, false, false, true, $openTagList));
    }

    /**
     * Notes: 微信分享
     *
     * @Author: 玄尘
     * @Date  : 2021/10/26 16:52
     * @param  Request  $request
     * @return JsonResponse|mixed
     * @throws InvalidArgumentException
     * @throws InvalidConfigException
     * @throws RuntimeException
     * @throws GuzzleException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function officialShare(Request $request)
    {
        $url = $request->url ?? '';
        if (empty($url)) {
            return $this->failed('地址必须传');
        }

        $app = app('wechat.official_account');

        $apis = [
            'updateAppMessageShareData',
            'updateTimelineShareData',
            //            'showOptionMenu',
            //            'showMenuItems',
            'showAllNonBaseMenuItem',
        ];

        $app->jssdk->setUrl($url);
        $cog = $app->jssdk->buildConfig($apis);

        return $this->success($cog);
    }


    /**
     * Notes: 获取小程序openid
     *
     * @Author: 玄尘
     * @Date: 2022/9/26 14:17
     * @return mixed|void
     */
    public function getMiniOpenid(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'code' => 'required',
        ], [
            'code.required' => '缺少参数：code',
        ]);

        if ($validator->fails()) {
            return $this->failed($validator->errors()->first());
        }

        $weChat  = app('wechat.mini_program');
        $session = $weChat->auth->session($request->code);

        if (isset($session->errcode)) {
            return $this->failed($session->errmsg);
        }

        return $this->success($session->openid);
    }

    /**
     * Notes: 获取公众号openid
     *
     * @Author: 玄尘
     * @Date: 2022/9/26 14:21
     */
    public function getOfficialOpenid()
    {
        try {
            $weUser = $this->getUserFromCode();
            return $weUser->getId();
        } catch (\Exception $exception) {
            return $this->failed($exception->getMessage());
        }
    }


}