BaseController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. $merchantId = $admin['merchantId'];
  33. if (empty($merchantId)) {
  34. util::fail('没有找到商家信息');
  35. }
  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. $merchant = MerchantService::getMerchantById($merchantId);
  45. $merchantExtend = MerchantExtendService::getByMerchantId($merchantId);
  46. $this->admin = $admin;
  47. $this->adminId = $adminId;
  48. $this->merchantId = $merchantId;
  49. $this->merchant = $merchant;
  50. $this->merchantExtend = $merchantExtend;
  51. return parent::beforeAction($action);
  52. }
  53. //shisq
  54. public function actionList()
  55. {
  56. $numargs = func_num_args();
  57. $select = $numargs >= 1 ? func_get_arg(0) : '*';
  58. $where = $numargs >= 2 ? func_get_arg(1) : null;
  59. $order = $numargs >= 3 ? func_get_arg(2) : '';
  60. $with = $numargs >= 4 ? func_get_arg(3) : '';
  61. $service = $this->service;
  62. $list = $service::getList($select, $where, $order, $with);
  63. util::success($list);
  64. }
  65. //shisq
  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. //shisq
  74. public function actionDetail()
  75. {
  76. $id = Yii::$app->request->get('id');
  77. $service = $this->service;
  78. $data = $service::getById($id);
  79. if (isset($data['merchantId']) == false || $this->merchantId != $data['merchantId']) {
  80. util::fail('没有权限');
  81. }
  82. if (!empty($data)) {
  83. util::success($data);
  84. }
  85. util::fail('');
  86. }
  87. //shisq
  88. public function actionUpdate()
  89. {
  90. $data = Yii::$app->request->post();
  91. $id = $data['id'];
  92. $service = $this->service;
  93. $re = $service::updateById($id, $data);
  94. if ($re['status'] === true) {
  95. util::success($re);
  96. }
  97. util::fail();
  98. }
  99. //shisq
  100. public function actionDelete()
  101. {
  102. $id = Yii::$app->request->get('id');
  103. $service = $this->service;
  104. $service::deleteById($id);
  105. util::success([], '删除成功');
  106. }
  107. }