BaseController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\jwt;
  7. use Yii;
  8. use common\components\util;
  9. class BaseController extends PublicController
  10. {
  11. public $admin, $adminId, $adminAvatar, $account;
  12. public $merchantId = 0, $merchantName, $merchant, $merchantExtend, $merchantLogo, $signPackage;
  13. public $openMerchant, $openMerchantId;
  14. public $appId;
  15. public $isWx = false;
  16. public $isLogin = false;
  17. public $withoutLogin = [];//不需要登陆的方法名
  18. public $validate = true;
  19. //shish 2020.3.8
  20. public function beforeAction($action)
  21. {
  22. //token验证
  23. $adminId = jwt::getLoginId();
  24. if (empty($adminId)) {
  25. $this->validate = false;
  26. }
  27. //游客可以访问的方法
  28. if (in_array($action->id, $this->withoutLogin)) {
  29. $this->validate = true;
  30. }
  31. if ($this->validate == false) {
  32. die;
  33. util::notLogin();
  34. }
  35. $admin = AdminService::getById($adminId);
  36. if (empty($admin)) {
  37. util::fail('没有找到管理员,但已经获取客户ID:' . $adminId);
  38. }
  39. $merchantId = $admin['merchantId'];
  40. if (empty($merchantId)) {
  41. util::fail('没有找到商家信息,但有管理员ID:' . $adminId);
  42. }
  43. $merchant = MerchantService::getMerchantById($merchantId);
  44. $merchantExtend = MerchantExtendService::getByMerchantId($merchantId);
  45. $this->admin = $admin;
  46. $this->adminId = $adminId;
  47. $this->merchantId = $merchantId;
  48. $this->merchant = $merchant;
  49. $this->merchantExtend = $merchantExtend;
  50. return parent::beforeAction($action);
  51. }
  52. //shisq
  53. public function actionList()
  54. {
  55. $numargs = func_num_args();
  56. $select = $numargs >= 1 ? func_get_arg(0) : '*';
  57. $where = $numargs >= 2 ? func_get_arg(1) : null;
  58. $order = $numargs >= 3 ? func_get_arg(2) : '';
  59. $with = $numargs >= 4 ? func_get_arg(3) : '';
  60. $service = $this->service;
  61. $list = $service::getList($select, $where, $order, $with);
  62. util::success($list);
  63. }
  64. //shisq
  65. public function actionAdd()
  66. {
  67. $data = Yii::$app->request->post();
  68. $service = $this->service;
  69. $re = $service::add($data);
  70. util::success($re);
  71. }
  72. //shisq
  73. public function actionDetail()
  74. {
  75. $id = Yii::$app->request->get('id');
  76. $service = $this->service;
  77. $data = $service::getById($id);
  78. if (isset($data['merchantId']) == false || $this->merchantId != $data['merchantId']) {
  79. util::fail('没有权限');
  80. }
  81. if (!empty($data)) {
  82. util::success($data);
  83. }
  84. util::fail('');
  85. }
  86. //shisq
  87. public function actionUpdate()
  88. {
  89. $data = Yii::$app->request->post();
  90. $id = $data['id'];
  91. $service = $this->service;
  92. $re = $service::updateById($id, $data);
  93. if ($re['status'] === true) {
  94. util::success($re);
  95. }
  96. util::fail();
  97. }
  98. //shisq
  99. public function actionDelete()
  100. {
  101. $id = Yii::$app->request->get('id');
  102. $service = $this->service;
  103. $service::deleteById($id);
  104. util::success([], '删除成功');
  105. }
  106. }