IndexController.php 2.1 KB
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\user\controller;

use cmf\controller\HomeBaseController;
use think\Db;
use EasyWeChat\Foundation\Application;

class IndexController extends HomeBaseController
{
    public function callback(){
        $config = [];

        $app = new Application($config);
        $oauth = $app->oauth;
        $user = $oauth->user();
        $wechat_user = $user->toArray();
        //todo $wechat_user与数据库对比
        session('wechat_user',$wechat_user);
        $target_url=session('target_url');
        $targetUrl = empty($target_url) ? '/' : $target_url;
        header('location:'. $targetUrl);
    }

    /**
     * 前台用户首页(公开)
     */
    public function index()
    {
        $id   = $this->request->param("id", 0, "intval");
        $userQuery = Db::name("User");
        $user = $userQuery->where('id',$id)->find();
        if (empty($user)) {
            session('user',null);
            $this->error("查无此人!");
        }
        $this->assign($user);
        return $this->fetch(":index");

    }

    /**
     * 前台ajax 判断用户登录状态接口
     */
    function isLogin()
    {
        if (cmf_is_user_login()) {
            $this->success("用户已登录",null,['user'=>cmf_get_current_user()]);
        } else {
            $this->error("此用户未登录!");
        }
    }

    /**
     * 退出登录
    */
    public function logout()
    {
        session("user", null);//只有前台用户退出
        return redirect($this->request->root() . "/");
    }

}