vr-shopxo-source/app/service/ResourcesService.php

800 lines
27 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://opensource.org/licenses/mit-license.php )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;
use think\facade\Db;
use app\service\UserService;
use app\service\SystemBaseService;
/**
* 资源服务层
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-01T21:51:08+0800
*/
class ResourcesService
{
/**
* 编辑器中内容的静态资源替换
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2017-01-22T16:07:58+0800
* @param [string] $content [在这个字符串中查找进行替换]
* @param [string] $type [操作类型[get读取额你让, add写入内容](编辑/展示传入get,数据写入数据库传入add)]
* @return [string] [正确返回替换后的内容, 则返回原内容]
*/
public static function ContentStaticReplace($content, $type = 'get')
{
// 配置文件附件url地址
$attachment_host = SystemBaseService::AttachmentHost();
if(empty($attachment_host))
{
$attachment_host = substr(__MY_PUBLIC_URL__, 0, -1);
}
$attachment_host_path = $attachment_host.'/static/';
// 根据类型处理附件地址
switch($type)
{
// 读取内容
case 'get':
return str_replace('src="/static/', 'src="'.$attachment_host_path, $content);
break;
// 内容写入
case 'add':
$search = [
'src="'.__MY_PUBLIC_URL__.'static/',
'src="'.__MY_ROOT_PUBLIC__.'static/',
'src="'.$attachment_host_path,
];
return str_replace($search, 'src="/static/', $content);
}
return $content;
}
/**
* 附件路径处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-12-12
* @desc description
* @param [string|array] $value [附件路径地址]
* @param [string] $field [url字段名称]
*/
public static function AttachmentPathHandle($value, $field = 'url')
{
// 配置文件附件url地址
$attachment_host = SystemBaseService::AttachmentHost();
$attachment_host_path = empty($attachment_host) ? __MY_PUBLIC_URL__ : $attachment_host.DS;
// 替换处理
$search = [
$attachment_host_path,
__MY_PUBLIC_URL__,
__MY_ROOT_PUBLIC__,
];
// 是否数组
if(!empty($value))
{
if(is_array($value))
{
foreach($value as &$v)
{
// 是否二级
if(isset($v[$field]))
{
$v[$field] = empty($v[$field]) ? '' : str_replace($search, DS, $v[$field]);
} else {
$v = empty($v) ? '' : str_replace($search, DS, $v);
}
}
} else {
$value = empty($value) ? '' : str_replace($search, DS, $value);
}
}
return $value;
}
/**
* 附件集合处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-08-07
* @desc description
* @param [array] $params [输入参数]
* @param [array] $data [字段列表]
*/
public static function AttachmentParams($params, $data)
{
$result = [];
if(!empty($data))
{
foreach($data as $field)
{
$result[$field] = isset($params[$field]) ? self::AttachmentPathHandle($params[$field]) : '';
}
}
return DataReturn('success', 0, $result);
}
/**
* 附件展示地址处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-01-13T15:13:30+0800
* @param [string|array] $value [附件地址]
* @param [string] $field [url字段名称]
*/
public static function AttachmentPathViewHandle($value, $field = 'url')
{
if(!empty($value))
{
// 附件地址
$host = SystemBaseService::AttachmentHost();
// 是否数组
if(is_array($value))
{
foreach($value as &$v)
{
// 是否二级
if(isset($v[$field]))
{
if(substr($v[$field], 0, 4) != 'http')
{
$v[$field] = $host.$v[$field];
}
} else {
if(substr($v, 0, 4) != 'http')
{
$v = $host.$v;
}
}
}
} else {
if(substr($value, 0, 4) != 'http')
{
$value = $host.$value;
}
}
}
return $value;
}
/**
* 相对路径文件新增
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-04-16
* @desc description
* @param [string] $value [相对路径文件 /static 开头]
* @param [string] $path_type [文件存储路径]
*/
public static function AttachmentPathAdd($value, $path_type)
{
// 文件是否存在
$file = ROOT.'public'.$value;
if(!file_exists($file))
{
return DataReturn('文件不存在', -1);
}
// 配置信息
$config = MyConfig('ueditor');
// 文件信息
$info = pathinfo($file);
$title = empty($info['basename']) ? substr(strrchr($file, '/'), 1) : $info['basename'];
$ext = strtolower(strrchr($file, '.'));
$type = in_array($ext, $config['imageAllowFiles']) ? 'image' : (in_array($ext, $config['videoAllowFiles']) ? 'video' : 'file');
// 添加文件
$data = [
"url" => $value,
"path" => $file,
"title" => $title,
"original" => $title,
"ext" => $ext,
"size" => filesize($file),
'type' => $type,
"hash" => hash_file('sha256', $file, false),
'path_type' => $path_type,
];
return self::AttachmentAdd($data);
}
/**
* 附件添加
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-06-25T00:13:33+0800
* @param [array] $params [输入参数]
*/
public static function AttachmentAdd($params = [])
{
// 请求参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'title',
'error_msg' => MyLang('common_service.resources.save_attachment_title_tips'),
],
[
'checked_type' => 'empty',
'key_name' => 'original',
'error_msg' => MyLang('common_service.resources.save_attachment_original_tips'),
],
[
'checked_type' => 'empty',
'key_name' => 'path_type',
'error_msg' => MyLang('common_service.resources.save_attachment_path_type_tips'),
],
[
'checked_type' => 'empty',
'key_name' => 'url',
'error_msg' => MyLang('common_service.resources.save_attachment_url_tips'),
],
[
'checked_type' => 'isset',
'key_name' => 'size',
'error_msg' => MyLang('common_service.resources.save_attachment_size_tips'),
],
[
'checked_type' => 'isset',
'key_name' => 'ext',
'error_msg' => MyLang('common_service.resources.save_attachment_ext_tips'),
],
[
'checked_type' => 'empty',
'key_name' => 'hash',
'error_msg' => MyLang('common_service.resources.save_attachment_hash_tips'),
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 数据组装
$data = [
'path_type' => $params['path_type'],
'original' => empty($params['original']) ? '' : mb_substr($params['original'], -160, null, 'utf-8'),
'title' => $params['title'],
'size' => $params['size'],
'ext' => $params['ext'],
'type' => isset($params['type']) ? $params['type'] : 'file',
'hash' => $params['hash'],
'url' => self::AttachmentPathHandle($params['url']),
'add_time' => time(),
];
// 附件上传前处理钩子
$hook_name = 'plugins_service_attachment_handle_begin';
MyEventTrigger($hook_name, [
'hook_name' => $hook_name,
'is_backend' => true,
'params' => $params,
'data' => &$data,
]);
// 添加到数据库
$attachment_id = Db::name('Attachment')->insertGetId($data);
if($attachment_id > 0)
{
// 附件上传后处理钩子
$hook_name = 'plugins_service_attachment_handle_end';
MyEventTrigger($hook_name, [
'hook_name' => $hook_name,
'is_backend' => true,
'params' => &$params,
'data' => &$data,
'attachment_id' => $attachment_id,
]);
$params['id'] = $attachment_id;
$params['url'] = self::AttachmentPathViewHandle($data['url']);
$params['add_time'] = date('Y-m-d H:i:s', $data['add_time']);
return DataReturn(MyLang('insert_success'), 0, $params);
}
// 删除本地图片
if(!empty($params['path']))
{
\base\FileUtil::UnlinkFile($params['path']);
}
return DataReturn(MyLang('insert_fail'), -100);
}
/**
* 获取附件总数
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-06-25T22:44:52+0800
* @param [array] $where [条件]
*/
public static function AttachmentTotal($where)
{
return (int) Db::name('Attachment')->where($where)->count();
}
/**
* 获取附件列表
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-06-25T22:44:52+0800
* @param [array] $params [参数]
*/
public static function AttachmentList($params = [])
{
$m = max(0, isset($params['m']) ? intval($params['m']) : 0);
$n = max(1, isset($params['n']) ? intval($params['n']) : 20);
$data = Db::name('Attachment')->where($params['where'])->order('id desc')->limit($m, $n)->select()->toArray();
if(!empty($data))
{
foreach($data as &$v)
{
// 附件列表处理前钩子
$hook_name = 'plugins_service_attachment_list_handle_begin';
$ret = EventReturnHandle(MyEventTrigger($hook_name, [
'hook_name' => $hook_name,
'is_backend' => true,
'data' => &$v,
]));
if(isset($ret['code']) && $ret[</