Advertisement
Guest User

Untitled

a guest
Jun 14th, 2017
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.61 KB | None | 0 0
  1. <?php
  2. /**
  3.  * 插件基类
  4.  * @DateTime 2017-03-01
  5.  * @Author PiaoRuiqing <piaoruiqing@gmail.com>
  6.  */
  7.  
  8. namespace app\common\plugin;
  9.  
  10. use app\common\model\User;
  11. use think\Session;
  12. use app\common\model\Plugin;
  13. use app\common\model\PluginPage;
  14.  
  15. abstract class BasePlugin {
  16.  
  17.     //插件名称必须和插件文件夹同名(首字母小写xxxYyyZzz,建议驼峰法命名)
  18.     protected $pluginName = "pluginName";
  19.     //是否开启全局检查权限
  20.     protected $globalCheck = false;
  21.     //允许权限范围, $globalCheck=true 时生效, 1-管理员,2-普通用户
  22.     protected $allowdAuthorityList = array(2);
  23.  
  24.     protected $request ;
  25.     protected $view;
  26.     protected $userGroup;
  27.     protected $data;
  28.  
  29.     public function __construct( $request = null , $view = null ){
  30.  
  31.         if($this -> globalCheck && !config('conf.debug')){
  32.             if(!in_array($this -> getAuthority() , $this -> allowdAuthorityList)){
  33.                 die;
  34.             }
  35.         }
  36.         $this -> request = $request;
  37.         $this -> view = $view;
  38.     }
  39.    
  40.     /**
  41.      * 跳转插件页面,仅用于跳转,不做任何数据处理
  42.      * 推荐前后端分离,使用index方法进行页面跳转,前端数据通过ajax请求获取
  43.      * 若想要使用ThinkPHP的模板功能,则action指定自定义方法,自行渲染输出模板即可
  44.      * @Author   PiaoRuiqing                            <piaoruiqing@gmail.com>
  45.      * @DateTime 2017-03-01T23:08:41+0800
  46.      * @return   [type]                   [description]
  47.      */
  48.     final public function index(){
  49.         $dir = $this -> getParamIgnoreWay('plugin');
  50.         $page = $this -> getParamIgnoreWay('page');
  51.         !$page && $page = 'index';
  52.         $this->assign('data',$this->data);
  53.         return $this -> fetch(ROOT_PATH . 'extend' .DIRECTORY_SEPARATOR. 'plugin' .DIRECTORY_SEPARATOR. $dir . DIRECTORY_SEPARATOR. 'view' .DIRECTORY_SEPARATOR. $page .'.html');
  54.     }
  55.  
  56.     /**
  57.      * 安装
  58.      * @Author   PiaoRuiqing                            <piaoruiqing@gmail.com>
  59.      * @DateTime 2017-03-01T23:08:47+0800
  60.      * @return   [type]                   [description]
  61.      */
  62.     final public function install(){
  63.  
  64.         !$this -> isAdmin() && die;
  65.         echo "----->>>>> Check Authority Success<br>";
  66.         echo "----->>>>> Base Install Start...<br>";
  67.         include getcwd().DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."extend".DIRECTORY_SEPARATOR."plugin".DIRECTORY_SEPARATOR. $this -> pluginName .DIRECTORY_SEPARATOR."conf.php";
  68.        
  69.         echo "----->>>>> Check Plugin Name...<br>";
  70.         if(!model('Plugin') -> where('name',$this -> pluginName) ->find()){
  71.             $plugin = new Plugin();
  72.             $plugin -> name = $this -> pluginName;
  73.             $plugin -> text = $text;
  74.             $plugin -> priority = 0;
  75.             $plugin -> is_display = 1;
  76.             $plugin -> status = 0;
  77.             $plugin -> install_time = date('Y-m-d H:i:s');
  78.             $plugin->save();
  79.             foreach($pluginPages as $value){
  80.                 echo "----->>>>> Init Page [". $value[0] ."]<br>";
  81.                 $pluginPage = new PluginPage();
  82.                 $pluginPage -> name = $value[0];
  83.                 $pluginPage -> action = $value[1];
  84.                 $pluginPage -> page = $value[2];
  85.                 $pluginPage -> pid = $plugin->pid;
  86.                 $pluginPage -> save();
  87.             }
  88.  
  89.             echo "----->>>>> Base Install Success<br>";
  90.             $this -> installCustomize();
  91.  
  92.             // echo "success";
  93.         }else{
  94.             echo "----->>>>> Plugin Name Already Exists : ". $this -> pluginName ."<br>";
  95.             echo "----->>>>> Plugin Install Fail <br>";
  96.         }
  97.     }
  98.  
  99.     /**
  100.      * 卸载
  101.      * @Author   PiaoRuiqing                            <piaoruiqing@gmail.com>
  102.      * @DateTime 2017-03-01T23:08:52+0800
  103.      * @return   [type]                   [description]
  104.      */
  105.     final public function uninstall(){
  106.  
  107.         !$this -> isAdmin() && die;
  108.         echo "----->>>>> Check Authority Success<br>";
  109.         echo "----->>>>> Base Uninstall Start...<br>";
  110.         include getcwd().DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."extend".DIRECTORY_SEPARATOR."plugin".DIRECTORY_SEPARATOR. $this -> pluginName .DIRECTORY_SEPARATOR."conf.php";
  111.  
  112.         echo "----->>>>> Check Plugin Name...<br>";
  113.         $plugin = model('Plugin') -> where('name',$this -> pluginName) ->find();
  114.        
  115.         if($plugin){
  116.  
  117.             foreach($pluginPages as $value){
  118.             echo "----->>>>> Remove Page [". $value[0] ."]<br>";
  119.                 model('PluginPage') -> where('pid', $plugin['pid']) -> delete();
  120.             }
  121.  
  122.             model('Plugin') -> where('name', $plugin['name']) -> delete();
  123.             echo "----->>>>> Base Uninstall Success<br>";
  124.             $this -> uninstallCustomize();
  125.  
  126.         }else{
  127.             echo "----->>>>> Plugin Does Not Exist : ". $this -> pluginName ."<br>";
  128.             echo "----->>>>> Plugin Uninstall Fail <br>";
  129.         }
  130.     }
  131.  
  132.     /**
  133.      * !!!若有自定义安装操作,请在自定义卸载中进行对应操作!!!
  134.      * 插件自定义安装操作,可选,如需要则在插件Index.php中重写该方法
  135.      * @Author   PiaoRuiqing                            <piaoruiqing@gmail.com>
  136.      * @DateTime 2017-05-14T19:34:19+0800
  137.      * @return   [type]                   [description]
  138.      */
  139.     protected function installCustomize(){
  140.         echo "----->>>>> Start Customize Install...<br>";
  141.         echo "----->>>>> Customize Install Success : No Operating<br>";
  142.     }
  143.  
  144.     /**
  145.      * !!!若有自定义安装操作,请在自定义卸载中进行对应操作!!!
  146.      * 插件自定义卸载操作,可选,如需要则在插件Index.php中重写该方法
  147.      * @Author   PiaoRuiqing                            <piaoruiqing@gmail.com>
  148.      * @DateTime 2017-05-14T19:36:39+0800
  149.      * @return   [type]                   [description]
  150.      */
  151.     protected function uninstallCustomize(){
  152.         echo "----->>>>> Start Customize Uninstall...<br>";
  153.         echo "----->>>>> Customize Uninstall Success : No Operating<br>";
  154.     }
  155.  
  156.     /**
  157.      * 检查是否有管理员权限
  158.      * @Author   PiaoRuiqing                            <piaoruiqing@gmail.com>
  159.      * @DateTime 2017-05-14T15:07:57+0800
  160.      * @return   [type]                   [description]
  161.      */
  162.     final protected function isAdmin(){
  163.         if(Session::get('authority') == 1){
  164.             return true;
  165.         }else{
  166.             return false;
  167.         }
  168.     }
  169.  
  170.     /**
  171.      * 获取用户权限类型, 1-管理员,2-普通用户...
  172.      * @Author   PiaoRuiqing                            <piaoruiqing@gmail.com>
  173.      * @DateTime 2017-05-16T22:14:03+0800
  174.      * @return   [type]                   [description]
  175.      */
  176.     final protected function getAuthority(){
  177.         return Session::get('authority');
  178.     }
  179.  
  180.     /**
  181.      * 获取请求参数
  182.      * @Author   PiaoRuiqing                    <piaoruiqing@gmail.com>
  183.      * @DateTime 2017-03-01T23:08:58+0800
  184.      * @param    [type]                   $name [参数名]
  185.      * @return   [type]                         [description]
  186.      */
  187.     final protected function getParamIgnoreWay($name){
  188.         $item = $this -> request -> only($name);
  189.         return array_key_exists($name,$item) ? $item[$name] : [];
  190.     }
  191.  
  192.     /**
  193.      * 加载模板输出
  194.      * @Author   PiaoRuiqing                        <piaoruiqing@gmail.com>
  195.      * @DateTime 2017-03-01T23:11:15+0800
  196.      * @param    string                   $template [模板文件名]
  197.      * @param    array                    $vars     [模板输出变量]
  198.      * @param    array                    $replace  [模板替换]
  199.      * @param    array                    $config   [模板参数]
  200.      * @return   [type]                             [description]
  201.      */
  202.     protected function fetch($template = '', $vars = [], $replace = [], $config = []){
  203.         return $this->view->fetch($template, $vars, $replace, $config);
  204.     }
  205.  
  206.     /**
  207.      * 渲染内容输出
  208.      * @Author   PiaoRuiqing                       <piaoruiqing@gmail.com>
  209.      * @DateTime 2017-03-01T23:10:47+0800
  210.      * @param    string                   $content [模板内容]
  211.      * @param    array                    $vars    [模板输出变量]
  212.      * @param    array                    $replace [替换内容]
  213.      * @param    array                    $config  [模板参数]
  214.      * @return   [type]                            [description]
  215.      */
  216.     protected function display($content = '', $vars = [], $replace = [], $config = []){
  217.         return $this->view->display($content, $vars, $replace, $config);
  218.     }
  219.  
  220.     /**
  221.      * 模板变量赋值
  222.      * @Author   PiaoRuiqing                     <piaoruiqing@gmail.com>
  223.      * @DateTime 2017-03-01T23:10:19+0800
  224.      * @param    [type]                   $name  [要显示的模板变量]
  225.      * @param    string                   $value [变量的值]
  226.      * @return   [type]                          [description]
  227.      */
  228.     protected function assign($name, $value = ''){
  229.         $this->view->assign($name, $value);
  230.     }
  231.  
  232.     /**
  233.      * 测试
  234.      * @Author   PiaoRuiqing                            <piaoruiqing@gmail.com>
  235.      * @DateTime 2017-03-01T23:10:05+0800
  236.      * @return   [type]                   [description]
  237.      */
  238.     public function test(){
  239.  
  240.         echo '<title>Plugin ['. $this -> pluginName .']</title>';
  241.         echo 'Plugin [test] <br><br>';
  242.         echo '[INFO] <br>';
  243.         if($this -> request -> isGet()){
  244.             echo 'Request : GET <br>';
  245.         }else if($this -> request -> isPost()){
  246.             echo 'Request : POST <br>';
  247.         }
  248.         echo 'Plugin &nbsp;&nbsp;&nbsp;: test <br>';
  249.     }
  250.  
  251. }
  252.  
  253.  
  254. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement