NotifyController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace addons\TinyShop\api\modules\v1\controllers\member;
  3. use Yii;
  4. use yii\data\ActiveDataProvider;
  5. use yii\web\NotFoundHttpException;
  6. use api\controllers\UserAuthController;
  7. use common\enums\StatusEnum;
  8. use common\enums\NotifyTypeEnum;
  9. use addons\TinyShop\common\models\common\NotifyMember;
  10. /**
  11. * Class NotifyController
  12. * @package addons\TinyShop\api\modules\v1\controllers\member
  13. * @author jianyan74 <751393839@qq.com>
  14. */
  15. class NotifyController extends UserAuthController
  16. {
  17. /**
  18. * @var NotifyMember
  19. */
  20. public $modelClass = NotifyMember::class;
  21. /**
  22. * @return array
  23. */
  24. public function actionIndex()
  25. {
  26. $type = Yii::$app->request->post('type', NotifyTypeEnum::REMIND);
  27. $member_id = Yii::$app->user->identity->member_id;
  28. $chat = [];
  29. $unReadCount = 0;
  30. if (Yii::$app->has('tinyChatService')) {
  31. $chat = Yii::$app->tinyChatService->conversation->findNewestByMemberId($member_id);
  32. $unReadCount = Yii::$app->tinyChatService->bubble->unReadCount($member_id);
  33. }
  34. return [
  35. 'chat' => [
  36. 'newest' => $chat,
  37. 'unReadCount' => $unReadCount
  38. ],
  39. 'list' => new ActiveDataProvider([
  40. 'query' => $this->modelClass::find()
  41. ->where(['status' => StatusEnum::ENABLED])
  42. ->andWhere(['type' => $type, 'member_id' => $member_id])
  43. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  44. ->with(['notify'])
  45. ->orderBy('id desc')
  46. ->asArray(),
  47. 'pagination' => [
  48. 'pageSize' => $this->pageSize,
  49. 'validatePage' => false,// 超出分页不返回data
  50. ],
  51. ]),
  52. ];
  53. }
  54. /**
  55. * @return array
  56. */
  57. public function actionNewest()
  58. {
  59. $memberId = Yii::$app->user->identity->member_id;
  60. $unReadCount = Yii::$app->tinyShopService->notifyMember->unReadCount($memberId);
  61. $newest = Yii::$app->tinyShopService->notifyMember->newest($memberId);
  62. return [
  63. 'newest' => $newest,
  64. 'unReadCount' => $unReadCount
  65. ];
  66. }
  67. /**
  68. * @param $id
  69. * @return \yii\db\ActiveRecord
  70. * @throws NotFoundHttpException
  71. */
  72. public function actionView($id)
  73. {
  74. $member_id = Yii::$app->user->identity->member_id;
  75. /* @var $model \yii\db\ActiveRecord */
  76. if (empty($id) || !($model = $this->modelClass::find()->where([
  77. 'id' => $id,
  78. 'status' => StatusEnum::ENABLED,
  79. 'member_id' => $member_id
  80. ])->andFilterWhere(['merchant_id' => $this->getMerchantId()])->with(['notify'])->asArray()->one())) {
  81. throw new NotFoundHttpException('请求的数据不存在');
  82. }
  83. // 设置为已读
  84. Yii::$app->tinyShopService->notifyMember->read($member_id, [$model['notify_id']]);
  85. return $model;
  86. }
  87. /**
  88. * 未读数量
  89. *
  90. * @return mixed
  91. */
  92. public function actionUnReadCount()
  93. {
  94. $member_id = Yii::$app->user->identity->member_id;
  95. return Yii::$app->tinyShopService->notifyMember->unReadCount($member_id);
  96. }
  97. /**
  98. * 单个已读
  99. *
  100. * @param $notify_id
  101. */
  102. public function actionRead($notify_id)
  103. {
  104. // 设置为已读
  105. $member_id = Yii::$app->user->identity->member_id;
  106. Yii::$app->tinyShopService->notifyMember->read($member_id, [$notify_id]);
  107. return true;
  108. }
  109. /**
  110. * 删除多个
  111. *
  112. * @return mixed
  113. */
  114. public function actionClear()
  115. {
  116. $notify_ids = Yii::$app->request->post('notify_ids');
  117. $notify_ids = explode(',', $notify_ids);
  118. $member_id = Yii::$app->user->identity->member_id;
  119. Yii::$app->tinyShopService->notifyMember->clear($member_id, $notify_ids);
  120. return true;
  121. }
  122. /**
  123. * 清空
  124. *
  125. * @return mixed
  126. */
  127. public function actionClearAll()
  128. {
  129. $type = Yii::$app->request->post('type', NotifyTypeEnum::REMIND);
  130. $member_id = Yii::$app->user->identity->member_id;
  131. Yii::$app->tinyShopService->notifyMember->clearAll($member_id, $type);
  132. return true;
  133. }
  134. /**
  135. * 全部已读
  136. *
  137. * @return mixed
  138. */
  139. public function actionReadAll()
  140. {
  141. // 设置为已读
  142. $member_id = Yii::$app->user->identity->member_id;
  143. Yii::$app->tinyShopService->notifyMember->readAll($member_id);
  144. return true;
  145. }
  146. }
粤ICP备19079148号