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

use think\Db;
use app\service\ResourcesService;
15
use app\service\PluginsAdminService;
D
Devil 已提交
16
use app\service\StoreService;
G
1.3.0  
gongfuxiang 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

/**
 * 应用服务层
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class PluginsService
{
    /**
     * 根据应用标记获取数据
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
D
devil_gong 已提交
34 35
     * @param   [string]          $plugins          [应用标记]
     * @param   [array]           $attachment_field [附件字段]
D
应用  
devil_gong 已提交
36
     * @param   [boolean]         $is_cache         [是否缓存读取, 默认true]
G
1.3.0  
gongfuxiang 已提交
37
     */
D
应用  
devil_gong 已提交
38
    public static function PluginsData($plugins, $attachment_field = [], $is_cache = true)
G
1.3.0  
gongfuxiang 已提交
39
    {
40
        // 从缓存获取数据
D
Devil 已提交
41
        $data = ($is_cache === true) ? self::PluginsCacheData($plugins) : [];
D
devil_gong 已提交
42 43

        // 数据不存在则从数据库读取
44
        if(empty($data))
G
1.3.0  
gongfuxiang 已提交
45
        {
46
            // 获取数据
D
devil_gong 已提交
47 48
            $ret = self::PluginsField($plugins, 'data');
            if(!empty($ret['data']))
49
            {
50 51 52 53 54 55 56
                // 数据处理
                $data = self::PluginsDataHandle($ret['data'], $attachment_field);

                // 存储缓存
                self::PluginsCacheStorage($plugins, $data);
            }
        }
D
Devil 已提交
57
        return DataReturn('处理成功', 0, empty($data) ? [] : $data);
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
    }

    /**
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-01-06
     * @desc    description
     * @param   [array|json]      $data             [应用配置数据]
     * @param   [array]           $attachment_field [附件字段]
     */
    public static function PluginsDataHandle($data, $attachment_field = [])
    {
        // 有数据并且非数组则解析json
        if(!empty($data) && !is_array($data))
        {
            $data = json_decode($data, true);
        }

        // 处理配置数据
        if(!empty($data) && is_array($data))
        {
            // 是否有自定义附件需要处理
            if(!empty($attachment_field) && is_array($attachment_field))
            {
                foreach($attachment_field as $field)
84
                {
85
                    if(isset($data[$field]))
86
                    {
87 88 89 90 91 92 93
                        $data[$field.'_old'] = $data[$field];
                        $data[$field] = ResourcesService::AttachmentPathViewHandle($data[$field]);
                    }
                }
            } else {
                // 所有附件后缀名称
                $attachment_ext = config('ueditor.fileManagerAllowFiles');
D
devil_gong 已提交
94

95 96 97 98 99 100
                // 未自定义附件则自动根据内容判断处理附件,建议自定义附件字段名称处理更高效
                if(!empty($attachment_ext) && is_array($attachment_ext))
                {
                    foreach($data as $k=>$v)
                    {
                        if(!empty($v) && !is_array($v) && !is_object($v))
D
devil_gong 已提交
101
                        {
102 103
                            $ext = strrchr(substr($v, -6), '.');
                            if($ext !== false)
D
devil_gong 已提交
104
                            {
105
                                if(in_array($ext, $attachment_ext))
D
devil_gong 已提交
106
                                {
107 108
                                    $data[$k.'_old'] = $v;
                                    $data[$k] = ResourcesService::AttachmentPathViewHandle($v);
D
devil_gong 已提交
109 110 111 112
                                }
                            }
                        }
                    }
113 114
                }
            }
G
1.3.0  
gongfuxiang 已提交
115
        }
116
        return empty($data) ? null : $data;
G
1.3.0  
gongfuxiang 已提交
117 118 119 120 121 122 123 124 125
    }

    /**
     * 应用数据保存
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
D
devil_gong 已提交
126 127
     * @param   [string]          $plugins          [应用标记]
     * @param   [array]           $attachment_field [附件字段]
G
1.3.0  
gongfuxiang 已提交
128
     */
D
devil_gong 已提交
129
    public static function PluginsDataSave($params = [], $attachment_field = [])
G
1.3.0  
gongfuxiang 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'plugins',
                'error_msg'         => '应用标记不能为空',
            ],
            [
                'checked_type'      => 'isset',
                'key_name'          => 'data',
                'error_msg'         => '数据参数不能为空',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

D
devil_gong 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163
        // 附件处理
        $attachment = ResourcesService::AttachmentParams($params['data'], $attachment_field);
        if($attachment['code'] != 0)
        {
            return $attachment;
        }
        if(!empty($attachment['data']))
        {
            foreach($attachment['data'] as $field=>$value)
            {
                $params['data'][$field] = $value;
            }
        }

D
devil_gong 已提交
164 165 166
        // 移除多余的字段
        unset($params['data']['pluginsname'], $params['data']['pluginscontrol'], $params['data']['pluginsaction']);

G
1.3.0  
gongfuxiang 已提交
167 168 169
        // 数据更新
        if(Db::name('Plugins')->where(['plugins'=>$params['plugins']])->update(['data'=>json_encode($params['data']), 'upd_time'=>time()]))
        {
170
            // 删除缓存
D
Devil 已提交
171
            self::PluginsCacheDelete($params['plugins']);
172
            
G
1.3.0  
gongfuxiang 已提交
173 174 175 176
            return DataReturn('操作成功');
        }
        return DataReturn('操作失败', -100);
    }
G
gongfuxiang 已提交
177

D
Devil 已提交
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
    /**
     * 应用缓存c存储
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2020-12-23
     * @desc    description
     * @param   [string]          $plugins [应用标记]
     * @param   [mixed]           $data    [应用数据]
     */
    public static function PluginsCacheStorage($plugins, $data)
    {
        return cache(config('shopxo.cache_plugins_data_key').$plugins, $data);
    }

    /**
     * 应用缓存获取
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2020-12-23
     * @desc    description
     * @param   [string]          $plugins [应用标记]
     */
    public static function PluginsCacheData($plugins)
    {
        $data = cache(config('shopxo.cache_plugins_data_key').$plugins);
        return empty($data) ? '' : $data;
    }

    /**
     * 应用缓存删除
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2020-12-23
     * @desc    description
     * @param   [string]          $plugins [应用标记]
     */
    public static function PluginsCacheDelete($plugins)
    {
        cache(config('shopxo.cache_plugins_data_key').$plugins, null);
    }

G
gongfuxiang 已提交
222 223 224 225 226 227 228 229 230
    /**
     * 根据应用标记获取指定字段数据
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [string]          $plugins        [应用标记]
     * @param   [string]          $field          [字段名称]
G
gongfuxiang 已提交
231
     * @return  [mixed]                           [不存在返回null, 则原始数据]
G
gongfuxiang 已提交
232 233 234 235 236 237
     */
    public static function PluginsField($plugins, $field)
    {
        $data = Db::name('Plugins')->where(['plugins'=>$plugins])->value($field);
        return DataReturn('操作成功', 0, $data);
    }
D
devil_gong 已提交
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-10-17
     * @desc    description
     * @param   [string]          $plugins        [应用标记]
     */
    public static function PluginsStatus($plugins)
    {
        $ret = self::PluginsField($plugins, 'is_enable');
        return $ret['data'];
    }
D
devil 已提交
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

    /**
     * 应用校验
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @date     2020-01-02
     * @param   [string]          $plugins        [应用标记]
     */
    public static function PluginsCheck($plugins)
    {
        $ret = self::PluginsStatus($plugins);
        if($ret === null)
        {
            return DataReturn('应用未安装['.$plugins.']', -10);
        }
        if($ret != 1)
        {
            return DataReturn('应用未启用['.$plugins.']', -10);
        }
        return DataReturn('验证成功', 0);
    }

    /**
     * 应用控制器调用
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @date     2020-01-02
282 283 284 285 286
     * @param    [string]          $plugins        [应用标记]
     * @param    [string]          $control        [应用控制器]
     * @param    [string]          $action         [应用方法]
     * @param    [string]          $group          [应用组(admin, index, api)]
     * @param    [array]           $params         [输入参数]
D
devil 已提交
287
     */
288
    public static function PluginsControlCall($plugins, $control, $action, $group = 'index', $params = [])
D
devil 已提交
289 290 291 292 293 294 295 296 297 298
    {
        // 应用校验
        $ret = self::PluginsCheck($plugins);
        if($ret['code'] != 0)
        {
            return $ret;
        }

        // 应用控制器
        $control = ucfirst($control);
D
Devil 已提交
299 300
        $plugins_class = '\app\plugins\\'.$plugins.'\\'.$group.'\\'.$control;
        if(!class_exists($plugins_class))
D
devil 已提交
301 302 303 304 305 306
        {
            return DataReturn('应用控制器未定义['.$control.']', -1);
        }

        // 调用方法
        $action = ucfirst($action);
D
Devil 已提交
307
        $obj = new $plugins_class($params);
D
devil 已提交
308 309 310 311
        if(!method_exists($obj, $action))
        {
            return DataReturn('应用方法未定义['.$action.']', -1);
        }
D
devil 已提交
312 313

        // 调用方法仅传递请求参数
D
Devil 已提交
314 315 316 317
        if(!empty($params) && isset($params['data_request']))
        {
            $params = $params['data_request'];
        }
D
Devil 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

        // 安全判断
        if(config('shopxo.is_develop') === false)
        {
            $key = 'plugins_legal_check_'.$plugins;
            $status = cache($key);
            if(empty($status))
            {
                $config = PluginsAdminService::GetPluginsConfig($plugins);
                if(!empty($config) && is_array($config))
                {
                    unset($config['hook']);
                } else {
                    $config = [];
                }
                $check_params = [
                    'config'    => $config,
                    'plugins'   => $plugins,
                ];
                $ret = StoreService::PluginsLegalCheck($check_params);
                if($ret['code'] != 0)
                {
                    return $ret;
                }
                cache($key, 1, 600);
            }
        }

        // 调用对应插件
D
Devil 已提交
347 348 349 350
        return DataReturn('调用成功', 0, $obj->$action($params));
    }

    /**
D
Devil 已提交
351
     * 应用回调事件调用
D
Devil 已提交
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
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @date     2020-01-02
     * @param    [string]          $plugins        [应用标记]
     * @param    [string]          $action         [事件方法(Upload 上传, Install 安装, Uninstall 卸载, Download 下载, Delete 删除)]
     * @param    [array]           $params         [输入参数]
     */
    public static function PluginsEventCall($plugins, $action, $params = [])
    {
        // 应用事件
        $plugins = '\app\plugins\\'.$plugins.'\\Event';
        if(!class_exists($plugins))
        {
            return DataReturn('应用事件未定义['.$plugins.']', -1);
        }

        // 调用方法
        $action = ucfirst($action);
        $obj = new $plugins($params);
        if(!method_exists($obj, $action))
        {
            return DataReturn('应用事件方法未定义['.$action.']', -1);
        }

        // 调用方法仅传递请求参数
        if(!empty($params) && isset($params['data_request']))
D
devil 已提交
379 380 381
        {
            $params = $params['data_request'];
        }
382
        return DataReturn('调用成功', 0, $obj->$action($params));
D
devil 已提交
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

    /**
     * 所有有效插件配置列表
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-01-06
     * @desc    description
     * @param   [array]           $params [输入参数]
     */
    public static function PluginsBaseList($params = [])
    {
        $data = Db::name('Plugins')->where(['is_enable'=>1])->order(PluginsAdminService::$plugins_order_by)->field('id,plugins,data')->select();
        if(!empty($data))
        {
            foreach($data as &$v)
            {
                $v['data'] = self::PluginsDataHandle($v['data']);
            }
        } else {
            $data = null;
        }
        return $data;
    }
G
1.3.0  
gongfuxiang 已提交
408 409
}
?>