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

D
Devil 已提交
13 14
use app\service\ResourcesService;

D
v1.2.0  
devil_gong 已提交
15 16 17 18 19 20 21 22 23 24
/**
 * 小程序服务层
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class AppMiniService
{
    // 当前小程序包名称
D
devil 已提交
25
    public static $application_name;
D
v1.2.0  
devil_gong 已提交
26 27

    // 原包地址/操作地址
D
devil 已提交
28 29 30 31 32 33 34
    public static $old_root;
    public static $new_root;
    public static $old_path;
    public static $new_path;

    // 当前默认主题
    public static $default_theme;
D
v1.2.0  
devil_gong 已提交
35 36 37 38 39 40 41 42 43

    /**
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-21
     * @desc    description
     * @param    [array]          $params [输入参数]
     */
44
    public static function Init($params = [])
D
v1.2.0  
devil_gong 已提交
45 46
    {
        // 当前小程序包名称
D
devil 已提交
47
        self::$application_name = isset($params['application_name']) ? $params['application_name'] : 'weixin';
D
v1.2.0  
devil_gong 已提交
48 49

        // 原包地址/操作地址
D
devil 已提交
50
        self::$old_root = ROOT.'sourcecode';
D
devil 已提交
51
        self::$new_root = ROOT.'public'.DS.'download'.DS.'sourcecode';
52 53
        self::$old_path = self::$old_root.DS.self::$application_name;
        self::$new_path = self::$new_root.DS.self::$application_name;
D
devil 已提交
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

        // 默认主题
        self::$default_theme = self::DefaultTheme();
    }

    /**
     * 默认主题标识符
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2020-11-20
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function DefaultTheme($params = [])
    {
        return MyC(self::DefaultThemeKey($params), 'default', true);
    }

    /**
     * 默认主题标识符数据key
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2020-11-21
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function DefaultThemeKey($params = [])
    {
        if(empty(self::$application_name))
        {
            // 初始化
            self::Init($params);
        }

        return 'common_app_mini_'.self::$application_name.'_default_theme';
    }

    /**
     * 获取模板列表
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-05-10T10:24:40+0800
     * @param    [array]          $params [输入参数]
     * @return   [array]                  [模板列表]
     */
    public static function ThemeList($params = [])
    {
        // 初始化
        self::Init($params);

        // 读取目录
        $result = [];
        $dir = self::$old_path.DS;
        if(is_dir($dir))
        {
            if($dh = opendir($dir))
            {
                $img_obj = \base\Images::Instance();
                $default_preview = __MY_PUBLIC_URL__.'static'.DS.'common'.DS.'images'.DS.'default-preview.jpg';
                while(($temp_file = readdir($dh)) !== false)
                {
                    if(substr($temp_file, 0, 1) == '.')
                    {
                        continue;
                    }

                    $config = $dir.$temp_file.DS.'config.json';
D
Devil 已提交
124
                    if(is_file($dir.$temp_file) || !is_dir($dir.$temp_file) || !is_file($config) || !file_exists($config))
D
devil 已提交
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
                    {
                        continue;
                    }

                    // 读取配置文件
                    $data = json_decode(file_get_contents($config), true);
                    if(!empty($data) && is_array($data))
                    {
                        if(empty($data['name']) || empty($data['ver']) || empty($data['author']))
                        {
                            continue;
                        }
                        $preview = $dir.$temp_file.DS.'images'.DS.'preview.jpg';
                        $result[] = array(
                            'theme'     =>  $temp_file,
                            'name'      =>  htmlentities($data['name']),
                            'ver'       =>  str_replace(array(',',','), ', ', htmlentities($data['ver'])),
                            'author'    =>  htmlentities($data['author']),
                            'home'      =>  isset($data['home']) ? $data['home'] : '',
                            'preview'   =>  $img_obj->ImageToBase64(file_exists($preview) ? $preview : $default_preview),
                            'is_delete' => ($temp_file == 'default') ? 0 : 1,
                        );
                    }
                }
                closedir($dh);
            }
        }
        return $result;
    }

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

        // 文件格式化校验
D
Devil 已提交
173
        $type = ResourcesService::ZipExtTypeList();
D
devil 已提交
174 175 176 177 178
        if(!in_array($_FILES['theme']['type'], $type))
        {
            return DataReturn('文件格式有误,请上传zip压缩包', -2);
        }

D
Devil 已提交
179
        // 上传处理
D
Devil 已提交
180
        return self::ThemeUploadHandle($_FILES['theme']['tmp_name'], $params);
D
Devil 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    }

    /**
     * 模板上传处理
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-12-19T00:53:45+0800
     * @param    [string]         $package_file [软件包地址]
     * @param    [array]          $params       [输入参数]
     */
    public static function ThemeUploadHandle($package_file, $params = [])
    {
        // 初始化
        self::Init($params);

D
devil 已提交
197 198 199 200 201 202 203 204 205 206
        // 主题目录
        $dir = self::$old_path.DS;

        // 目录是否有权限
        if(!is_writable($dir))
        {
            return DataReturn('视图目录没权限['.$dir.']', -10);
        }

        // 开始解压文件
D
Devil 已提交
207
        $resource = zip_open($package_file);
D
devil 已提交
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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
        while(($temp_resource = zip_read($resource)) !== false)
        {
            if(zip_entry_open($resource, $temp_resource))
            {
                // 资源文件
                $file = zip_entry_name($temp_resource);

                // 排除系统.开头的临时文件和目录
                if(strpos($file, '/.') !== false)
                {
                    continue;
                }

                // 截取文件路径
                $file_path = $dir.substr($file, 0, strrpos($file, '/'));

                // 路径不存在则创建
                if(!is_dir($file_path))
                {
                    mkdir($file_path, 0777, true);
                }

                // 如果不是目录则写入文件
                if(!is_dir($dir.$file))
                {
                    // 读取这个文件
                    $file_size = zip_entry_filesize($temp_resource);
                    $file_content = zip_entry_read($temp_resource, $file_size);
                    file_put_contents($dir.$file, $file_content);
                }
                
                // 关闭目录项  
                zip_entry_close($temp_resource);
            }
        }
        return DataReturn('安装成功');
    }

    /**
     * 模板删除
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-12-19T00:46:02+0800
     * @param    [array]          $params [输入参数]
     */
    public static function ThemeDelete($params = [])
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '模板id有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 初始化
        self::Init($params);

        // 防止路径回溯
        $id = htmlentities(str_replace(array('.', '/', '\\', ':'), '', strip_tags($params['id'])));
        if(empty($id))
        {
            return DataReturn('主题名称有误', -1);
        }

        // default不能删除
        if($id == 'default')
        {
            return DataReturn('系统模板不能删除', -2);
        }

        // 不能删除正在使用的主题
        if(self::$default_theme == $id)
        {
            return DataReturn('不能删除正在使用的主题', -2);
        }

        // 开始删除主题
        if(\base\FileUtil::UnlinkDir(self::$old_path.DS.$id))
        {
            return DataReturn('删除成功');
        }
        return DataReturn('删除失败或资源不存在', -100);
    }

    /**
     * 主题打包
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-03-22
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function ThemeDownload($params = [])
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '模板id有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 初始化
        self::Init($params);

        // 是否开启开发者模式
D
Devil 已提交
329
        if(MyConfig('shopxo.is_develop') !== true)
D
devil 已提交
330 331 332 333 334 335 336 337 338 339 340 341
        {
            return DataReturn('请先开启开发者模式', -1); 
        }

        // 防止路径回溯
        $theme = htmlentities(str_replace(array('.', '/', '\\', ':'), '', strip_tags($params['id'])));
        if(empty($theme))
        {
            return DataReturn('主题名称有误', -1);
        }

        // 获取配置信息
D
Devil 已提交
342 343
        $config_res = self::MiniThemeConfig($theme, $params);
        if($config_res['code'] != 0)
D
devil 已提交
344
        {
D
Devil 已提交
345
            return $config_res;
D
devil 已提交
346
        }
D
Devil 已提交
347
        $config = $config_res['data'];
D
devil 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

        // 目录不存在则创建
        $new_dir = ROOT.'runtime'.DS.'data'.DS.'theme_appmini_package'.DS.$theme;
        \base\FileUtil::CreateDir($new_dir);

        // 复制包目录
        $old_dir = self::$old_path.DS.$theme;
        if(is_dir($old_dir))
        {
            if(\base\FileUtil::CopyDir($old_dir, $new_dir) != true)
            {
                return DataReturn('项目包复制失败', -2);
            }
        }

D
Devil 已提交
363 364 365 366 367 368 369
        // 历史信息更新
        $ret = self::HistoryUpdateHandle($new_dir);
        if(isset($ret['code']) && $ret['code'] != 0)
        {
            return $ret;
        }

D
devil 已提交
370 371 372 373 374 375 376 377 378 379 380
        // 生成压缩包
        $zip = new \base\ZipFolder();
        if(!$zip->zip($new_dir.'.zip', $new_dir))
        {
            return DataReturn('压缩包生成失败', -100);
        }

        // 生成成功删除目录
        \base\FileUtil::UnlinkDir($new_dir);

        // 开始下载
D
Devil 已提交
381 382
        $appmini_type = lang('common_appmini_type');
        $application_name = array_key_exists(self::$application_name, $appmini_type) ? $appmini_type[self::$application_name]['name'].'-' : '';
D
Devil 已提交
383
        if(\base\FileUtil::DownloadFile($new_dir.'.zip', $application_name.$config['name'].'_v'.$config['ver'].'.zip'))
D
devil 已提交
384 385 386 387 388
        {
            @unlink($new_dir.'.zip');
        } else {
            return DataReturn('下载失败', -100);
        }
D
v1.2.0  
devil_gong 已提交
389 390
    }

D
Devil 已提交
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
    /**
     * 主题配置信息
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-04-22
     * @desc    description
     * @param   [string]         $theme     [主题标识]
     * @param   [array]          $params    [输入参数]
     */
    public static function MiniThemeConfig($theme, $params)
    {
        // 初始化
        self::Init($params);

        // 获取配置信息
        $config_file = self::$old_path.DS.$theme.DS.'config.json';
        if(!file_exists($config_file))
        {
            return DataReturn('小程序主题配置文件不存在', -1);
        }
        $config = json_decode(file_get_contents($config_file), true);
        if(empty($config))
        {
            return DataReturn('主小程序题配置信息有误', -1);
        }
        return DataReturn('success', 0, $config);
    }

D
v1.2.0  
devil_gong 已提交
420
    /**
D
devil 已提交
421
     * 获取小程序下载包数据列表
D
v1.2.0  
devil_gong 已提交
422 423 424 425 426 427
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-05-10T10:24:40+0800
     * @param    [array]          $params [输入参数]
     */
D
devil 已提交
428
    public static function DownloadDataList($params = [])
D
v1.2.0  
devil_gong 已提交
429 430
    {
        // 初始化
431
        self::Init($params);
D
v1.2.0  
devil_gong 已提交
432 433 434

        // 获取包列表
        $result = [];
435
        if(is_dir(self::$new_path))
D
v1.2.0  
devil_gong 已提交
436
        {
437
            if($dh = opendir(self::$new_path))
D
v1.2.0  
devil_gong 已提交
438 439 440 441 442
            {
                while(($temp_file = readdir($dh)) !== false)
                {
                    if($temp_file != '.' && $temp_file != '..')
                    {
443
                        $file_path = self::$new_path.DS.$temp_file;
D
devil 已提交
444
                        $url = __MY_PUBLIC_URL__.'download'.DS.'sourcecode'.DS.self::$application_name.DS.$temp_file;
D
v1.2.0  
devil_gong 已提交
445
                        $result[] = [
D
devil 已提交
446
                            'name'  => (substr($temp_file, -4) === '.zip') ? substr($temp_file, 0, strlen($temp_file)-4) : $temp_file,
D
v1.2.0  
devil_gong 已提交
447 448 449 450 451 452 453 454 455
                            'url'   => substr($url, -4) == '.zip' ? $url : '',
                            'size'  => FileSizeByteToUnit(filesize($file_path)),
                            'time'  => date('Y-m-d H:i:s', filectime($file_path)),
                        ];
                    }
                }
                closedir($dh);
            }
        }
D
devil 已提交
456
        return DataReturn('success', 0, $result);
D
v1.2.0  
devil_gong 已提交
457 458 459 460 461 462 463 464 465 466 467
    }

    /**
     * 源码包生成
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-21
     * @desc    description
     * @param    [array]          $params [输入参数]
     */
468
    public static function Created($params = [])
D
v1.2.0  
devil_gong 已提交
469
    {
470
        // 是否https
D
Devil 已提交
471
        if(MyConfig('shopxo.is_develop') !== true)
472
        {
D
devil_gong 已提交
473 474 475 476
            if(__MY_HTTP__ != 'https')
            {
                return DataReturn('请使用https协议', -1);
            }
477 478
        }

D
v1.2.0  
devil_gong 已提交
479
        // 初始化
480
        self::Init($params);
D
v1.2.0  
devil_gong 已提交
481 482

        // 配置内容
D
devil 已提交
483 484 485
        $title = MyC('common_app_mini_'.self::$application_name.'_title');
        $describe = MyC('common_app_mini_'.self::$application_name.'_describe');
        if(empty($title) || empty($describe))
D
v1.2.0  
devil_gong 已提交
486 487 488 489
        {
            return DataReturn('配置信息不能为空', -1);
        }

490 491 492 493
        // 源码目录不存在则创建
        \base\FileUtil::CreateDir(self::$new_root);

        // 源码目标目录是否存在
494
        if(!is_dir(self::$new_root))
D
v1.2.0  
devil_gong 已提交
495
        {
496
            return DataReturn('源码目标目录不存在['.self::$new_root.']', -1);
D
v1.2.0  
devil_gong 已提交
497 498
        }

499
        // 源码目标目录没有权限
500
        if(!is_writable(self::$new_root))
D
v1.2.0  
devil_gong 已提交
501
        {
502
            return DataReturn('源码目标目录没有权限['.self::$new_root.']', -1);
D
v1.2.0  
devil_gong 已提交
503 504 505
        }

        // 目录不存在则创建
506
        \base\FileUtil::CreateDir(self::$new_path);
D
v1.2.0  
devil_gong 已提交
507 508

        // 复制包目录
D
devil 已提交
509
        $old_dir = self::$old_path.DS.self::$default_theme;
510
        $new_dir = self::$new_path.DS.date('YmdHis');
D
devil 已提交
511
        if(\base\FileUtil::CopyDir($old_dir, $new_dir) != true)
D
v1.2.0  
devil_gong 已提交
512 513 514 515 516 517 518 519 520 521 522 523
        {
            return DataReturn('项目包复制失败', -2);
        }

        // 校验基础文件是否存在
        if(!file_exists($new_dir.DS.'app.js') || !file_exists($new_dir.DS.'app.json'))
        {
            return DataReturn('包基础文件不存在,请重新生成', -3);
        }

        // 替换内容
        // app.js
D
devil 已提交
524 525 526 527
        $search = [
            '{{request_url}}',
            '{{application_title}}',
            '{{application_describe}}',
D
devil 已提交
528
            '{{currency_symbol}}',
D
devil 已提交
529 530 531
        ];
        $replace = [
            __MY_URL__,
D
devil 已提交
532 533
            $title,
            $describe,
D
Devil 已提交
534
            MyConfig('shopxo.currency_symbol'),
D
devil 已提交
535 536
        ];
        $status = file_put_contents($new_dir.DS.'app.js', str_replace($search, $replace, file_get_contents($new_dir.DS.'app.js')));
D
v1.2.0  
devil_gong 已提交
537 538 539 540 541 542
        if($status === false)
        {
            return DataReturn('基础配置替换失败', -4);
        }

        // app.json
D
devil 已提交
543
        $status = file_put_contents($new_dir.DS.'app.json', str_replace(['{{application_title}}'], [$title], file_get_contents($new_dir.DS.'app.json')));
D
v1.2.0  
devil_gong 已提交
544 545 546 547 548
        if($status === false)
        {
            return DataReturn('基础配置替换失败', -4);
        }

G
gongfuxiang 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
        // 小程序额外处理
        switch(self::$application_name)
        {
            // 微信
            case 'weixin' :
                $ret = self::ExtendHandleWeixin($new_dir);
                break;

            // 默认
            default :
                $ret = DataReturn('无需处理', 0);
        }
        if(isset($ret['code']) && $ret['code'] != 0)
        {
            return $ret;
        }

D
Devil 已提交
566 567 568 569 570 571 572
        // 历史信息更新
        $ret = self::HistoryUpdateHandle($new_dir);
        if(isset($ret['code']) && $ret['code'] != 0)
        {
            return $ret;
        }

D
v1.2.0  
devil_gong 已提交
573 574 575 576 577 578 579 580 581 582 583 584 585
        // 生成压缩包
        $zip = new \base\ZipFolder();
        if(!$zip->zip($new_dir.'.zip', $new_dir))
        {
            return DataReturn('压缩包生成失败', -100);
        }

        // 生成成功删除目录
        \base\FileUtil::UnlinkDir($new_dir);

        return DataReturn('生成成功', 0);
    }

D
Devil 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599
    /**
     * 历史信息更新
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-02-18
     * @desc    description
     * @param   [string]          $new_dir [新源码包目录]
     */
    public static function HistoryUpdateHandle($new_dir)
    {
        // 配置信息
        $file = $new_dir.DS.'app.json';
        $config = json_decode(file_get_contents($file), true);
600 601 602 603 604 605 606 607

        // 插件配置为空防止成为数组
        if(array_key_exists('plugins', $config) && empty($config['plugins']))
        {
            $config['plugins'] = (object) [];
        }

        // 历史信息
D
Devil 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
        if(empty($config['history']))
        {
            $config['history'] = [];
        }
        $config['history'][] = [
            'host'  => __MY_HOST__,
            'url'   => __MY_URL__,
            'ip'    => __MY_ADDR__,
            'time'  => date('Y-m-d H:i:s'),
        ];
        if(@file_put_contents($file, JsonFormat($config)) === false)
        {
            return DataReturn('新应用配置文件更新失败', -11);
        }
    }

G
gongfuxiang 已提交
624 625 626 627 628 629 630
    /**
     * 扩展处理 - 微信
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-21
     * @desc    description
D
Devil 已提交
631
     * @param   [string]          $new_dir [新源码包目录]
G
gongfuxiang 已提交
632 633 634
     */
    private static function ExtendHandleWeixin($new_dir)
    {
D
devil 已提交
635 636
        // 启用好物推荐
        if(MyC('common_app_is_good_thing', 0) == 1)
G
gongfuxiang 已提交
637 638 639
        {
            // app.json
            $file = $new_dir.DS.'app.json';
D
Devil 已提交
640 641
            $config = json_decode(file_get_contents($file), true);
            if(is_array($config) && isset($config['plugins']))
G
gongfuxiang 已提交
642
            {
D
Devil 已提交
643
                $config['plugins']['goodsSharePlugin'] = [
D
devil 已提交
644
                    'version'   => MyC('common_app_is_good_thing_ver', '4.0.1', true),
G
gongfuxiang 已提交
645 646
                    'provider'  => 'wx56c8f077de74b07c',
                ];
D
Devil 已提交
647
                if(file_put_contents($file, JsonFormat($config)) === false)
G
gongfuxiang 已提交
648 649 650 651 652 653 654
                {
                    return DataReturn('好物推荐主配置失败', -50);
                }
            }

            // goods-detail.json
            $file = $new_dir.DS.'pages'.DS.'goods-detail'.DS.'goods-detail.json';
D
Devil 已提交
655 656
            $config = json_decode(file_get_contents($file), true);
            if(is_array($config) && isset($config['usingComponents']))
G
gongfuxiang 已提交
657
            {
D
Devil 已提交
658 659
                $config['usingComponents']['share-button'] = 'plugin://goodsSharePlugin/share-button';
                if(file_put_contents($file, JsonFormat($config)) === false)
G
gongfuxiang 已提交
660 661 662 663 664
                {
                    return DataReturn('好物推荐商品配置失败', -51);
                }
            }
        }
D
devil 已提交
665 666 667 668 669 670

        // 启用直播
        if(MyC('common_app_weixin_liveplayer', 0) == 1)
        {
            // app.json
            $file = $new_dir.DS.'app.json';
D
Devil 已提交
671 672
            $config = json_decode(file_get_contents($file), true);
            if(is_array($config) && isset($config['plugins']))
D
devil 已提交
673
            {
D
Devil 已提交
674
                $config['plugins']['live-player-plugin'] = [
D
v2.1.0  
Devil 已提交
675
                    'version'   => MyC('common_app_weixin_liveplayer_ver', '1.3.0', true),
D
devil 已提交
676 677
                    'provider'  => 'wx2b03c6e691cd7370',
                ];
D
Devil 已提交
678
                if(file_put_contents($file, JsonFormat($config)) === false)
D
devil 已提交
679 680 681 682 683 684
                {
                    return DataReturn('直播配置失败', -50);
                }
            }
        }

G
gongfuxiang 已提交
685 686 687
        return DataReturn('配置成功', 0);
    }

D
v1.2.0  
devil_gong 已提交
688 689 690 691 692 693 694
    /**
     * 源码包删除
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-21
     * @desc    description
G
gongfuxiang 已提交
695
     * @param   [array]          $params [输入参数]
D
v1.2.0  
devil_gong 已提交
696
     */
697
    public static function Delete($params = [])
D
v1.2.0  
devil_gong 已提交
698
    {
D
devil 已提交
699 700
        // 参数是否有误
        if(empty($params['ids']))
D
v1.2.0  
devil_gong 已提交
701
        {
D
devil 已提交
702 703 704 705 706 707
            return DataReturn('操作id有误', -1);
        }
        // 是否数组
        if(!is_array($params['ids']))
        {
            $params['ids'] = explode(',', $params['ids']);
D
v1.2.0  
devil_gong 已提交
708 709 710
        }

        // 初始化
711
        self::Init($params);
D
v1.2.0  
devil_gong 已提交
712

D
devil 已提交
713 714 715 716
        // 循环操作
        $sucs = 0;
        $fail = 0;
        foreach($params['ids'] as $id)
717
        {
D
devil 已提交
718 719 720 721 722 723 724 725 726
            // 目录处理
            $suffix = '';
            if(substr($id, -4) === '.zip')
            {
                $name = substr($id, 0, strlen($id)-4);
                $suffix = '.zip';
            } else {
                $name = $id;
            }
727

D
devil 已提交
728 729 730
            // 防止路径回溯
            $path_name = self::$new_path.DS.htmlentities(str_replace(array('.', '/', '\\', ':'), '', strip_tags($id)));
            $path_zip = $path_name.'.zip';
731

D
devil 已提交
732 733 734 735 736 737 738 739 740
            // 删除包
            $statusz = \base\FileUtil::UnlinkFile($path_zip);
            $statusf = \base\FileUtil::UnlinkDir($path_name);
            if($statusz || $statusf)
            {
                $sucs++;
            } else {
                $fail++;
            }
D
v1.2.0  
devil_gong 已提交
741
        }
D
devil 已提交
742 743 744

        // 成功
        if($sucs == count($params['ids']))
D
v1.2.0  
devil_gong 已提交
745 746 747
        {
            return DataReturn('删除成功');
        }
D
devil 已提交
748 749 750 751 752 753 754 755

        // 失败
        if($fail == count($params['ids']))
        {
            return DataReturn('删除失败', -100);
        }

        return DataReturn('成功['.$sucs.'],失败['.$fail.']');
D
v1.2.0  
devil_gong 已提交
756
    }
D
Devil 已提交
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803

    /**
     * 小程序主题更新信息
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-04-22
     * @desc    description
     * @param   [array]           $params [输入参数]
     */
    public static function AppMiniUpgradeInfo($params = [])
    {
        if(!empty($params) && !empty($params['data']) && !empty($params['terminal']))
        {
            // 数据处理
            $data = [];
            foreach($params['data'] as $v)
            {
                if(!empty($v['name']) && !empty($v['ver']) && !empty($v['theme']) && !empty($v['author']))
                {
                    $data[] = [
                        'plugins'   => $v['theme'],
                        'name'      => $v['name'],
                        'ver'       => $v['ver'],
                        'author'    => $v['author'],
                    ];
                }
            }
            if(!empty($data))
            {
                // 获取更新信息
                $request_params = [
                    'plugins_type'      => 'minitheme',
                    'plugins_data'      => $data,
                    'plugins_terminal'  => $params['terminal'],
                ];
                $res = StoreService::PluginsUpgradeInfo($request_params);
                if(!empty($res['data']))
                {
                    $res['data'] = array_column($res['data'], null, 'plugins');
                }
                return $res;
            }
        }

        return DataReturn('无插件数据', 0);
    }
D
v1.2.0  
devil_gong 已提交
804 805
}
?>