Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * 插件基类
- * @DateTime 2017-03-01
- * @Author PiaoRuiqing <piaoruiqing@gmail.com>
- */
- namespace app\common\plugin;
- use app\common\model\User;
- use think\Session;
- use app\common\model\Plugin;
- use app\common\model\PluginPage;
- abstract class BasePlugin {
- //插件名称必须和插件文件夹同名(首字母小写xxxYyyZzz,建议驼峰法命名)
- protected $pluginName = "pluginName";
- //是否开启全局检查权限
- protected $globalCheck = false;
- //允许权限范围, $globalCheck=true 时生效, 1-管理员,2-普通用户
- protected $allowdAuthorityList = array(2);
- protected $request ;
- protected $view;
- protected $userGroup;
- protected $data;
- public function __construct( $request = null , $view = null ){
- if($this -> globalCheck && !config('conf.debug')){
- if(!in_array($this -> getAuthority() , $this -> allowdAuthorityList)){
- die;
- }
- }
- $this -> request = $request;
- $this -> view = $view;
- }
- /**
- * 跳转插件页面,仅用于跳转,不做任何数据处理
- * 推荐前后端分离,使用index方法进行页面跳转,前端数据通过ajax请求获取
- * 若想要使用ThinkPHP的模板功能,则action指定自定义方法,自行渲染输出模板即可
- * @Author PiaoRuiqing <piaoruiqing@gmail.com>
- * @DateTime 2017-03-01T23:08:41+0800
- * @return [type] [description]
- */
- final public function index(){
- $dir = $this -> getParamIgnoreWay('plugin');
- $page = $this -> getParamIgnoreWay('page');
- !$page && $page = 'index';
- $this->assign('data',$this->data);
- return $this -> fetch(ROOT_PATH . 'extend' .DIRECTORY_SEPARATOR. 'plugin' .DIRECTORY_SEPARATOR. $dir . DIRECTORY_SEPARATOR. 'view' .DIRECTORY_SEPARATOR. $page .'.html');
- }
- /**
- * 安装
- * @Author PiaoRuiqing <piaoruiqing@gmail.com>
- * @DateTime 2017-03-01T23:08:47+0800
- * @return [type] [description]
- */
- final public function install(){
- !$this -> isAdmin() && die;
- echo "----->>>>> Check Authority Success<br>";
- echo "----->>>>> Base Install Start...<br>";
- include getcwd().DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."extend".DIRECTORY_SEPARATOR."plugin".DIRECTORY_SEPARATOR. $this -> pluginName .DIRECTORY_SEPARATOR."conf.php";
- echo "----->>>>> Check Plugin Name...<br>";
- if(!model('Plugin') -> where('name',$this -> pluginName) ->find()){
- $plugin = new Plugin();
- $plugin -> name = $this -> pluginName;
- $plugin -> text = $text;
- $plugin -> priority = 0;
- $plugin -> is_display = 1;
- $plugin -> status = 0;
- $plugin -> install_time = date('Y-m-d H:i:s');
- $plugin->save();
- foreach($pluginPages as $value){
- echo "----->>>>> Init Page [". $value[0] ."]<br>";
- $pluginPage = new PluginPage();
- $pluginPage -> name = $value[0];
- $pluginPage -> action = $value[1];
- $pluginPage -> page = $value[2];
- $pluginPage -> pid = $plugin->pid;
- $pluginPage -> save();
- }
- echo "----->>>>> Base Install Success<br>";
- $this -> installCustomize();
- // echo "success";
- }else{
- echo "----->>>>> Plugin Name Already Exists : ". $this -> pluginName ."<br>";
- echo "----->>>>> Plugin Install Fail <br>";
- }
- }
- /**
- * 卸载
- * @Author PiaoRuiqing <piaoruiqing@gmail.com>
- * @DateTime 2017-03-01T23:08:52+0800
- * @return [type] [description]
- */
- final public function uninstall(){
- !$this -> isAdmin() && die;
- echo "----->>>>> Check Authority Success<br>";
- echo "----->>>>> Base Uninstall Start...<br>";
- include getcwd().DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."extend".DIRECTORY_SEPARATOR."plugin".DIRECTORY_SEPARATOR. $this -> pluginName .DIRECTORY_SEPARATOR."conf.php";
- echo "----->>>>> Check Plugin Name...<br>";
- $plugin = model('Plugin') -> where('name',$this -> pluginName) ->find();
- if($plugin){
- foreach($pluginPages as $value){
- echo "----->>>>> Remove Page [". $value[0] ."]<br>";
- model('PluginPage') -> where('pid', $plugin['pid']) -> delete();
- }
- model('Plugin') -> where('name', $plugin['name']) -> delete();
- echo "----->>>>> Base Uninstall Success<br>";
- $this -> uninstallCustomize();
- }else{
- echo "----->>>>> Plugin Does Not Exist : ". $this -> pluginName ."<br>";
- echo "----->>>>> Plugin Uninstall Fail <br>";
- }
- }
- /**
- * !!!若有自定义安装操作,请在自定义卸载中进行对应操作!!!
- * 插件自定义安装操作,可选,如需要则在插件Index.php中重写该方法
- * @Author PiaoRuiqing <piaoruiqing@gmail.com>
- * @DateTime 2017-05-14T19:34:19+0800
- * @return [type] [description]
- */
- protected function installCustomize(){
- echo "----->>>>> Start Customize Install...<br>";
- echo "----->>>>> Customize Install Success : No Operating<br>";
- }
- /**
- * !!!若有自定义安装操作,请在自定义卸载中进行对应操作!!!
- * 插件自定义卸载操作,可选,如需要则在插件Index.php中重写该方法
- * @Author PiaoRuiqing <piaoruiqing@gmail.com>
- * @DateTime 2017-05-14T19:36:39+0800
- * @return [type] [description]
- */
- protected function uninstallCustomize(){
- echo "----->>>>> Start Customize Uninstall...<br>";
- echo "----->>>>> Customize Uninstall Success : No Operating<br>";
- }
- /**
- * 检查是否有管理员权限
- * @Author PiaoRuiqing <piaoruiqing@gmail.com>
- * @DateTime 2017-05-14T15:07:57+0800
- * @return [type] [description]
- */
- final protected function isAdmin(){
- if(Session::get('authority') == 1){
- return true;
- }else{
- return false;
- }
- }
- /**
- * 获取用户权限类型, 1-管理员,2-普通用户...
- * @Author PiaoRuiqing <piaoruiqing@gmail.com>
- * @DateTime 2017-05-16T22:14:03+0800
- * @return [type] [description]
- */
- final protected function getAuthority(){
- return Session::get('authority');
- }
- /**
- * 获取请求参数
- * @Author PiaoRuiqing <piaoruiqing@gmail.com>
- * @DateTime 2017-03-01T23:08:58+0800
- * @param [type] $name [参数名]
- * @return [type] [description]
- */
- final protected function getParamIgnoreWay($name){
- $item = $this -> request -> only($name);
- return array_key_exists($name,$item) ? $item[$name] : [];
- }
- /**
- * 加载模板输出
- * @Author PiaoRuiqing <piaoruiqing@gmail.com>
- * @DateTime 2017-03-01T23:11:15+0800
- * @param string $template [模板文件名]
- * @param array $vars [模板输出变量]
- * @param array $replace [模板替换]
- * @param array $config [模板参数]
- * @return [type] [description]
- */
- protected function fetch($template = '', $vars = [], $replace = [], $config = []){
- return $this->view->fetch($template, $vars, $replace, $config);
- }
- /**
- * 渲染内容输出
- * @Author PiaoRuiqing <piaoruiqing@gmail.com>
- * @DateTime 2017-03-01T23:10:47+0800
- * @param string $content [模板内容]
- * @param array $vars [模板输出变量]
- * @param array $replace [替换内容]
- * @param array $config [模板参数]
- * @return [type] [description]
- */
- protected function display($content = '', $vars = [], $replace = [], $config = []){
- return $this->view->display($content, $vars, $replace, $config);
- }
- /**
- * 模板变量赋值
- * @Author PiaoRuiqing <piaoruiqing@gmail.com>
- * @DateTime 2017-03-01T23:10:19+0800
- * @param [type] $name [要显示的模板变量]
- * @param string $value [变量的值]
- * @return [type] [description]
- */
- protected function assign($name, $value = ''){
- $this->view->assign($name, $value);
- }
- /**
- * 测试
- * @Author PiaoRuiqing <piaoruiqing@gmail.com>
- * @DateTime 2017-03-01T23:10:05+0800
- * @return [type] [description]
- */
- public function test(){
- echo '<title>Plugin ['. $this -> pluginName .']</title>';
- echo 'Plugin [test] <br><br>';
- echo '[INFO] <br>';
- if($this -> request -> isGet()){
- echo 'Request : GET <br>';
- }else if($this -> request -> isPost()){
- echo 'Request : POST <br>';
- }
- echo 'Plugin : test <br>';
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement