@ -0,0 +1,118 @@ |
|||
<?php |
|||
// +---------------------------------------------------------------------- |
|||
// | Niucloud-admin 企业快速开发的多应用管理平台 |
|||
// +---------------------------------------------------------------------- |
|||
// | 官方网址:https://www.niucloud.com |
|||
// +---------------------------------------------------------------------- |
|||
// | niucloud团队 版权所有 开源版本可自由商用 |
|||
// +---------------------------------------------------------------------- |
|||
// | Author: Niucloud Team |
|||
// +---------------------------------------------------------------------- |
|||
|
|||
namespace app\api\controller\apiController; |
|||
|
|||
use app\Request; |
|||
use core\base\BaseApiService; |
|||
|
|||
/** |
|||
* 静态资源访问接口 |
|||
* Class StaticResource |
|||
* @package app\api\controller\apiController |
|||
*/ |
|||
class StaticResource extends BaseApiService |
|||
{ |
|||
|
|||
/** |
|||
* 获取静态资源URL |
|||
*/ |
|||
public function getResourceUrl(Request $request) |
|||
{ |
|||
$resource_path = $request->param('path', ''); // 资源路径,如:icon-img/home.png |
|||
|
|||
if (empty($resource_path)) { |
|||
return fail('资源路径不能为空'); |
|||
} |
|||
|
|||
// 基础URL配置 - 从配置文件或环境变量读取 |
|||
$baseUrl = $request->domain() ?: 'http://localhost:20080'; |
|||
|
|||
// 构建完整的静态资源URL |
|||
$resourceUrl = $baseUrl . '/static/resource/uniapp/' . $resource_path; |
|||
|
|||
return success([ |
|||
'resource_url' => $resourceUrl, |
|||
'base_url' => $baseUrl . '/static/resource/uniapp/', |
|||
'path' => $resource_path |
|||
]); |
|||
} |
|||
|
|||
/** |
|||
* 批量获取静态资源URL |
|||
*/ |
|||
public function getBatchResourceUrls(Request $request) |
|||
{ |
|||
$paths = $request->param('paths', []); // 资源路径数组 |
|||
|
|||
if (empty($paths) || !is_array($paths)) { |
|||
return fail('资源路径数组不能为空'); |
|||
} |
|||
|
|||
// 基础URL配置 |
|||
$baseUrl = $request->domain() ?: 'http://localhost:20080'; |
|||
$baseResourceUrl = $baseUrl . '/static/resource/uniapp/'; |
|||
|
|||
$results = []; |
|||
foreach ($paths as $path) { |
|||
$results[$path] = $baseResourceUrl . $path; |
|||
} |
|||
|
|||
return success([ |
|||
'base_url' => $baseResourceUrl, |
|||
'resources' => $results |
|||
]); |
|||
} |
|||
|
|||
/** |
|||
* 获取所有可用的图标资源 |
|||
*/ |
|||
public function getIconList(Request $request) |
|||
{ |
|||
$baseUrl = $request->domain() ?: 'http://localhost:20080'; |
|||
$baseResourceUrl = $baseUrl . '/static/resource/uniapp/'; |
|||
|
|||
// 定义所有可用的图标 |
|||
$icons = [ |
|||
'delete.png', |
|||
'ding_wei.png', |
|||
'empty.png', |
|||
'guoqi.png', |
|||
'home-active.png', |
|||
'home.png', |
|||
'kkry.png', |
|||
'liu.png', |
|||
'loading_white.png', |
|||
'notice.png', |
|||
'profile-active.png', |
|||
'profile.png', |
|||
'tou.png', |
|||
'uploadImg.png', |
|||
'used.png', |
|||
'warn.png', |
|||
'weixin.png' |
|||
]; |
|||
|
|||
$iconUrls = []; |
|||
foreach ($icons as $icon) { |
|||
$iconUrls['icon-img/' . $icon] = $baseResourceUrl . 'icon-img/' . $icon; |
|||
} |
|||
|
|||
// 添加评分图标 |
|||
$iconUrls['rate/none.png'] = $baseResourceUrl . 'rate/none.png'; |
|||
|
|||
return success([ |
|||
'base_url' => $baseResourceUrl, |
|||
'icons' => $iconUrls, |
|||
'count' => count($iconUrls) |
|||
]); |
|||
} |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
<?php |
|||
// +---------------------------------------------------------------------- |
|||
// | Niucloud-admin 企业快速开发的多应用管理平台 |
|||
// +---------------------------------------------------------------------- |
|||
// | 官方网址:https://www.niucloud.com |
|||
// +---------------------------------------------------------------------- |
|||
// | niucloud团队 版权所有 开源版本可自由商用 |
|||
// +---------------------------------------------------------------------- |
|||
// | Author: Niucloud Team |
|||
// +---------------------------------------------------------------------- |
|||
|
|||
use app\api\controller\apiController\StaticResource; |
|||
use think\facade\Route; |
|||
|
|||
/** |
|||
* 静态资源路由 |
|||
*/ |
|||
Route::group(function () { |
|||
// 获取单个静态资源URL |
|||
Route::get('resource/url', 'apiController.StaticResource/getResourceUrl'); |
|||
|
|||
// 批量获取静态资源URL |
|||
Route::post('resource/batch-urls', 'apiController.StaticResource/getBatchResourceUrls'); |
|||
|
|||
// 获取所有图标列表 |
|||
Route::get('resource/icons', 'apiController.StaticResource/getIconList'); |
|||
|
|||
})->middleware([ |
|||
app\api\middleware\ApiPersonnelCheckToken::class |
|||
]); |
|||
|
After Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 3.6 KiB After Width: | Height: | Size: 3.6 KiB |
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
|
Before Width: | Height: | Size: 3.2 KiB After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 7.7 KiB |
|
After Width: | Height: | Size: 6.3 KiB |
|
Before Width: | Height: | Size: 5.7 KiB After Width: | Height: | Size: 5.7 KiB |
|
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 1.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB After Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.0 KiB After Width: | Height: | Size: 2.0 KiB |
|
After Width: | Height: | Size: 6.7 KiB |
|
After Width: | Height: | Size: 5.3 KiB |
|
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 36 KiB |
|
Before Width: | Height: | Size: 3.7 KiB After Width: | Height: | Size: 3.7 KiB |
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 5.0 KiB After Width: | Height: | Size: 5.0 KiB |
|
Before Width: | Height: | Size: 3.6 KiB After Width: | Height: | Size: 3.6 KiB |
|
After Width: | Height: | Size: 2.0 KiB |
|
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 7.7 KiB |
|
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 6.3 KiB |
|
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 6.8 KiB |
|
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 5.3 KiB |
@ -0,0 +1,124 @@ |
|||
/** |
|||
* 静态资源URL工具类 |
|||
* 用于获取后端提供的静态资源URL |
|||
*/ |
|||
|
|||
// 配置基础URL
|
|||
const config = { |
|||
// 后端API基础URL,从环境变量或配置文件读取
|
|||
baseUrl: process.env.NODE_ENV === 'development' ? 'http://localhost:20080' : 'https://your-domain.com', |
|||
// 静态资源基础路径
|
|||
staticBasePath: '/static/resource/uniapp/' |
|||
} |
|||
|
|||
/** |
|||
* 获取静态资源的完整URL |
|||
* @param {string} resourcePath - 资源相对路径,如:'icon-img/home.png' |
|||
* @returns {string} 完整的资源URL |
|||
*/ |
|||
export function getStaticResourceUrl(resourcePath) { |
|||
if (!resourcePath) { |
|||
console.warn('staticResource: resourcePath is empty') |
|||
return '' |
|||
} |
|||
|
|||
// 移除开头的 '/' 或 'static/'
|
|||
const cleanPath = resourcePath.replace(/^(\/|static\/)?/, '') |
|||
|
|||
return config.baseUrl + config.staticBasePath + cleanPath |
|||
} |
|||
|
|||
/** |
|||
* 批量获取静态资源URL |
|||
* @param {Array<string>} resourcePaths - 资源路径数组 |
|||
* @returns {Object} 路径到URL的映射对象 |
|||
*/ |
|||
export function getBatchStaticResourceUrls(resourcePaths) { |
|||
if (!Array.isArray(resourcePaths)) { |
|||
console.warn('staticResource: resourcePaths must be an array') |
|||
return {} |
|||
} |
|||
|
|||
const results = {} |
|||
resourcePaths.forEach(path => { |
|||
results[path] = getStaticResourceUrl(path) |
|||
}) |
|||
|
|||
return results |
|||
} |
|||
|
|||
/** |
|||
* 获取图标资源URL的便捷方法 |
|||
* @param {string} iconName - 图标文件名,如:'home.png' |
|||
* @returns {string} 图标URL |
|||
*/ |
|||
export function getIconUrl(iconName) { |
|||
return getStaticResourceUrl(`icon-img/${iconName}`) |
|||
} |
|||
|
|||
/** |
|||
* 获取评分图标URL |
|||
* @param {string} rateName - 评分图标名,如:'none.png' |
|||
* @returns {string} 评分图标URL |
|||
*/ |
|||
export function getRateIconUrl(rateName) { |
|||
return getStaticResourceUrl(`rate/${rateName}`) |
|||
} |
|||
|
|||
/** |
|||
* 预定义的常用图标URL |
|||
*/ |
|||
export const COMMON_ICONS = { |
|||
// 导航图标
|
|||
HOME: () => getIconUrl('home.png'), |
|||
HOME_ACTIVE: () => getIconUrl('home-active.png'), |
|||
PROFILE: () => getIconUrl('profile.png'), |
|||
PROFILE_ACTIVE: () => getIconUrl('profile-active.png'), |
|||
|
|||
// 功能图标
|
|||
EMPTY: () => getIconUrl('empty.png'), |
|||
DELETE: () => getIconUrl('delete.png'), |
|||
LOCATION: () => getIconUrl('ding_wei.png'), |
|||
NOTICE: () => getIconUrl('notice.png'), |
|||
WARNING: () => getIconUrl('warn.png'), |
|||
LOADING: () => getIconUrl('loading_white.png'), |
|||
|
|||
// 用户相关
|
|||
AVATAR: () => getIconUrl('tou.png'), |
|||
UPLOAD: () => getIconUrl('uploadImg.png'), |
|||
|
|||
// 其他
|
|||
EXPIRED: () => getIconUrl('guoqi.png'), |
|||
USED: () => getIconUrl('used.png'), |
|||
WECHAT: () => getIconUrl('weixin.png'), |
|||
|
|||
// 评分
|
|||
RATE_NONE: () => getRateIconUrl('none.png') |
|||
} |
|||
|
|||
/** |
|||
* 更新配置 |
|||
* @param {Object} newConfig - 新的配置对象 |
|||
*/ |
|||
export function updateConfig(newConfig) { |
|||
Object.assign(config, newConfig) |
|||
} |
|||
|
|||
/** |
|||
* 获取当前配置 |
|||
* @returns {Object} 当前配置 |
|||
*/ |
|||
export function getConfig() { |
|||
return { ...config } |
|||
} |
|||
|
|||
// 默认导出主要函数
|
|||
export default { |
|||
getStaticResourceUrl, |
|||
getBatchStaticResourceUrls, |
|||
getIconUrl, |
|||
getRateIconUrl, |
|||
COMMON_ICONS, |
|||
updateConfig, |
|||
getConfig |
|||
} |
|||