BaseController.php 3.2 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 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::getMerchantById($merchantId);
  52. $merchantExtend = MerchantExtendService::getByMerchantId($merchantId);
  53. $merchantAsset = MerchantAssetService::getByMerchantId($merchantId);
  54. $this->admin = $admin;
  55. $this->adminId = $adminId;
  56. $this->merchantId = $merchantId;
  57. $this->merchant = $merchant;
  58. $this->merchantExtend = $merchantExtend;
  59. $this->merchantAsset = $merchantAsset;
  60. return parent::beforeAction($action);
  61. }
  62. public function actionList()
  63. {
  64. $numargs = func_num_args();
  65. $select = $numargs >= 1 ? func_get_arg(0) : '*';
  66. $where = $numargs >= 2 ? func_get_arg(1) : null;
  67. $order = $numargs >= 3 ? func_get_arg(2) : '';
  68. $with = $numargs >= 4 ? func_get_arg(3) : '';
  69. $service = $this->service;
  70. $list = $service::getList($select, $where, $order, $with);
  71. util::success($list);
  72. }
  73. public function actionAdd()
  74. {
  75. $data = Yii::$app->request->post();
  76. $service = $this->service;
  77. $re = $service::add($data);
  78. util::success($re);
  79. }
  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. public function actionUpdate()
  94. {
  95. $data = Yii::$app->request->post();
  96. $id = $data['id'];
  97. $service = $this->service;
  98. $re = $service::updateById($id, $data);
  99. if ($re['status'] === true) {
  100. util::success($re);
  101. }
  102. util::fail();
  103. }
  104. public function actionDelete()
  105. {
  106. $id = Yii::$app->request->get('id');
  107. $service = $this->service;
  108. $service::deleteById($id);
  109. util::success([], '删除成功');
  110. }
  111. }