BaseController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace business\controllers;
  3. use biz\admin\services\AdminToMerchantService;
  4. use biz\merchant\services\AdminService;
  5. use biz\merchant\services\MerchantAssetService;
  6. use biz\merchant\services\MerchantExtendService;
  7. use biz\merchant\services\MerchantService;
  8. use Yii;
  9. use common\components\util;
  10. class BaseController extends PublicController
  11. {
  12. public $admin, $adminId, $adminAvatar, $account;
  13. public $merchantId = 0, $merchantName, $merchant, $merchantExtend, $merchantAsset, $merchantLogo, $signPackage;
  14. public $openMerchant, $openMerchantId;
  15. public $appId;
  16. public $isWeixin = false;
  17. public $isLogin = false;
  18. public $withoutLogin = [];//不需要登陆的方法名
  19. public $validate = true;
  20. // Service 模型实例
  21. protected $service;
  22. // 设置允许与不允许方法 shizq 2019-12-05
  23. protected $allowActions = [];
  24. protected $notAllowActions = [];
  25. public function beforeAction($action)
  26. {
  27. //token验证
  28. //$adminId = jwt::getLoginId();
  29. $adminId = 1;
  30. if (empty($adminId)) {
  31. $this->validate = false;
  32. }
  33. //方法开放设置 shizq 2019-12-05
  34. if (in_array($action->id, $this->notAllowActions)) {
  35. util::fail($action->id . ' not allow');
  36. }
  37. //游客可以访问的方法
  38. if (in_array($action->id, $this->withoutLogin)) {
  39. $this->validate = true;
  40. }
  41. if ($this->validate == false) {
  42. util::notLogin();
  43. }
  44. $admin = AdminService::getById($adminId);
  45. if (empty($admin)) {
  46. util::fail('没有找到管理员');
  47. }
  48. $merchantId = AdminToMerchantService::getDefaultMerchant($adminId);
  49. if (empty($merchantId)) {
  50. util::fail('没有找到商家信息');
  51. }
  52. $merchant = MerchantService::getById($merchantId);
  53. $merchantExtend = MerchantExtendService::getByMerchantId($merchantId);
  54. $merchantAsset = MerchantAssetService::getByMerchantId($merchantId);
  55. Yii::$app->params['adminId'] = $adminId;
  56. Yii::$app->params['admin'] = $admin;
  57. Yii::$app->params['merchantId'] = $merchantId;
  58. Yii::$app->params['merchant'] = $merchant;
  59. Yii::$app->params['merchant'] = $merchant;
  60. Yii::$app->params['merchantAsset'] = $merchantAsset;
  61. Yii::$app->params['merchantExtend'] = $merchantExtend;
  62. $this->adminId = $adminId;
  63. $this->merchantId = $merchantId;
  64. $this->merchant = $merchant;
  65. $this->merchantExtend = $merchantExtend;
  66. $this->merchantAsset = $merchantAsset;
  67. return parent::beforeAction($action);
  68. }
  69. public function actionList()
  70. {
  71. $numargs = func_num_args();
  72. $select = $numargs >= 1 ? func_get_arg(0) : '*';
  73. $where = $numargs >= 2 ? func_get_arg(1) : null;
  74. $order = $numargs >= 3 ? func_get_arg(2) : '';
  75. $with = $numargs >= 4 ? func_get_arg(3) : '';
  76. $service = $this->service;
  77. $list = $service::getList($select, $where, $order, $with);
  78. util::success($list);
  79. }
  80. public function actionAdd()
  81. {
  82. $data = Yii::$app->request->post();
  83. $service = $this->service;
  84. $re = $service::add($data);
  85. util::success($re);
  86. }
  87. public function actionDetail()
  88. {
  89. $id = Yii::$app->request->get('id');
  90. $service = $this->service;
  91. $data = $service::getById($id);
  92. if(!empty($data)){
  93. util::success($data);
  94. }
  95. util::fail('');
  96. }
  97. public function actionUpdate()
  98. {
  99. $data = Yii::$app->request->post();
  100. $id = $data['id'];
  101. $service = $this->service;
  102. $re = $service::updateById($id, $data);
  103. if($re['status'] === true){
  104. util::success($re);
  105. }
  106. util::fail();
  107. }
  108. public function actionDelete()
  109. {
  110. $id = Yii::$app->request->get('id');
  111. $service = $this->service;
  112. $re = $service::deleteById($id);
  113. if($re == 1){
  114. util::success($re);
  115. }else{
  116. util::fail();
  117. }
  118. }
  119. }