| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- // +----------------------------------------------------------------------
- // | likeshop100%开源免费商用商城系统
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | 开源版本可自由商用,可去除界面版权logo
- // | 商业版本务必购买商业授权,以免引起法律纠纷
- // | 禁止对系统程序代码以任何目的,任何形式的再发布
- // | gitee下载:https://gitee.com/likeshop_gitee
- // | github下载:https://github.com/likeshop-github
- // | 访问官网:https://www.likeshop.cn
- // | 访问社区:https://home.likeshop.cn
- // | 访问手册:http://doc.likeshop.cn
- // | 微信公众号:likeshop技术社区
- // | likeshop团队 版权所有 拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeshopTeam
- // +----------------------------------------------------------------------
- namespace app\shopapi\logic;
- use app\common\model\Info;
- use app\common\cache\InfoCache;
- class InfoLogic
- {
- /**
- * @notes 获取信息详情
- * @param $params
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author Tab
- * @date 2021/7/14 15:13
- */
- public static function detail($params)
- {
- // // 增加浏览量
- // $info = Info::find($params['id']);
- // $visit = $info->visit ?? 1;
- // $info->visit = $visit + 1 ;
- // $info->save();
- // return Info::field('id,cid,title,synopsis,image,address,phone,latitude,longitude,content,visit,create_time')
- // ->append(['category'])->find($params['id'])->toArray();
- $infoCache = new InfoCache();
- // 先尝试从缓存获取
- $cachedInfo = $infoCache->getInfoDetail($params['id']);
- if ($cachedInfo) {
- // 缓存存在,仍需要增加浏览量
- $info = Info::find($params['id']);
- if ($info) {
- $visit = $info->visit ?? 1;
- $info->visit = $visit + 1;
- $info->save();
- // 更新缓存中的浏览量
- $cachedInfo['visit'] = $visit + 1;
- $infoCache->setInfoDetail($params['id'], $cachedInfo);
- }
- return $cachedInfo;
- }
- // 缓存不存在,从数据库查询
- $info = Info::find($params['id']);
- if (!$info) {
- return [];
- }
- // 增加浏览量
- $visit = $info->visit ?? 1;
- $info->visit = $visit + 1;
- $info->save();
- // 查询详细信息
- $result = Info::field('id,cid,title,synopsis,image,address,phone,latitude,longitude,content,visit,create_time')
- ->append(['category'])->find($params['id'])->toArray();
- // 设置缓存,缓存1小时
- $infoCache->setInfoDetail($params['id'], $result, 3600);
- return $result;
- }
- }
|