Files
shopxo/app/service/ResourcesService.php

1147 lines
39 KiB
PHP
Raw Normal View History

2018-12-28 18:58:37 +08:00
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
2018-12-28 18:58:37 +08:00
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Licensed ( https://opensource.org/licenses/mit-license.php )
2018-12-28 18:58:37 +08:00
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;
2021-07-18 23:42:10 +08:00
use think\facade\Db;
2021-04-15 10:29:07 +08:00
use app\service\UserService;
2021-07-06 23:57:03 +08:00
use app\service\SystemBaseService;
2018-12-28 18:58:37 +08:00
/**
* 资源服务层
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-01T21:51:08+0800
*/
class ResourcesService
{
/**
2020-12-05 20:35:13 +08:00
* 编辑器中内容的静态资源替换
2018-12-28 18:58:37 +08:00
* @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')
2018-12-28 18:58:37 +08:00
{
2023-08-27 16:59:15 +08:00
// 仅处理字符串和数字类型
if(is_string($content) || is_int($content))
{
2023-08-27 16:59:15 +08:00
// 配置文件附件url地址
$attachment_host = SystemBaseService::AttachmentHost();
if(empty($attachment_host))
{
$attachment_host = substr(__MY_PUBLIC_URL__, 0, -1);
}
$attachment_host_path = $attachment_host.'/static/';
2023-08-27 16:59:15 +08:00
// 根据类型处理附件地址
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);
}
2018-12-28 18:58:37 +08:00
}
return $content;
}
/**
* 附件路径处理
* @author Devil
2018-12-28 18:58:37 +08:00
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-12-12
* @desc description
* @param [string|array] $value [附件路径地址]
* @param [string] $field [url字段名称]
2018-12-28 18:58:37 +08:00
*/
public static function AttachmentPathHandle($value, $field = 'url')
2018-12-28 18:58:37 +08:00
{
// 配置文件附件url地址
2021-07-06 23:57:03 +08:00
$attachment_host = SystemBaseService::AttachmentHost();
$attachment_host_path = empty($attachment_host) ? __MY_PUBLIC_URL__ : $attachment_host.DS;
// 替换处理
$search = [
2022-11-09 21:49:46 +08:00
$attachment_host_path,
__MY_PUBLIC_URL__,
__MY_ROOT_PUBLIC__,
];
2022-10-13 15:25:09 +08:00
// 是否数组
if(!empty($value))
{
if(is_array($value))
{
foreach($value as &$v)
{
// 是否二级
if(isset($v[$field]))
2022-10-13 15:25:09 +08:00
{
$v[$field] = empty($v[$field]) ? '' : str_replace($search, DS, $v[$field]);
2022-10-13 15:25:09 +08:00
} else {
$v = empty($v) ? '' : str_replace($search, DS, $v);
}
}
} else {
$value = empty($value) ? '' : str_replace($search, DS, $value);
}
}
return $value;
2018-12-28 18:58:37 +08:00
}
/**
* 附件集合处理
* @author Devil
2018-12-28 18:58:37 +08:00
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-08-07
* @desc description
* @param [array] $params [输入参数]
2022-10-13 15:25:09 +08:00
* @param [array] $data [字段列表]
2018-12-28 18:58:37 +08:00
*/
public static function AttachmentParams($params, $data)
2018-12-28 18:58:37 +08:00
{
$result = [];
if(!empty($data))
{
foreach($data as $field)
{
$result[$field] = isset($params[$field]) ? self::AttachmentPathHandle($params[$field]) : '';
2018-12-28 18:58:37 +08:00
}
}
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))
{
2022-10-13 15:25:09 +08:00
// 是否数组
if(is_array($value))
{
2023-11-08 15:43:08 +08:00
$host = SystemBaseService::AttachmentHost();
2022-10-13 15:25:09 +08:00
foreach($value as &$v)
{
// 是否二级
if(isset($v[$field]))
2022-10-13 15:25:09 +08:00
{
if(substr($v[$field], 0, 4) != 'http')
2022-10-13 15:25:09 +08:00
{
$v[$field] = $host.$v[$field];
2022-10-13 15:25:09 +08:00
}
} else {
if(substr($v, 0, 4) != 'http')
{
$v = $host.$v;
}
}
}
} else {
if(substr($value, 0, 4) != 'http')
{
2023-11-08 15:43:08 +08:00
$value = SystemBaseService::AttachmentHost().$value;
2022-10-13 15:25:09 +08:00
}
}
}
2022-10-13 15:25:09 +08:00
return $value;
}
2019-06-25 01:10:37 +08:00
2020-04-19 21:52:10 +08:00
/**
* 相对路径文件新增
* @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);
}
// 配置信息
2021-08-02 15:53:05 +08:00
$config = MyConfig('ueditor');
2020-04-19 21:52:10 +08:00
// 文件信息
$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);
}
2019-06-25 01:10:37 +08:00
/**
* 附件添加
* @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 = [])
{
2019-06-26 00:13:47 +08:00
// 请求参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'title',
2023-02-11 17:01:04 +08:00
'error_msg' => MyLang('common_service.resources.save_attachment_title_tips'),
2019-06-26 00:13:47 +08:00
],
[
'checked_type' => 'empty',
'key_name' => 'original',
2023-02-11 17:01:04 +08:00
'error_msg' => MyLang('common_service.resources.save_attachment_original_tips'),
2019-06-26 00:13:47 +08:00
],
2019-06-26 16:47:15 +08:00
[
'checked_type' => 'empty',
'key_name' => 'path_type',
2023-02-11 17:01:04 +08:00
'error_msg' => MyLang('common_service.resources.save_attachment_path_type_tips'),
2019-06-26 16:47:15 +08:00
],
2019-06-26 00:13:47 +08:00
[
'checked_type' => 'empty',
'key_name' => 'url',
2023-02-11 17:01:04 +08:00
'error_msg' => MyLang('common_service.resources.save_attachment_url_tips'),
2019-06-26 00:13:47 +08:00
],
[
'checked_type' => 'isset',
'key_name' => 'size',
2023-02-11 17:01:04 +08:00
'error_msg' => MyLang('common_service.resources.save_attachment_size_tips'),
2019-06-26 00:13:47 +08:00
],
[
'checked_type' => 'isset',
'key_name' => 'ext',
2023-02-11 17:01:04 +08:00
'error_msg' => MyLang('common_service.resources.save_attachment_ext_tips'),
2019-06-26 00:13:47 +08:00
],
[
'checked_type' => 'empty',
'key_name' => 'hash',
2023-02-11 17:01:04 +08:00
'error_msg' => MyLang('common_service.resources.save_attachment_hash_tips'),
2019-06-26 00:13:47 +08:00
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
2019-06-25 01:10:37 +08:00
{
2019-06-26 00:13:47 +08:00
return DataReturn($ret, -1);
}
// 数据组装
$data = [
2019-06-26 16:47:15 +08:00
'path_type' => $params['path_type'],
2019-06-26 00:13:47 +08:00
'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';
2021-07-18 23:42:10 +08:00
MyEventTrigger($hook_name, [
2019-06-26 00:13:47 +08:00
'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';
2021-07-18 23:42:10 +08:00
MyEventTrigger($hook_name, [
2019-06-25 01:10:37 +08:00
'hook_name' => $hook_name,
'is_backend' => true,
2019-06-26 00:13:47 +08:00
'params' => &$params,
2019-06-25 01:10:37 +08:00
'data' => &$data,
2019-06-26 00:13:47 +08:00
'attachment_id' => $attachment_id,
2019-06-25 01:10:37 +08:00
]);
2020-03-04 18:48:09 +08:00
$params['id'] = $attachment_id;
2020-03-05 12:53:01 +08:00
$params['url'] = self::AttachmentPathViewHandle($data['url']);
2020-03-04 18:48:09 +08:00
$params['add_time'] = date('Y-m-d H:i:s', $data['add_time']);
2023-01-19 17:44:03 +08:00
return DataReturn(MyLang('insert_success'), 0, $params);
2019-06-26 00:13:47 +08:00
}
// 删除本地图片
if(!empty($params['path']))
{
\base\FileUtil::UnlinkFile($params['path']);
}
2023-01-19 17:44:03 +08:00
return DataReturn(MyLang('insert_fail'), -100);
2019-06-26 00:13:47 +08:00
}
/**
* 获取附件总数
* @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);
2021-07-18 23:42:10 +08:00
$data = Db::name('Attachment')->where($params['where'])->order('id desc')->limit($m, $n)->select()->toArray();
2019-06-26 00:13:47 +08:00
if(!empty($data))
{
foreach($data as &$v)
2019-06-25 01:10:37 +08:00
{
2019-06-26 00:13:47 +08:00
// 附件列表处理前钩子
$hook_name = 'plugins_service_attachment_list_handle_begin';
2021-07-18 23:42:10 +08:00
$ret = EventReturnHandle(MyEventTrigger($hook_name, [
2019-06-26 00:13:47 +08:00
'hook_name' => $hook_name,
'is_backend' => true,
'data' => &$v,
2019-12-02 20:58:24 +08:00
]));
2019-06-26 00:13:47 +08:00
if(isset($ret['code']) && $ret['code'] != 0)
{
return $ret;
}
// 数据处理
$v['url'] = self::AttachmentPathViewHandle($v['url']);
$v['add_time'] = date('Y-m-d H:i:s');
// 附件列表处理后钩子
$hook_name = 'plugins_service_attachment_list_handle_end';
2021-07-18 23:42:10 +08:00
$ret = EventReturnHandle(MyEventTrigger($hook_name, [
2019-06-25 01:10:37 +08:00
'hook_name' => $hook_name,
'is_backend' => true,
2019-06-26 00:13:47 +08:00
'data' => &$v,
2019-12-02 20:58:24 +08:00
]));
2019-06-26 00:13:47 +08:00
if(isset($ret['code']) && $ret['code'] != 0)
{
return $ret;
}
}
}
2020-03-04 18:48:09 +08:00
return DataReturn('success', 0, $data);
2019-06-26 00:13:47 +08:00
}
/**
* 附件删除
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-06-25T23:35:27+0800
* @param [array] $params [输入参数]
*/
public static function AttachmentDelete($params = [])
{
// 请求参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'id',
2023-02-04 16:47:48 +08:00
'error_msg' => MyLang('data_id_error_tips'),
2019-06-26 00:13:47 +08:00
]
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 获取数据
$data = Db::name('Attachment')->find(intval($params['id']));
if(empty($data))
{
2023-02-04 16:47:48 +08:00
return DataReturn(MyLang('data_no_exist_or_delete_error_tips'), -1);
2019-06-26 00:13:47 +08:00
}
// 删除文件
$path = substr(ROOT_PATH, 0, -1).$data['url'];
if(file_exists($path))
{
if(is_writable($path))
{
if(DB::name('Attachment')->where(['id'=>$data['id']])->delete())
{
// 删除附件
\base\FileUtil::UnlinkFile($path);
2019-06-25 01:10:37 +08:00
2023-01-19 17:44:03 +08:00
$ret = DataReturn(MyLang('delete_success'), 0);
2019-06-26 00:13:47 +08:00
} else {
2023-01-19 17:44:03 +08:00
$ret = DataReturn(MyLang('delete_fail'), -100);
2019-06-26 00:13:47 +08:00
}
} else {
2023-02-11 17:01:04 +08:00
$ret = DataReturn(MyLang('common_service.resources.delete_no_power_tips'), -1);
2019-06-25 01:10:37 +08:00
}
2019-06-26 00:13:47 +08:00
} else {
2019-06-26 00:18:28 +08:00
if(DB::name('Attachment')->where(['id'=>$data['id']])->delete())
{
2023-01-19 17:44:03 +08:00
$ret = DataReturn(MyLang('delete_success'), 0);
2019-06-26 00:18:28 +08:00
} else {
2023-01-19 17:44:03 +08:00
$ret = DataReturn(MyLang('delete_fail'), -100);
2019-06-26 00:18:28 +08:00
}
}
// 处理
if($ret['code'] == 0)
{
// 附件删除成功后处理钩子
$hook_name = 'plugins_service_attachment_delete_success';
2021-07-18 23:42:10 +08:00
MyEventTrigger($hook_name, [
2019-06-26 00:18:28 +08:00
'hook_name' => $hook_name,
'is_backend' => true,
'data' => $data,
]);
2019-06-25 01:10:37 +08:00
}
2019-06-26 00:18:28 +08:00
return $ret;
2019-06-25 01:10:37 +08:00
}
2019-06-26 16:47:15 +08:00
/**
* 附件根据标记删除
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-06-25T23:35:27+0800
* @param [string] $path_type [唯一标记]
*/
public static function AttachmentPathTypeDelete($path_type)
{
// 获取附件数据
$where = ['path_type'=>$path_type];
2021-07-18 23:42:10 +08:00
$data = DB::name('Attachment')->where($where)->select()->toArray();
if(!empty($data))
2019-06-26 16:47:15 +08:00
{
// 删除数据库数据
if(!DB::name('Attachment')->where($where)->delete())
{
2023-01-19 17:44:03 +08:00
return DataReturn(MyLang('delete_fail'), -1);
}
// 删除磁盘文件
$path = substr(ROOT_PATH, 0, -1);
foreach($data as $v)
{
2021-06-23 19:35:01 +08:00
$file = $path.$v['url'];
if(file_exists($file) && is_writable($file))
{
\base\FileUtil::UnlinkFile($file);
}
}
2019-06-26 16:47:15 +08:00
}
2021-06-23 19:35:01 +08:00
// 附件删除成功后处理钩子
$hook_name = 'plugins_service_attachment_path_type_delete_success';
2021-07-18 23:42:10 +08:00
MyEventTrigger($hook_name, [
2021-06-23 19:35:01 +08:00
'hook_name' => $hook_name,
'is_backend' => true,
'data' => $data,
]);
2023-01-19 17:44:03 +08:00
return DataReturn(MyLang('delete_success'), 0);
2019-06-26 16:47:15 +08:00
}
2019-08-02 21:35:25 +08:00
/**
* 附件根据地址删除
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-06-25T23:35:27+0800
* @param [string|array] $url [图片url地址]
*/
public static function AttachmentUrlDelete($url)
{
// url格式处理
if(!is_array($url))
{
$url = explode(',', $url);
}
foreach($url as &$v)
{
$v = self::AttachmentPathHandle($v);
}
// 获取附件数据
$data = DB::name('Attachment')->where(['url'=>$url])->select()->toArray();
if(!empty($data))
{
// 删除数据库数据
if(!DB::name('Attachment')->where(['id'=>array_column($data, 'id')])->delete())
{
return DataReturn(MyLang('delete_fail'), -1);
}
// 删除磁盘文件
$path = substr(ROOT_PATH, 0, -1);
foreach($data as $v)
{
$file = $path.$v['url'];
if(file_exists($file) && is_writable($file))
{
\base\FileUtil::UnlinkFile($file);
}
}
}
// 附件删除成功后处理钩子
$hook_name = 'plugins_service_attachment_url_delete_success';
MyEventTrigger($hook_name, [
'hook_name' => $hook_name,
'is_backend' => true,
'data' => $data,
]);
return DataReturn(MyLang('delete_success'), 0);
}
2019-08-02 21:35:25 +08:00
/**
* 磁盘附加同步到数据库
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-08-02
* @desc description
* @param [string] $dir_path [附件路径类型]
* @param [string] $path_type [附件路径值类型]
2019-08-02 21:35:25 +08:00
*/
public static function AttachmentDiskFilesToDb($dir_path, $path_type = '')
2019-08-02 21:35:25 +08:00
{
// 未指定类型值则使用路径值
if(empty($path_type))
{
$path_type = $dir_path;
}
2019-08-02 21:35:25 +08:00
// 处理状态总数
$count = 0;
$success = 0;
$error = 0;
// 视频/文件/图片
$path_all = [
'video' => __MY_ROOT_PUBLIC__.'static/upload/video/'.$dir_path.'/',
'file' => __MY_ROOT_PUBLIC__.'static/upload/file/'.$dir_path.'/',
'image' => __MY_ROOT_PUBLIC__.'static/upload/images/'.$dir_path.'/',
2019-08-02 21:35:25 +08:00
];
foreach($path_all as $type=>$path)
{
$path = GetDocumentRoot() . (substr($path, 0, 1) == "/" ? "":"/") . $path;
$files =self::AttachmentDiskFilesList($path, $type, $path_type);
if(!empty($files))
{
$count += count($files);
foreach($files as $v)
{
2021-10-30 22:47:49 +08:00
$temp = Db::name('Attachment')->where(['title'=>$v['title'], 'hash'=>$v['hash'], 'path_type'=>$path_type])->find();
2019-08-02 21:35:25 +08:00
if(empty($temp))
{
$ret = self::AttachmentAdd($v);
if($ret['code'] == 0)
{
$success++;
} else {
$error++;
}
} else {
$success++;
}
}
}
}
2023-03-01 11:20:47 +08:00
return DataReturn(MyLang('common_service.resources.sync_file_to_db_tips', ['count'=>$count, 'success'=>$success, 'error'=>$error]), 0);
2019-08-02 21:35:25 +08:00
}
/**
* 遍历获取目录下的指定类型的文件
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2017-01-17T23:24:59+0800
* @param [string] $path [路径地址]
* @param [string] $type [允许的文件]
* @param [string] $path_type [路径类型]
* @param [array] &$files [数据]
* @return [array] [数据]
*/
public static function AttachmentDiskFilesList($path, $type, $path_type, &$files = [])
{
if(!is_dir($path)) return null;
if(substr($path, strlen($path) - 1) != '/') $path .= '/';
$handle = opendir($path);
$document_root = GetDocumentRoot();
while(false !== ($file = readdir($handle)))
{
if($file != 'index.html' && $file != '.' && $file != '..' && substr($file, 0, 1) != '.')
{
$temp_path = $path . $file;
if(is_dir($temp_path))
{
self::AttachmentDiskFilesList($temp_path, $type, $path_type, $files);
} else {
$url = self::AttachmentPathHandle(substr($temp_path, strlen($document_root)));
$title = substr($url, strripos($url, '/')+1);
$root_path = ROOT.'public'.$url;
$files[] = array(
'url' => $url,
'original' => $title,
'title' => $title,
'type' => $type,
'path_type' => $path_type,
'size' => file_exists($root_path) ? filesize($root_path) : 0,
'hash' => file_exists($root_path) ? hash_file('sha256', $root_path, false) : '',
'ext' => substr($title, strripos($title, '.')),
);
}
}
}
return $files;
}
2020-01-06 01:32:36 +08:00
/**
* 小程序富文本标签处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-01-04
* @desc description
* @param [string] $content [需要处理的富文本内容]
*/
public static function ApMiniRichTextContentHandle($content)
{
// 标签处理兼容小程序rich-text
$search = [
'<img ',
'<section',
'/section>',
'<p style="',
'<p>',
'<div>',
'<table',
'<tr',
'<td',
];
$replace = [
'<img style="max-width:100%;height:auto;margin:0;padding:0;display:block;" ',
2020-01-06 01:32:36 +08:00
'<div',
'/div>',
'<p style="margin:0;',
'<p style="margin:0;">',
'<div style="margin:0;">',
'<table style="width:100%;margin:0px;border-collapse:collapse;border-color:#ddd;border-style:solid;border-width:0 1px 1px 0;"',
'<tr style="border-top:1px solid #ddd;"',
'<td style="margin:0;padding:5px;border-left:1px solid #ddd;"',
];
return str_replace($search, $replace, $content);
}
2020-06-16 23:27:12 +08:00
/**
* 正则匹配富文本图片
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-06-16
* @desc description
2020-08-30 00:41:07 +08:00
* @param [string] $content [内容]
* @param [string] $business [业务模块名称]
* @param [string] $type [附件类型images 图片, file 文件, video 视频)]
2020-06-16 23:27:12 +08:00
*/
2020-08-30 00:41:07 +08:00
public static function RichTextMatchContentAttachment($content, $business, $type = 'images')
2020-06-16 23:27:12 +08:00
{
if(!empty($content))
{
2020-08-30 00:41:07 +08:00
$pattern = '/<img.*?src=[\'|\"](\/static\/upload\/'.$type.'\/'.$business.'\/.*?[\.png|\.jpg|\.jpeg|\.gif|\.bmp|\.flv|\.swf|\.mkv|\.avi|\.rm|\.rmvb|\.mpeg|\.mpg|\.ogg|\.ogv|\.mov|\.wmv|\.mp4|\.webm|\.mp3|\.wav|\.mid|\.rar|\.zip|\.tar|\.gz|\.7z|\.bz2|\.cab|\.iso|\.doc|\.docx|\.xls|\.xlsx|\.ppt|\.pptx|\.pdf|\.txt|\.md|\.xml])[\'|\"].*?[\/]?>/';
2020-06-16 23:27:12 +08:00
preg_match_all($pattern, self::AttachmentPathHandle($content), $match);
return empty($match[1]) ? [] : $match[1];
}
return [];
}
2020-09-10 18:12:50 +08:00
/**
2020-09-17 22:22:37 +08:00
* 货币信息
2020-09-10 18:12:50 +08:00
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-09-10
* @desc description
*/
2020-09-17 22:22:37 +08:00
public static function CurrencyData()
2020-09-10 18:12:50 +08:00
{
2020-09-17 22:22:37 +08:00
// 默认从配置文件读取货币信息
$data = [
2021-07-18 23:42:10 +08:00
'currency_symbol' => MyConfig('shopxo.currency_symbol'),
'currency_code' => MyConfig('shopxo.currency_code'),
'currency_rate' => MyConfig('shopxo.currency_rate'),
'currency_name' => MyConfig('shopxo.currency_name'),
2020-09-17 22:22:37 +08:00
];
2020-09-10 18:12:50 +08:00
2021-01-27 22:21:04 +08:00
// 钩子
2020-09-17 22:22:37 +08:00
$hook_name = 'plugins_service_currency_data';
2021-07-18 23:42:10 +08:00
MyEventTrigger($hook_name, [
2020-09-10 18:12:50 +08:00
'hook_name' => $hook_name,
'is_backend' => true,
'data' => &$data,
]);
return $data;
}
2020-09-17 22:22:37 +08:00
/**
* 货币信息-符号
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-09-10
* @desc description
*/
public static function CurrencyDataSymbol()
{
$res = self::CurrencyData();
2021-07-18 23:42:10 +08:00
return empty($res['currency_symbol']) ? MyConfig('shopxo.currency_symbol') : $res['currency_symbol'];
2020-09-17 22:22:37 +08:00
}
2021-01-27 22:21:04 +08:00
/**
* 编辑器文件存放地址
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-01-27
* @desc description
* @param [string] $value [位置路径名称([ - ]作为目录分隔符)]
*/
public static function EditorPathTypeValue($value)
{
// 当前操作名称, 兼容插件模块名称
2021-07-18 23:42:10 +08:00
$module_name = RequestModule();
$controller_name = RequestController();
$action_name = RequestAction();
2021-01-27 22:21:04 +08:00
// 钩子
$hook_name = 'plugins_service_editor_path_type_'.$module_name.'_'.$controller_name.'_'.$action_name;
2021-07-18 23:42:10 +08:00
MyEventTrigger($hook_name, [
2021-01-27 22:21:04 +08:00
'hook_name' => $hook_name,
'is_backend' => true,
'value' => &$value,
]);
return $value;
}
2021-02-15 22:45:33 +08:00
/**
* zip压缩包扩展可用格式
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-02-02
* @desc description
* @param array $params [description]
*/
public static function ZipExtTypeList($params = [])
{
return [
'application/zip',
'application/octet-stream',
'application/x-zip-compressed',
];
}
2021-04-15 10:29:07 +08:00
/**
* 获取用户唯一id
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-04-15
* @desc 未登录取[uuid]前端传过来的uuid、已登录取[用户id]、都没有则返回空字符串
*/
public static function UserUniqueId()
{
// 取参数uuid、默认空
$uid = input('uuid', '');
2023-08-27 16:59:15 +08:00
// 取当当前session
2021-10-30 22:47:49 +08:00
if(empty($uid))
{
$uid = MySession('uuid');
}
2023-08-27 16:59:15 +08:00
// 取当当前cookie
if(empty($uid))
{
$uid = MyCookie('uuid');
}
2021-10-30 22:47:49 +08:00
2021-04-15 10:29:07 +08:00
// 用户信息
$user = UserService::LoginUserInfo();
if(!empty($user) && !empty($user['id']))
{
$uid = $user['id'];
}
return empty($uid) ? '' : md5($uid);
}
/**
* 获取表结构
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2022-11-12
* @desc description
* @param [string] $table [表名称、可以是大写字母会自动转为小写前面加下划线分隔]
*/
public static function TableStructureData($table)
{
// 表名处理及sql
$table_name = MyConfig('database.connections.mysql.prefix').strtolower(preg_replace('/\B([A-Z])/', '_$1', $table));
2023-08-27 16:59:15 +08:00
$sql = "SELECT COLUMN_NAME AS field,COLUMN_COMMENT AS name FROM INFORMATION_SCHEMA.Columns WHERE `table_name`='".$table_name."'";
// 从缓存获取
$key = SystemService::CacheKey('shopxo.cache_table_structure_key').'_'.md5($sql);
$data = MyCache($key);
if($data === null || MyEnv('app_debug'))
{
// 查询表结构
$res = Db::query($sql);
$data = empty($res) ? [] : array_column($res, 'name', 'field');
// 存储缓存
MyCache($key, $data, 180);
}
return $data;
}
2024-04-15 10:25:01 +08:00
/**
* 页面静态资源地址信息
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2024-01-29
* @desc description
* @param [string] $theme [当前主题标识]
* @param [string] $group [当前模块]
* @param [string] $controller [控制器]
* @param [string] $action [方法]
*/
public static function StaticCssOrJsPathData($theme, $group, $controller, $action)
{
// 公共css,js
$common_css = $group.DS.$theme.DS.'css'.DS.'common.css';
if(!file_exists(ROOT_PATH.'static'.DS.$common_css))
{
$default_common_css = $group.DS.'default'.DS.'css'.DS.'common.css';
if(file_exists(ROOT_PATH.'static'.DS.$default_common_css))
{
$common_css = $default_common_css;
} else {
$common_css = '';
}
}
$common_js = $group.DS.$theme.DS.'js'.DS.'common.js';
if(!file_exists(ROOT_PATH.'static'.DS.$common_js))
{
$default_common_js = $group.DS.'default'.DS.'js'.DS.'common.js';
if(file_exists(ROOT_PATH.'static'.DS.$default_common_js))
{
$common_js = $default_common_js;
} else {
$common_js = '';
}
}
// 主题指定引入css,js
$theme_import_css = [];
$theme_import_js = [];
$config = APP_PATH.$group.DS.'view'.DS.$theme.DS.'config.json';
if(file_exists($config))
{
$theme_config = json_decode(file_get_contents($config), true);
if(!empty($theme_config['import_css']))
{
if(is_array($theme_config['import_css']))
{
foreach($theme_config['import_css'] as $v)
{
if(!empty($v) && !is_array($v))
{
// 前面是否增加了斜杠、则去除
if(substr($v, 0, 1) == DS)
{
$v = substr($v, 1);
}
if(file_exists(ROOT_PATH.'static'.DS.$v))
{
$theme_import_css[] = $v;
}
}
}
} else {
// 前面是否增加了斜杠、则去除
if(substr($theme_config['import_css'], 0, 1) == DS)
{
$theme_config['import_css'] = substr($theme_config['import_css'], 1);
}
if(file_exists(ROOT_PATH.'static'.DS.$theme_config['import_css']))
{
$theme_import_css[] = $theme_config['import_css'];
}
}
}
if(!empty($theme_config['import_js']))
{
if(is_array($theme_config['import_js']))
{
foreach($theme_config['import_js'] as $v)
{
if(!empty($v) && !is_array($v))
{
// 前面是否增加了斜杠、则去除
if(substr($v, 0, 1) == DS)
{
$v = substr($v, 1);
}
if(file_exists(ROOT_PATH.'static'.DS.$v))
{
$theme_import_js[] = $v;
}
}
}
} else {
// 前面是否增加了斜杠、则去除
if(substr($theme_config['import_js'], 0, 1) == DS)
{
$theme_config['import_js'] = substr($theme_config['import_js'], 1);
}
if(file_exists(ROOT_PATH.'static'.DS.$theme_config['import_js']))
{
$theme_import_js[] = $theme_config['import_js'];
}
}
}
}
// 主题专属css,js
$other_css = $group.DS.$theme.DS.'css'.DS.'other.css';
if(!file_exists(ROOT_PATH.'static'.DS.$other_css))
{
$default_other_css = $group.DS.'default'.DS.'css'.DS.'other.css';
if(file_exists(ROOT_PATH.'static'.DS.$default_other_css))
{
$other_css = $default_other_css;
} else {
$other_css = '';
}
}
$other_js = $group.DS.$theme.DS.'js'.DS.'other.js';
if(!file_exists(ROOT_PATH.'static'.DS.$other_js))
{
$default_other_js = $group.DS.'default'.DS.'js'.DS.'other.js';
if(file_exists(ROOT_PATH.'static'.DS.$default_other_js))
{
$other_js = $default_other_js;
} else {
$other_js = '';
}
}
// 公共模块css,js
$module_css = $group.DS.$theme.DS.'css'.DS.'module.css';
if(!file_exists(ROOT_PATH.'static'.DS.$module_css))
{
$default_module_css = $group.DS.'default'.DS.'css'.DS.'module.css';
if(file_exists(ROOT_PATH.'static'.DS.$default_module_css))
{
$module_css = $default_module_css;
} else {
$module_css = '';
}
}
$module_js = $group.DS.$theme.DS.'js'.DS.'module.js';
if(!file_exists(ROOT_PATH.'static'.DS.$module_js))
{
$default_module_js = $group.DS.'default'.DS.'js'.DS.'module.js';
if(file_exists(ROOT_PATH.'static'.DS.$default_module_js))
{
$module_js = $default_module_js;
} else {
$module_js = '';
}
}
// 控制器静态文件状态css,js
// 页面css
$page_css = '';
$css = $group.DS.$theme.DS.'css'.DS.$controller;
// 对应方法不存在 或 非默认主题则走默认主题的文件
if(file_exists(ROOT_PATH.'static'.DS.$css.'.'.$action.'.css') && $theme != 'default')
{
$page_css = $css.'.'.$action.'.css';
} else {
$default_css = $group.DS.'default'.DS.'css'.DS.$controller;
if(file_exists(ROOT_PATH.'static'.DS.$default_css.'.'.$action.'.css'))
{
$page_css = $default_css.'.'.$action.'.css';
}
}
if(empty($page_css))
{
$page_css = $css.'.css';
if(!file_exists(ROOT_PATH.'static'.DS.$page_css))
{
// 不存在则赋空
$page_css = '';
// 非默认主题则走默认主题的文件
if($theme != 'default')
{
$default_css = $group.DS.'default'.DS.'css'.DS.$controller.'.css';
if(file_exists(ROOT_PATH.'static'.DS.$default_css))
{
$page_css = $default_css;
}
}
}
}
// 页面js
$page_js = '';
$js = $group.DS.$theme.DS.'js'.DS.$controller;
// 对应方法不存在 或 非默认主题则走默认主题的文件
if(file_exists(ROOT_PATH.'static'.DS.$js.'.'.$action.'.js') && $theme != 'default')
{
$page_js = $js.'.'.$action.'.js';
} else {
$default_js = $group.DS.'default'.DS.'js'.DS.$controller;
if(file_exists(ROOT_PATH.'static'.DS.$default_js.'.'.$action.'.js'))
{
$page_js = $default_js.'.'.$action.'.js';
}
}
if(empty($page_js))
{
$page_js = $js.'.js';
if(!file_exists(ROOT_PATH.'static'.DS.$page_js))
{
// 不存在则赋空
$page_js = '';
// 非默认主题则走默认主题的文件
if($theme != 'default')
{
$default_js = $group.DS.'default'.DS.'js'.DS.$controller.'.js';
if(file_exists(ROOT_PATH.'static'.DS.$default_js))
{
$page_js = $default_js;
}
}
}
}
// 是否插件
$plugins_css = '';
$plugins_js = '';
$control = RequestController();
$plugins_name = PluginsRequestName();
if($control == 'plugins' && !empty($plugins_name))
{
$plugins_control = PluginsRequestController();
$plugins_action = PluginsRequestAction();
// 页面css
$pcss = $control.DS.'css'.DS.$plugins_name.DS.$group.DS.$plugins_control;
$pcss .= file_exists(ROOT_PATH.'static'.DS.$pcss.'.'.$plugins_action.'.css') ? '.'.$plugins_action.'.css' : '.css';
$page_css = file_exists(ROOT_PATH.'static'.DS.$pcss) ? $pcss : '';
// 页面js
$pjs = $control.DS.'js'.DS.$plugins_name.DS.$group.DS.$plugins_control;
$pjs .= file_exists(ROOT_PATH.'static'.DS.$pjs.'.'.$plugins_action.'.js') ? '.'.$plugins_action.'.js' : '.js';
$page_js = file_exists(ROOT_PATH.'static'.DS.$pjs) ? $pjs : '';
// 应用公共css,js
$plugins_css = $control.DS.'css'.DS.$plugins_name.DS.$group.DS.'common.css';
$plugins_css = file_exists(ROOT_PATH.'static'.DS.$plugins_css) ? $plugins_css : '';
$plugins_js = $control.DS.'js'.DS.$plugins_name.DS.$group.DS.'common.js';
$plugins_js = file_exists(ROOT_PATH.'static'.DS.$plugins_js) ? $plugins_js : '';
}
return [
// 公共
'common_css' => $common_css,
'common_js' => $common_js,
// 主题指定
'theme_import_css' => $theme_import_css,
'theme_import_js' => $theme_import_js,
// 主题专属
'other_css' => $other_css,
'other_js' => $other_js,
// 公共模块
'module_css' => $module_css,
'module_js' => $module_js,
// 当前页面
'page_css' => $page_css,
'page_js' => $page_js,
// 插件
'plugins_css' => $plugins_css,
'plugins_js' => $plugins_js,
];
}
2018-12-28 18:58:37 +08:00
}
?>