BaseController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace shop\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. $merchantId = 0;
  25. if (empty($adminId)) {
  26. $this->validate = false;
  27. } else {
  28. $admin = AdminService::getById($adminId);
  29. if (empty($admin)) {
  30. util::fail('帐号无效');
  31. }
  32. $adminId = $admin['id'];
  33. $relateId = Yii::$app->redis->executeCommand('GET', ['ADMIN_RELATION_' . $adminId]);
  34. //不是数字可能被清缓存,需要重新登陆,$relateId = 0 表示没有关联商家
  35. if (is_numeric($relateId) == false) {
  36. util::notLogin();
  37. }
  38. $merchantId = $admin['merchantId'];
  39. if (empty($merchantId)) {
  40. util::fail('没有找到商家信息');
  41. }
  42. }
  43. //游客可以访问的方法
  44. if (in_array($action->id, $this->withoutLogin)) {
  45. $this->validate = true;
  46. }
  47. if ($this->validate == false) {
  48. util::notLogin();
  49. }
  50. $merchant = MerchantService::getMerchantById($merchantId);
  51. $merchantExtend = MerchantExtendService::getByMerchantId($merchantId);
  52. $this->admin = $admin;
  53. $this->adminId = $adminId;
  54. $this->merchantId = $merchantId;
  55. $this->merchant = $merchant;
  56. $this->merchantExtend = $merchantExtend;
  57. return parent::beforeAction($action);
  58. }
  59. //shisq
  60. public function actionList()
  61. {
  62. $numargs = func_num_args();
  63. $select = $numargs >= 1 ? func_get_arg(0) : '*';
  64. $where = $numargs >= 2 ? func_get_arg(1) : null;
  65. $order = $numargs >= 3 ? func_get_arg(2) : '';
  66. $with = $numargs >= 4 ? func_get_arg(3) : '';
  67. $service = $this->service;
  68. $list = $service::getList($select, $where, $order, $with);
  69. util::success($list);
  70. }
  71. //shisq
  72. public function actionAdd()
  73. {
  74. $data = Yii::$app->request->post();
  75. $service = $this->service;
  76. $re = $service::add($data);
  77. util::success($re);
  78. }
  79. //shisq
  80. public function actionDetail()
  81. {
  82. $id = Yii::$app->request->get('id');
  83. $service = $this->service;
  84. $data = $service::getById($id);
  85. if (isset($data['merchantId']) == false || $this->merchantId != $data['merchantId']) {
  86. util::fail('没有权限');
  87. }
  88. if (!empty($data)) {
  89. util::success($data);
  90. }
  91. util::fail('');
  92. }
  93. //shisq
  94. public function actionUpdate()
  95. {
  96. $data = Yii::$app->request->post();
  97. $id = $data['id'];
  98. $service = $this->service;
  99. $re = $service::updateById($id, $data);
  100. if ($re['status'] === true) {
  101. util::success($re);
  102. }
  103. util::fail();
  104. }
  105. //shisq
  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. }