BaseController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 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, $merchantAsset, $merchantLogo, $signPackage;
  13. public $openMerchant, $openMerchantId;
  14. public $appId;
  15. public $isWeixin = false;
  16. public $isLogin = false;
  17. public $withoutLogin = [];//不需要登陆的方法名
  18. public $validate = true;
  19. // Service 模型实例
  20. protected $service;
  21. // 设置允许与不允许方法 shizq 2019-12-05
  22. protected $allowActions = [];
  23. protected $notAllowActions = [];
  24. public function beforeAction($action)
  25. {
  26. //token验证
  27. //$adminId = jwt::getLoginId();
  28. $adminId = 1;
  29. if (empty($adminId)) {
  30. $this->validate = false;
  31. }
  32. //方法开放设置 shizq 2019-12-05
  33. if (in_array($action->id, $this->notAllowActions)) {
  34. util::fail($action->id . ' not allow');
  35. }
  36. //游客可以访问的方法
  37. if (in_array($action->id, $this->withoutLogin)) {
  38. $this->validate = true;
  39. }
  40. if ($this->validate == false) {
  41. util::notLogin();
  42. }
  43. $admin = AdminService::getById($adminId);
  44. if (empty($admin)) {
  45. util::fail('没有找到管理员');
  46. }
  47. $merchantId = $admin['merchantId'];
  48. if (empty($merchantId)) {
  49. util::fail('没有找到商家信息');
  50. }
  51. $merchant = MerchantService::getById($merchantId);
  52. $merchantExtend = MerchantExtendService::getByMerchantId($merchantId);
  53. $merchantAsset = MerchantAssetService::getByMerchantId($merchantId);
  54. Yii::$app->params['merchantId'] = $merchantId;
  55. Yii::$app->params['merchant'] = $merchant;
  56. $this->adminId = $adminId;
  57. $this->merchantId = $merchantId;
  58. $this->merchant = $merchant;
  59. $this->merchantExtend = $merchantExtend;
  60. $this->merchantAsset = $merchantAsset;
  61. return parent::beforeAction($action);
  62. }
  63. public function actionList()
  64. {
  65. $numargs = func_num_args();
  66. $select = $numargs >= 1 ? func_get_arg(0) : '*';
  67. $where = $numargs >= 2 ? func_get_arg(1) : null;
  68. $order = $numargs >= 3 ? func_get_arg(2) : '';
  69. $with = $numargs >= 4 ? func_get_arg(3) : '';
  70. $service = $this->service;
  71. $list = $service::getList($select, $where, $order, $with);
  72. util::success($list);
  73. }
  74. public function actionAdd()
  75. {
  76. $data = Yii::$app->request->post();
  77. $service = $this->service;
  78. $re = $service::add($data);
  79. util::success($re);
  80. }
  81. public function actionDetail()
  82. {
  83. $id = Yii::$app->request->get('id');
  84. $service = $this->service;
  85. $data = $service::getById($id);
  86. if (isset($data['merchantId']) == false || $this->merchantId != $data['merchantId']) {
  87. util::fail('没有权限');
  88. }
  89. if (!empty($data)) {
  90. util::success($data);
  91. }
  92. util::fail('');
  93. }
  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. public function actionDelete()
  106. {
  107. $id = Yii::$app->request->get('id');
  108. $service = $this->service;
  109. $service::deleteById($id);
  110. util::success([], '删除成功');
  111. }
  112. }