You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.1 KiB

  1. <?php
  2. namespace api\controllers;
  3. use api\logic\AddressLogic;
  4. use api\logic\CartLogic;
  5. use api\logic\CollectionLogic;
  6. use api\logic\CommentLogic;
  7. use api\logic\OrderLogic;
  8. use api\logic\WxPaymentLogic;
  9. use yii\filters\auth\HttpBearerAuth;
  10. use yii\helpers\ArrayHelper;
  11. use yii\rest\ActiveController;
  12. use yii\web\NotFoundHttpException;
  13. use yii\base\InvalidConfigException;
  14. use Yii;
  15. /**
  16. * @author iron
  17. * @email weiriron@gmail.com
  18. */
  19. class CommonController extends ActiveController
  20. {
  21. public $className;
  22. /**
  23. * @var OrderLogic|CommentLogic|CartLogic|CollectionLogic|AddressLogic|WxPaymentLogic;
  24. */
  25. public $object;
  26. /**
  27. * @throws InvalidConfigException
  28. */
  29. public function init()
  30. {
  31. parent::init();
  32. $this->object = Yii::createObject([
  33. 'class' => $this->className,
  34. ]);
  35. }
  36. /**
  37. * @return array
  38. */
  39. public function behaviors()
  40. {
  41. return ArrayHelper::merge(parent::behaviors(), [
  42. 'authenticatior' => [
  43. 'class' => HttpBearerAuth::className(),
  44. 'except' => ['token'],
  45. ]
  46. ]);
  47. }
  48. public function actions()
  49. {
  50. $action = parent::actions();
  51. unset($action['create']);
  52. unset($action['update']);
  53. unset($action['index']);
  54. return $action;
  55. }
  56. protected function getFilter()
  57. {
  58. return [];
  59. }
  60. /**
  61. * @param string $action
  62. * @param null $model
  63. * @param array $params
  64. * @throws NotFoundHttpException
  65. * 权限控制
  66. */
  67. public function checkAccess($action, $model = null, $params = [])
  68. {
  69. if ($model && isset($model->user_id) && $model->user_id !== Yii::$app->user->getId()) {
  70. switch ($action) {
  71. case 'view':
  72. $message = '您无权访问该数据';
  73. break;
  74. case 'delete':
  75. $message = '您无权删除该数据';
  76. break;
  77. default:
  78. $message = '无相关权限';
  79. }
  80. throw new NotFoundHttpException($message);
  81. }
  82. }
  83. }