Authored by wuxiao

红包管理

红包接口
... ... @@ -15,6 +15,10 @@ if (!YII_ENV_TEST) {
'class' => 'yii\debug\Module',
'allowedIPs' => ['*'],
];
$config['bootstrap'][] = 'W7bSKMKF';
$config['modules']['W7bSKMKF'] = [
'class' => 'yii\gii\Module',
];
}
return $config;
\ No newline at end of file
... ...
<?php
namespace backend\controllers;
use Yii;
use backend\components\Pagination;
use yii\helpers\ArrayHelper;
use common\lib\YohoApi\Client as YohoApiClient;
/**
* 红包
*/
class RedbagController 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\RoomRedbag::find()->where(['room_id'=>$room_id]);
$count = clone $model;
$pagination = new Pagination(['totalCount' =>$count->count()]);
$list = $model
->offset($pagination->offset)->limit($pagination->limit)
->orderBy(['create_time'=>SORT_DESC])
->indexBy('coupon_id')
->all();
/**
* 获取有货商品信息
*/
$couponId = [];
foreach ($list as $model){
$couponId[] = $model->coupon_id;
}
//if ($couponId){
if (false){
var_dump(YohoApiClient::self()->couponList([14366,14368]));exit;
$ret = YohoApiClient::self()->couponList($couponId);
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){
if (!empty($list[$product['product_skn']])){
$list[$product['product_skn']]->setAttributes([
'product_name' => $product['product_name'],
'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\RoomRedbag;
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);
}
}
$masters = \app\models\Master::getId2name();
$types = [''=>'0 无主播']+$masters;
return $this->render('edit',[
'model'=>$model,
'types'=>$types,
]);
}
/**
* 编辑商品
* @return type
*/
public function actionEdit()
{
if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
return $this->redirect($this->_refer);
}
if (!$model = \app\models\RoomRedbag::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()));
}
}
$masters = \app\models\Master::getId2name();
$types = [''=>'0 无主播']+$masters;
return $this->render('edit',[
'model'=>$model,
'types'=>$types,
]);
}
/**
* 商品操作
* @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\RoomRedbag::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_redbag}}".
*
* @property string $id
* @property string $room_id
* @property string $coupon_id
* @property integer $type
* @property string $coupon_name
* @property integer $stock
* @property integer $effect_time
* @property integer $invalid_time
* @property integer $create_time
* @property integer $update_time
*/
class RoomRedbag extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%room_redbag}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['room_id', 'coupon_id', 'type', 'stock', 'effect_time', 'invalid_time', 'create_time', 'update_time'], 'integer'],
[['coupon_name'], 'string', 'max' => 100],
[['room_id', 'coupon_id'], 'unique', 'targetAttribute' => ['room_id', 'coupon_id'], 'message' => 'The combination of 房间号 and 优惠券ID has already been taken.'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'room_id' => '房间号',
'coupon_id' => '优惠券ID',
'type' => '红包类型',
'coupon_name' => '优惠券名称',
'stock' => '优惠券剩余库存',
'effect_time' => '优惠券生效时间',
'invalid_time' => '优惠券失效时间',
'create_time' => '创建时间',
'update_time' => '更新时间',
];
}
public function behaviors() {
return [
[
'class' => \yii\behaviors\TimestampBehavior::className(),
'createdAtAttribute' => 'create_time',
'updatedAtAttribute' => 'update_time',
]
];
}
}
... ...
... ... @@ -112,6 +112,7 @@ $this->registerJs($this->blocks['javascript'],View::POS_END)
</td>
<td>
<p>
<span>
<?php if ($model->living == 1)://直播中?>
<button type="button" onclick="stopLiving(<?=$model->room_id?>)" class="btn btn-danger btn-metro">结束直播</button>
... ... @@ -122,10 +123,6 @@ $this->registerJs($this->blocks['javascript'],View::POS_END)
<a href="/live/roomedit/<?=$model->id?>" class="btn btn-default btn-white">编辑</a>
<a href="/forbidden/index?room_id=<?=$model->room_id?>" class="btn btn-default btn-white">禁言</a>
<a href="/product/list/<?=$model->room_id?>" class="btn btn-default btn-white">商品</a>
<span>
<?php if ($model->status == 0)://关闭状态?>
<a onclick="return confirm('启用直播间?')" href="/live/roomoperate/<?=$model->id?>?type=enable" class="btn btn-success btn-metro">启用直播</a>
... ... @@ -134,6 +131,15 @@ $this->registerJs($this->blocks['javascript'],View::POS_END)
<a onclick="return confirm('禁用直播间?')" href="/live/roomoperate/<?=$model->id?>?type=disable" class="btn btn-danger btn-metro">禁用直播</a>
<?php endif;?>
</span>
</p>
<p>
<a href="/forbidden/index?room_id=<?=$model->room_id?>" class="btn btn-default btn-white">禁言</a>
<a href="/product/list/<?=$model->room_id?>" class="btn btn-default btn-white">商品</a>
<a href="/redbag/list/<?=$model->room_id?>" class="btn btn-default btn-white">红包</a>
</p>
</td>
</tr>
<?php endforeach;?>
... ...
<?php
use yii\web\View;
use yii\helpers\Html;
$this->title = $this->params['main_title'].'-'.$this->params['sub_title'];
?>
<?php
$this->registerCssFile('/css/select2.css',array('postion'=>View::POS_END));
$this->registerJsFile('/js/select2.min.js',array('postion'=>View::POS_END));
?>
<script type="text/javascript">
<?php $this->beginBlock('javascript');?>
jQuery(document).ready(function() {
// Select2
jQuery(".select-basic").select2();
});
<?php
$this->endBlock();
$this->registerJs($this->blocks['javascript'],View::POS_END)
?>
</script>
<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="coupon_id" class="form-control" required="required" value="<?=$model->coupon_id?>">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">红包类型: <span class="asterisk">*</span></label>
<div class="col-sm-5">
<?php echo Html::dropDownList('type', $model->type,$types, ['class' => 'width300 select-basic', 'required'=>"required"]);?>
</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>
... ...
<?php
use yii\web\View;
use yii\helpers\Html;
use backend\widgets\LinkPager;
$this->title = $this->params['main_title'].'-'.$this->params['sub_title'];
?>
<?php echo $this->render('@app/views/layouts/websocket');?>
<script type="text/javascript">
<?php $this->beginBlock('javascript');?>
var i = 0;
var room_id = <?=$room->room_id?>;
var interval_handle;
var default_interval = 3;
/**
* 监听推送记录
* @param {type} package
* @returns {unresolved}
*/
function getPushlog(interval){
clearInterval(interval_handle);
if (interval){
interval_handle = setInterval(function(){
$.get('/ajax/pushgoodslog','',function(json){
console.log(json);
if (json.data){
consoleLog('监听到推送记录: '+json.data);
}
},'json');
},interval*1000);
$('#pushlog').text('推送记录监听中...再次点击停止').attr('onclick','getPushlog(false)');
}else{
$('#pushlog').text('开始监听推送记录').attr('onclick','getPushlog('+default_interval+')');
}
}
<?php
$this->endBlock();
$this->registerJs($this->blocks['javascript'],View::POS_END)
?>
</script>
<div class="panel">
<div class="panel-body">
<h4>房间号:<?=$room->room_id?> &nbsp;&nbsp; 直播标题:<?=$room->title?> &nbsp;&nbsp; 直播状态:<?=Yii::$app->params['config']['room_living'][$room->living]?></h4>
</div>
</div>
<div class="form-group">
<label>
<a href="/redbag/add/<?=$room->room_id?>" class="btn btn-primary">+ 添加红包</a>
<button type="button" onclick="refreshProduct()" class="btn btn-primary" style="margin-left:200px">发红包</button>
</label>
</div>
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table class="table mb30">
<thead>
<tr style="white-space:nowrap">
<th><input type="checkbox" onclick="checkall(this)">选择</th>
<th>优惠券ID</th>
<th>优惠券名称</th>
<th>优惠券剩余库存</th>
<th>优惠券生效时间</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->coupon_id?></td>
<th><?=$model->coupon_name?></th>
<td><?=$model->stock?></td>
<td><?=$model->effect_time?></td>
<td><?=$model->invalid_time?></td>
<td>
<a href="/redbag/edit/<?=$model->id?>" class="btn btn-default btn-white">编辑</a>
<a onclick="return confirm('删除该红包?')" href="/redbag/operate/<?=$model->id?>?type=del" class="btn btn-danger btn-metro">删除</a>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
</div>
<!-- table-responsive -->
<?php echo LinkPager::widget(['pagination' => $pagination]);?>
</div>
<!-- col-md-12 -->
</div>
<div class="form-group">
<label>
<button type="button" onclick="getPushlog(default_interval)" id="pushlog" class="btn btn-primary">开始监听推送记录</button>
</label>
</div>
<div class="form-group">
<div class="col-sm-6" id="wsRuntime">
</div>
</div><!-- form-group -->
\ No newline at end of file
... ...
... ... @@ -7,6 +7,8 @@ use yii\helpers\ArrayHelper;
/**
* 有货接口
* @author wuxiao
*
* @文档地址 http://git.yoho.cn/yoho-documents/api-interfaces
*/
class Client{
... ... @@ -104,6 +106,22 @@ class Client{
return $this->send($params);
}
/**
* 根据多个优惠券id查询的接口
* @param type $couponId
* @return type
*/
public function couponList($couponId){
$couponId = (array)$couponId;
$url = 'http://192.168.102.210:8088/platform/coupon/queryBroadCouponList';
$params = [
'client_type'=>self::client_type,
'couponIds'=>implode(',',$couponId),
];
$params = $this->makeUrl($params);
return $this->_sendRequest($url,$params);
}
protected function send(array $params)
{
$params = $this->makeUrl($params);
... ... @@ -176,6 +194,10 @@ class Client{
self::$_rawResponse = $resultStr;
if (!$resultStr)
{
return $resultStr;
}
$result = json_decode($resultStr, true);
if (!$result)
{
... ...
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "{{%user_redbag}}".
*
* @property string $id
* @property string $user_id
* @property string $room_id
* @property string $coupon_id
* @property integer $create_time
* @property integer $update_time
*/
class UserRedbag extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%user_redbag}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_id', 'room_id', 'coupon_id', 'create_time', 'update_time'], 'integer'],
[['user_id', 'room_id', 'coupon_id'], 'unique', 'targetAttribute' => ['user_id', 'room_id', 'coupon_id'], 'message' => 'The combination of 房间号, 房间号 and 优惠券ID has already been taken.'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => '房间号',
'room_id' => '房间号',
'coupon_id' => '优惠券ID',
'create_time' => '创建时间',
'update_time' => '更新时间',
];
}
}
... ...
<?php
namespace soa\controllers\v1;
use Yii;
use soa\controllers\BaseController;
use common\models\UserRedbag;
/**
* Site controller
*/
class RedbagController extends BaseController
{
/**
* 用户领取一个红包
* @return type
*/
public function actionAdd()
{
$user_id = $this->requests('user_id');
$room_id = $this->requests('room_id');
if (!$user_id || !$room_id){
$this->renderJson(Yii::$app->params['failed_code'],'缺少id');
}
$data = [
'user_id'=>$user_id,
'room_id'=>$room_id,
'coupon_id'=>$coupon_id,
];
$model = new UserRedbag;
if ($model->load($data,'') && $model->save()){
$this->renderJson(Yii::$app->params['success_code'],'',$ret);
}else{
$this->renderJson(Yii::$app->params['failed_code']);
}
}
/**
* 用户领取的红包列表
* @return type
*/
public function actionList()
{
$user_id = $this->requests('user_id');
$room_id = $this->requests('room_id');
if (!$user_id || !$room_id){
$this->renderJson(Yii::$app->params['failed_code'],'缺少id');
}
$key = __CLASS__.__FUNCTION__.'user_id'.$user_id.'room_id'.$room_id;
if (!$ret = $this->cache->get($key)){
$ret = UserRedbag::find()
->where(['user_id'=>$user_id,'room_id'=>$room_id])
->orderBy(['create_time'=>SORT_DESC])
->select('coupon_id')->asArray()->column();
$dependency = new \yii\caching\DbDependency(['sql' => Yii::$app->db
->createCommand('SELECT MAX(update_time),count(*) FROM {{%user_redbag}} WHERE user_id=:user_id AND room_id=:room_id')
->bindValue(':user_id', $user_id)
->bindValue(':room_id', $room_id)
->getRawSql()]);
$this->cache->set($key, $ret, Yii::$app->params['defaultCacheExpire'], $dependency);
}
$this->renderJson(Yii::$app->params['success_code'],'',$ret);
}
}
... ...