CouponController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace addons\TinyShop\api\modules\v1\controllers\member;
  3. use Yii;
  4. use yii\web\NotFoundHttpException;
  5. use yii\data\ActiveDataProvider;
  6. use api\controllers\UserAuthController;
  7. use addons\TinyShop\common\models\marketing\Coupon;
  8. use common\enums\StatusEnum;
  9. use common\enums\UseStateEnum;
  10. /**
  11. * 我的优惠券
  12. *
  13. * Class CouponController
  14. * @package addons\TinyShop\api\controllers\member
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class CouponController extends UserAuthController
  18. {
  19. /**
  20. * @var Coupon
  21. */
  22. public $modelClass = Coupon::class;
  23. /**
  24. * @return array
  25. */
  26. public function actionIndex()
  27. {
  28. $member_id = Yii::$app->user->identity->member_id;
  29. $state = Yii::$app->request->get('state', UseStateEnum::GET);
  30. switch ($state) {
  31. case UseStateEnum::GET :
  32. $where = [
  33. 'and',
  34. ['member_id' => Yii::$app->user->identity->member_id],
  35. ['state' => $state],
  36. ['status' => StatusEnum::ENABLED],
  37. ['between', 'start_time', 'end_time', time()],
  38. ];
  39. $orderBy = 'fetch_time desc, id desc';
  40. break;
  41. case UseStateEnum::PAST_DUE :
  42. $where = [
  43. 'and',
  44. ['member_id' => Yii::$app->user->identity->member_id],
  45. ['status' => StatusEnum::ENABLED],
  46. [
  47. 'or',
  48. ['state' => $state],
  49. [
  50. 'and',
  51. ['state' => UseStateEnum::GET],
  52. ['<', 'end_time', time()],
  53. ],
  54. ],
  55. ];
  56. $orderBy = 'end_time desc, id desc';
  57. break;
  58. default :
  59. $where = [
  60. 'and',
  61. ['member_id' => Yii::$app->user->identity->member_id],
  62. ['state' => $state],
  63. ['status' => StatusEnum::ENABLED],
  64. ];
  65. $orderBy = 'use_time desc, id desc';
  66. break;
  67. }
  68. $data = new ActiveDataProvider([
  69. 'query' => $this->modelClass::find()
  70. ->where($where)
  71. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  72. ->with(['couponType', 'baseMerchant'])
  73. ->orderBy($orderBy)
  74. ->asArray(),
  75. 'pagination' => [
  76. 'pageSize' => $this->pageSize,
  77. 'validatePage' => false,// 超出分页不返回data
  78. ],
  79. ]);
  80. return [
  81. 'list' => $data->getModels(),
  82. 'groupCount' => Yii::$app->tinyShopService->marketingCoupon->findStateCount($member_id),
  83. ];
  84. }
  85. /**
  86. * @param $id
  87. * @return \yii\db\ActiveRecord
  88. * @throws NotFoundHttpException
  89. */
  90. public function actionView($id)
  91. {
  92. /* @var $model \yii\db\ActiveRecord */
  93. if (empty($id) || !($model = $this->modelClass::find()
  94. ->where([
  95. 'id' => $id,
  96. 'status' => StatusEnum::ENABLED,
  97. ])
  98. ->with(['couponType', 'baseMerchant'])
  99. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  100. ->asArray()
  101. ->one())
  102. ) {
  103. throw new NotFoundHttpException('请求的数据不存在');
  104. }
  105. return $model;
  106. }
  107. /**
  108. * 清空已过期的优惠券
  109. *
  110. * @param $member_id
  111. */
  112. public function actionClear()
  113. {
  114. return Coupon::updateAll(['status' => StatusEnum::DELETE], [
  115. 'member_id' => Yii::$app->user->identity->member_id,
  116. 'status' => StatusEnum::ENABLED,
  117. 'state' => UseStateEnum::PAST_DUE
  118. ]);
  119. }
  120. /**
  121. * 权限验证
  122. *
  123. * @param string $action 当前的方法
  124. * @param null $model 当前的模型类
  125. * @param array $params $_GET变量
  126. * @throws \yii\web\BadRequestHttpException
  127. */
  128. public function checkAccess($action, $model = null, $params = [])
  129. {
  130. // 方法名称
  131. if (in_array($action, ['delete', 'update', 'create'])) {
  132. throw new \yii\web\BadRequestHttpException('权限不足');
  133. }
  134. }
  135. }
粤ICP备19079148号