BaseController.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * User: shish <479439056@qq.com>
  4. * Date: 2019/6/29
  5. * Time: 13:07
  6. */
  7. namespace mini\controllers;
  8. use biz\user\services\UserService;
  9. use common\components\util;
  10. use common\services\xhMerchantService;
  11. use Yii;
  12. use yii\web\Controller;
  13. class BaseController extends Controller
  14. {
  15. public $merchantId, $merchant, $userId = 0, $userInfo = [], $isLogin = false;
  16. public $withoutLogin = [];//不需要登陆的方法名
  17. public function beforeAction($action)
  18. {
  19. //获取token
  20. $ignore = ['host', 'accept', 'content-length', 'content-type'];
  21. $headers = [];
  22. foreach ($_SERVER as $key => $value) {
  23. if (substr($key, 0, 5) === 'HTTP_') {
  24. $key = substr($key, 5);
  25. $key = str_replace('_', ' ', $key);
  26. $key = str_replace(' ', '-', $key);
  27. $key = strtolower($key);
  28. if (!in_array($key, $ignore)) {
  29. $headers[$key] = $value;
  30. }
  31. }
  32. }
  33. $token = isset($headers['token']) ? $headers['token'] : '';
  34. $account = isset($headers['account']) && !empty($headers['account']) ? $headers['account'] : 0;
  35. $userId = Yii::$app->redis->executeCommand('GET', [$token]);
  36. if (empty($account)) {
  37. $account = Yii::$app->request->get('account', 0);
  38. }
  39. if (empty($account)) {
  40. util::sendFail('没有店铺信息');
  41. }
  42. $merchant = xhMerchantService::getByAccount($account);
  43. if (empty($merchant)) {
  44. util::sendFail('没有找到店铺信息');
  45. }
  46. if (in_array($action->id, $this->withoutLogin) == false) {
  47. if (empty($userId)) {
  48. util::sendFail('您没有权限访问');
  49. }
  50. }
  51. if (!empty($userId)) {
  52. $userInfo = UserService::getById($userId);
  53. $this->userInfo = $userInfo;
  54. Yii::$app->params['userInfo'] = $userInfo;
  55. }
  56. $this->userId = $userId;
  57. Yii::$app->params['userId'] = $userId;
  58. //全局变量设置
  59. Yii::$app->params['merchantId'] = $account;
  60. Yii::$app->params['merchant'] = $merchant;
  61. $page = Yii::$app->request->get('page', 1);
  62. Yii::$app->params['page'] = $page;
  63. //获取头信息里的token
  64. $ignore = ['host', 'accept', 'content-length', 'content-type'];
  65. $headers = [];
  66. foreach ($_SERVER as $key => $value) {
  67. if (substr($key, 0, 5) === 'HTTP_') {
  68. $key = substr($key, 5);
  69. $key = str_replace('_', ' ', $key);
  70. $key = str_replace(' ', '-', $key);
  71. $key = strtolower($key);
  72. if (!in_array($key, $ignore)) {
  73. $headers[$key] = $value;
  74. }
  75. }
  76. }
  77. $token = isset($headers['token']) ? $headers['token'] : '';
  78. //小程序端默认的输出方式是json
  79. Yii::$app->params['exportStyle'] = 'json';
  80. $this->merchant = $merchant;
  81. $this->merchantId = $account;
  82. return parent::beforeAction($action);
  83. }
  84. }