GoodsLists.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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,0 as is_multi_gauge')
  58. ->where(['status'=>GoodsEnum::STATUS_SELL])
  59. ->limit($this->limitOffset, $this->limitLength)
  60. ->order('sort desc,id desc')
  61. ->select();
  62. // dump($list);die;
  63. foreach ($list as $goods){
  64. $goods_item_count = GoodsItem::where(['goods_id'=>$goods['id']])->count();
  65. if($goods_item_count > 1){
  66. $goods->is_multi_gauge = 1;
  67. }
  68. }
  69. $showPrice = ConfigService::get('goods_set', 'show_price', 1);
  70. if(0 == $showPrice){
  71. foreach ($list as $goods){
  72. $goods->lineation_price = 0;
  73. }
  74. }
  75. $name = $this->params['name'] ?? '';
  76. if ($name && $this->userId && $this->userInfo['terminal'] != UserTerminalEnum::PC) {//记录关键词
  77. $this->recordKeyWord(trim($name), $this->userId);
  78. }
  79. //设置缓存
  80. $HandleConcurrencyCache->setCache($HandleConcurrencyCache->getGoodsListsKey(md5(serialize($this->params))),$list->toArray(),600);
  81. return $list->toArray();
  82. }
  83. /**
  84. * @notes 商品统计
  85. * @return int
  86. * @author cjhao
  87. * @date 2021/7/26 16:48
  88. */
  89. public function count(): int
  90. {
  91. return Goods::withSearch($this->setSearch(), $this->params)
  92. ->where(['status'=>GoodsEnum::STATUS_SELL])
  93. ->count();
  94. }
  95. /**
  96. * @notes 记录商品搜索
  97. * @param $name
  98. * @param $userId
  99. * @return bool
  100. * @author cjhao
  101. * @date 2021/8/11 17:43
  102. */
  103. public function recordKeyWord($name,$userId):bool
  104. {
  105. $searchRecord = SearchRecord::where(['user_id'=>$userId,'keyword'=>$name])->findOrEmpty();
  106. if($searchRecord->isEmpty()){
  107. $searchRecord->user_id = $userId;
  108. $searchRecord->keyword = $name;
  109. $searchRecord->save();
  110. return true;
  111. }
  112. $searchRecord->count = Db::raw('count+1');
  113. $searchRecord->save();
  114. //删除缓存
  115. $HandleConcurrencyCache = new HandleConcurrencyCache();
  116. $HandleConcurrencyCache->deleteCache($HandleConcurrencyCache->getGoodsSearchRecordKey($userId));
  117. return true;
  118. }
  119. }