BaseController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 common\components\jwt;
  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 $isWx = false;
  17. public $isLogin = false;
  18. public $withoutLogin = [];//不需要登陆的方法名
  19. public $validate = true;
  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. //授权方式获取客户信息 xhAdmin userId存的是每个公众号店长的微信userId
  36. $admin = AdminService::getByCondition(['userId' => $adminId]);
  37. if (empty($admin)) {
  38. util::fail('没有找到管理员,但已经获取客户ID:' . $adminId);
  39. }
  40. $merchantId = $admin['merchantId'];
  41. if (empty($merchantId)) {
  42. util::fail('没有找到商家信息');
  43. }
  44. $merchant = MerchantService::getMerchantById($merchantId);
  45. $merchantExtend = MerchantExtendService::getByMerchantId($merchantId);
  46. $merchantAsset = MerchantAssetService::getByMerchantId($merchantId);
  47. $this->admin = $admin;
  48. $this->adminId = $adminId;
  49. $this->merchantId = $merchantId;
  50. $this->merchant = $merchant;
  51. $this->merchantExtend = $merchantExtend;
  52. $this->merchantAsset = $merchantAsset;
  53. return parent::beforeAction($action);
  54. }
  55. public function actionList()
  56. {
  57. $numargs = func_num_args();
  58. $select = $numargs >= 1 ? func_get_arg(0) : '*';
  59. $where = $numargs >= 2 ? func_get_arg(1) : null;
  60. $order = $numargs >= 3 ? func_get_arg(2) : '';
  61. $with = $numargs >= 4 ? func_get_arg(3) : '';
  62. $service = $this->service;
  63. $list = $service::getList($select, $where, $order, $with);
  64. util::success($list);
  65. }
  66. public function actionAdd()
  67. {
  68. $data = Yii::$app->request->post();
  69. $service = $this->service;
  70. $re = $service::add($data);
  71. util::success($re);
  72. }
  73. public function actionDetail()
  74. {
  75. $id = Yii::$app->request->get('id');
  76. $service = $this->service;
  77. $data = $service::getById($id);
  78. if (isset($data['merchantId']) == false || $this->merchantId != $data['merchantId']) {
  79. util::fail('没有权限');
  80. }
  81. if (!empty($data)) {
  82. util::success($data);
  83. }
  84. util::fail('');
  85. }
  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. public function actionDelete()
  98. {
  99. $id = Yii::$app->request->get('id');
  100. $service = $this->service;
  101. $service::deleteById($id);
  102. util::success([], '删除成功');
  103. }
  104. }