dataList(1); /** * 渲染模板 */ return $this->render('index', $params); } /** * 禁用的品牌列表 */ public function actionDisabled() { $params = $this->dataList(2); /** * 渲染模板 */ return $this->render('disabled', $params); } protected function dataList($type = 0) { $request = Yii::$app->request; /** * 查询过滤处理 */ $get = []; /** * 组织SQL */ $where = ['and']; // 英文名称 $englishName = $request->get('englishName'); $get['englishName'] = $englishName; if ($englishName) { $where[] = ['like', 'english_name', $englishName]; } // 中文名称 $chineseName = $request->get('chineseName'); $get['chineseName'] = $chineseName; if ($chineseName) { $where[] = ['like', 'chinese_name', $chineseName]; } $pageSize = $request->get("pageSize") ? (int) $request->get("pageSize") : 20; if ($type == 1) { /** * 分页处理 */ $where[] = ['is_available' => 1]; $pages = new Pagination(['totalCount' => BrandRepository::getBrandWithWhereCount($where), 'pageSize' => $pageSize]); $brands = BrandRepository::getBrandWithWhere($where, $pages, 'english_name ASC'); } else if ($type == 2) { $where[] = ['is_available' => 0]; $pages = new Pagination(['totalCount' => BrandRepository::getBrandWithWhereCount($where), 'pageSize' => $pageSize]); $brands = BrandRepository::getBrandWithWhere($where, $pages, 'english_name ASC'); } /** * 数据整理 */ $data = []; foreach ($brands as $brand) { if ($brand['img_url']) { $logo = ImageManager::getUrl($brand['img_url'], ImageManager::$STYLE_90); } else { $logo = Yii::$app->request->BaseUrl . '/images/default-item.jpg'; } $data[] = [ 'id' => $brand['id'], 'logo' => $logo, 'chinese_name' => $brand['chinese_name'], 'english_name' => $brand['english_name'], 'status' => ($brand['is_available'] == 1) ? '已启用' : '已禁用', 'created_at' => date('Y-m-d H:i:s', $brand['created_at']), 'updated_at' => date('Y-m-d H:i:s', $brand['updated_at']), 'enable' => ($brand['is_available'] == 1) ? '禁用' : '启用', ]; } return [ 'brands' => $data, 'pages' => $pages, 'get' => $get, ]; } /** * 新增界面 */ public function actionCreate() { return $this->render('create'); } /** * 新增执行动作 */ public function actionDoAdd() { $post = Yii::$app->request->post(); if (empty($post)) { return; } $model = Yii::createObject(BrandModel::className()); $model->chinese_name = $post['chinese_name']; $model->english_name = $post['english_name']; $model->is_available = $post['is_available']; $model->internal_code = 0; if (!empty($post['img_path'])) { $suffix = substr($post['img_path'],(strripos($post['img_path'],".")+1)); $newPath = ImageManager::getBrandLogoPath($suffix); ImageManager::move($post['img_path'],$newPath); $model->img_url = $newPath; } // logo /* if (!empty($_FILES['logo']['name'])) { $image = $_FILES['logo']; // 图片路径数据整理 $tmp = explode('.', $image['name']); $suffix = end($tmp); $savePath = ImageManager::getBrandLogoPath($suffix); // 上传文件 ImageManager::add($image["tmp_name"], $savePath); $model->img_url = $savePath; }*/ $model->save(); Yii::$app->session->setFlash('success', '品牌添加成功'); return $this->redirect(['index']); } /** * 更新界面 */ public function actionUpdate($id) { $model = $this->findModel($id); return $this->render('update', [ 'model' => $model ]); } /** * 更新执行动作 */ public function actionDoUpdate($id) { $post = Yii::$app->request->post(); if (empty($post)) { return; } $model = $this->findModel($id); $model->chinese_name = $post['chinese_name']; $model->english_name = $post['english_name']; $model->is_available = $post['is_enable']; if (!empty($post['img_path'])) { if (!empty($model->img_url)) { ImageManager::delete($model->img_url); } $suffix = substr($post['img_path'],(strripos($post['img_path'],".")+1)); $newPath = ImageManager::getBrandLogoPath($suffix); ImageManager::move($post['img_path'],$newPath); $model->img_url = $newPath; } $model->updated_at = time(); // logo if (!empty($_FILES['logo']['name'])) { $image = $_FILES['logo']; // 图片路径数据整理 $tmp = explode('.', $image['name']); $suffix = end($tmp); $savePath = ImageManager::getBrandLogoPath($suffix); if ($model->img_url) { ImageManager::replace($model->img_url, $savePath, $image["tmp_name"]); $model->img_url = $savePath; } else { ImageManager::add($image["tmp_name"], $savePath); $model->img_url = $savePath; } } Yii::$app->session->setFlash('success', '品牌更新成功'); $model->save(); return $this->redirect(['index']); } /** * 删除执行动作 */ public function actionDelete($id) { $count = ModelModel::find()->where(['brand_id' => $id])->count(); if ($count == 0) { BrandModel::findOne(["id"=>$id])->delete(); Yii::$app->session->setFlash('success', '品牌删除完成'); } else { Yii::$app->session->setFlash('danger', '已经为此品牌选择了设备,不能删除'); } return $this->redirect(['index']); } /** * 根据主键查找模型 */ protected function findModel($id) { $brand = Yii::createObject(BrandModel::className()); if (($model = $brand::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('访问页面不存在'); } } /** * 启用或禁用 */ public function actionEnable($id) { $model = $this->findModel($id); $model->is_available = ($model->is_available == 1) ? 0 : 1; $model->save(); return $this->redirect(['index']); } /** * 批量启用 */ public function actionBatchEnable($ids) { $_ids = explode(',', $ids); $_ids = array_filter($_ids); foreach ($_ids as $id) { $model = $this->findModel($id); $model->is_available = 1; $model->save(); } Yii::$app->getSession()->setFlash('success', '批量启用成功'); return $this->redirect(['index']); } /** * 批量禁用 */ public function actionBatchDisable($ids) { $_ids = explode(',', $ids); $_ids = array_filter($_ids); foreach ($_ids as $id) { $model = $this->findModel($id); $model->is_available = 0; $model->save(); } Yii::$app->getSession()->setFlash('success', '批量禁用成功'); return $this->redirect(['disabled']); } /** * AJAX * 保存分类图标题 */ public function actionAjaxUploadImg() { if (empty($_FILES['brand_img']['name'])) { return json_encode([ 'success' => false, ]); } $image = $_FILES['brand_img']; // 图片路径数据整理 $tmp = explode('.', $image['name']); $suffix = end($tmp); //$savePath = ImageManager::getBrandLogoPath($suffix); $savePath = ImageManager::getTempImgPath($suffix); ImageManager::add($image["tmp_name"], $savePath); /** * 后台操作日志 */ AdminLogs::doImport('品牌图片', 1); return json_encode([ 'success' => true, 'url' => ImageManager::getUrl($savePath), 'path' => $savePath ]); } }