StoreBusinessController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace addons\Happiness\backend\controllers;
  3. use addons\Happiness\common\enums\HappinessEnum;
  4. use addons\Happiness\common\models\HappinessBusiness;
  5. use common\enums\StatusEnum;
  6. use common\models\user\User;
  7. use Yii;
  8. /**
  9. * 默认控制器
  10. *
  11. * Class StoreBusinessController
  12. * @package addons\Happiness\backend\controllers
  13. */
  14. class StoreBusinessController extends BaseController
  15. {
  16. /**
  17. * 首页
  18. *
  19. * @return string
  20. * @throws \yii\web\NotFoundHttpException
  21. */
  22. public function actionIndex()
  23. {
  24. $request = \Yii::$app->request;
  25. if ($request->isAjax) {
  26. if ($request->isGet) {
  27. $get = \Yii::$app->request->get();
  28. // 检查提交的参数
  29. $res = \Yii::$app->services->validate->validate($get, [
  30. [['level'], 'integer'],
  31. [['user_keyword'], 'string'],
  32. ]);
  33. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  34. !empty($get['level']) && $where[] = ['=', 'level', $get['level']];
  35. $where[] = ['=', 'mall_id', $this->mall_id];
  36. $where[] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  37. if(!empty($get['user_keyword']))
  38. {
  39. $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [
  40. 'or',
  41. ['=', 'id', $get['user_keyword']],
  42. ['like', 'nickname', $get['user_keyword'] . '%', false],
  43. ['like', 'mobile', $get['user_keyword'] . '%', false]
  44. ]], 'select' => 'id']);
  45. $user_ids = array_column($user_list, 'id');
  46. $where[]=['in', 'user_id', $user_ids];
  47. }
  48. $order = 'created_at desc';
  49. $select = '*';
  50. $params = array('where' => $where, 'select' => $select, 'order' => $order, 'limit' => $get['limit']);
  51. $params['with'] = ['user' => function ($query) {
  52. $query->select('id, nickname, mobile,avatar_url');
  53. }];
  54. $list = HappinessBusiness::listPage($params, false, true);
  55. if($list==false)
  56. {
  57. return $this->error(HappinessBusiness::getStaticError(), []);
  58. }
  59. $config= \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
  60. $boss_list=!empty($config['business_list'])?json_decode($config['business_list'],true):[];
  61. if(!empty($list['list']))
  62. {
  63. if(!empty($boss_list))
  64. {
  65. $boss_list=array_column($boss_list,null,'level');
  66. }
  67. foreach ($list['list'] as &$item)
  68. {
  69. $item['level_name']=isset($boss_list[$item['level']])?$boss_list[$item['level']]['name']:'';
  70. }
  71. }
  72. $list['business_list'] = $boss_list;
  73. return $this->success('获取成功', $list);
  74. }
  75. if ($request->isPost) {
  76. $post = \Yii::$app->request->post();
  77. $res = \Yii::$app->services->validate->validate($post, [
  78. [['level', 'user_id'], 'required'],
  79. [['level', 'user_id'], 'integer'],
  80. ]);
  81. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  82. $post['mall_id'] = $this->mall_id;
  83. $res=HappinessBusiness::setData($post);
  84. if($res===false)
  85. {
  86. return $this->error(HappinessBusiness::getStaticError());
  87. }
  88. return $this->success('操作成功');
  89. }
  90. }
  91. return $this->render('index');
  92. }
  93. public function actionSwitchStatus()
  94. {
  95. if (\Yii::$app->request->isAjax) {
  96. if (\Yii::$app->request->isPost) {
  97. $params = \Yii::$app->request->post();
  98. $res = Yii::$app->services->validate->validate($params, [
  99. [['id'], 'required'],
  100. [['status'], 'integer']
  101. ]);
  102. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  103. $params['mall_id'] = $this->mall_id;
  104. $res = HappinessBusiness::setData($params);
  105. if ($res === false) return $this->error(HappinessBusiness::getStaticError());
  106. return $this->success('操作成功');
  107. }
  108. }
  109. return $this->error();
  110. }
  111. public function actionDelete()
  112. {
  113. if (\Yii::$app->request->isAjax) {
  114. if (\Yii::$app->request->isPost) {
  115. $params = \Yii::$app->request->post();
  116. $res = Yii::$app->services->validate->validate($params, [
  117. [['id'], 'required'],
  118. ]);
  119. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  120. $params['mall_id'] = $this->mall_id;
  121. $params['status'] = StatusEnum::DELETE;
  122. $res = HappinessBusiness::setData($params);
  123. if ($res === false) return $this->error(HappinessBusiness::getStaticError());
  124. return $this->success('操作成功');
  125. }
  126. }
  127. return $this->error();
  128. }
  129. public function actionEdit()
  130. {
  131. if (\Yii::$app->request->isAjax) {
  132. if (\Yii::$app->request->isPost) {
  133. }
  134. if (\Yii::$app->request->isGet) {
  135. $id = \Yii::$app->request->get('id');
  136. $boss = HappinessBusiness::find() ->with(['user'=>function ($query){
  137. $query->select('id,nickname,mobile,avatar_url');
  138. }])->where(['id' => $id, 'mall_id' =>
  139. $this->mall_id])
  140. ->asArray()
  141. ->one();
  142. if (empty($boss)) {
  143. return $this->error('数据未找到');
  144. }
  145. return $this->success('success', $boss);
  146. }
  147. }
  148. }
  149. }