PluginsAdminService.php 31.8 KB
Newer Older
D
devil_gong 已提交
1 2 3 4
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
D
devil_gong 已提交
5
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
D
devil_gong 已提交
6 7 8 9 10 11 12 13 14
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;

use think\Db;
use app\service\ResourcesService;
D
devil_gong 已提交
15
use app\service\SqlconsoleService;
D
devil_gong 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

/**
 * 应用管理服务层
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class PluginsAdminService
{
    /**
     * 列表
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function PluginsList($params = [])
    {
        $where = empty($params['where']) ? [] : $params['where'];
        $m = isset($params['m']) ? intval($params['m']) : 0;
        $n = isset($params['n']) ? intval($params['n']) : 10;
        $order_by = empty($params['order_by']) ? 'id desc' : $params['order_by'];

        // 获取数据列表
        $data = Db::name('Plugins')->where($where)->limit($m, $n)->order($order_by)->select();
        
        return DataReturn('处理成功', 0, self::PluginsDataHandle($data));
    }

    /**
     * 数据处理
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $data [数据]
     */
    private static function PluginsDataHandle($data)
    {
        $result = [];
        if(!empty($data))
        {
            foreach($data as $v)
            {
                $config = self::GetPluginsConfig($v['plugins']);
                if($config !== false)
                {
                    $base = $config['base'];
                    $result[] = [
                        'id'            => $v['id'],
                        'plugins'       => $v['plugins'],
                        'is_enable'     => $v['is_enable'],
                        'logo_old'      => $base['logo'],
                        'logo'          => ResourcesService::AttachmentPathViewHandle($base['logo']),
                        'is_home'       => isset($base['is_home']) ? $base['is_home'] : false,
                        'name'          => isset($base['name']) ? $base['name'] : '',
                        'author'        => isset($base['author']) ? $base['author'] : '',
                        'author_url'    => isset($base['author_url']) ? $base['author_url'] : '',
                        'version'       => isset($base['version']) ? $base['version'] : '',
                        'desc'          => isset($base['desc']) ? $base['desc'] : '',
                        'apply_version' => isset($base['apply_version']) ? $base['apply_version'] : [],
                        'apply_terminal'=> isset($base['apply_terminal']) ? $base['apply_terminal'] : [],
                        'add_time_time' => date('Y-m-d H:i:s', $v['add_time']),
                        'add_time_date' => date('Y-m-d', $v['add_time']),
                    ];
                }
            }
        }

        return $result;
    }

    /**
     * 总数
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $where [条件]
     */
    public static function PluginsTotal($where = [])
    {
        return (int) Db::name('Plugins')->where($where)->count();
    }

    /**
     * 列表条件
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function PluginsListWhere($params = [])
    {
        $where = [];
        return $where;
    }

    /**
     * 状态更新
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
    public static function PluginsStatusUpdate($params = [])
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '操作id有误',
            ],
            [
                'checked_type'      => 'in',
                'key_name'          => 'state',
                'checked_data'      => [0,1],
                'error_msg'         => '状态有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

G
gongfuxiang 已提交
151 152 153
        // 开启事务
        Db::startTrans();

D
devil_gong 已提交
154 155 156
        // 数据更新
        if(Db::name('Plugins')->where(['id'=>$params['id']])->update(['is_enable'=>intval($params['state']), 'upd_time'=>time()]))
        {
D
devil_gong 已提交
157 158
            // 钩子部署
            $ret = self::PluginsHookDeployment();
G
gongfuxiang 已提交
159
            if($ret['code'] == 0)
D
devil_gong 已提交
160
            {
G
gongfuxiang 已提交
161 162 163
                // 提交事务
                Db::commit();
                return DataReturn('操作成功');
D
devil_gong 已提交
164
            }
G
gongfuxiang 已提交
165 166
        } else {
            $ret = DataReturn('操作失败', -100);
D
devil_gong 已提交
167
        }
G
gongfuxiang 已提交
168 169 170 171

        // 事务回退
        Db::rollback();
        return $ret;
D
devil_gong 已提交
172 173
    }

D
devil_gong 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    /**
     * 应用钩子部署
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-02-13
     * @desc    description
     */
    private static function PluginsHookDeployment()
    {
        // 钩子配置文件
        $tags_file = APP_PATH.'tags.php';
        if(!is_writable($tags_file))
        {
            return DataReturn('钩子配置文件没有操作权限'.'['.$tags_file.']', -3);
        }

        // 钩子容器
        $result = [];

        // 系统自带钩子处理
        if(file_exists($tags_file))
        {
            $tags = require $tags_file;
            if(!empty($tags) && is_array($tags))
            {
                $system_hook_list = [
                    'app_init',
                    'app_dispatch',
                    'app_begin',
                    'module_init',
                    'action_begin',
                    'view_filter',
                    'app_end',
                    'log_write',
                    'log_level',
                    'response_send',
                    'response_end'
                ];
                foreach($system_hook_list as $system_hook)
                {
                    if(isset($tags[$system_hook]))
                    {
                        $result[$system_hook] = $tags[$system_hook];
                    }
                }
            }
        }

        // 处理应用钩子
        $data = Db::name('Plugins')->where(['is_enable'=>1])->column('plugins');
        if(!empty($data))
        {
            foreach($data as $plugins)
            {
                if(file_exists(APP_PATH.'plugins'.DS.$plugins.DS.'config.json'))
                {
                    $config = json_decode(file_get_contents(APP_PATH.'plugins'.DS.$plugins.DS.'config.json'), true);
                    if(!empty($config['hook']) && is_array($config['hook']))
                    {
                        foreach($config['hook'] as $hook_key=>$hook_value)
                        {
                            if(isset($result[$hook_key]))
                            {
                               $result[$hook_key] = array_merge($result[$hook_key], $hook_value); 
                            } else {
                                $result[$hook_key] = $hook_value;
                            }
                        }
                    }
                }
            }
        }

        // 部署钩子到文件
        $ret = @file_put_contents($tags_file, "<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
D
devil_gong 已提交
253
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
D
devil_gong 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------

// 应用行为扩展定义文件\nreturn ".var_export($result, true).";\n?>");
        if($ret === false)
        {
            return DataReturn('应用钩子部署失败', -10);
        }

        return DataReturn('处理成功', 0);
    }

D
devil_gong 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    /**
     * 删除
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-18
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function PluginsDelete($params = [])
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '操作id有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }
        
        // 获取应用标记
        $where = ['id'=>intval($params['id'])];
        $plugins = Db::name('Plugins')->where($where)->value('plugins');
        if(empty($plugins))
        {
           return DataReturn('应用不存在', -10); 
        }

D
devil_gong 已提交
302 303 304
        // 开启事务
        Db::startTrans();

D
devil_gong 已提交
305 306 307
        // 删除操作
        if(Db::name('Plugins')->where($where)->delete())
        {
D
devil_gong 已提交
308 309 310 311
            // 钩子部署
            $ret = self::PluginsHookDeployment();
            if($ret['code'] == 0)
            {
D
devil_gong 已提交
312 313 314 315 316 317 318
                // sql运行
                $uninstall_sql = APP_PATH.'plugins'.DS.$plugins.DS.'uninstall.sql';
                if(file_exists($uninstall_sql))
                {
                    SqlconsoleService::Implement(['sql'=>file_get_contents($uninstall_sql)]);
                }

D
devil_gong 已提交
319 320
                // 删除应用文件
                self::PluginsResourcesDelete($plugins);
D
devil_gong 已提交
321

D
devil_gong 已提交
322 323 324 325 326 327
                // 提交事务
                Db::commit();
                return DataReturn('删除成功');
            }
        } else {
            $ret = DataReturn('删除失败或资源不存在', -100);
D
devil_gong 已提交
328 329
        }

D
devil_gong 已提交
330 331 332
        // 事务回退
        Db::rollback();
        return $ret;
D
devil_gong 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
    }

    /**
     * 应用资源删除
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-02-13
     * @desc    description
     * @param   [string]          $plugins [唯一标记]
     */
    private static function PluginsResourcesDelete($plugins)
    {
        \base\FileUtil::UnlinkDir(APP_PATH.'plugins'.DS.$plugins);
        \base\FileUtil::UnlinkDir(APP_PATH.'plugins'.DS.'view'.DS.$plugins);
        \base\FileUtil::UnlinkDir(ROOT.'public'.DS.'static'.DS.'plugins'.DS.'css'.DS.$plugins);
        \base\FileUtil::UnlinkDir(ROOT.'public'.DS.'static'.DS.'plugins'.DS.'js'.DS.$plugins);
        \base\FileUtil::UnlinkDir(ROOT.'public'.DS.'static'.DS.'plugins'.DS.'images'.DS.$plugins);
        \base\FileUtil::UnlinkDir(ROOT.'public'.DS.'static'.DS.'upload'.DS.'images'.DS.'plugins_'.$plugins);
        \base\FileUtil::UnlinkDir(ROOT.'public'.DS.'static'.DS.'upload'.DS.'video'.DS.'plugins_'.$plugins);
        \base\FileUtil::UnlinkDir(ROOT.'public'.DS.'static'.DS.'upload'.DS.'file'.DS.'plugins_'.$plugins);
    }

    /**
     * 获取应用配置信息
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-17
     * @desc    description
     * @param   [string]          $plugins [应用名称]
     */
    private static function GetPluginsConfig($plugins)
    {
        $config_file = APP_PATH.'plugins'.DS.$plugins.DS.'config.json';
        if(file_exists($config_file))
        {
            return json_decode(file_get_contents($config_file), true);
        }
        return false;
    }

    /**
     * 保存
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-18
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function PluginsSave($params = [])
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'plugins',
                'error_msg'         => '应用唯一标记不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'logo',
                'error_msg'         => '请上传LOGO',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'name',
                'error_msg'         => '应用名称不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'author',
                'error_msg'         => '作者不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'author_url',
                'error_msg'         => '作者主页不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'version',
                'error_msg'         => '版本号不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'desc',
                'error_msg'         => '描述不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'apply_terminal',
                'error_msg'         => '请至少选择一个适用终端',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'apply_version',
                'error_msg'         => '请至少选择一个适用系统版本',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 权限校验
D
devil_gong 已提交
441
        $ret = self::PowerCheck();
D
devil_gong 已提交
442 443 444 445 446
        if($ret['code'] != 0)
        {
            return $ret;
        }

D
devil_gong 已提交
447 448 449
        // 应用唯一标记
        $plugins = trim($params['plugins']);

D
devil_gong 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
        // 应用不存在则添加
        $ret = self::PluginsExistInsert($params, $plugins);
        if($ret['code'] != 0)
        {
            return $ret;
        }

        // 应用目录不存在则创建
        $app_dir = APP_PATH.'plugins'.DS.$plugins;
        if(\base\FileUtil::CreateDir($app_dir) !== true)
        {
            return DataReturn('应用主目录创建失败', -10);
        }

        // 生成配置文件
        $ret = self::PluginsConfigCreated($params, $app_dir);
        if($ret['code'] != 0)
        {
            return $ret;
        }

        // 应用主文件生成
472 473
        $ret = self::PluginsApplicationCreated($params, $app_dir);
        if($ret['code'] != 0)
D
devil_gong 已提交
474
        {
475
            return $ret;
D
devil_gong 已提交
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
        }

        return DataReturn(empty($params['id']) ? '创建成功' : '更新成功', 0);
    }

    /**
     * 应用文件生成
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-18
     * @desc    description
     * @param   [array]          $params    [输入参数]
     * @param   [string]         $app_dir   [主目录地址]
     */
    private static function PluginsApplicationCreated($params, $app_dir)
    {
        $plugins = trim($params['plugins']);
$admin=<<<php
<?php
namespace app\plugins\\$plugins;
497

D
devil_gong 已提交
498
use think\Controller;
499

D
devil_gong 已提交
500 501 502 503 504 505 506
/**
 * {$params['name']} - 后台管理
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
D
devil_gong 已提交
507
class Admin extends Controller
D
devil_gong 已提交
508 509 510 511 512
{
    // 后台管理入口
    public function index(\$params = [])
    {
        // 数组组装
D
devil_gong 已提交
513 514 515
        \$this->assign('data', ['hello', 'world!']);
        \$this->assign('msg', 'hello world! admin');
        return \$this->fetch('../../../plugins/view/$plugins/admin/index');
D
devil_gong 已提交
516 517 518 519 520 521 522 523
    }
}
?>
php;

$hook=<<<php
<?php
namespace app\plugins\\$plugins;
524

D
devil_gong 已提交
525
use think\Controller;
526

D
devil_gong 已提交
527 528 529 530 531 532 533
/**
 * {$params['name']} - 钩子入口
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
D
devil_gong 已提交
534
class Hook extends Controller
D
devil_gong 已提交
535 536 537 538 539
{
    // 应用响应入口
    public function run(\$params = [])
    {
        // 是否控制器钩子
540
        // is_backend 当前为后端业务处理
541
        // hook_name 钩子名称
542
        if(isset(\$params['is_backend']) && \$params['is_backend'] === true && !empty(\$params['hook_name']))
D
devil_gong 已提交
543
        {
544 545 546 547
            // 参数一   描述
            // 参数二   0 为处理成功, 负数为失败
            // 参数三   返回数据
            return DataReturn('返回描述', 0);
D
devil_gong 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560

        // 默认返回视图
        } else {
            return 'hello world!';
        }
    }
}
?>
php;

$index=<<<php
<?php
namespace app\plugins\\$plugins;
561

D
devil_gong 已提交
562
use think\Controller;
563

D
devil_gong 已提交
564 565 566 567 568 569 570
/**
 * {$params['name']} - 前端独立页面入口
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
D
devil_gong 已提交
571
class Index extends Controller
D
devil_gong 已提交
572 573 574 575 576
{
    // 前端页面入口
    public function index(\$params = [])
    {
        // 数组组装
D
devil_gong 已提交
577 578 579
        \$this->assign('data', ['hello', 'world!']);
        \$this->assign('msg', 'hello world! index');
        return \$this->fetch('../../../plugins/view/$plugins/index/index');
D
devil_gong 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
    }
}
?>
php;

$admin_view=<<<php
{{include file="public/header" /}}

<!-- right content start  -->
<div class="content-right">
    <div class="content">
        <h1>后台管理页面</h1>
        {{:print_r(\$data)}}
        <p class="msg">{{\$msg}}</p>
    </div>
</div>
<!-- right content end  -->
        
<!-- footer start -->
{{include file="public/footer" /}}
<!-- footer end -->
php;

$index_view=<<<php
{{include file="public/header" /}}

<!-- nav start -->
{{include file="public/nav" /}}
<!-- nav end -->

<!-- header top nav -->
{{include file="public/header_top_nav" /}}

<!-- search -->
{{include file="public/nav_search" /}}

<!-- header nav -->
{{include file="public/header_nav" /}}

<!-- goods category -->
{{include file="public/goods_category" /}}

<!-- content start -->
<div class="am-g my-content">
    <div class="am-u-md-6 am-u-sm-centered">
        <h1>前端页面</h1>
        {{:print_r(\$data)}}
        <p class="msg">{{\$msg}}</p>
    </div>
</div>
<!-- content end -->

<!-- footer start -->
{{include file="public/footer" /}}
<!-- footer end -->
php;

$admin_css=<<<php
h1 {
    font-size: 60px;
}
.msg {
    font-size: 38px;
    color: #F00;
}
php;

$index_css=<<<php
h1 {
    font-size: 60px;
}
.msg {
    font-size: 68px;
    color: #4CAF50;
}
php;
656
        // 静态文件目录
D
devil_gong 已提交
657 658 659 660 661
        $app_static_css_dir = ROOT.'public'.DS.'static'.DS.'plugins'.DS.'css'.DS.trim($params['plugins']);
        if(\base\FileUtil::CreateDir($app_static_css_dir) !== true)
        {
            return DataReturn('应用静态目录创建失败[css]', -10);
        }
662 663 664

        // 编辑模式下不生成后端文件
        if(empty($params['id']))
D
devil_gong 已提交
665
        {
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
            // 创建文件
            if(@file_put_contents($app_dir.DS.'Admin.php', $admin) === false)
            {
                return DataReturn('应用文件创建失败[admin]', -11);
            }
            if(@file_put_contents($app_dir.DS.'Hook.php', $hook) === false)
            {
                return DataReturn('应用文件创建失败[hook]', -11);
            }

            // 应用后台视图目录不存在则创建
            $app_view_admin_dir = APP_PATH.'plugins'.DS.'view'.DS.trim($params['plugins']).DS.'admin';
            if(\base\FileUtil::CreateDir($app_view_admin_dir) !== true)
            {
                return DataReturn('应用视图目录创建失败[admin]', -10);
            }
            if(@file_put_contents($app_view_admin_dir.DS.'index.html', $admin_view) === false)
            {
                return DataReturn('应用视图文件创建失败[admin-view]', -11);
            }

            // css创建
            if(@file_put_contents($app_static_css_dir.DS.'admin.css', $admin_css) === false)
            {
                return DataReturn('应用静态文件创建失败[admin-css]', -11);
            }
D
devil_gong 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
        }


        // 是否有前端页面
        if(isset($params['is_home']) && $params['is_home'] == 1)
        {
            // 创建文件
            if(@file_put_contents($app_dir.DS.'Index.php', $index) === false)
            {
                return DataReturn('应用文件创建失败[index]', -11);
            }

            // 应用前端视图目录不存在则创建
            $app_view_index_dir = APP_PATH.'plugins'.DS.'view'.DS.trim($params['plugins']).DS.'index';
            if(\base\FileUtil::CreateDir($app_view_index_dir) !== true)
            {
                return DataReturn('应用视图目录创建失败[index]', -10);
            }
            if(@file_put_contents($app_view_index_dir.DS.'index.html', $index_view) === false)
            {
                return DataReturn('应用视图文件创建失败[index-view]', -11);
            }

            // css创建
            if(@file_put_contents($app_static_css_dir.DS.'index.css', $index_css) === false)
            {
                return DataReturn('应用静态文件创建失败[index-css]', -11);
            }

        // 没有独立前端页面则删除文件
        } else {
            \base\FileUtil::UnlinkFile($app_dir.DS.'Index.php');
            \base\FileUtil::UnlinkDir(APP_PATH.'plugins'.DS.'view'.DS.trim($params['plugins']).DS.'index');
            \base\FileUtil::UnlinkFile($app_static_css_dir.DS.'index.css');
        }

        return DataReturn('创建成功', 0);
    }

    /**
     * 应用配置文件生成
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-18
     * @desc    description
     * @param   [array]          $params    [输入参数]
     * @param   [string]         $app_dir   [主目录地址]
     */
    private static function PluginsConfigCreated($params, $app_dir)
    {
D
devil_gong 已提交
743 744 745 746 747 748 749 750
        // 模块名称
        $plugins = trim($params['plugins']);

        // 配置信息
        $config = self::GetPluginsConfig($plugins);
        $hook = empty($config['hook']) ? [] : $config['hook'];

        // 配置信息组装
D
devil_gong 已提交
751 752 753
        $data = [
            // 基础信息
            'base'  => [
D
devil_gong 已提交
754
                'plugins'           => $plugins,
D
devil_gong 已提交
755 756 757 758 759 760 761 762 763 764 765 766
                'name'              => $params['name'],
                'logo'              => $params['logo'],
                'author'            => $params['author'],
                'author_url'        => $params['author_url'],
                'version'           => $params['version'],
                'desc'              => $params['desc'],
                'apply_terminal'    => explode(',', $params['apply_terminal']),
                'apply_version'     => explode(',', $params['apply_version']),
                'is_home'           => (isset($params['is_home']) && $params['is_home'] == 1) ? true : false,
            ],

            // 钩子配置
D
devil_gong 已提交
767
            'hook'  => (object) $hook,
D
devil_gong 已提交
768 769
        ];

D
devil_gong 已提交
770 771 772 773 774 775 776
        // 文件存在是否有权限
        $config_file = $app_dir.DS.'config.json';
        if(file_exists($config_file) && !is_writable($config_file))
        {
            return DataReturn('应用配置文件没有操作权限'.'['.$config_file.']', -3);
        }

D
devil_gong 已提交
777
        // 创建配置文件
D
devil_gong 已提交
778
        if(@file_put_contents($config_file, JsonFormat($data)) === false)
D
devil_gong 已提交
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
        {
            return DataReturn('应用配置文件创建失败', -10);
        }

        return DataReturn('创建成功', 0);
    }

    /**
     * 应用添加
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-18
     * @desc    description
     * @param   [array]           $params    [输入参数]
     * @param   [string]          $plugins   [应用唯一标记]
     */
    private static function PluginsExistInsert($params, $plugins)
    {
        $temp_plugins = Db::name('Plugins')->where(['plugins'=>$plugins])->value('plugins');
        if(empty($temp_plugins))
        {
D
devil_gong 已提交
801
           if(Db::name('Plugins')->insertGetId(['plugins'=>$plugins, 'is_enable'=>0, 'add_time'=>time()]) <= 0)
D
devil_gong 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
            {
                return DataReturn('应用添加失败', -1);
            } 
        } else {
            if(empty($params['id']) && $temp_plugins == $plugins)
            {
                return DataReturn('应用名称已存在['.$plugins.']', -1);
            }
        }
        return DataReturn('添加成功', 0);
    }

    /**
     * 权限校验
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-09-29T00:01:49+0800
     */
D
devil_gong 已提交
821
    private static function PowerCheck()
D
devil_gong 已提交
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
    {
        // 应用目录
        $app_dir = APP_PATH.'plugins';
        if(!is_writable($app_dir))
        {
            return DataReturn('应用目录没有操作权限'.'['.$app_dir.']', -3);
        }

        // 应用视图目录
        $app_view_dir = APP_PATH.'plugins'.DS.'view';
        if(!is_writable($app_view_dir))
        {
            return DataReturn('应用视图目录没有操作权限'.'['.$app_view_dir.']', -3);
        }

        // 应用css目录
        $app_static_css_dir = ROOT.'public'.DS.'static'.DS.'plugins'.DS.'css';
        if(!is_writable($app_static_css_dir))
        {
            return DataReturn('应用css目录没有操作权限'.'['.$app_static_css_dir.']', -3);
        }

        // 应用js目录
        $app_static_js_dir = ROOT.'public'.DS.'static'.DS.'plugins'.DS.'js';
        if(!is_writable($app_static_js_dir))
        {
            return DataReturn('应用js目录没有操作权限'.'['.$app_static_js_dir.']', -3);
        }

        // 应用images目录
        $app_static_images_dir = ROOT.'public'.DS.'static'.DS.'plugins'.DS.'images';
        if(!is_writable($app_static_images_dir))
        {
            return DataReturn('应用images目录没有操作权限'.'['.$app_static_images_dir.']', -3);
        }
        return DataReturn('权限正常', 0);
    }
D
devil_gong 已提交
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877

    /**
     * 应用上传
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-12-19T00:53:45+0800
     * @param    [array]          $params [输入参数]
     */
    public static function PluginsUpload($params = [])
    {
        // 文件上传校验
        $error = FileUploadError('file');
        if($error !== true)
        {
            return DataReturn($error, -1);
        }

        // 文件格式化校验
D
devil_gong 已提交
878
        $type = array('application/zip', 'application/octet-stream', 'application/x-zip-compressed');
D
devil_gong 已提交
879 880 881 882 883 884 885 886 887 888 889 890
        if(!in_array($_FILES['file']['type'], $type))
        {
            return DataReturn('文件格式有误,请上传zip压缩包', -2);
        }

        // 权限校验
        $ret = self::PowerCheck();
        if($ret['code'] != 0)
        {
            return $ret;
        }

D
devil_gong 已提交
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
        // 资源目录
        $dir_list = [
            '_controller_'      => APP_PATH.'plugins'.DS,
            '_view_'            => APP_PATH.'plugins'.DS.'view'.DS,
            '_css_'             => ROOT.'public'.DS.'static'.DS.'plugins'.DS.'css'.DS,
            '_js_'              => ROOT.'public'.DS.'static'.DS.'plugins'.DS.'js'.DS,
            '_images_'          => ROOT.'public'.DS.'static'.DS.'plugins'.DS.'images'.DS,
            '_uploadfile_'      => ROOT.'public'.DS.'static'.DS.'upload'.DS.'file'.DS,
            '_uploadimages_'    => ROOT.'public'.DS.'static'.DS.'upload'.DS.'images'.DS,
            '_uploadvideo_'     => ROOT.'public'.DS.'static'.DS.'upload'.DS.'video'.DS,
        ];

        // 包名
        $plugins_name = '';

D
devil_gong 已提交
906 907 908 909 910 911 912 913 914
        // 开始解压文件
        $resource = zip_open($_FILES['file']['tmp_name']);
        while(($temp_resource = zip_read($resource)) !== false)
        {
            if(zip_entry_open($resource, $temp_resource))
            {
                // 当前压缩包中项目名称
                $file = zip_entry_name($temp_resource);

D
devil_gong 已提交
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
                // 获取包名
                if(empty($plugins_name))
                {
                    // 应用不存在则添加
                    $plugins_name = substr($file, 0, strpos($file, '/'));
                    $ret = self::PluginsExistInsert([], $plugins_name);
                    if($ret['code'] != 0)
                    {
                        zip_entry_close($temp_resource);
                        return $ret;
                    }
                }

                // 去除包名
                $file = substr($file, strpos($file, '/')+1);

D
devil_gong 已提交
931 932 933
                // 排除临时文件和临时目录
                if(strpos($file, '/.') === false && strpos($file, '__') === false)
                {
D
devil_gong 已提交
934
                    // 文件包对应系统所在目录
D
devil_gong 已提交
935
                    $is_has_find = false;
D
devil_gong 已提交
936
                    foreach($dir_list as $dir_key=>$dir_value)
D
devil_gong 已提交
937
                    {
D
devil_gong 已提交
938 939 940
                        if(strpos($file, $dir_key) !== false)
                        {
                            $file = str_replace($dir_key.'/', '', $dir_value.$file);
D
devil_gong 已提交
941
                            $is_has_find = true;
D
devil_gong 已提交
942 943
                            break;
                        }
D
devil_gong 已提交
944 945
                    }

D
devil_gong 已提交
946 947 948 949 950 951
                    // 没有匹配到则指定目录跳过
                    if($is_has_find == false)
                    {
                        continue;
                    }

D
devil_gong 已提交
952 953
                    // 截取文件路径
                    $file_path = substr($file, 0, strrpos($file, '/'));
D
devil_gong 已提交
954
                    
D
devil_gong 已提交
955
                    // 路径不存在则创建
D
devil_gong 已提交
956
                    \base\FileUtil::CreateDir($file_path);
D
devil_gong 已提交
957 958 959 960 961 962 963

                    // 如果不是目录则写入文件
                    if(!is_dir($file))
                    {
                        // 读取这个文件
                        $file_size = zip_entry_filesize($temp_resource);
                        $file_content = zip_entry_read($temp_resource, $file_size);
D
devil_gong 已提交
964
                        @file_put_contents($file, $file_content);
D
devil_gong 已提交
965
                    }
D
devil_gong 已提交
966

D
devil_gong 已提交
967 968 969 970 971
                    // 关闭目录项  
                    zip_entry_close($temp_resource);
                }
            }
        }
D
devil_gong 已提交
972 973 974 975 976 977 978 979 980 981 982 983 984

        // sql运行
        $install_sql = APP_PATH.'plugins'.DS.$plugins_name.DS.'install.sql';
        if(!empty($plugins_name) && file_exists($install_sql))
        {
            // 开始处理
            $ret = SqlconsoleService::Implement(['sql'=>file_get_contents($install_sql)]);
            if($ret['code'] != 0)
            {
                return $ret;
            }
        }

D
devil_gong 已提交
985
        return DataReturn('安装成功');
D
devil_gong 已提交
986
    }
D
devil_gong 已提交
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023

    /**
     * 应用打包
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-03-22
     * @desc    description
     * @param    [array]          $params [输入参数]
     */
    public static function PluginsDownload($params = [])
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '操作id有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }
        
        // 获取应用标记
        $where = ['id'=>intval($params['id'])];
        $plugins = Db::name('Plugins')->where($where)->value('plugins');
        if(empty($plugins))
        {
           return DataReturn('应用不存在', -10); 
        }

        // 开始打包
        return DataReturn('开发中', -10); 
    }
D
devil_gong 已提交
1024 1025
}
?>