shopxo/application/service/PluginsService.php

191 lines
6.6 KiB
PHP
Raw Normal View History

2019-02-09 09:15:23 +08:00
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
2019-02-18 13:52:07 +08:00
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
2019-02-09 09:15:23 +08:00
// +----------------------------------------------------------------------
// | 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
2019-02-28 17:27:24 +08:00
* @param [string] $plugins [应用标记]
* @param [array] $attachment_field [附件字段]
2019-04-18 16:12:43 +08:00
* @param [boolean] $is_cache [是否缓存读取, 默认true]
2019-02-09 09:15:23 +08:00
*/
2019-04-18 16:12:43 +08:00
public static function PluginsData($plugins, $attachment_field = [], $is_cache = true)
2019-02-09 09:15:23 +08:00
{
// 从缓存获取数据
2019-10-17 15:18:41 +08:00
$data = [];
$key = config('shopxo.cache_plugins_data_key').$plugins;
2019-04-18 16:12:43 +08:00
if($is_cache === true)
{
$data = cache($key);
}
2019-10-16 20:45:41 +08:00
// 数据不存在则从数据库读取
if(empty($data))
2019-02-09 09:15:23 +08:00
{
// 获取数据
2019-10-17 14:06:52 +08:00
$ret = self::PluginsField($plugins, 'data');
if(!empty($ret['data']))
{
2019-10-17 14:06:52 +08:00
$data = json_decode($ret['data'], true);
2019-11-06 21:06:01 +08:00
if(!empty($data) && is_array($data))
{
2019-11-06 21:06:01 +08:00
// 是否有自定义附件需要处理
if(!empty($attachment_field) && is_array($attachment_field))
{
2019-11-06 21:06:01 +08:00
foreach($attachment_field as $field)
{
2019-11-06 21:06:01 +08:00
if(isset($data[$field]))
{
$data[$field.'_old'] = $data[$field];
$data[$field] = ResourcesService::AttachmentPathViewHandle($data[$field]);
}
}
2019-11-06 21:06:01 +08:00
} else {
// 所有附件后缀名称
$attachment_ext = config('ueditor.fileManagerAllowFiles');
2019-10-16 20:45:41 +08:00
2019-11-06 21:06:01 +08:00
// 未自定义附件则自动根据内容判断处理附件,建议自定义附件字段名称处理更高效
if(!empty($attachment_ext) && is_array($attachment_ext))
2019-10-16 20:45:41 +08:00
{
2019-11-06 21:06:01 +08:00
foreach($data as $k=>$v)
2019-10-16 20:45:41 +08:00
{
2019-11-06 21:06:01 +08:00
if(!empty($v) && !is_array($v) && !is_object($v))
2019-10-16 20:45:41 +08:00
{
2019-11-06 21:06:01 +08:00
$ext = strrchr(substr($v, -6), '.');
if($ext !== false)
2019-10-28 11:26:22 +08:00
{
2019-11-06 21:06:01 +08:00
if(in_array($ext, $attachment_ext))
{
$data[$k.'_old'] = $v;
$data[$k] = ResourcesService::AttachmentPathViewHandle($v);
}
2019-10-28 11:26:22 +08:00
}
2019-10-16 20:45:41 +08:00
}
}
}
}
}
// 存储缓存
cache($key, $data);
}
2019-02-09 09:15:23 +08:00
}
2019-05-27 00:58:59 +08:00
return DataReturn('处理成功', 0, $data);
2019-02-09 09:15:23 +08:00
}
/**
* 应用数据保存
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-29
* @desc description
2019-02-28 17:27:24 +08:00
* @param [string] $plugins [应用标记]
* @param [array] $attachment_field [附件字段]
2019-02-09 09:15:23 +08:00
*/
2019-02-28 17:27:24 +08:00
public static function PluginsDataSave($params = [], $attachment_field = [])
2019-02-09 09:15:23 +08:00
{
// 请求参数
$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);
}
2019-02-28 17:27:24 +08:00
// 附件处理
$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;
}
}
2019-10-16 20:45:41 +08:00
// 移除多余的字段
unset($params['data']['pluginsname'], $params['data']['pluginscontrol'], $params['data']['pluginsaction']);
2019-02-09 09:15:23 +08:00
// 数据更新
if(Db::name('Plugins')->where(['plugins'=>$params['plugins']])->update(['data'=>json_encode($params['data']), 'upd_time'=>time()]))
{
// 删除缓存
cache(config('shopxo.cache_plugins_data_key').$params['plugins'], null);
2019-02-09 09:15:23 +08:00
return DataReturn('操作成功');
}
return DataReturn('操作失败', -100);
}
2019-05-27 00:58:59 +08:00
/**
* 根据应用标记获取指定字段数据
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-29
* @desc description
* @param [string] $plugins [应用标记]
* @param [string] $field [字段名称]
2019-08-18 00:01:57 +08:00
* @return [mixed] [不存在返回null, 则原始数据]
2019-05-27 00:58:59 +08:00
*/
public static function PluginsField($plugins, $field)
{
$data = Db::name('Plugins')->where(['plugins'=>$plugins])->value($field);
return DataReturn('操作成功', 0, $data);
}
2019-10-17 14:06:52 +08:00
/**
* 应用状态
* @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'];
}
2019-02-09 09:15:23 +08:00
}
?>