Files
xk-api-yii/admin/controllers/base/NavController.php

172 lines
4.7 KiB
PHP

<?php
namespace admin\controllers\base;
use admin\components\BaseAdminController;
use admin\models\forms\drug\NavForm;
use common\models\oldDb\Nav;
use yii\db\Exception;
class NavController extends BaseAdminController
{
/**
* @doc-name 轮播图类型
*/
public function actionTypeList()
{
return [
['key' => 0, 'value' => '轮播图'],
];
}
/**
* @doc-name 轮播图列表
* @doc-param int is_home 是否首页0否1是 / optional
* @doc-param int type 类型1活动2专题 / optional
* @doc-return mixed @nav{id,pic,external_link-string-外部链接,is_home-int-是否首页0否1是,home_time-string-首页时间,type-int-类型1活动2专题} 轮播图
*/
public function actionList()
{
$get = \Yii::$app->request->get();
$this->requestValidate($get, [
['store_id', 'required']
]);
$admin = \Yii::$app->user->identity;
$query = Nav::find()->where([
'store_id' => $get['store_id'],
'is_delete' => 0,
])->orderBy(['id' => SORT_DESC]);
if (!empty($get['is_home'])) {
$query->andWhere([
'is_home' => $get['is_home']
]);
}
if (!empty($get['type'])) {
$query->andWhere([
'type' => $get['type']
]);
}
$this->field = [
Nav::class => [
'id', 'pic', 'external_link', 'is_home', 'home_time', 'type', 'sort', 'store_id','link_type'
]
];
return $this->create($query, $get);
}
/**
* @doc-name 新增轮播图
* @doc-param int store_id 门店ID / optional
* @doc-param int sort 排序 / optional
* @doc-param string pic 轮播图
* @doc-param int link_type 1内部2外部 / optional
* @doc-param string external_link 外部链接 / optional
* @doc-param int is_home 是否首页0否1是 / optional
* @doc-param int type 类型1轮播图 / optional
*/
public function actionSaveNav()
{
$post = \Yii::$app->request->post();
$NavForm = new NavForm();
$NavForm->attributes = $post;
return $NavForm->save();
}
/**
* @doc-name 修改轮播图
* @doc-param int id 轮播图id
* @doc-param string store_id 门店ID
* @doc-param string sort 排序
* @doc-param string pic 轮播图
* @doc-param string external_link
* @doc-param int is_home 是否首页0否1是
* @doc-param int type 类型1活动2专题
*/
public function actionEditNav()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
['id', 'required']
]);
$NavForm = new NavForm();
$NavForm->attributes = $post;
return $NavForm->update();
}
/**
* @doc-name 删除轮播图
* @doc-param int id 轮播图id
*/
public function actionDel()
{
$get = \Yii::$app->request->get();
$this->requestValidate($get, [
['id', 'required']
]);
$NavForm = new NavForm();
$NavForm->attributes = $get;
return $NavForm->del();
}
/**
* @doc-name 轮播图详情
* @doc-param int id 轮播图id
* @doc-return mixed @nav{*} 轮播图
*/
public function actionInfo()
{
$get =\Yii::$app->request->get();
$this->requestValidate($get, [
['id', 'required']
]);
$nav = Nav::find()->where([
'id' => $get['id'],
'is_delete' => 0
])->one();
if (!$nav) throw new Exception('轮播图不存在');
return $nav;
}
/**
* @doc-name 是否推送的首页
* @doc-param int id 轮播图id
* @doc-param int status 状态0否1是
*/
public function actionIsHome()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
[['id', 'status'], 'required']
]);
$nav = Nav::find()->where([
'id' => $post['id'],
'is_delete' => 0
])->one();
if (!$nav) throw new Exception('轮播图不存在');
switch ($post['status']) {
case 0:
$nav->is_home = 0;
$nav->home_time = date('Y-m-d H:i:s', time());
$nav->saveOrFail();
return ['已取消'];
break;
case 1:
$nav->is_home = 1;
$nav->home_time = date('Y-m-d H:i:s', time());
$nav->saveOrFail();
return ['已推送'];
break;
default:
throw new Exception('参数错误');
}
}
}