Authored by wuxiao

修改左侧菜单逻辑

直播间关联商品管理
... ... @@ -21,6 +21,11 @@ class BaseController extends Controller
//用户实例
protected $user;
protected $main_id;//一级菜单id
protected $sub_id;//二级菜单id
protected $main_title;//一级菜单名称
protected $sub_title;//二级菜单名称
public function init()
{
... ... @@ -50,8 +55,12 @@ class BaseController extends Controller
$this->_refer = \common\lib\Referer::get();
$view = $this->getView();
$view->params['main_title'] = @Yii::$app->params['menu'][Yii::$app->controller->id][0] ? : '';
$view->params['sub_title'] = @Yii::$app->params['menu'][Yii::$app->controller->id][1][Yii::$app->controller->action->id][0] ? : '';
$main_id = $this->main_id ? : Yii::$app->controller->id;
$sub_id = $this->sub_id ? : Yii::$app->controller->action->id;
$view->params['main_id'] = $main_id;
$view->params['sub_id'] = $sub_id;
$view->params['main_title'] = $this->main_title ? : (@Yii::$app->params['menu'][$main_id][0] ? : '');
$view->params['sub_title'] = $this->sub_title ? : (@Yii::$app->params['menu'][$main_id][1][$sub_id][0] ? : '');
return parent::beforeAction($action);
}
... ...
... ... @@ -2,12 +2,24 @@
namespace backend\controllers;
use Yii;
use backend\components\Pagination;
use yii\helpers\ArrayHelper;
/**
* Live controller
*/
class ProductController extends BaseController
{
public function init()
{
parent::init();
$this->main_id = 'live';
$this->sub_id = 'room';
$this->sub_title = '商品管理';
}
/**
* 房间商品列表
* @return type
... ... @@ -18,7 +30,111 @@ class ProductController extends BaseController
return $this->redirect($this->_refer);
}
return $this->render('list');
$model = \app\models\RoomProduct::find()->where(['room_id'=>$room_id]);
$count = clone $model;
$pagination = new Pagination(['totalCount' =>$count->count()]);
$list = $model
->offset($pagination->offset)->limit($pagination->limit)
->orderBy(['sort'=>SORT_ASC,'create_time'=>SORT_DESC])
->all();
return $this->render('list',[
'room'=> \app\models\Room::findOne(['room_id'=>$room_id]),
'pagination'=>$pagination,
'list'=>$list,
]);
}
/**
* 添加商品
* @return type
*/
public function actionAdd()
{
if (!$room_id = Yii::$app->getRequest()->getQueryParam('id')){
return $this->redirect($this->_refer);
}
$model = new \app\models\RoomProduct;
if (Yii::$app->getRequest()->isPost){
$post = Yii::$app->getRequest()->post();
$model->room_id = $room_id;
if ($model->load($post,'') && $model->save()){
Yii::$app->session->setFlash('success', '创建成功。');
return $this->redirect($this->_refer);
}else{
Yii::$app->session->setFlash('error', '创建失败。');
$model->setAttributes($post);
}
}
return $this->render('edit',[
'model'=>$model,
]);
}
/**
* 编辑商品
* @return type
*/
public function actionEdit()
{
if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
return $this->redirect($this->_refer);
}
if (!$model = \app\models\RoomProduct::findOne($id)){
Yii::$app->session->setFlash('error', '找不到该商品');
return $this->redirect($this->_refer);
}
if (Yii::$app->getRequest()->isPost){
$post = Yii::$app->getRequest()->post();
if ($model->load($post,'') && $model->save()){
Yii::$app->session->setFlash('success', '编辑成功。');
$model->refresh();
}else{
Yii::$app->session->setFlash('error', '编辑失败。');
}
}
return $this->render('edit',[
'model'=>$model,
]);
}
/**
* 商品操作
* @return type
*/
public function actionOperate()
{
if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
return $this->redirect($this->_refer);
}
if (!$type = Yii::$app->getRequest()->getQueryParam('type')){
return $this->redirect($this->_refer);
}
if (!$model = \app\models\RoomProduct::findOne($id)){
Yii::$app->session->setFlash('error', '找不到该商品');
return $this->redirect($this->_refer);
}
switch ($type){
case 'del'://删除
$r = $model->delete();
break;
default:
break;
}
if (!empty($r)){
Yii::$app->session->setFlash('success', '操作成功');
}else{
Yii::$app->session->setFlash('error', '操作失败');
}
return $this->redirect($this->_refer);
}
}
... ...
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "{{%room_product}}".
*
* @property string $id
* @property string $room_id
* @property string $product_skn
* @property integer $sort
* @property integer $create_time
* @property integer $update_time
*/
class RoomProduct extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%room_product}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['room_id'], 'required'],
[['room_id', 'product_id', 'sort', 'create_time', 'update_time'], 'integer'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'room_id' => '房间ID',
'product_id' => '商品ID',
'sort' => '排序',
'create_time' => 'Create Time',
'update_time' => 'Update Time',
];
}
public function behaviors() {
return [
[
'class' => \yii\behaviors\TimestampBehavior::className(),
'createdAtAttribute' => 'create_time',
'updatedAtAttribute' => 'update_time',
]
];
}
}
... ...
... ... @@ -15,14 +15,14 @@
<?php foreach (Yii::$app->params['menu'] as $controller=>$main):?>
<?php if (is_array($main[1])):?>
<li class="parent <?=(Yii::$app->controller->id == $controller) ? 'active' : ''?>"><a href="javascript:;"><i class="fa fa-suitcase"></i> <span><?=$main[0]?></span></a>
<li class="parent <?=($this->params['main_id'] == $controller) ? 'active' : ''?>"><a href="javascript:;"><i class="fa fa-suitcase"></i> <span><?=$main[0]?></span></a>
<ul class="children">
<?php foreach ($main[1] as $action=>$sub):?>
<li class="<?=(Yii::$app->controller->action->id == $action) ? 'active' : ''?>"><a href="<?php echo Yii::getAlias('@web/'.$sub[1])?>"><?=$sub[0]?></a></li>
<li class="<?=($this->params['sub_id'] == $action) ? 'active' : ''?>"><a href="<?php echo Yii::getAlias('@web/'.$sub[1])?>"><?=$sub[0]?></a></li>
<?php endforeach;?>
</ul>
<?php else:?>
<li class="<?=(Yii::$app->controller->id == $controller) ? 'active' : ''?>"><a href="<?php echo Yii::getAlias('@web/'.$main[1])?>"><i class="fa fa-suitcase"></i> <span><?=$main[0]?></span></a>
<li class="<?=($this->params['main_id'] == $controller) ? 'active' : ''?>"><a href="<?php echo Yii::getAlias('@web/'.$main[1])?>"><i class="fa fa-suitcase"></i> <span><?=$main[0]?></span></a>
<?php endif;?>
</li>
... ...
<?php
use yii\web\View;
use yii\helpers\Html;
$this->title = $this->params['main_title'].'-'.$this->params['sub_title'];
?>
<div class="col-md-6">
<form id="form" method="POST" action="">
<div class="panel panel-default">
<div class="panel-body">
<div class="row">
<div class="form-group">
<label class="col-sm-3 control-label">商品ID: <span class="asterisk">*</span></label>
<div class="col-sm-5">
<input type="text" name="product_id" class="form-control" required="required" value="<?=$model->product_id?>">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">排序: <span class="asterisk">*</span></label>
<div class="col-sm-5">
<input type="text" name="sort" class="form-control" required="required" value="<?=$model->sort?>">
</div>
</div>
</div><!-- row -->
</div><!-- panel-body -->
<div class="panel-footer">
<div class="row">
<div class="col-sm-9 col-sm-offset-3">
<button class="btn btn-primary mr5" id="upload_button">保存</button>
<a href="<?=$_refer?>" class="btn btn-dark">取消</a>
</div>
</div>
</div><!-- panel-footer -->
</div><!-- panel -->
</form>
</div>
... ...
... ... @@ -5,9 +5,15 @@ use backend\widgets\LinkPager;
$this->title = $this->params['main_title'].'-'.$this->params['sub_title'];
?>
<div class="panel">
<div class="panel-body">
<h4>房间号:<?=$room->room_id?> &nbsp;&nbsp; 直播标题:<?=$room->title?></h4>
</div>
</div>
<div class="form-group">
<label>
<a href="/live/roomcreate" class="btn btn-primary">+ 创建房间</a>
<a href="/product/add/<?=$room->room_id?>" class="btn btn-primary">+ 添加商品</a>
</label>
</div>
... ... @@ -18,24 +24,41 @@ $this->title = $this->params['main_title'].'-'.$this->params['sub_title'];
<thead>
<tr style="white-space:nowrap">
<th><input type="checkbox" onclick="checkall(this)">选择</th>
<th>房间号</th>
<th>APP</th>
<th>直播标题</th>
<th>头图</th>
<th>主播</th>
<th>预告开始时间</th>
<th>房间密钥</th>
<th>是否精选</th>
<th>马甲状态</th>
<th>直播状态</th>
<th>商品ID</th>
<th>排序</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($list as $model):?>
<tr data-stock="1" data-source="1" data-status="2" data-id="5011880">
<td><input type="checkbox" value="<?=$model->id?>" name="product[]"></td>
<td><?=$model->product_id?></td>
<td><?=$model->sort?></td>
<td>
<a href="/product/edit/<?=$model->id?>" class="btn btn-default btn-white">编辑</a>
<a onclick="return confirm('删除该商品?')" href="/product/operate/<?=$model->id?>?type=del" class="btn btn-danger btn-metro">删除</a>
<a onclick="sendProduct()" href="javascript:;" class="btn btn-danger btn-metro">弹框</a>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
</div>
<!-- table-responsive -->
</div>
<!-- col-md-12 -->
</div>
\ No newline at end of file
</div>
<script>
function sendProduct(product_id){
if (!confirm('将推送该商品,确定?')){
return;
}
alert('已发送!');
}
</script>
\ No newline at end of file
... ...