<?php namespace Phpcmf\Controllers\Admin;

class Account extends \Phpcmf\Common
{

    public function __construct() {
        parent::__construct();

        if (IS_ADMIN && SITE_ID > 1) {
            $this->_admin_msg(0, '请登录主站账号管理微信插件');
        }

        \Phpcmf\Service::V()->assign('menu', \Phpcmf\Service::M('auth')->_admin_menu(
            [
                WEIXIN_MORE_NAME. '账号接入' => [APP_DIR.'/account/index', 'fa fa-weixin'],
                'help' => [808],
            ]
        ));
    }

    public function gz_index() {

        $rt = weixin_get_access_token();
        if ($rt['code']) {
            $access_token = $rt['msg'];
            $param = [
                'expire_seconds' => 2592000,
                'action_name' => 'QR_STR_SCENE',
                'action_info' => array(
                    'scene' => array(
                        'scene_str' => 'login-guanzhu',
                    ),
                )
            ];

            $rt = wx_post_https_json_data(
                'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=' . $access_token,
                $param
            );
            if (!$rt['code']) {
                $this->_admin_msg(0, '生成二维码失败');
            }

            $url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . $rt['data']['ticket'];

            echo '<div style="text-align: center"><img src="'.$url.'"></div>';exit;
        } else {
            $this->_admin_msg(0, $rt['msg']);
        }

    }

    public function index() {


        $page = intval(\Phpcmf\Service::L('Input')->get('page'));
        $data = \Phpcmf\Service::M('Weixin', 'Weixin')->get_config('account');

        if (IS_AJAX_POST) {

            $post = \Phpcmf\Service::L('Input')->post('data');
            $rt = \Phpcmf\Service::M('Weixin', 'Weixin')->save_config('account', $post);
            if (!$rt['code']) {
                $this->_json(0, $rt['msg']);
            }
            \Phpcmf\Service::M('Weixin', 'Weixin')->cache();
            $rt = weixin_get_access_token($post);
            if (!$rt['code']) {
                $this->_json(0, $rt['msg']);
            }
            $this->_json(1, dr_lang('操作成功'));
        }

        $rt = weixin_get_access_token();
        $access_tocken = $rt['code'] ? '': $rt['msg'];

        \Phpcmf\Service::V()->assign([
            'data' => $data,
            'page' => $page,
            'form' => dr_form_hidden(['page' => $page]),
            'access_tocken' => $access_tocken,
        ]);
        \Phpcmf\Service::V()->display('account_index.html');
    }


}
