Authored by wuxiao

修改左侧菜单逻辑

直播间关联商品管理
@@ -21,6 +21,11 @@ class BaseController extends Controller @@ -21,6 +21,11 @@ class BaseController extends Controller
21 21
22 //用户实例 22 //用户实例
23 protected $user; 23 protected $user;
  24 +
  25 + protected $main_id;//一级菜单id
  26 + protected $sub_id;//二级菜单id
  27 + protected $main_title;//一级菜单名称
  28 + protected $sub_title;//二级菜单名称
24 29
25 public function init() 30 public function init()
26 { 31 {
@@ -50,8 +55,12 @@ class BaseController extends Controller @@ -50,8 +55,12 @@ class BaseController extends Controller
50 $this->_refer = \common\lib\Referer::get(); 55 $this->_refer = \common\lib\Referer::get();
51 56
52 $view = $this->getView(); 57 $view = $this->getView();
53 - $view->params['main_title'] = @Yii::$app->params['menu'][Yii::$app->controller->id][0] ? : '';  
54 - $view->params['sub_title'] = @Yii::$app->params['menu'][Yii::$app->controller->id][1][Yii::$app->controller->action->id][0] ? : ''; 58 + $main_id = $this->main_id ? : Yii::$app->controller->id;
  59 + $sub_id = $this->sub_id ? : Yii::$app->controller->action->id;
  60 + $view->params['main_id'] = $main_id;
  61 + $view->params['sub_id'] = $sub_id;
  62 + $view->params['main_title'] = $this->main_title ? : (@Yii::$app->params['menu'][$main_id][0] ? : '');
  63 + $view->params['sub_title'] = $this->sub_title ? : (@Yii::$app->params['menu'][$main_id][1][$sub_id][0] ? : '');
55 64
56 return parent::beforeAction($action); 65 return parent::beforeAction($action);
57 } 66 }
@@ -2,12 +2,24 @@ @@ -2,12 +2,24 @@
2 namespace backend\controllers; 2 namespace backend\controllers;
3 3
4 use Yii; 4 use Yii;
  5 +use backend\components\Pagination;
  6 +use yii\helpers\ArrayHelper;
  7 +
5 /** 8 /**
6 * Live controller 9 * Live controller
7 */ 10 */
8 class ProductController extends BaseController 11 class ProductController extends BaseController
9 { 12 {
10 13
  14 + public function init()
  15 + {
  16 + parent::init();
  17 +
  18 + $this->main_id = 'live';
  19 + $this->sub_id = 'room';
  20 + $this->sub_title = '商品管理';
  21 + }
  22 +
11 /** 23 /**
12 * 房间商品列表 24 * 房间商品列表
13 * @return type 25 * @return type
@@ -18,7 +30,111 @@ class ProductController extends BaseController @@ -18,7 +30,111 @@ class ProductController extends BaseController
18 return $this->redirect($this->_refer); 30 return $this->redirect($this->_refer);
19 } 31 }
20 32
21 - return $this->render('list'); 33 + $model = \app\models\RoomProduct::find()->where(['room_id'=>$room_id]);
  34 +
  35 + $count = clone $model;
  36 + $pagination = new Pagination(['totalCount' =>$count->count()]);
  37 + $list = $model
  38 + ->offset($pagination->offset)->limit($pagination->limit)
  39 + ->orderBy(['sort'=>SORT_ASC,'create_time'=>SORT_DESC])
  40 + ->all();
  41 +
  42 + return $this->render('list',[
  43 + 'room'=> \app\models\Room::findOne(['room_id'=>$room_id]),
  44 + 'pagination'=>$pagination,
  45 + 'list'=>$list,
  46 + ]);
22 } 47 }
23 48
  49 + /**
  50 + * 添加商品
  51 + * @return type
  52 + */
  53 + public function actionAdd()
  54 + {
  55 + if (!$room_id = Yii::$app->getRequest()->getQueryParam('id')){
  56 + return $this->redirect($this->_refer);
  57 + }
  58 +
  59 + $model = new \app\models\RoomProduct;
  60 + if (Yii::$app->getRequest()->isPost){
  61 + $post = Yii::$app->getRequest()->post();
  62 + $model->room_id = $room_id;
  63 + if ($model->load($post,'') && $model->save()){
  64 + Yii::$app->session->setFlash('success', '创建成功。');
  65 + return $this->redirect($this->_refer);
  66 + }else{
  67 + Yii::$app->session->setFlash('error', '创建失败。');
  68 + $model->setAttributes($post);
  69 + }
  70 + }
  71 +
  72 + return $this->render('edit',[
  73 + 'model'=>$model,
  74 + ]);
  75 + }
  76 +
  77 + /**
  78 + * 编辑商品
  79 + * @return type
  80 + */
  81 + public function actionEdit()
  82 + {
  83 + if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
  84 + return $this->redirect($this->_refer);
  85 + }
  86 +
  87 + if (!$model = \app\models\RoomProduct::findOne($id)){
  88 + Yii::$app->session->setFlash('error', '找不到该商品');
  89 + return $this->redirect($this->_refer);
  90 + }
  91 +
  92 + if (Yii::$app->getRequest()->isPost){
  93 + $post = Yii::$app->getRequest()->post();
  94 + if ($model->load($post,'') && $model->save()){
  95 + Yii::$app->session->setFlash('success', '编辑成功。');
  96 + $model->refresh();
  97 + }else{
  98 + Yii::$app->session->setFlash('error', '编辑失败。');
  99 + }
  100 + }
  101 +
  102 + return $this->render('edit',[
  103 + 'model'=>$model,
  104 + ]);
  105 + }
  106 +
  107 + /**
  108 + * 商品操作
  109 + * @return type
  110 + */
  111 + public function actionOperate()
  112 + {
  113 + if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
  114 + return $this->redirect($this->_refer);
  115 + }
  116 + if (!$type = Yii::$app->getRequest()->getQueryParam('type')){
  117 + return $this->redirect($this->_refer);
  118 + }
  119 +
  120 + if (!$model = \app\models\RoomProduct::findOne($id)){
  121 + Yii::$app->session->setFlash('error', '找不到该商品');
  122 + return $this->redirect($this->_refer);
  123 + }
  124 +
  125 + switch ($type){
  126 + case 'del'://删除
  127 + $r = $model->delete();
  128 + break;
  129 + default:
  130 + break;
  131 + }
  132 + if (!empty($r)){
  133 + Yii::$app->session->setFlash('success', '操作成功');
  134 + }else{
  135 + Yii::$app->session->setFlash('error', '操作失败');
  136 + }
  137 +
  138 + return $this->redirect($this->_refer);
  139 + }
24 } 140 }
  1 +<?php
  2 +
  3 +namespace app\models;
  4 +
  5 +use Yii;
  6 +
  7 +/**
  8 + * This is the model class for table "{{%room_product}}".
  9 + *
  10 + * @property string $id
  11 + * @property string $room_id
  12 + * @property string $product_skn
  13 + * @property integer $sort
  14 + * @property integer $create_time
  15 + * @property integer $update_time
  16 + */
  17 +class RoomProduct extends \yii\db\ActiveRecord
  18 +{
  19 + /**
  20 + * @inheritdoc
  21 + */
  22 + public static function tableName()
  23 + {
  24 + return '{{%room_product}}';
  25 + }
  26 +
  27 + /**
  28 + * @inheritdoc
  29 + */
  30 + public function rules()
  31 + {
  32 + return [
  33 + [['room_id'], 'required'],
  34 + [['room_id', 'product_id', 'sort', 'create_time', 'update_time'], 'integer'],
  35 + ];
  36 + }
  37 +
  38 + /**
  39 + * @inheritdoc
  40 + */
  41 + public function attributeLabels()
  42 + {
  43 + return [
  44 + 'id' => 'ID',
  45 + 'room_id' => '房间ID',
  46 + 'product_id' => '商品ID',
  47 + 'sort' => '排序',
  48 + 'create_time' => 'Create Time',
  49 + 'update_time' => 'Update Time',
  50 + ];
  51 + }
  52 +
  53 + public function behaviors() {
  54 + return [
  55 + [
  56 + 'class' => \yii\behaviors\TimestampBehavior::className(),
  57 + 'createdAtAttribute' => 'create_time',
  58 + 'updatedAtAttribute' => 'update_time',
  59 + ]
  60 + ];
  61 + }
  62 +}
@@ -15,14 +15,14 @@ @@ -15,14 +15,14 @@
15 <?php foreach (Yii::$app->params['menu'] as $controller=>$main):?> 15 <?php foreach (Yii::$app->params['menu'] as $controller=>$main):?>
16 16
17 <?php if (is_array($main[1])):?> 17 <?php if (is_array($main[1])):?>
18 - <li class="parent <?=(Yii::$app->controller->id == $controller) ? 'active' : ''?>"><a href="javascript:;"><i class="fa fa-suitcase"></i> <span><?=$main[0]?></span></a> 18 + <li class="parent <?=($this->params['main_id'] == $controller) ? 'active' : ''?>"><a href="javascript:;"><i class="fa fa-suitcase"></i> <span><?=$main[0]?></span></a>
19 <ul class="children"> 19 <ul class="children">
20 <?php foreach ($main[1] as $action=>$sub):?> 20 <?php foreach ($main[1] as $action=>$sub):?>
21 - <li class="<?=(Yii::$app->controller->action->id == $action) ? 'active' : ''?>"><a href="<?php echo Yii::getAlias('@web/'.$sub[1])?>"><?=$sub[0]?></a></li> 21 + <li class="<?=($this->params['sub_id'] == $action) ? 'active' : ''?>"><a href="<?php echo Yii::getAlias('@web/'.$sub[1])?>"><?=$sub[0]?></a></li>
22 <?php endforeach;?> 22 <?php endforeach;?>
23 </ul> 23 </ul>
24 <?php else:?> 24 <?php else:?>
25 - <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> 25 + <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>
26 <?php endif;?> 26 <?php endif;?>
27 </li> 27 </li>
28 28
  1 +<?php
  2 +use yii\web\View;
  3 +use yii\helpers\Html;
  4 +
  5 +$this->title = $this->params['main_title'].'-'.$this->params['sub_title'];
  6 +?>
  7 +
  8 +<div class="col-md-6">
  9 + <form id="form" method="POST" action="">
  10 + <div class="panel panel-default">
  11 +
  12 + <div class="panel-body">
  13 + <div class="row">
  14 + <div class="form-group">
  15 + <label class="col-sm-3 control-label">商品ID: <span class="asterisk">*</span></label>
  16 + <div class="col-sm-5">
  17 + <input type="text" name="product_id" class="form-control" required="required" value="<?=$model->product_id?>">
  18 + </div>
  19 + </div>
  20 +
  21 + <div class="form-group">
  22 + <label class="col-sm-3 control-label">排序: <span class="asterisk">*</span></label>
  23 + <div class="col-sm-5">
  24 + <input type="text" name="sort" class="form-control" required="required" value="<?=$model->sort?>">
  25 + </div>
  26 + </div>
  27 +
  28 + </div><!-- row -->
  29 + </div><!-- panel-body -->
  30 + <div class="panel-footer">
  31 + <div class="row">
  32 + <div class="col-sm-9 col-sm-offset-3">
  33 + <button class="btn btn-primary mr5" id="upload_button">保存</button>
  34 + <a href="<?=$_refer?>" class="btn btn-dark">取消</a>
  35 + </div>
  36 + </div>
  37 + </div><!-- panel-footer -->
  38 + </div><!-- panel -->
  39 + </form>
  40 +
  41 +</div>
@@ -5,9 +5,15 @@ use backend\widgets\LinkPager; @@ -5,9 +5,15 @@ use backend\widgets\LinkPager;
5 $this->title = $this->params['main_title'].'-'.$this->params['sub_title']; 5 $this->title = $this->params['main_title'].'-'.$this->params['sub_title'];
6 ?> 6 ?>
7 7
  8 +<div class="panel">
  9 + <div class="panel-body">
  10 + <h4>房间号:<?=$room->room_id?> &nbsp;&nbsp; 直播标题:<?=$room->title?></h4>
  11 + </div>
  12 +</div>
  13 +
8 <div class="form-group"> 14 <div class="form-group">
9 <label> 15 <label>
10 - <a href="/live/roomcreate" class="btn btn-primary">+ 创建房间</a> 16 + <a href="/product/add/<?=$room->room_id?>" class="btn btn-primary">+ 添加商品</a>
11 </label> 17 </label>
12 </div> 18 </div>
13 19
@@ -18,24 +24,41 @@ $this->title = $this->params['main_title'].'-'.$this->params['sub_title']; @@ -18,24 +24,41 @@ $this->title = $this->params['main_title'].'-'.$this->params['sub_title'];
18 <thead> 24 <thead>
19 <tr style="white-space:nowrap"> 25 <tr style="white-space:nowrap">
20 <th><input type="checkbox" onclick="checkall(this)">选择</th> 26 <th><input type="checkbox" onclick="checkall(this)">选择</th>
21 - <th>房间号</th>  
22 - <th>APP</th>  
23 - <th>直播标题</th>  
24 - <th>头图</th>  
25 - <th>主播</th>  
26 - <th>预告开始时间</th>  
27 - <th>房间密钥</th>  
28 - <th>是否精选</th>  
29 - <th>马甲状态</th>  
30 - <th>直播状态</th> 27 + <th>商品ID</th>
  28 + <th>排序</th>
31 <th>操作</th> 29 <th>操作</th>
32 </tr> 30 </tr>
33 </thead> 31 </thead>
34 <tbody> 32 <tbody>
  33 + <?php foreach ($list as $model):?>
  34 + <tr data-stock="1" data-source="1" data-status="2" data-id="5011880">
  35 + <td><input type="checkbox" value="<?=$model->id?>" name="product[]"></td>
  36 + <td><?=$model->product_id?></td>
  37 + <td><?=$model->sort?></td>
  38 + <td>
  39 +
  40 + <a href="/product/edit/<?=$model->id?>" class="btn btn-default btn-white">编辑</a>
  41 +
  42 + <a onclick="return confirm('删除该商品?')" href="/product/operate/<?=$model->id?>?type=del" class="btn btn-danger btn-metro">删除</a>
  43 +
  44 + <a onclick="sendProduct()" href="javascript:;" class="btn btn-danger btn-metro">弹框</a>
  45 +
  46 + </td>
  47 + </tr>
  48 + <?php endforeach;?>
35 </tbody> 49 </tbody>
36 </table> 50 </table>
37 </div> 51 </div>
38 <!-- table-responsive --> 52 <!-- table-responsive -->
39 </div> 53 </div>
40 <!-- col-md-12 --> 54 <!-- col-md-12 -->
41 -</div>  
  55 +</div>
  56 +
  57 +<script>
  58 + function sendProduct(product_id){
  59 + if (!confirm('将推送该商品,确定?')){
  60 + return;
  61 + }
  62 + alert('已发送!');
  63 + }
  64 +</script>