BaseController.php 2.9 KB

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