ProductController.php
4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace backend\controllers;
use Yii;
use backend\components\Pagination;
use common\lib\YohoApi\Client as YohoApiClient;
/**
* Live controller
*/
class ProductController extends BaseController
{
public function init()
{
parent::init();
$this->main_id = 'live';
$this->sub_id = 'room';
$this->sub_title = '商品管理';
}
/**
* 房间商品列表
* @return type
*/
public function actionList()
{
if (!$room_id = Yii::$app->getRequest()->getQueryParam('id')){
return $this->redirect($this->_refer);
}
$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])
->indexBy('product_id')
->all();
/**
* 获取有货商品信息
*/
$productSkn = [];
foreach ($list as $model){
$productSkn[] = $model->product_id;
}
if ($productSkn){
$ret = YohoApiClient::self()->h5ProductBatch($productSkn);
if (empty($ret['product_list'])){
array_shift($productSkn);
$ret = YohoApiClient::self()->h5ProductBatch($productSkn);
}
//var_dump(YohoApiClient::self()->h5ProductBatch([51188407,51188408,51188414,51188421,51188468]));
//var_dump(YohoApiClient::getRequestUrl());
if (!empty($ret['product_list'])){
foreach ($ret['product_list'] as $product){
$list[$product['product_skn']]->product_name = $product['product_name'];
$list[$product['product_skn']]->sales_price = $product['sales_price'];
}
}
}
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('warning', current($model->getFirstErrors()));
$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', '编辑成功。');
return $this->redirect($this->_refer);
}else{
Yii::$app->session->setFlash('warning', current($model->getFirstErrors()));
}
}
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);
}
}