InfoLogic.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\logic;
  20. use app\common\model\Info;
  21. use app\common\cache\InfoCache;
  22. class InfoLogic
  23. {
  24. /**
  25. * @notes 获取信息详情
  26. * @param $params
  27. * @return array
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\DbException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. * @author Tab
  32. * @date 2021/7/14 15:13
  33. */
  34. public static function detail($params)
  35. {
  36. // // 增加浏览量
  37. // $info = Info::find($params['id']);
  38. // $visit = $info->visit ?? 1;
  39. // $info->visit = $visit + 1 ;
  40. // $info->save();
  41. // return Info::field('id,cid,title,synopsis,image,address,phone,latitude,longitude,content,visit,create_time')
  42. // ->append(['category'])->find($params['id'])->toArray();
  43. $infoCache = new InfoCache();
  44. // 先尝试从缓存获取
  45. $cachedInfo = $infoCache->getInfoDetail($params['id']);
  46. if ($cachedInfo) {
  47. // 缓存存在,仍需要增加浏览量
  48. $info = Info::find($params['id']);
  49. if ($info) {
  50. $visit = $info->visit ?? 1;
  51. $info->visit = $visit + 1;
  52. $info->save();
  53. // 更新缓存中的浏览量
  54. $cachedInfo['visit'] = $visit + 1;
  55. $infoCache->setInfoDetail($params['id'], $cachedInfo);
  56. }
  57. return $cachedInfo;
  58. }
  59. // 缓存不存在,从数据库查询
  60. $info = Info::find($params['id']);
  61. if (!$info) {
  62. return [];
  63. }
  64. // 增加浏览量
  65. $visit = $info->visit ?? 1;
  66. $info->visit = $visit + 1;
  67. $info->save();
  68. // 查询详细信息
  69. $result = Info::field('id,cid,title,synopsis,image,address,phone,latitude,longitude,content,visit,create_time')
  70. ->append(['category'])->find($params['id'])->toArray();
  71. // 设置缓存,缓存1小时
  72. $infoCache->setInfoDetail($params['id'], $result, 3600);
  73. return $result;
  74. }
  75. }