Login.php 2.6 KB
<?php


namespace app\api\controller;

use addons\third\controller\Api;

/**
 * 微信登录
 * @package app\api\controller
 */
class Login extends BaseApi
{
    protected $noNeedLogin = ['getLoginUrl','third'];
    protected $noNeedRight = '*';

    /**
     * 获取会员登录地址
     * @ApiTitle    (获取会员登录地址)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/login/getLoginUrl)
     * @ApiParams   (name="url", type="string", required=true, description="前端回调路径")
     * @ApiParams   (name="platform", type="string", required=true, description="请填写:wechat")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ()
     */
    public function getLoginUrl(){
        (new Api())->getAuthUrl();
    }

    /**
     * 第三方登录
     * @ApiTitle    (第三方登录)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/login/third)
     * @ApiParams   (name="platform", type="string", required=true, description="平台名称:wechat")
     * @ApiParams   (name="code", type="string", required=true, description="Code码")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ()
     */
    public function third()
    {
        $platform = $this->request->request("platform");
        $code = $this->request->request("code");
        $config = get_addon_config('third');
        if (!$config || !isset($config[$platform])) {
            $this->error(__('Invalid parameters'));
        }
        $app = new \addons\third\library\Application($config);
        //通过code换access_token和绑定会员
        session('state','STATE');
        $result = $app->{$platform}->getUserInfo(['state'=>'STATE','code' => $code]);
        if ($result) {
            $loginret = \addons\third\library\Service::connect($platform, $result);
            if ($loginret) {
                $data = [
                    'token' => $this->auth->getToken(),
                    'userinfo'  => $this->auth->getUserinfo(),
                    'thirdinfo' => $result,
                ];
                $this->success(__('Logged in successful'), $data);
            }
        }

        $this->error(__('Operation failed'));
    }

}