AppserverTokenController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\app\appserver\modules;
  10. use fec\controllers\FecController;
  11. use fec\helpers\CConfig;
  12. use Yii;
  13. use yii\web\Response;
  14. use yii\rest\Controller;
  15. use yii\base\InvalidValueException;
  16. use yii\filters\auth\CompositeAuth;
  17. use yii\filters\auth\HttpBasicAuth;
  18. use yii\filters\auth\HttpBearerAuth;
  19. use fecshop\yii\filters\auth\QueryParamAuth;
  20. use yii\filters\RateLimiter;
  21. /**
  22. * @author Terry Zhao <2358269014@qq.com>
  23. * @since 1.0
  24. */
  25. class AppserverTokenController extends Controller
  26. {
  27. public $blockNamespace;
  28. public $enableCsrfValidation = false ;
  29. public function init()
  30. {
  31. Yii::$service->page->translate->category = 'appserver';
  32. parent::init();
  33. // \Yii::$app->user->enableSession = false;
  34. }
  35. public function behaviors()
  36. {
  37. $behaviors = parent::behaviors();
  38. $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
  39. $behaviors["corsFilter"] = [
  40. 'class' => \yii\filters\Cors::className(),
  41. 'cors' => Yii::$service->helper->appserver->getCors(),
  42. ];
  43. $behaviors['authenticator'] = [
  44. 'class' => CompositeAuth::className(),
  45. 'authMethods' => [
  46. # 下面是三种验证access_token方式
  47. //HttpBasicAuth::className(),
  48. //HttpBearerAuth::className(),
  49. # 这是GET参数验证的方式
  50. # http://10.10.10.252:600/user/index/index?access-token=xxxxxxxxxxxxxxxxxxxx
  51. QueryParamAuth::className(),
  52. ],
  53. ];
  54. # rate limit部分,速度的设置是在
  55. # \myapp\code\core\Erp\User\models\User::getRateLimit($request, $action){
  56. /* 官方文档:
  57. 当速率限制被激活,默认情况下每个响应将包含以下HTTP头发送 目前的速率限制信息:
  58. X-Rate-Limit-Limit: 同一个时间段所允许的请求的最大数目;
  59. X-Rate-Limit-Remaining: 在当前时间段内剩余的请求的数量;
  60. X-Rate-Limit-Reset: 为了得到最大请求数所等待的秒数。
  61. 你可以禁用这些头信息通过配置 yii\filters\RateLimiter::enableRateLimitHeaders 为false, 就像在上面的代码示例所示。
  62. */
  63. $rateLimit = Yii::$app->params['rateLimit'];
  64. if(isset($rateLimit['enable']) && $rateLimit['enable']){
  65. $behaviors['rateLimiter'] = [
  66. 'class' => RateLimiter::className(),
  67. 'enableRateLimitHeaders' => true,
  68. ];
  69. }
  70. return $behaviors;
  71. }
  72. /**
  73. * get current block
  74. * you can change $this->blockNamespace.
  75. */
  76. public function getBlock($blockName = '')
  77. {
  78. if (!$blockName) {
  79. $blockName = $this->action->id;
  80. }
  81. if (!$this->blockNamespace) {
  82. $this->blockNamespace = Yii::$app->controller->module->blockNamespace;
  83. }
  84. if (!$this->blockNamespace) {
  85. throw new \yii\web\HttpException(406, 'blockNamespace is empty , you should config it in module->blockNamespace or controller blockNamespace ');
  86. }
  87. $viewId = $this->id;
  88. $viewId = str_replace('/', '\\', $viewId);
  89. $relativeFile = '\\'.$this->blockNamespace;
  90. $relativeFile .= '\\'.$viewId.'\\'.ucfirst($blockName);
  91. //查找是否在rewriteMap中存在重写
  92. $relativeFile = Yii::mapGetName($relativeFile);
  93. return new $relativeFile();
  94. }
  95. }