固定数据增加缓存

This commit is contained in:
gongfuxiang 2023-01-14 20:29:37 +08:00
parent e54eec18e4
commit 1ca8f46467
6 changed files with 404 additions and 341 deletions

View File

@ -366,7 +366,7 @@ class Common extends BaseController
$assign['user'] = $this->user;
// 用户中心菜单
$assign['user_left_menu'] = NavigationService::UsersCenterLeftList();
$assign['user_left_menu'] = NavigationService::UserCenterLeftList();
// 商品大分类
$assign['goods_category_list'] = GoodsService::GoodsCategoryAll();

View File

@ -35,7 +35,7 @@ class ArticleService
public static function HomeArticleList($params = [])
{
// 从缓存获取
$key = SystemService::CacheKey('shopxo.cache_home_article_list_key');
$key = SystemService::CacheKey('shopxo.cache_home_article_list_key').APPLICATION_CLIENT_TYPE;
$data = MyCache($key);
if($data === null || MyEnv('app_debug'))
{
@ -51,9 +51,6 @@ class ArticleService
// 存储缓存
MyCache($key, $data, 180);
} else {
// 处理数据、由于平台不一样url地址或者其他数据也会不一样
$data = self::ArticleListHandle($data);
}
return $data;
}

View File

@ -616,39 +616,49 @@ class GoodsCommentsService
*/
public static function GoodsCommentsScore($goods_id)
{
// 默认
$rating_list = [
1 => ['rating'=>1, 'name'=>'1分', 'count'=>0, 'portion'=>0],
2 => ['rating'=>2, 'name'=>'2分', 'count'=>0, 'portion'=>0],
3 => ['rating'=>3, 'name'=>'3分', 'count'=>0, 'portion'=>0],
4 => ['rating'=>4, 'name'=>'4分', 'count'=>0, 'portion'=>0],
5 => ['rating'=>5, 'name'=>'5分', 'count'=>0, 'portion'=>0],
];
$where = [
['goods_id', '=', $goods_id],
['rating', '>', 0],
];
$data = Db::name('GoodsComments')->where($where)->group('rating')->column('count(*) as count', 'rating');
if(!empty($data))
// 从缓存获取
$key = SystemService::CacheKey('shopxo.cache_goods_comments_score_key');
$data = MyCache($key);
if($data === null || MyEnv('app_debug'))
{
$sum = array_sum($data);
foreach($data as $rating=>$count)
// 默认
$rating_list = [
1 => ['rating'=>1, 'name'=>'1分', 'count'=>0, 'portion'=>0],
2 => ['rating'=>2, 'name'=>'2分', 'count'=>0, 'portion'=>0],
3 => ['rating'=>3, 'name'=>'3分', 'count'=>0, 'portion'=>0],
4 => ['rating'=>4, 'name'=>'4分', 'count'=>0, 'portion'=>0],
5 => ['rating'=>5, 'name'=>'5分', 'count'=>0, 'portion'=>0],
];
$where = [
['goods_id', '=', $goods_id],
['rating', '>', 0],
];
$data = Db::name('GoodsComments')->where($where)->group('rating')->column('count(*) as count', 'rating');
if(!empty($data))
{
if($rating > 0 && $rating <= 5)
$sum = array_sum($data);
foreach($data as $rating=>$count)
{
$rating_list[$rating]['count'] = $count;
$rating_list[$rating]['portion'] = round(($count/$sum)*100);
if($rating > 0 && $rating <= 5)
{
$rating_list[$rating]['count'] = $count;
$rating_list[$rating]['portion'] = round(($count/$sum)*100);
}
}
}
sort($rating_list);
$avg = PriceNumberFormat(Db::name('GoodsComments')->where($where)->avg('rating'), 1);
$rate = ($avg <= 0) ? 100 : intval(($avg/5)*100);
$data = [
'avg' => $avg,
'rate' => $rate,
'rating' => $rating_list,
];
// 存储缓存
MyCache($key, $data, 180);
}
sort($rating_list);
$avg = PriceNumberFormat(Db::name('GoodsComments')->where($where)->avg('rating'), 1);
$rate = ($avg <= 0) ? 100 : intval(($avg/5)*100);
return [
'avg' => $avg,
'rate' => $rate,
'rating' => $rating_list,
];
return $data;
}
/**

View File

@ -3011,77 +3011,85 @@ class GoodsService
*/
public static function GoodsDetailMiddleTabsNavList($goods)
{
// 是否展示商品评价
$is_comments = MyC('common_is_show_goods_comments', 1);
// app与web端不一致
if(APPLICATION == 'app')
// 从缓存获取
$key = SystemService::CacheKey('shopxo.cache_goods_detail_middle_tabs_key').APPLICATION;
$data = MyCache($key);
if($data === null || MyEnv('app_debug'))
{
// 这里的 ent 值必须和系统中区域块定义的一致
$data = [
['name'=>'商品', 'ent'=>'.page'],
];
// 是否展示商品评价
if($is_comments == 1)
$is_comments = MyC('common_is_show_goods_comments', 1);
// app与web端不一致
if(APPLICATION == 'app')
{
$data[] = ['name'=>'评价', 'ent'=>'.goods-comment'];
}
// 这里的 ent 值必须和系统中区域块定义的一致
$data = [
['name'=>'商品', 'ent'=>'.page'],
];
// 商品详情介绍
$data[] = ['name'=>'详情', 'ent'=>'.goods-detail'];
} else {
// 评论总数
$comments_count = isset($goods['comments_count']) ? $goods['comments_count'] : GoodsCommentsService::GoodsCommentsTotal(['goods_id'=>$goods['id'], 'is_show'=>1]);
// 是否展示商品评价
if($is_comments == 1)
{
$data[] = ['name'=>'评价', 'ent'=>'.goods-comment'];
}
// 列表
// type 类型
// name 名称
// active 选中(可选)
// value 数据值(可选)
$data = [
[
'type' => 'detail',
'name' => '详情',
'active' => 1,
],
];
// 商品详情介绍
$data[] = ['name'=>'详情', 'ent'=>'.goods-detail'];
} else {
// 评论总数
$comments_count = isset($goods['comments_count']) ? $goods['comments_count'] : GoodsCommentsService::GoodsCommentsTotal(['goods_id'=>$goods['id'], 'is_show'=>1]);
// 是否展示商品评价
if($is_comments == 1)
{
// 列表
// type 类型
// name 名称
// active 选中(可选)
// value 数据值(可选)
$data = [
[
'type' => 'detail',
'name' => '详情',
'active' => 1,
],
];
// 是否展示商品评价
if($is_comments == 1)
{
$data[] = [
'type' => 'comments',
'name' => '评价('.$comments_count.')',
];
}
// 猜你喜欢,目前以销量最高推荐
$data[] = [
'type' => 'comments',
'name' => '评价('.$comments_count.')',
'type' => 'guess_you_like',
'name' => '猜你喜欢',
];
}
// 猜你喜欢,目前以销量最高推荐
$data[] = [
'type' => 'guess_you_like',
'name' => '猜你喜欢',
];
}
// 商品详情中间导航钩子
$hook_name = 'plugins_service_goods_detail_middle_tabs_nav_handle';
MyEventTrigger($hook_name, [
'hook_name' => $hook_name,
'is_backend' => true,
'goods' => $goods,
'data' => &$data,
]);
// 商品详情中间导航钩子
$hook_name = 'plugins_service_goods_detail_middle_tabs_nav_handle';
MyEventTrigger($hook_name, [
'hook_name' => $hook_name,
'is_backend' => true,
'goods' => $goods,
'data' => &$data,
]);
// web端处理格式
if(APPLICATION == 'web')
{
$data = [
'nav' => $data,
'type' => array_column($data, 'type'),
];
}
// 返回数据
if(APPLICATION == 'app')
{
return $data;
} else {
return [
'nav' => $data,
'type' => array_column($data, 'type'),
];
// 存储缓存
MyCache($key, $data, 180);
}
return $data;
}
/**
@ -3213,54 +3221,63 @@ class GoodsService
*/
public static function GoodsBreadcrumbData($goods)
{
// 默认首页
$result = [
[
'type' => 0,
'name' => '首页',
'url' => SystemService::HomeUrl(),
'icon' => 'am-icon-home',
],
];
// 商品分类
$cids = Db::name('GoodsCategoryJoin')->where(['goods_id'=>$goods['id']])->column('category_id');
if(!empty($cids))
// 从缓存获取
$key = SystemService::CacheKey('shopxo.cache_goods_detail_breadcrumb_key');
$data = MyCache($key);
if($data === null || MyEnv('app_debug'))
{
$where = [
['id', 'in', $cids],
['is_enable', '=', 1],
// 默认首页
$data = [
[
'type' => 0,
'name' => '首页',
'url' => SystemService::HomeUrl(),
'icon' => 'am-icon-home',
],
];
$category = Db::name('GoodsCategory')->where($where)->field('id,name')->select()->toArray();
if(!empty($category))
// 商品分类
$cids = Db::name('GoodsCategoryJoin')->where(['goods_id'=>$goods['id']])->column('category_id');
if(!empty($cids))
{
$category = array_map(function($v)
$where = [
['id', 'in', $cids],
['is_enable', '=', 1],
];
$category = Db::name('GoodsCategory')->where($where)->field('id,name')->select()->toArray();
if(!empty($category))
{
$v['url'] = MyUrl('index/search/index', ['cid'=>$v['id']]);
return $v;
}, $category);
if(count($category) == 1)
{
$result[] = [
'type' => 0,
'name' => $category[0]['name'],
'url' => $category[0]['url'],
];
} else {
$result[] = [
'type' => 1,
'name' => '商品分类',
'data' => $category,
];
$category = array_map(function($v)
{
$v['url'] = MyUrl('index/search/index', ['cid'=>$v['id']]);
return $v;
}, $category);
if(count($category) == 1)
{
$data[] = [
'type' => 0,
'name' => $category[0]['name'],
'url' => $category[0]['url'],
];
} else {
$data[] = [
'type' => 1,
'name' => '商品分类',
'data' => $category,
];
}
}
}
}
// 当前商品名称
$result[] = [
'type' => 0,
'name' => $goods['title'],
];
return $result;
// 当前商品名称
$data[] = [
'type' => 0,
'name' => $goods['title'],
];
// 存储缓存
MyCache($key, $data, 180);
}
return $data;
}
/**

View File

@ -643,88 +643,107 @@ class NavigationService
*/
public static function HomeHavTopRight($params = [])
{
$cart_total = 0;
$message_total = -1;
if(!empty($params['user']))
{
// 购物车商品汇总
$cart_res = GoodsCartService::UserGoodsCartTotal(['user'=>$params['user']]);
$cart_total = $cart_res['buy_number'];
// 从缓存获取
$key = SystemService::CacheKey('shopxo.cache_header_navigation_top_right_key');
$data = MyCache($key);
if($data === null || MyEnv('app_debug'))
{
// 列表
$data = [
[
'name' => '个人中心',
'type' => 'center',
'is_login' => 1,
'badge' => null,
'icon' => 'am-icon-user',
'url' => MyUrl('index/user/index'),
'items' => [],
],
[
'name' => '我的商城',
'type' => 'myself',
'is_login' => 1,
'badge' => null,
'icon' => 'am-icon-cube',
'url' => '',
'items' => [
[
'name' => '我的订单',
'url' => MyUrl('index/order/index'),
],
],
],
[
'name' => '我的收藏',
'type' => 'favor',
'is_login' => 1,
'badge' => null,
'icon' => 'am-icon-heart',
'url' => '',
'items' => [
[
'name' => '商品收藏',
'url' => MyUrl('index/usergoodsfavor/index'),
],
],
],
[
'name' => '购物车',
'type' => 'cart',
'is_login' => 1,
'badge' => -1,
'icon' => 'am-icon-shopping-cart',
'url' => MyUrl('index/cart/index'),
'items' => [],
],
[
'name' => '消息',
'type' => 'message',
'is_login' => 1,
'badge' => 0,
'icon' => 'am-icon-bell',
'url' => MyUrl('index/message/index'),
'items' => [],
],
];
// 未读消息总数
$message_params = ['user'=>$params['user'], 'is_more'=>1, 'is_read'=>0, 'user_type'=>'user'];
$message_total = MessageService::UserMessageTotal($message_params);
$message_total = ($message_total <= 0) ? -1 : $message_total;
// 顶部小导航右侧钩子
$hook_name = 'plugins_service_header_navigation_top_right_handle';
MyEventTrigger($hook_name, [
'hook_name' => $hook_name,
'is_backend' => true,
'params' => &$params,
'data' => &$data,
]);
// 存储缓存
MyCache($key, $data, 180);
}
// 列表
$data = [
[
'name' => '个人中心',
'type' => 'center',
'is_login' => 1,
'badge' => null,
'icon' => 'am-icon-user',
'url' => MyUrl('index/user/index'),
'items' => [],
],
[
'name' => '我的商城',
'type' => 'myself',
'is_login' => 1,
'badge' => null,
'icon' => 'am-icon-cube',
'url' => '',
'items' => [
[
'name' => '我的订单',
'url' => MyUrl('index/order/index'),
],
],
],
[
'name' => '我的收藏',
'type' => 'favor',
'is_login' => 1,
'badge' => null,
'icon' => 'am-icon-heart',
'url' => '',
'items' => [
[
'name' => '商品收藏',
'url' => MyUrl('index/usergoodsfavor/index'),
],
],
],
[
'name' => '购物车',
'type' => 'cart',
'is_login' => 1,
'badge' => $cart_total,
'icon' => 'am-icon-shopping-cart',
'url' => MyUrl('index/cart/index'),
'items' => [],
],
[
'name' => '消息',
'type' => 'message',
'is_login' => 1,
'badge' => $message_total,
'icon' => 'am-icon-bell',
'url' => MyUrl('index/message/index'),
'items' => [],
],
];
// 顶部小导航右侧钩子
$hook_name = 'plugins_service_header_navigation_top_right_handle';
MyEventTrigger($hook_name, [
'hook_name' => $hook_name,
'is_backend' => true,
'params' => &$params,
'data' => &$data,
]);
// 实时数据处理
if(!empty($data) && !empty($params['user']))
{
// 所有类型
$type = array_column($data, 'type');
// 消息总数
$index = array_search('message', $type);
if($index !== false)
{
// 未读消息总数
$message_params = ['user'=>$params['user'], 'is_more'=>1, 'is_read'=>0, 'user_type'=>'user'];
$message_total = MessageService::UserMessageTotal($message_params);
$data[$index]['badge'] = ($message_total <= 0) ? -1 : $message_total;
}
// 购物车商品汇总
$index = array_search('cart', $type);
if($index !== false)
{
$cart_res = GoodsCartService::UserGoodsCartTotal(['user'=>$params['user']]);
$data[$index]['badge'] = $cart_res['buy_number'];
}
}
return $data;
}
@ -852,7 +871,7 @@ class NavigationService
* @desc description
* @param [array] $params [输入信息]
*/
public static function UsersCenterLeftList($params = [])
public static function UserCenterLeftList($params = [])
{
// name 名称
// url 页面地址
@ -862,139 +881,147 @@ class NavigationService
// item 二级数据
// is_system 是否系统内置菜单0否, 1是扩展数据可空或0
// 菜单列表
$data = [
'center' => [
'name' => '个人中心',
'url' => MyUrl('index/user/index'),
'is_show' => 1,
'contains' => ['indexuserindex'],
'icon' => 'am-icon-home',
'is_system' => 1,
],
'business' => [
'name' => '业务管理',
'is_show' => 1,
'icon' => 'am-icon-cube',
'is_system' => 1,
'item' => [
[
'name' => '订单管理',
'url' => MyUrl('index/order/index'),
'is_show' => 1,
'contains' => ['indexorderindex', 'indexorderdetail', 'indexordercomments'],
'icon' => 'am-icon-th-list',
'is_system' => 1,
],
[
'name' => '订单售后',
'url' => MyUrl('index/orderaftersale/index'),
'is_show' => 1,
'contains' => ['indexorderaftersaleindex', 'indexorderaftersaledetail'],
'icon' => 'am-icon-puzzle-piece',
'is_system' => 1,
],
[
'name' => '商品收藏',
'url' => MyUrl('index/usergoodsfavor/index'),
'contains' => ['indexusergoodsfavorindex'],
'is_show' => 1,
'icon' => 'am-icon-heart-o',
'is_system' => 1,
],
]
],
'property' => [
'name' => '财产中心',
'is_show' => 1,
'icon' => 'am-icon-trophy',
'is_system' => 1,
'item' => [
[
'name' => '我的积分',
'url' => MyUrl('index/userintegral/index'),
'contains' => ['indexuserintegralindex'],
'is_show' => 1,
'icon' => 'am-icon-fire',
'is_system' => 1,
],
]
],
'base' => [
'name' => '资料管理',
'is_show' => 1,
'icon' => 'am-icon-user',
'is_system' => 1,
'item' => [
[
'name' => '个人资料',
'url' => MyUrl('index/personal/index'),
'contains' => ['indexpersonalindex', 'indexpersonalsaveinfo'],
'is_show' => 1,
'icon' => 'am-icon-gear',
'is_system' => 1,
],
[
'name' => '我的地址',
'url' => MyUrl('index/useraddress/index'),
'contains' => ['indexuseraddressindex', 'indexuseraddresssaveinfo'],
'is_show' => 1,
'icon' => 'am-icon-street-view',
'is_system' => 1,
],
[
'name' => '安全设置',
'url' => MyUrl('index/safety/index'),
'contains' => ['indexsafetyindex', 'indexsafetyloginpwdinfo', 'indexsafetymobileinfo', 'indexsafetynewmobileinfo', 'indexsafetyemailinfo', 'indexsafetynewemailinfo', 'indexsafetylogoutinfo'],
'is_show' => 1,
'icon' => 'am-icon-user-secret',
'is_system' => 1,
],
[
'name' => '我的消息',
'url' => MyUrl('index/message/index'),
'contains' => ['indexmessageindex'],
'is_show' => 1,
'icon' => 'am-icon-bell-o',
'is_system' => 1,
],
[
'name' => '我的足迹',
'url' => MyUrl('index/usergoodsbrowse/index'),
'contains' => ['indexusergoodsbrowseindex'],
'is_show' => 1,
'icon' => 'am-icon-lastfm',
'is_system' => 1,
],
[
'name' => '问答/留言',
'url' => MyUrl('index/answer/index'),
'contains' => ['indexanswerindex'],
'is_show' => 1,
'icon' => 'am-icon-question',
'is_system' => 1,
],
]
],
'logout' => [
'name' => '安全退出',
'url' => MyUrl('index/user/logout'),
'contains' => ['indexuserlogout'],
'is_show' => 1,
'icon' => 'am-icon-power-off',
'is_system' => 1,
],
];
// 从缓存获取
$key = SystemService::CacheKey('shopxo.cache_user_center_left_nav_key');
$data = MyCache($key);
if($data === null || MyEnv('app_debug'))
{
// 菜单列表
$data = [
'center' => [
'name' => '个人中心',
'url' => MyUrl('index/user/index'),
'is_show' => 1,
'contains' => ['indexuserindex'],
'icon' => 'am-icon-home',
'is_system' => 1,
],
'business' => [
'name' => '业务管理',
'is_show' => 1,
'icon' => 'am-icon-cube',
'is_system' => 1,
'item' => [
[
'name' => '订单管理',
'url' => MyUrl('index/order/index'),
'is_show' => 1,
'contains' => ['indexorderindex', 'indexorderdetail', 'indexordercomments'],
'icon' => 'am-icon-th-list',
'is_system' => 1,
],
[
'name' => '订单售后',
'url' => MyUrl('index/orderaftersale/index'),
'is_show' => 1,
'contains' => ['indexorderaftersaleindex', 'indexorderaftersaledetail'],
'icon' => 'am-icon-puzzle-piece',
'is_system' => 1,
],
[
'name' => '商品收藏',
'url' => MyUrl('index/usergoodsfavor/index'),
'contains' => ['indexusergoodsfavorindex'],
'is_show' => 1,
'icon' => 'am-icon-heart-o',
'is_system' => 1,
],
]
],
'property' => [
'name' => '财产中心',
'is_show' => 1,
'icon' => 'am-icon-trophy',
'is_system' => 1,
'item' => [
[
'name' => '我的积分',
'url' => MyUrl('index/userintegral/index'),
'contains' => ['indexuserintegralindex'],
'is_show' => 1,
'icon' => 'am-icon-fire',
'is_system' => 1,
],
]
],
'base' => [
'name' => '资料管理',
'is_show' => 1,
'icon' => 'am-icon-user',
'is_system' => 1,
'item' => [
[
'name' => '个人资料',
'url' => MyUrl('index/personal/index'),
'contains' => ['indexpersonalindex', 'indexpersonalsaveinfo'],
'is_show' => 1,
'icon' => 'am-icon-gear',
'is_system' => 1,
],
[
'name' => '我的地址',
'url' => MyUrl('index/useraddress/index'),
'contains' => ['indexuseraddressindex', 'indexuseraddresssaveinfo'],
'is_show' => 1,
'icon' => 'am-icon-street-view',
'is_system' => 1,
],
[
'name' => '安全设置',
'url' => MyUrl('index/safety/index'),
'contains' => ['indexsafetyindex', 'indexsafetyloginpwdinfo', 'indexsafetymobileinfo', 'indexsafetynewmobileinfo', 'indexsafetyemailinfo', 'indexsafetynewemailinfo', 'indexsafetylogoutinfo'],
'is_show' => 1,
'icon' => 'am-icon-user-secret',
'is_system' => 1,
],
[
'name' => '我的消息',
'url' => MyUrl('index/message/index'),
'contains' => ['indexmessageindex'],
'is_show' => 1,
'icon' => 'am-icon-bell-o',
'is_system' => 1,
],
[
'name' => '我的足迹',
'url' => MyUrl('index/usergoodsbrowse/index'),
'contains' => ['indexusergoodsbrowseindex'],
'is_show' => 1,
'icon' => 'am-icon-lastfm',
'is_system' => 1,
],
[
'name' => '问答/留言',
'url' => MyUrl('index/answer/index'),
'contains' => ['indexanswerindex'],
'is_show' => 1,
'icon' => 'am-icon-question',
'is_system' => 1,
],
]
],
'logout' => [
'name' => '安全退出',
'url' => MyUrl('index/user/logout'),
'contains' => ['indexuserlogout'],
'is_show' => 1,
'icon' => 'am-icon-power-off',
'is_system' => 1,
],
];
// 用户中心左侧菜单钩子
$hook_name = 'plugins_service_users_center_left_menu_handle';
MyEventTrigger($hook_name, [
'hook_name' => $hook_name,
'is_backend' => true,
'params' => &$params,
'data' => &$data,
]);
// 用户中心左侧菜单钩子
$hook_name = 'plugins_service_users_center_left_menu_handle';
MyEventTrigger($hook_name, [
'hook_name' => $hook_name,
'is_backend' => true,
'params' => &$params,
'data' => &$data,
]);
// 存储缓存
MyCache($key, $data, 180);
}
return $data;
}

View File

@ -64,6 +64,15 @@ return [
// 商品大分类缓存
'cache_goods_category_key' => 'cache_goods_category_key_data',
// 商品详情面包屑导航
'cache_goods_detail_breadcrumb_key' => 'cache_goods_detail_breadcrumb_key_data',
// 商品详情中间导航
'cache_goods_detail_middle_tabs_key' => 'cache_goods_detail_middle_tabs_key_data',
// 商品评论评分
'cache_goods_comments_score_key' => 'cache_goods_comments_score_key_data',
// 应用数据缓存
'cache_plugins_data_key' => 'cache_plugins_data_key_data_',
@ -103,6 +112,9 @@ return [
// 首页展示的友情链接列表数据
'cache_home_link_list_key' => 'cache_home_link_list_data',
// 用户中心左侧菜单
'cache_user_center_left_nav_key' => 'cache_user_center_left_nav_key_data',
// 站点域名地址
'domain_url' => $domain_url,