BaseController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace business\controllers;
  3. use biz\merchant\services\AdminService;
  4. use biz\merchant\services\MerchantAssetService;
  5. use biz\merchant\services\MerchantExtendService;
  6. use biz\merchant\services\MerchantService;
  7. use common\components\httpUtil;
  8. use common\components\jwt;
  9. use Yii;
  10. use common\components\util;
  11. class BaseController extends PublicController
  12. {
  13. public $admin, $adminId, $adminAvatar, $account;
  14. public $merchantId = 0, $merchantName, $merchant, $merchantExtend, $merchantAsset, $merchantLogo, $signPackage;
  15. public $openMerchant, $openMerchantId;
  16. public $appId;
  17. public $isWx = false;
  18. public $isLogin = false;
  19. public $withoutLogin = [];//不需要登陆的方法名
  20. public $validate = true;
  21. public function beforeAction($action)
  22. {
  23. //token验证
  24. $adminId = jwt::getLoginId();
  25. if (empty($adminId)) {
  26. //本地环境请求接口没有登陆用户时设置一个默认管理员
  27. if (getenv('YII_ENV') == 'local') {
  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. $merchantAsset = MerchantAssetService::getByMerchantId($merchantId);
  56. $this->admin = $admin;
  57. $this->adminId = $adminId;
  58. $this->merchantId = $merchantId;
  59. $this->merchant = $merchant;
  60. $this->merchantExtend = $merchantExtend;
  61. $this->merchantAsset = $merchantAsset;
  62. return parent::beforeAction($action);
  63. }
  64. public function actionList()
  65. {
  66. $numargs = func_num_args();
  67. $select = $numargs >= 1 ? func_get_arg(0) : '*';
  68. $where = $numargs >= 2 ? func_get_arg(1) : null;
  69. $order = $numargs >= 3 ? func_get_arg(2) : '';
  70. $with = $numargs >= 4 ? func_get_arg(3) : '';
  71. $service = $this->service;
  72. $list = $service::getList($select, $where, $order, $with);
  73. util::success($list);
  74. }
  75. public 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 function actionDetail()
  83. {
  84. $id = Yii::$app->request->get('id');
  85. $service = $this->service;
  86. $data = $service::getById($id);
  87. if (isset($data['merchantId']) == false || $this->merchantId != $data['merchantId']) {
  88. util::fail('没有权限');
  89. }
  90. if (!empty($data)) {
  91. util::success($data);
  92. }
  93. util::fail('');
  94. }
  95. public function actionUpdate()
  96. {
  97. $data = Yii::$app->request->post();
  98. $id = $data['id'];
  99. $service = $this->service;
  100. $re = $service::updateById($id, $data);
  101. if ($re['status'] === true) {
  102. util::success($re);
  103. }
  104. util::fail();
  105. }
  106. public function actionDelete()
  107. {
  108. $id = Yii::$app->request->get('id');
  109. $service = $this->service;
  110. $service::deleteById($id);
  111. util::success([], '删除成功');
  112. }
  113. }