PluginsService.php 4.7 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);
        }
44
        if(empty($data))
G
1.3.0  
gongfuxiang 已提交
45
        {
46 47 48
            // 获取数据
            $data = Db::name('Plugins')->where(['plugins'=>$plugins])->value('data');
            if(!empty($data))
49
            {
50 51 52 53
                $data = json_decode($data, true);

                // 是否有图片需要处理
                if(!empty($attachment_field) && is_array($attachment_field))
54
                {
55
                    foreach($attachment_field as $field)
56
                    {
57 58 59 60 61
                        if(isset($data[$field]))
                        {
                            $data[$field.'_old'] = $data[$field];
                            $data[$field] = ResourcesService::AttachmentPathViewHandle($data[$field]);
                        }
62 63
                    }
                }
64 65

                // 存储缓存
66
                cache($key, $data);
67
            }
G
1.3.0  
gongfuxiang 已提交
68
        }
G
gongfuxiang 已提交
69
        return DataReturn('处理成功', 0, $data);
G
1.3.0  
gongfuxiang 已提交
70 71 72 73 74 75 76 77 78
    }

    /**
     * 应用数据保存
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
D
devil_gong 已提交
79 80
     * @param   [string]          $plugins          [应用标记]
     * @param   [array]           $attachment_field [附件字段]
G
1.3.0  
gongfuxiang 已提交
81
     */
D
devil_gong 已提交
82
    public static function PluginsDataSave($params = [], $attachment_field = [])
G
1.3.0  
gongfuxiang 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    {
        // 请求参数
        $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 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116
        // 附件处理
        $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;
            }
        }

G
1.3.0  
gongfuxiang 已提交
117 118 119
        // 数据更新
        if(Db::name('Plugins')->where(['plugins'=>$params['plugins']])->update(['data'=>json_encode($params['data']), 'upd_time'=>time()]))
        {
120
            // 删除缓存
121
            cache(config('shopxo.cache_plugins_data_key').$params['plugins'], null);
122
            
G
1.3.0  
gongfuxiang 已提交
123 124 125 126
            return DataReturn('操作成功');
        }
        return DataReturn('操作失败', -100);
    }
G
gongfuxiang 已提交
127 128 129 130 131 132 133 134 135 136

    /**
     * 根据应用标记获取指定字段数据
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [string]          $plugins        [应用标记]
     * @param   [string]          $field          [字段名称]
G
gongfuxiang 已提交
137
     * @return  [mixed]                           [不存在返回null, 则原始数据]
G
gongfuxiang 已提交
138 139 140 141 142 143
     */
    public static function PluginsField($plugins, $field)
    {
        $data = Db::name('Plugins')->where(['plugins'=>$plugins])->value($field);
        return DataReturn('操作成功', 0, $data);
    }
G
1.3.0  
gongfuxiang 已提交
144 145
}
?>