BaseController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. $admin = AdminService::getById($adminId);
  41. if (empty($admin)) {
  42. util::fail('没有找到管理员,但已经获取客户ID:' . $adminId);
  43. }
  44. $merchantId = $admin['merchantId'];
  45. if (empty($merchantId)) {
  46. util::fail('没有找到商家信息,但有管理员ID:' . $adminId);
  47. }
  48. $merchant = MerchantService::getMerchantById($merchantId);
  49. $merchantExtend = MerchantExtendService::getByMerchantId($merchantId);
  50. $this->admin = $admin;
  51. $this->adminId = $adminId;
  52. $this->merchantId = $merchantId;
  53. $this->merchant = $merchant;
  54. $this->merchantExtend = $merchantExtend;
  55. return parent::beforeAction($action);
  56. }
  57. public
  58. function actionList()
  59. {
  60. $numargs = func_num_args();
  61. $select = $numargs >= 1 ? func_get_arg(0) : '*';
  62. $where = $numargs >= 2 ? func_get_arg(1) : null;
  63. $order = $numargs >= 3 ? func_get_arg(2) : '';
  64. $with = $numargs >= 4 ? func_get_arg(3) : '';
  65. $service = $this->service;
  66. $list = $service::getList($select, $where, $order, $with);
  67. util::success($list);
  68. }
  69. public
  70. function actionAdd()
  71. {
  72. $data = Yii::$app->request->post();
  73. $service = $this->service;
  74. $re = $service::add($data);
  75. util::success($re);
  76. }
  77. public
  78. function actionDetail()
  79. {
  80. $id = Yii::$app->request->get('id');
  81. $service = $this->service;
  82. $data = $service::getById($id);
  83. if (isset($data['merchantId']) == false || $this->merchantId != $data['merchantId']) {
  84. util::fail('没有权限');
  85. }
  86. if (!empty($data)) {
  87. util::success($data);
  88. }
  89. util::fail('');
  90. }
  91. public
  92. function actionUpdate()
  93. {
  94. $data = Yii::$app->request->post();
  95. $id = $data['id'];
  96. $service = $this->service;
  97. $re = $service::updateById($id, $data);
  98. if ($re['status'] === true) {
  99. util::success($re);
  100. }
  101. util::fail();
  102. }
  103. public
  104. function actionDelete()
  105. {
  106. $id = Yii::$app->request->get('id');
  107. $service = $this->service;
  108. $service::deleteById($id);
  109. util::success([], '删除成功');
  110. }
  111. }