WechatBase.php 2.0 KB
<?php

namespace app\common\controller;

use app\common\library\Auth;
use EasyWeChat\Foundation\Application;
use think\Config;
use think\Controller;
use think\Hook;
use think\Lang;
use think\Loader;

/**
 * 前台控制器基类
 */
class WechatBase extends Controller
{

    /**
     * 布局模板
     * @var string
     */
    protected $layout = '';

    /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = [];

    /**
     * 权限Auth
     * @var Auth
     */
    protected $auth = null;

    public function _initialize()
    {

    }

    /**
     * 加载语言文件
     * @param string $name
     */
    protected function loadlang($name)
    {
        Lang::load(APP_PATH . $this->request->module() . '/lang/' . $this->request->langset() . '/' . str_replace('.', '/', $name) . '.php');
    }

    /**
     * 渲染配置信息
     * @param mixed $name  键名或数组
     * @param mixed $value 值
     */
    protected function assignconfig($name, $value = '')
    {
        $this->view->config = array_merge($this->view->config ? $this->view->config : [], is_array($name) ? $name : [$name => $value]);
    }

    /**
     * 检查微信用户登录
     */
    public function checkWeChatUserLogin()
    {
        $userId = get_current_user_id();
        if (empty($userId)) {
            $config = [
                'app_id'  => config('wechat.app_id'),
                'secret'  => config('wechat.secret'),
                'oauth' => [
                    'scopes'   => ['snsapi_userinfo'],
                    'callback' => url('home/user/callback'),
                ],
            ];
            $app = new Application($config);
            $target_url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            session('target_url',$target_url);
            $app->oauth->redirect()->send();
        }
    }
}