BaseController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace business\controllers;
  3. use biz\merchant\services\AdminService;
  4. use biz\merchant\services\MerchantExtendService;
  5. use biz\merchant\services\MerchantService;
  6. use common\components\httpUtil;
  7. use common\components\jwt;
  8. use Yii;
  9. use common\components\util;
  10. class BaseController extends PublicController
  11. {
  12. public $admin, $adminId, $adminAvatar, $account;
  13. public $merchantId = 0, $merchantName, $merchant, $merchantExtend, $merchantLogo, $signPackage;
  14. public $openMerchant, $openMerchantId;
  15. public $appId;
  16. public $isWx = false;
  17. public $isLogin = false;
  18. public $withoutLogin = [];//不需要登陆的方法名
  19. public $validate = true;
  20. public function beforeAction($action)
  21. {
  22. //token验证
  23. $adminId = jwt::getLoginId();
  24. if (empty($adminId)) {
  25. //本地环境请求接口没有登陆用户时设置一个默认管理员
  26. $env = getenv('YII_ENV', 'local');
  27. if (in_array($env, ['local', 'dev'])) {
  28. $adminId = 6;
  29. } else {
  30. $this->validate = false;
  31. }
  32. }
  33. //游客可以访问的方法
  34. if (in_array($action->id, $this->withoutLogin)) {
  35. $this->validate = true;
  36. }
  37. if ($this->validate == false) {
  38. util::notLogin();
  39. }
  40. if (httpUtil::isWx()) {
  41. //授权方式获取客户信息 xhAdmin userId存的是每个公众号店长的微信userId
  42. $admin = AdminService::getByCondition(['userId' => $adminId]);
  43. } else {
  44. $admin = AdminService::getById($adminId);
  45. }
  46. if (empty($admin)) {
  47. util::fail('没有找到管理员,但已经获取客户ID:' . $adminId);
  48. }
  49. $merchantId = $admin['merchantId'];
  50. if (empty($merchantId)) {
  51. util::fail('没有找到商家信息,但有管理员ID:' . $adminId);
  52. }
  53. $merchant = MerchantService::getMerchantById($merchantId);
  54. $merchantExtend = MerchantExtendService::getByMerchantId($merchantId);
  55. $this->admin = $admin;
  56. $this->adminId = $adminId;
  57. $this->merchantId = $merchantId;
  58. $this->merchant = $merchant;
  59. $this->merchantExtend = $merchantExtend;
  60. return parent::beforeAction($action);
  61. }
  62. public
  63. function actionList()
  64. {
  65. $numargs = func_num_args();
  66. $select = $numargs >= 1 ? func_get_arg(0) : '*';
  67. $where = $numargs >= 2 ? func_get_arg(1) : null;
  68. $order = $numargs >= 3 ? func_get_arg(2) : '';
  69. $with = $numargs >= 4 ? func_get_arg(3) : '';
  70. $service = $this->service;
  71. $list = $service::getList($select, $where, $order, $with);
  72. util::success($list);
  73. }
  74. public
  75. function actionAdd()
  76. {
  77. $data = Yii::$app->request->post();
  78. $service = $this->service;
  79. $re = $service::add($data);
  80. util::success($re);
  81. }
  82. public
  83. function actionDetail()
  84. {
  85. $id = Yii::$app->request->get('id');
  86. $service = $this->service;
  87. $data = $service::getById($id);
  88. if (isset($data['merchantId']) == false || $this->merchantId != $data['merchantId']) {
  89. util::fail('没有权限');
  90. }
  91. if (!empty($data)) {
  92. util::success($data);
  93. }
  94. util::fail('');
  95. }
  96. public
  97. function actionUpdate()
  98. {
  99. $data = Yii::$app->request->post();
  100. $id = $data['id'];
  101. $service = $this->service;
  102. $re = $service::updateById($id, $data);
  103. if ($re['status'] === true) {
  104. util::success($re);
  105. }
  106. util::fail();
  107. }
  108. public
  109. function actionDelete()
  110. {
  111. $id = Yii::$app->request->get('id');
  112. $service = $this->service;
  113. $service::deleteById($id);
  114. util::success([], '删除成功');
  115. }
  116. }