Store.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\controller;
  12. use app\wap\model\store\StoreCategory;
  13. use app\wap\model\store\StoreProduct;
  14. use service\GroupDataService;
  15. use service\SystemConfigService;
  16. use app\wap\model\topic\Relation;
  17. use think\Cache;
  18. use think\Request;
  19. use think\Url;
  20. use service\JsonService;
  21. /**商品控制器
  22. * Class Store
  23. * @package app\wap\controller
  24. */
  25. class Store extends AuthController
  26. {
  27. /*
  28. * 白名单
  29. * */
  30. public static function WhiteList()
  31. {
  32. return [
  33. 'index',
  34. 'getCategory',
  35. 'getProductList',
  36. 'getAssociatedTopics',
  37. 'detail',
  38. ];
  39. }
  40. /**商城列表
  41. * @param string $keyword
  42. * @return mixed
  43. */
  44. public function index()
  45. {
  46. $banner = json_encode(GroupDataService::getData('product_list_carousel') ?: []);
  47. $this->assign(compact('banner'));
  48. return $this->fetch();
  49. }
  50. /**获取分类
  51. * @throws \think\exception\DbException
  52. */
  53. public function getCategory()
  54. {
  55. $parentCategory = StoreCategory::pidByCategory(0, 'id,cate_name');
  56. $parentCategory = count($parentCategory) > 0 ? $parentCategory->toArray() : [];
  57. return JsonService::successful($parentCategory);
  58. }
  59. /**商品列表
  60. * @param string $keyword
  61. * @param int $cId
  62. * @param int $first
  63. * @param int $limit
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. * @throws \think\exception\DbException
  67. */
  68. public function getProductList($page = 1, $limit = 8, $cId = 0)
  69. {
  70. if (!empty($keyword)) $keyword = base64_decode(htmlspecialchars($keyword));
  71. $model = StoreProduct::validWhere();
  72. if (!empty($cId)) $model = $model->where('cate_id', $cId);
  73. if (!empty($keyword)) $model->where('keyword|store_name', 'LIKE', "%$keyword%");
  74. $model->order('sort DESC, add_time DESC');
  75. $list = $model->page((int)$page, (int)$limit)->field('id,mer_id,store_name,image,sales,price,stock,IFNULL(sales,0) + IFNULL(ficti,0) as sales,keyword')->select();
  76. $list = count($list) > 0 ? $list->toArray() : [];
  77. return JsonService::successful($list);
  78. }
  79. /**商品详情
  80. * @param int $id
  81. * @return mixed|void
  82. */
  83. public function detail($id = 0)
  84. {
  85. if (!$id) return $this->failed('参数错误!', Url::build('store/index'));
  86. $storeInfo = StoreProduct::getValidProduct($id);
  87. if (!$storeInfo) return $this->failed('商品不存在或已下架!', Url::build('store/index'));
  88. $site_url = SystemConfigService::get('site_url') . Url::build('store/detail') . '?id=' . $id . '&spread_uid=' . $this->uid;
  89. $this->assign(['storeInfo' => $storeInfo, 'site_url' => $site_url]);
  90. return $this->fetch();
  91. }
  92. /**获取关联专题
  93. * @param int $id
  94. */
  95. public function getAssociatedTopics($id = 0, $page = 1, $list = 10)
  96. {
  97. if (!$id) return JsonService::fail('参数错误!');
  98. $data = Relation::getRelationSpecial(5, $id, $page, $list);
  99. foreach ($data as $key => &$item) {
  100. if (is_string($item['label'])) $item['label'] = json_decode($item['label'], true);
  101. }
  102. return JsonService::successful($data);
  103. }
  104. }