Controller.php 4.6 KB
Newer Older
李光春's avatar
李光春 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
李光春's avatar
李光春 已提交
14 15 16
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
李光春's avatar
李光春 已提交
17 18 19 20 21
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------

namespace DtApp\ThinkLibrary;

李光春's avatar
李光春 已提交
22
use stdClass;
李光春's avatar
李光春 已提交
23 24 25 26 27 28 29 30 31
use think\App;
use think\exception\HttpResponseException;
use think\Request;

/**
 * 标准控制器基类
 * Class Controller
 * @package DtApp\ThinkLibrary
 */
李光春's avatar
李光春 已提交
32
class Controller extends stdClass
李光春's avatar
李光春 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
{
    /**
     * 应用容器
     * @var App
     */
    public $app;

    /**
     * 请求对象
     * @var Request
     */
    public $request;

    /**
     * Controller constructor.
     * @param App $app
     */
    public function __construct(App $app)
    {
        $this->app = $app;
        $this->request = $app->request;
        $this->app->bind('DtApp\ThinkLibrary\Controller', $this);
李光春's avatar
李光春 已提交
55
        if (in_array($this->request->action(), get_class_methods(__CLASS__))) {
李光春's avatar
李光春 已提交
56 57
            $this->error('Access without permission.');
        }
李光春's avatar
李光春 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        $this->initialize();
    }

    /**
     * 控制器初始化
     */
    protected function initialize()
    {
    }

    /**
     * 返回失败的操作
     * @param mixed $info 消息内容
     * @param mixed $data 返回数据
     * @param integer $code 返回代码
     */
李光春's avatar
李光春 已提交
74
    public function error($info, $data = '{-null-}', $code = 0): void
李光春's avatar
李光春 已提交
75
    {
李光春's avatar
李光春 已提交
76 77 78
        if ($data === '{-null-}') {
            $data = new stdClass();
        }
李光春's avatar
李光春 已提交
79 80 81 82 83 84 85 86 87 88 89
        throw new HttpResponseException(json([
            'code' => $code, 'info' => $info, 'data' => $data,
        ]));
    }

    /**
     * 返回成功的操作
     * @param mixed $info 消息内容
     * @param mixed $data 返回数据
     * @param integer $code 返回代码
     */
李光春's avatar
李光春 已提交
90
    public function success($info, $data = '{-null-}', $code = 1): void
李光春's avatar
李光春 已提交
91
    {
李光春's avatar
李光春 已提交
92 93 94
        if ($data === '{-null-}') {
            $data = new stdClass();
        }
李光春's avatar
李光春 已提交
95 96 97 98 99 100 101 102 103 104
        throw new HttpResponseException(json([
            'code' => $code, 'info' => $info, 'data' => $data,
        ]));
    }

    /**
     * URL重定向
     * @param string $url 跳转链接
     * @param integer $code 跳转代码
     */
李光春's avatar
李光春 已提交
105
    public function redirect($url, $code = 301): void
李光春's avatar
李光春 已提交
106 107 108 109 110 111 112 113 114
    {
        throw new HttpResponseException(redirect($url, $code));
    }

    /**
     * 返回视图内容
     * @param string $tpl 模板名称
     * @param array $vars 模板变量
     */
李光春's avatar
李光春 已提交
115
    public function fetch($tpl = '', $vars = []): void
李光春's avatar
李光春 已提交
116
    {
李光春's avatar
李光春 已提交
117 118 119
        foreach ($this as $name => $value) {
            $vars[$name] = $value;
        }
李光春's avatar
李光春 已提交
120 121 122 123 124 125 126 127 128
        throw new HttpResponseException(view($tpl, $vars));
    }

    /**
     * 模板变量赋值
     * @param mixed $name 要显示的模板变量
     * @param mixed $value 变量的值
     * @return $this
     */
李光春's avatar
李光春 已提交
129
    public function assign($name, $value = ''): self
李光春's avatar
李光春 已提交
130 131 132 133 134
    {
        if (is_string($name)) {
            $this->$name = $value;
        } elseif (is_array($name)) {
            foreach ($name as $k => $v) {
李光春's avatar
李光春 已提交
135 136 137
                if (is_string($k)) {
                    $this->$k = $v;
                }
李光春's avatar
李光春 已提交
138 139 140 141 142 143 144 145 146 147 148 149
            }
        }
        return $this;
    }

    /**
     * 数据回调处理机制
     * @param string $name 回调方法名称
     * @param mixed $one 回调引用参数1
     * @param mixed $two 回调引用参数2
     * @return boolean
     */
李光春's avatar
李光春 已提交
150
    public function callback($name, &$one = [], &$two = []): bool
李光春's avatar
李光春 已提交
151
    {
李光春's avatar
李光春 已提交
152
        if (is_callable($name)) {
李光春's avatar
李光春 已提交
153
            return $name($this, $one, $two);
李光春's avatar
李光春 已提交
154
        }
李光春's avatar
李光春 已提交
155 156 157 158 159 160 161 162
        foreach ([$name, "_{$this->app->request->action()}{$name}"] as $method) {
            if (method_exists($this, $method) && false === $this->$method($one, $two)) {
                return false;
            }
        }
        return true;
    }
}