GoodsLists.php 3.5 KB

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