PluginsService.php 6.2 KB
Newer Older
G
1.3.0  
gongfuxiang 已提交
1 2 3 4
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
D
devil_gong 已提交
5
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
G
1.3.0  
gongfuxiang 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;

use think\Db;
use app\service\ResourcesService;

/**
 * 应用服务层
 * @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 已提交
32 33
     * @param   [string]          $plugins          [应用标记]
     * @param   [array]           $attachment_field [附件字段]
D
应用  
devil_gong 已提交
34
     * @param   [boolean]         $is_cache         [是否缓存读取, 默认true]
G
1.3.0  
gongfuxiang 已提交
35
     */
D
应用  
devil_gong 已提交
36
    public static function PluginsData($plugins, $attachment_field = [], $is_cache = true)
G
1.3.0  
gongfuxiang 已提交
37
    {
38
        // 从缓存获取数据
39
        $key = config('shopxo.cache_plugins_data_key').$plugins;
D
应用  
devil_gong 已提交
40 41 42 43
        if($is_cache === true)
        {
            $data = cache($key);
        }
D
devil_gong 已提交
44 45

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

D
devil_gong 已提交
54
                // 是否有自定义附件需要处理
55
                if(!empty($attachment_field) && is_array($attachment_field))
56
                {
57
                    foreach($attachment_field as $field)
58
                    {
59 60 61 62 63
                        if(isset($data[$field]))
                        {
                            $data[$field.'_old'] = $data[$field];
                            $data[$field] = ResourcesService::AttachmentPathViewHandle($data[$field]);
                        }
64
                    }
D
devil_gong 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
                } else {
                    // 所有附件后缀名称
                    $attachment_ext = config('ueditor.fileManagerAllowFiles');

                    // 未自定义附件则自动根据内容判断处理附件,建议自定义附件字段名称处理更高效
                    if(!empty($attachment_ext) && is_array($attachment_ext))
                    {
                        foreach($data as $k=>$v)
                        {
                            $ext = strrchr(substr($v, -6), '.');
                            if($ext !== false)
                            {
                                if(in_array($ext, $attachment_ext))
                                {
                                    $data[$k.'_old'] = $v;
                                    $data[$k] = ResourcesService::AttachmentPathViewHandle($v);
                                }
                            }
                        }
                    }
85
                }
86 87

                // 存储缓存
88
                cache($key, $data);
89
            }
G
1.3.0  
gongfuxiang 已提交
90
        }
G
gongfuxiang 已提交
91
        return DataReturn('处理成功', 0, $data);
G
1.3.0  
gongfuxiang 已提交
92 93 94 95 96 97 98 99 100
    }

    /**
     * 应用数据保存
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
D
devil_gong 已提交
101 102
     * @param   [string]          $plugins          [应用标记]
     * @param   [array]           $attachment_field [附件字段]
G
1.3.0  
gongfuxiang 已提交
103
     */
D
devil_gong 已提交
104
    public static function PluginsDataSave($params = [], $attachment_field = [])
G
1.3.0  
gongfuxiang 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    {
        // 请求参数
        $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 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138
        // 附件处理
        $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 已提交
139 140 141
        // 移除多余的字段
        unset($params['data']['pluginsname'], $params['data']['pluginscontrol'], $params['data']['pluginsaction']);

G
1.3.0  
gongfuxiang 已提交
142 143 144
        // 数据更新
        if(Db::name('Plugins')->where(['plugins'=>$params['plugins']])->update(['data'=>json_encode($params['data']), 'upd_time'=>time()]))
        {
145
            // 删除缓存
146
            cache(config('shopxo.cache_plugins_data_key').$params['plugins'], null);
147
            
G
1.3.0  
gongfuxiang 已提交
148 149 150 151
            return DataReturn('操作成功');
        }
        return DataReturn('操作失败', -100);
    }
G
gongfuxiang 已提交
152 153 154 155 156 157 158 159 160 161

    /**
     * 根据应用标记获取指定字段数据
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [string]          $plugins        [应用标记]
     * @param   [string]          $field          [字段名称]
G
gongfuxiang 已提交
162
     * @return  [mixed]                           [不存在返回null, 则原始数据]
G
gongfuxiang 已提交
163 164 165 166 167 168
     */
    public static function PluginsField($plugins, $field)
    {
        $data = Db::name('Plugins')->where(['plugins'=>$plugins])->value($field);
        return DataReturn('操作成功', 0, $data);
    }
D
devil_gong 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

    /**
     * 应用状态
     * @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'];
    }
G
1.3.0  
gongfuxiang 已提交
184 185
}
?>