<?php namespace Wechat\Api;
use Dever;
use Dever\Helper\Secure;
use Manage\Lib\Auth as Core;
class Auth extends Core
{
    public function __construct()
    {
        $this->account = 'wechat_open';
        $this->api = Dever::load('account', 'api')->run($this->account, 'msg', array(), 1, 'setting');
        $extend = Dever::load('common', 'manage')->extend();
        if (!$extend) {
            Dever::error('身份不存在');
        }
        $this->info = Dever::db('info', 'place')->find(array('id' => $extend['data_id']));
        if (!$this->info) {
            Dever::error('身份不存在');
        }
        $this->env = Dever::input('env', 'is_numeric', '环境', 6);
    }

    public function link()
    {
        $param['component_appid'] = $this->api->field->appid;
        $param['pre_auth_code'] = Dever::load('info', 'wechat')->getPreAuthCode();
        $param['redirect_uri'] = Dever::url('wechat/auth.code', array(), true);
        /**
         * - 1 表示手机端仅展示公众号；2 表示仅展示小程序，3 表示公众号和小程序都展示。
- 4表示小程序推客账号；
- 5表示视频号账号；
- 6表示全部，即公众号、小程序、视频号都展示
         */
        $param['auth_type'] = Dever::input('type');
        Dever::load('account', 'api')->run($this->account, 'auth_link', $param, $this->env, 'jump');
    }

    public function code()
    {
        $auth_code = Dever::input('auth_code');
        $expires_in = Dever::input('expires_in');

        Dever::load('info', 'wechat')->up(false, 'auth_code', $auth_code, $expires_in, $this->info['id']);

        $param['auth_code'] = $auth_code;
        $param['component_access_token'] = Dever::load('info', 'wechat')->getAccessToken();
        $data = Dever::load('account', 'api')->run($this->account, 'auth_token', $param);
        if (isset($data['authorization_info'])) {
            $data = $data['authorization_info'];
            Dever::load('auth', 'wechat')->up(false, $this->info['id'], $data['authorizer_access_token'], $data['authorizer_refresh_token'], $data['expires_in'], $data['authorizer_appid'], $data['func_info']);
            echo '授权成功';die;
        }
        echo '授权失败';die;
    }
}
