assign('breadcrumb1','红包活动');
$this->assign('breadcrumb2','余额红包');
}
/**
* 列表页
*
* */
public function index(Model $Model){
// 接收参数
$name = request('name','');
// 查询条件
$map = [];
// 组合条件
if( $name ) $map[] = ['name','=',$name];
// 查询数据
$list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10))->appends(request()->all());
// 循环处理数据
foreach ($list as $key => $value) {
// 小程序链接
$value['mp_urllink']= $this->getUrlLink($value['id']);
// 重组
$list[$key] = $value;
}
// 分配数据
$this->assign('empty', '
~~暂无数据 |
');
$this->assign('list',$list);
// 加载模板
return $this->fetch();
}
/**
* 获取小程序链接
*
*/
private function getUrlLink($id){
// 结果数据
$link = cache('admin:redpacket:active:urllink:'.$id);
// 不存在数据
if ( is_null($link) ) {
// 从数据库获取数据
$link = Mini::getUrlLink('pages/redpacket/active','id='.$id);
// 存起来
cache(['admin:redpacket:active:urllink:'.$id=>$link],$link ? now()->addDays(28) : now()->addMinutes(3));
}
// 返回结果
return $link;
}
/**
* 添加
*
* */
public function add(Request $request,Model $Model,City $City,WeiBanTagList $WeiBanTagList,Product $Product,Custom $Custom){
if( request()->isMethod('post') ){
// 验证参数
$request->scene('add')->validate();
// 接收数据
$data['logo'] = request('logo','');
$data['name'] = request('name','');
$data['rule'] = request('rule','');
$data['freq'] = request('freq',0);
$data['lucky_num'] = request('lucky_num',0);
$data['is_arrive'] = request('is_arrive',0);
$data['max_reward'] = request('max_reward',0);
$data['start_time'] = request('start_time','');
$data['end_time'] = request('end_time','');
$data['tag_scope_type'] = request('tag_scope_type',0);
$data['custom_type'] = request('custom_type',0);
$data['order_start_time']= request('order_start_time','');
$data['order_end_time'] = request('order_end_time','');
$data['custom_start_time']= request('custom_start_time','');
$data['custom_end_time']= request('custom_end_time','');
$cityIds = request('city_ids',[]);
$tagScope = request('tag_scope',[]);
$tagExcept = request('tag_except',[]);
$productScope = request('product_scope','');
$customScope = request('custom_scope','');
$phoneScope = request('phone_scope','');
// 时间
$data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
$data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
// 报单
$data['is_order'] = request('is_order',0);
$data['order_start_time']= $data['order_start_time'] ? strtotime($data['order_start_time']) : 0;
$data['order_end_time'] = $data['order_end_time'] ? strtotime($data['order_end_time']) : 0;
// 客户注册
$data['custom_start_time']= $data['custom_start_time'] ? strtotime($data['custom_start_time']) : 0;
$data['custom_end_time']= $data['custom_end_time'] ? strtotime($data['custom_end_time']) : 0;
// 城市和标签
$data['city_ids'] = implode(',',$cityIds);
$data['tag_scope'] = implode(',',$tagScope);
$data['tag_except'] = implode(',',$tagExcept);
// 范围
$productScope = scope_to_array($productScope);
$phoneScope = scope_to_array($phoneScope);
$customScope = scope_to_array($customScope);
// 产品编码批量循环转ID
foreach ($productScope as $key => $code) {
// 获取产品ID
$code = $Product->codeToId($code);
// 如果存在ID
if ( !$code ) {
// 删除
unset($productScope[$key]);
continue;
}
// 重组
$productScope[$key] = $code;
}
// 产品编码批量循环转ID
foreach ($customScope as $key => $code) {
// 获取产品ID
$code = $Custom->codeToId($code);
// 如果存在ID
if ( !$code ) {
// 删除
unset($customScope[$key]);
continue;
}
// 重组
$customScope[$key] = $code;
}
// 转字符串存储
$data['product_scope'] = implode(',',$productScope);
$data['phone_scope'] = implode(',',$phoneScope);
$data['custom_scope'] = implode(',',$customScope);
// 状态
$data['status'] = 1;
// 如果要下单的话,判断订单时间
if( $data['custom_type'] ) {
if( !$data['custom_start_time'] || !$data['custom_end_time'] ) return json_send(['code'=>'error','msg'=>'限定注册时间']);
}
// 如果要下单的话,判断订单时间
if( $data['product_scope'] ) {
if( !$data['is_order'] ) return json_send(['code'=>'error','msg'=>'限定下单产品时,是否报单请选择是,并限定下单时间']);
}
// 如果要下单的话,判断订单时间
if( $data['is_order'] ) {
if( !$data['order_start_time'] || !$data['order_end_time'] ) return json_send(['code'=>'error','msg'=>'下单时间不能为空']);
}
// 如果操作失败
if( $data['max_reward'] > $data['lucky_num'] ) return json_send(['code'=>'error','msg'=>'中奖次数不能大于抽奖次数']);
// 写入数据表
$id = $Model->add($data);
// 如果操作失败
if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
// 记录行为
$this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
// 告知结果
return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
}
// 获取列表
$cityList = $City->getCityList();
// 标签列表
$tagData = $WeiBanTagList->query()->get(['tag_group_name as group','tag_name as name'])->toArray();
// 标签列表
$tagList = [];
// 循环数据
foreach ($tagData as $value) {
$tagList[$value['group']][] = $value['name'];
}
// 分配数据
$this->assign('cityList',$cityList);
$this->assign('tagList',$tagList);
// 分配数据
$this->assign('crumbs','新增');
// 加载模板
return $this->fetch();
}
/**
* 修改
*
* */
public function edit(Request $request,Model $Model,City $City,WeiBanTagList $WeiBanTagList,Product $Product,Custom $Custom){
// 接收参数
$id = request('id',0);
// 查询用户
$oldData = $Model->where(['id'=>$id])->first();
// 修改
if(request()->isMethod('post')){
// 验证参数
$request->scene('edit')->validate();
/// 接收数据
$data['logo'] = request('logo','');
$data['name'] = request('name','');
$data['rule'] = request('rule','');
$data['freq'] = request('freq',0);
$data['lucky_num'] = request('lucky_num',0);
$data['is_arrive'] = request('is_arrive',0);
$data['max_reward'] = request('max_reward',0);
$data['start_time'] = request('start_time','');
$data['end_time'] = request('end_time','');
$data['tag_scope_type'] = request('tag_scope_type',0);
$data['custom_type'] = request('custom_type',0);
$data['order_start_time']= request('order_start_time','');
$data['order_end_time'] = request('order_end_time','');
$data['custom_start_time']= request('custom_start_time','');
$data['custom_end_time']= request('custom_end_time','');
$cityIds = request('city_ids',[]);
$tagScope = request('tag_scope',[]);
$tagExcept = request('tag_except',[]);
$productScope = request('product_scope','');
$customScope = request('custom_scope','');
$phoneScope = request('phone_scope','');
// 时间
$data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
$data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
// 报单
$data['is_order'] = request('is_order',0);
$data['order_start_time']= $data['order_start_time'] ? strtotime($data['order_start_time']) : 0;
$data['order_end_time'] = $data['order_end_time'] ? strtotime($data['order_end_time']) : 0;
// 客户注册
$data['custom_start_time']= $data['custom_start_time'] ? strtotime($data['custom_start_time']) : 0;
$data['custom_end_time']= $data['custom_end_time'] ? strtotime($data['custom_end_time']) : 0;
// 城市和标签
$data['city_ids'] = implode(',',$cityIds);
$data['tag_scope'] = implode(',',$tagScope);
$data['tag_except'] = implode(',',$tagExcept);
// 范围
$productScope = scope_to_array($productScope);
$phoneScope = scope_to_array($phoneScope);
$customScope = scope_to_array($customScope);
// 产品编码批量循环转ID
foreach ($productScope as $key => $code) {
// 获取产品ID
$code = $Product->codeToId($code);
// 如果存在ID
if ( !$code ) {
// 删除
unset($productScope[$key]);
continue;
}
// 重组
$productScope[$key] = $code;
}
// 产品编码批量循环转ID
foreach ($customScope as $key => $code) {
// 获取产品ID
$code = $Custom->codeToId($code);
// 如果存在ID
if ( !$code ) {
// 删除
unset($customScope[$key]);
continue;
}
// 重组
$customScope[$key] = $code;
}
// 转字符串存储
$data['product_scope'] = implode(',',$productScope);
$data['phone_scope'] = implode(',',$phoneScope);
$data['custom_scope'] = implode(',',$customScope);
// 如果要下单的话,判断订单时间
if( $data['custom_type'] ) {
if( !$data['custom_start_time'] || !$data['custom_end_time'] ) return json_send(['code'=>'error','msg'=>'限定注册时间']);
}
// 如果要下单的话,判断订单时间
if( $data['product_scope'] ) {
if( !$data['is_order'] ) return json_send(['code'=>'error','msg'=>'限定下单产品时,是否报单请选择是,并限定下单时间']);
}
// 如果要下单的话,判断订单时间
if( $data['is_order'] ) {
if( !$data['order_start_time'] || !$data['order_end_time'] ) return json_send(['code'=>'error','msg'=>'下单时间不能为空']);
}
// 如果操作失败
if( $data['max_reward'] > $data['lucky_num'] ) return json_send(['code'=>'error','msg'=>'中奖次数不能大于抽奖次数']);
// 写入数据表
$result = $Model->edit($id,$data);
// 如果操作失败
if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
// 记录行为
$this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
// 告知结果
return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
}
// 错误告知
if( !$oldData ) return $this->error('查无数据');
// 获取城市ID
$oldData['city_ids'] = explode(',',$oldData['city_ids']);
$oldData['tag_scope'] = explode(',',$oldData['tag_scope']);
$oldData['tag_except'] = explode(',',$oldData['tag_except']);
$productScope = $oldData['product_scope'] ? explode(',',$oldData['product_scope']) : [];
$phoneScope = $oldData['phone_scope'] ? explode(',',$oldData['phone_scope']) : [];
$customScope = $oldData['custom_scope'] ? explode(',',$oldData['custom_scope']) : [];
// 产品编码批量循环转ID
foreach ($productScope as $key => $value) {
// 重组
$productScope[$key] = $Product->idToCode($value);
}
// 产品编码批量循环转ID
foreach ($customScope as $key => $value) {
// 重组
$customScope[$key] = $Custom->idToCode($value);
}
// 转数据
$oldData['product_scope'] = implode("\n",$productScope);
$oldData['phone_scope'] = implode("\n",$phoneScope);
$oldData['custom_scope'] = implode("\n",$customScope);
// 获取列表
$cityList = $City->getCityList();
// 标签列表
$tagData = $WeiBanTagList->query()->get(['tag_group_name as group','tag_name as name'])->toArray();
// 标签列表
$tagList = [];
// 循环数据
foreach ($tagData as $value) {
$tagList[$value['group']][] = $value['name'];
}
// 分配数据
$this->assign('cityList',$cityList);
$this->assign('tagList',$tagList);
$this->assign('oldData',$oldData);
$this->assign('crumbs','修改');
// 加载模板
return $this->fetch();
}
/**
* 修改状态
*
* */
public function set_status(Request $request,Model $Model,ActiveReward $ActiveReward){
// 验证参数
$request->scene('set_status')->validate();
// 设置状态
$id = request('id',0);
$status = request('status',0);
// 查询用户
$oldData = $Model->where(['id'=>$id])->first();
// 如果用户不存在
if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
// 如果设置上架
if( !$status ) {
// 判断奖品个数
$count = $ActiveReward->query()->where([['active_id','=',$id],['status','=',0]])->count();
// 获取统计数量
if( !$count ) return json_send(['code'=>'error','msg'=>'请设置奖项再启用']);
}
// 执行修改
$result = $Model->edit($id,['status'=>$status]);
// 提示新增失败
if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
// 记录行为
$this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
// 告知结果
return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
}
}