FootprintController.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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\models\member\Footprint;
  9. use addons\TinyShop\common\forms\ProductSearchForm;
  10. /**
  11. * 足迹
  12. *
  13. * Class FootprintController
  14. * @package addons\TinyShop\api\modules\v1\controllers\member
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class FootprintController extends UserAuthController
  18. {
  19. /**
  20. * @var Footprint
  21. */
  22. public $modelClass = Footprint::class;
  23. /**
  24. * @return array|ActiveDataProvider|\yii\db\ActiveRecord[]
  25. */
  26. public function actionIndex()
  27. {
  28. $start_time = Yii::$app->request->get('start_time');
  29. $end_time = Yii::$app->request->get('end_time');
  30. $data = new ActiveDataProvider([
  31. 'query' => $this->modelClass::find()
  32. ->select(['id', 'product_id', 'updated_at'])
  33. ->where(['status' => StatusEnum::ENABLED, 'member_id' => Yii::$app->user->identity->member_id])
  34. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  35. ->andFilterWhere(['>', 'updated_at', $start_time])
  36. ->andFilterWhere(['<', 'updated_at', $end_time])
  37. ->orderBy('updated_at desc')
  38. ->asArray(),
  39. 'pagination' => [
  40. 'pageSize' => $this->pageSize,
  41. 'validatePage' => false,// 超出分页不返回data
  42. ],
  43. ]);
  44. $models = $data->getModels();
  45. $productIds = ArrayHelper::getColumn($models, 'product_id');
  46. if (empty($productIds)) {
  47. return [];
  48. }
  49. // 查询商品
  50. $model = new ProductSearchForm();
  51. $model->ids = $productIds;
  52. $model->current_level = Yii::$app->tinyShopService->member->getCurrentLevel(Yii::$app->user->identity->member_id);
  53. $products = Yii::$app->tinyShopService->product->getListBySearch($model);
  54. $products = ArrayHelper::arrayKey($products, 'id');
  55. // 重新排序
  56. foreach ($models as &$model) {
  57. unset($model['id']);
  58. $model['created_at'] = $model['updated_at'];
  59. isset($products[$model['product_id']]) && $model = ArrayHelper::merge($model, $products[$model['product_id']]);
  60. }
  61. return $models;
  62. }
  63. }
粤ICP备19079148号