GoodsLists.php 4.1 KB

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