GoodsLists.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\lists;
  20. use app\common\{cache\HandleConcurrencyCache,
  21. enum\UserTerminalEnum,
  22. model\Goods,
  23. enum\GoodsEnum,
  24. model\GoodsItem,
  25. model\SearchRecord,
  26. service\ConfigService};
  27. use think\facade\Db;
  28. /**
  29. * 商品列表接口
  30. * Class GoodsLists
  31. * @package app\shopapi\lists
  32. */
  33. class GoodsLists extends BaseShopDataLists
  34. {
  35. public function setSearch(): array
  36. {
  37. return array_diff(array_keys($this->params), ['page_no', 'page_size']);
  38. }
  39. /**
  40. * @note 首页商品列表
  41. * @return array
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\DbException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. * @author cjhao
  46. * @date 2021/7/26 16:47
  47. */
  48. public function lists(): array
  49. {
  50. //读取缓存
  51. $HandleConcurrencyCache = new HandleConcurrencyCache();
  52. $list = $HandleConcurrencyCache->getCache($HandleConcurrencyCache->getGoodsListsKey(md5(serialize($this->params))));
  53. if ($list !== false) {
  54. return $list;
  55. }
  56. $list = Goods::withSearch($this->setSearch(), $this->params)
  57. ->field('id,name,image,virtual_sales_num+sales_num as sales_num,min_price as sell_price,min_lineation_price as lineation_price')
  58. ->where(['status'=>GoodsEnum::STATUS_SELL])
  59. ->limit($this->limitOffset, $this->limitLength)
  60. ->order('sort desc,id desc')
  61. ->select();
  62. foreach($list as &$gv){
  63. $goods_item_count = GoodsItem::where(['goods_id'=>$gv['id']])->count();
  64. if($goods_item_count > 1){
  65. $gv['is_multi_gauge'] = 1;
  66. }else{
  67. $gv['is_multi_gauge'] = 0;
  68. }
  69. }
  70. $showPrice = ConfigService::get('goods_set', 'show_price', 1);
  71. if(0 == $showPrice){
  72. foreach ($list as $goods){
  73. $goods->lineation_price = 0;
  74. }
  75. }
  76. $name = $this->params['name'] ?? '';
  77. if ($name && $this->userId && $this->userInfo['terminal'] != UserTerminalEnum::PC) {//记录关键词
  78. $this->recordKeyWord(trim($name), $this->userId);
  79. }
  80. //设置缓存
  81. $HandleConcurrencyCache->setCache($HandleConcurrencyCache->getGoodsListsKey(md5(serialize($this->params))),$list->toArray(),600);
  82. return $list->toArray();
  83. }
  84. /**
  85. * @notes 商品统计
  86. * @return int
  87. * @author cjhao
  88. * @date 2021/7/26 16:48
  89. */
  90. public function count(): int
  91. {
  92. return Goods::withSearch($this->setSearch(), $this->params)
  93. ->where(['status'=>GoodsEnum::STATUS_SELL])
  94. ->count();
  95. }
  96. /**
  97. * @notes 记录商品搜索
  98. * @param $name
  99. * @param $userId
  100. * @return bool
  101. * @author cjhao
  102. * @date 2021/8/11 17:43
  103. */
  104. public function recordKeyWord($name,$userId):bool
  105. {
  106. $searchRecord = SearchRecord::where(['user_id'=>$userId,'keyword'=>$name])->findOrEmpty();
  107. if($searchRecord->isEmpty()){
  108. $searchRecord->user_id = $userId;
  109. $searchRecord->keyword = $name;
  110. $searchRecord->save();
  111. return true;
  112. }
  113. $searchRecord->count = Db::raw('count+1');
  114. $searchRecord->save();
  115. //删除缓存
  116. $HandleConcurrencyCache = new HandleConcurrencyCache();
  117. $HandleConcurrencyCache->deleteCache($HandleConcurrencyCache->getGoodsSearchRecordKey($userId));
  118. return true;
  119. }
  120. }