request->isAjax) { return $this->render('/store-user-associated/index'); } if (\Yii::$app->request->isGet) { $get = \Yii::$app->request->get(); $store_id = $get['store_id']; $where = [ ['mall_id' => $this->mall_id], ['store_id' => $store_id], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]], ]; $user_where = []; if (!empty($get['keyword'])) { $user_where[] = [ 'OR', ['id' => $get['keyword']], ['like', 'nickname', $get['keyword']] ]; } if (!empty($get['related_start'])) $where[] = ['>=', 'updated_at', strtotime($get['related_start'])]; if (!empty($get['related_end'])) $where[] = ['<=', 'updated_at', strtotime($get['related_end'])]; if (!empty($user_where)) { $user_list = User::lists(['where' => $user_where, 'select' => 'id']); $user_id_arr = array_column($user_list, 'id'); $where[] = ['user_id' => $user_id_arr]; } $params = [ 'where' => $where, 'with' => ['user'], 'order' => 'id desc', 'select' => ['id', 'mall_id', 'store_id', 'user_id', 'updated_at'], // 指定要查询的字段 ]; !empty($get['limit']) && $params['limit'] = $get['limit']; $list = StoreAssociatedUser::listPage($params); if ($list === false) return $this->error(StoreAssociatedUser::getStaticError()); return $this->success('ok', $list); } } /** * @name: * @msg: 获取会员列表 * @param {*} * @return {*} */ public function actionGetUsers() { if (!\Yii::$app->request->isGet) { return $this->error('请求方式错误'); } $user_id = \Yii::$app->request->get('user_id'); $keyword = \Yii::$app->request->get('keyword'); $page = \Yii::$app->request->get('page', 1); $limit = \Yii::$app->request->get('limit', $this->pageSize); $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]; if (!empty($user_id)) { $wheres[] = ['id' => $user_id]; } if (!empty($keyword)) { $wheres[] = ['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]]; } $store_user_list = StoreAssociatedUser::lists(['where' => [ 'mall_id' => $this->mall_id ], 'select' => 'user_id']); $store_user_ids = array_column($store_user_list, 'user_id'); $wheres[] = ['not in', 'id', $store_user_ids]; $params = [ // 'alias' => 'user', 'select' => 'id,nickname,avatar_url,mobile,level,parent_id', 'where' => $wheres, 'order' => 'created_at desc', 'page' => $page, 'limit' => $limit, // 'join' => [['left join',StoreUser::tableName().' as su','su.user_id=user.id and mall_id' => $this->mall_id]], ]; $user_list = User::listPage($params); if (empty($user_list['list'])) { if (false === $user_list['list'] || $user_list === false) { return $this->error(User::getStaticError()); } } else { $levels = UserLevel::find() ->select('id,level,name') ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]) ->asArray() ->indexBy('level') ->all(); $parent_ids = array_column($user_list['list'], 'parent_id'); $parent_users = User::find() ->select('id,nickname,avatar_url,mobile,level,parent_id') ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $parent_ids]) ->asArray() ->indexBy('id') ->all(); foreach ($user_list['list'] as &$user) { if (empty($user['avatar_url'])) { $user['avatar_url'] = \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png'; } $level = $levels[$user['level']] ?? []; $parent_user = $parent_users[$user['parent_id']] ?? []; $user['level_name'] = $level['name'] ?? ''; $user['parent_nickname'] = $parent_user['nickname'] ?? ''; } } return $this->success('success', $user_list); } /** * @name: * @msg: 编辑会员关联门店 * @param {*} * @return {*} */ public function actionAddUserStore() { try { if (\Yii::$app->request->isGet) { return $this->error('请求方式错误'); } $post =\Yii::$app->request->post(); // 检查提交的参数 $res = Yii::$app->services->validate->validate($post, [ [['user_id', 'store_id'], 'required'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $user_id = $post['user_id']; $store_id = $post['store_id']; StoreAssociatedUser::setData([ 'mall_id' => $this->mall_id, 'user_id' => $user_id, 'store_id' => $store_id, ]); return $this->success('操作成功', []); } catch (\Exception $e) { return $this->error($e->getMessage()); } } /** * @name: * @msg: 编辑会员关联门店 * @param {*} * @return {*} */ public function actionEditUserStore() { try { if (\Yii::$app->request->isGet) { return $this->error('请求方式错误'); } $post =\Yii::$app->request->post(); // 检查提交的参数 $res = Yii::$app->services->validate->validate($post, [ [['user_id', 'store_id', 'old_store_id'], 'required'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $user_ids = explode(',', $post['user_id']); $store_id = $post['store_id']; $old_store_id = $post['old_store_id']; $db = Yii::$app->db->beginTransaction(); foreach ($user_ids as $user_id) { StoreAssociatedUser::setData([ 'mall_id' => $this->mall_id, 'user_id' => $user_id, 'store_id' => $old_store_id, 'new_store_id' => $store_id ]); } $db->commit(); return $this->success('操作成功', []); } catch (\Exception $e) { $db->rollBack(); return $this->error($e->getMessage()); } } /** * @name: * @msg: 批量编辑会员关联门店 * @param {*} * @return {*} */ public function actionBatchEditUserStore() { try { if (\Yii::$app->request->isGet) { return $this->error('请求方式错误'); } $post =\Yii::$app->request->post(); // 检查提交的参数 $res = Yii::$app->services->validate->validate($post, [ [['store_id', 'old_store_id'], 'required'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $old_store_id = $post['old_store_id']; $store_id = $post['store_id']; $where[] = ['mall_id' => $this->mall_id]; $where[] = ['store_id' => $old_store_id]; $store_user_list = StoreAssociatedUser::lists(['where' => $where , 'select' => 'user_id']); if (empty($store_user_list)) { return $this->success('操作成功', []); } $user_ids = array_column($store_user_list, 'user_id'); $db = Yii::$app->db->beginTransaction(); foreach ($user_ids as $user_id) { StoreAssociatedUser::setData([ 'mall_id' => $this->mall_id, 'user_id' => $user_id, 'store_id' => $old_store_id, 'new_store_id' => $store_id ]); } $db->commit(); return $this->success('操作成功', []); } catch (\Exception $e) { $db->rollBack(); return $this->error($e->getMessage()); } } }