FollowController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace addons\TinyShop\api\modules\v1\controllers\common;
  3. use Yii;
  4. use yii\base\Model;
  5. use yii\web\UnprocessableEntityHttpException;
  6. use api\controllers\OnAuthController;
  7. use common\enums\StatusEnum;
  8. use common\helpers\ResultHelper;
  9. use addons\TinyShop\common\enums\CommonModelMapEnum;
  10. use addons\TinyShop\common\models\common\Collect;
  11. /**
  12. * Class FollowController
  13. * @package addons\TinyShop\api\modules\v1\controllers\common
  14. * @author jianyan74 <751393839@qq.com>
  15. */
  16. abstract class FollowController extends OnAuthController
  17. {
  18. /**
  19. * @return array|mixed|\yii\db\ActiveRecord|null
  20. */
  21. public function actionCreate()
  22. {
  23. $topic_id = Yii::$app->request->post('topic_id');
  24. $topic_type = Yii::$app->request->post('topic_type');
  25. /** @var Model $class */
  26. if (!($class = CommonModelMapEnum::getValue($topic_type))) {
  27. return ResultHelper::json(422, '找不到可用的类型');
  28. }
  29. $model = $this->findByTopicId($topic_id, $topic_type);
  30. if ($model->status == StatusEnum::ENABLED) {
  31. return ResultHelper::json(422, '请不要重复操作');
  32. }
  33. // 未登陆直接回调
  34. if (Yii::$app->user->isGuest) {
  35. $this->callBack($model, $class, 1);
  36. return $model;
  37. }
  38. // 开始事物
  39. $transaction = Yii::$app->db->beginTransaction();
  40. try {
  41. $model->attributes = Yii::$app->request->post();
  42. $model->member_id = Yii::$app->user->identity->member_id ?? 0;
  43. $model->status = StatusEnum::ENABLED;
  44. if (!$model->save()) {
  45. // 返回数据验证失败
  46. return ResultHelper::json(422, $this->getError($model));
  47. }
  48. // 收藏回调
  49. $this->callBack($model, $class, 1);
  50. $transaction->commit();
  51. } catch (\Exception $e) {
  52. $transaction->rollBack();
  53. // 返回数据验证失败
  54. return ResultHelper::json(422, $e->getMessage());
  55. }
  56. return $model;
  57. }
  58. /**
  59. * 取消收藏
  60. *
  61. * @param $id
  62. * @return bool|mixed
  63. * @throws \Throwable
  64. */
  65. public function actionDelete($id)
  66. {
  67. /** @var Collect $model */
  68. $model = $this->findById($id);
  69. if (!$model) {
  70. return ResultHelper::json(422, '找不到可用的类型');
  71. }
  72. /** @var Model $class */
  73. if (!($class = CommonModelMapEnum::getValue($model->topic_type))) {
  74. return ResultHelper::json(422, '找不到可用的类型');
  75. }
  76. // 开始事物
  77. $transaction = Yii::$app->db->beginTransaction();
  78. try {
  79. if ($model->status == StatusEnum::DELETE) {
  80. throw new UnprocessableEntityHttpException('请不要重复操作');
  81. }
  82. $model->status = StatusEnum::DELETE;
  83. $model->save();
  84. // 回调
  85. $this->callBack($model, $class, -1);
  86. $transaction->commit();
  87. } catch (\Exception $e) {
  88. $transaction->rollBack();
  89. // 返回数据验证失败
  90. return ResultHelper::json(422, $e->getMessage());
  91. }
  92. return true;
  93. }
  94. /**
  95. * 权限验证
  96. *
  97. * @param string $action 当前的方法
  98. * @param null $model 当前的模型类
  99. * @param array $params $_GET变量
  100. * @throws \yii\web\BadRequestHttpException
  101. */
  102. public function checkAccess($action, $model = null, $params = [])
  103. {
  104. // 方法名称
  105. if (in_array($action, ['index', 'update', 'view'])) {
  106. throw new \yii\web\BadRequestHttpException('权限不足');
  107. }
  108. }
  109. /**
  110. * @param $topic_id
  111. * @param $topic_type
  112. * @return mixed
  113. */
  114. abstract function findByTopicId($topic_id, $topic_type);
  115. /**
  116. * @param $id
  117. * @return mixed
  118. */
  119. abstract function findById($id);
  120. /**
  121. * @param $model
  122. * @param $class
  123. * @param $num
  124. * @return mixed
  125. */
  126. abstract function callBack($model, $class, $num);
  127. }
粤ICP备19079148号