CollectController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace addons\TinyShop\api\modules\v1\controllers\member;
  3. use Yii;
  4. use yii\data\ActiveDataProvider;
  5. use common\enums\StatusEnum;
  6. use common\helpers\ArrayHelper;
  7. use api\controllers\UserAuthController;
  8. use addons\TinyShop\common\enums\CommonModelMapEnum;
  9. use addons\TinyShop\common\models\common\Collect;
  10. use addons\TinyShop\common\forms\ProductSearchForm;
  11. /**
  12. * 我的收藏
  13. *
  14. * Class CollectController
  15. * @package addons\TinyShop\api\modules\v1\controllers\member
  16. * @author jianyan74 <751393839@qq.com>
  17. */
  18. class CollectController extends UserAuthController
  19. {
  20. /**
  21. * @var Collect
  22. */
  23. public $modelClass = Collect::class;
  24. /**
  25. * @return array|ActiveDataProvider
  26. */
  27. public function actionIndex()
  28. {
  29. $topic_type = CommonModelMapEnum::PRODUCT;
  30. $data = new ActiveDataProvider([
  31. 'query' => $this->modelClass::find()
  32. ->select(['id', 'topic_id', 'updated_at'])
  33. ->where(['status' => StatusEnum::ENABLED, 'member_id' => Yii::$app->user->identity->member_id])
  34. ->andWhere(['topic_type' => $topic_type])
  35. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  36. ->orderBy('updated_at desc')
  37. ->asArray(),
  38. 'pagination' => [
  39. 'pageSize' => $this->pageSize,
  40. 'validatePage' => false,// 超出分页不返回data
  41. ],
  42. ]);
  43. $models = $data->getModels();
  44. $productIds = ArrayHelper::getColumn($models, 'topic_id');
  45. if (empty($productIds)) {
  46. return [];
  47. }
  48. // 查询商品
  49. $model = new ProductSearchForm();
  50. $model->ids = $productIds;
  51. $model->current_level = Yii::$app->tinyShopService->member->getCurrentLevel(Yii::$app->user->identity->member_id);
  52. $products = Yii::$app->tinyShopService->product->getListBySearch($model);
  53. $products = ArrayHelper::arrayKey($products, 'id');
  54. // 重新排序
  55. foreach ($models as &$model) {
  56. unset($model['id']);
  57. $model['created_at'] = $model['updated_at'];
  58. isset($products[$model['topic_id']]) && $model = ArrayHelper::merge($model, $products[$model['topic_id']]);
  59. }
  60. return $models;
  61. }
  62. /**
  63. * 首页
  64. *
  65. * @return ActiveDataProvider
  66. */
  67. public function actionMerchant()
  68. {
  69. $topic_type = CommonModelMapEnum::MERCHANT;
  70. $data = new ActiveDataProvider([
  71. 'query' => $this->modelClass::find()
  72. ->where(['status' => StatusEnum::ENABLED, 'member_id' => Yii::$app->user->identity->member_id])
  73. ->andWhere(['topic_type' => $topic_type])
  74. ->orderBy('updated_at desc')
  75. ->asArray(),
  76. 'pagination' => [
  77. 'pageSize' => $this->pageSize,
  78. 'validatePage' => false,// 超出分页不返回data
  79. ],
  80. ]);
  81. $models = $data->getModels();
  82. foreach ($models as &$model) {
  83. $model['merchant'] = Yii::$app->services->merchant->findBaseById($model['topic_id']);
  84. $form = new ProductSearchForm();
  85. $form->merchant_id = $model['topic_id'];
  86. $form->limit = 5;
  87. $model['products'] = Yii::$app->tinyShopService->product->getListBySearch($form);
  88. $model['productTotal'] = Yii::$app->tinyShopService->product->findSellCount($form->merchant_id) ?? 0;
  89. }
  90. $data->setModels($models);
  91. return $data;
  92. }
  93. }
粤ICP备19079148号