| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace addons\Happiness\backend\controllers;
- use addons\Happiness\common\enums\HappinessEnum;
- use addons\Happiness\common\models\HappinessBusiness;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use Yii;
- /**
- * 默认控制器
- *
- * Class StoreBusinessController
- * @package addons\Happiness\backend\controllers
- */
- class StoreBusinessController extends BaseController
- {
- /**
- * 首页
- *
- * @return string
- * @throws \yii\web\NotFoundHttpException
- */
- public function actionIndex()
- {
- $request = \Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- $get = \Yii::$app->request->get();
- // 检查提交的参数
- $res = \Yii::$app->services->validate->validate($get, [
- [['level'], 'integer'],
- [['user_keyword'], 'string'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- !empty($get['level']) && $where[] = ['=', 'level', $get['level']];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- if(!empty($get['user_keyword']))
- {
- $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [
- 'or',
- ['=', 'id', $get['user_keyword']],
- ['like', 'nickname', $get['user_keyword'] . '%', false],
- ['like', 'mobile', $get['user_keyword'] . '%', false]
- ]], 'select' => 'id']);
- $user_ids = array_column($user_list, 'id');
- $where[]=['in', 'user_id', $user_ids];
- }
- $order = 'created_at desc';
- $select = '*';
- $params = array('where' => $where, 'select' => $select, 'order' => $order, 'limit' => $get['limit']);
- $params['with'] = ['user' => function ($query) {
- $query->select('id, nickname, mobile,avatar_url');
- }];
- $list = HappinessBusiness::listPage($params, false, true);
- if($list==false)
- {
- return $this->error(HappinessBusiness::getStaticError(), []);
- }
- $config= \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
- $boss_list=!empty($config['business_list'])?json_decode($config['business_list'],true):[];
- if(!empty($list['list']))
- {
- if(!empty($boss_list))
- {
- $boss_list=array_column($boss_list,null,'level');
- }
- foreach ($list['list'] as &$item)
- {
- $item['level_name']=isset($boss_list[$item['level']])?$boss_list[$item['level']]['name']:'';
- }
- }
- $list['business_list'] = $boss_list;
- return $this->success('获取成功', $list);
- }
- if ($request->isPost) {
- $post = \Yii::$app->request->post();
- $res = \Yii::$app->services->validate->validate($post, [
- [['level', 'user_id'], 'required'],
- [['level', 'user_id'], 'integer'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $post['mall_id'] = $this->mall_id;
- $res=HappinessBusiness::setData($post);
- if($res===false)
- {
- return $this->error(HappinessBusiness::getStaticError());
- }
- return $this->success('操作成功');
- }
- }
- return $this->render('index');
- }
- public function actionSwitchStatus()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- [['status'], 'integer']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $res = HappinessBusiness::setData($params);
- if ($res === false) return $this->error(HappinessBusiness::getStaticError());
- return $this->success('操作成功');
- }
- }
- return $this->error();
- }
- public function actionDelete()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $params['status'] = StatusEnum::DELETE;
- $res = HappinessBusiness::setData($params);
- if ($res === false) return $this->error(HappinessBusiness::getStaticError());
- return $this->success('操作成功');
- }
- }
- return $this->error();
- }
- public function actionEdit()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- }
- if (\Yii::$app->request->isGet) {
- $id = \Yii::$app->request->get('id');
- $boss = HappinessBusiness::find() ->with(['user'=>function ($query){
- $query->select('id,nickname,mobile,avatar_url');
- }])->where(['id' => $id, 'mall_id' =>
- $this->mall_id])
- ->asArray()
- ->one();
- if (empty($boss)) {
- return $this->error('数据未找到');
- }
- return $this->success('success', $boss);
- }
- }
- }
- }
|