assign('breadcrumb1','任务打卡');
$this->assign('breadcrumb2','打卡任务');
}
/**
* 首页列表
*
* */
public function index(Model $Model, Coupon $Coupon){
$activeId = request('active_id',0);
// 组合查询条件
$map = [];
if( $activeId ) $map[] = ['active_id','=',$activeId];
// 查询数据
$list = $Model->where($map)->orderBy('what_day')->orderBy('id')->paginate(request('limit',config('page_num',10)))->appends(request()->all());
foreach ($list as $k=>$v){
if ($v['coupon_id']) {
$v['coupon_code'] = $Coupon->idToCode($v['coupon_id']);
$coupon = $Coupon->getOne($v['coupon_id']);
if ($coupon) $v['coupon_name'] = $coupon['name'];
}
$list[$k] = $v;
}
// 分配数据
$this->assign('empty', '
~~暂无数据 |
');
$this->assign('list', $list);
$this->assign('active_id', $activeId);
// 加载模板
return $this->fetch();
}
/**
* 添加
*
* */
public function add( Request $request,Model $Model,Coupon $Coupon){
if( request()->isMethod('post') ){
// 验证参数
$request->scene('add')->validate();
// 组合数据
$data['what_day'] = (int) request('what_day',0);
$data['reward'] = (int) request('reward',0);
$data['active_id'] = request('active_id',0);
$data['status'] = 0;
$coupon_code = request('coupon_code','');
$data['coupon_id'] = $Coupon->codeToId($coupon_code);
// 查询是否已经有
$oldDId = $Model->query()->where([['what_day','=',$data['what_day']],['active_id','=',$data['active_id']]])->value('id');
// 提示新增失败
if( $oldDId ) return json_send(['code'=>'error','msg'=>'打卡天数已存在']);
// 写入用户表
$id = $Model->add($data);
// 提示新增失败
if( isset($id['error']) ) return json_send(['code'=>'error','msg'=>$id['error']]);
// 记录行为
$this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
// 告知结果
return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
}
// 分配数据
$this->assign('crumbs','新增');
// 加载模板
return $this->fetch();
}
/**
* 编辑
*
* */
public function edit( Request $request,Model $Model,Coupon $Coupon){
// 接收参数
$id = request('id',0);
// 查询用户
$oldData = $Model->where(['id'=>$id])->first();
// 如果是post
if(request()->isMethod('post')){
// 验证参数
$request->scene('edit')->validate();
$data['what_day'] = (int) request('what_day',0);
$data['reward'] = (int) request('reward',0);
$coupon_code = request('coupon_code','');
$data['coupon_id'] = $Coupon->codeToId($coupon_code);
// 查询是否已经有
$oldDId = $Model->query()->where([['what_day','=',$data['what_day']],['active_id','=',$oldData['active_id']]])->value('id');
// 提示
if( $oldDId && $id != $oldDId ) return json_send(['code'=>'error','msg'=>'打卡天数已存在']);
// 写入用户表
$result = $Model->edit($id,$data);
// 提示新增失败
if( isset($result['error']) ) return json_send(['code'=>'error','msg'=>$result['error']]);
// 记录行为
$this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
// 告知结果
return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
}
// 如果是没有数据
if( !$oldData ) return $this->error('查无数据');
// 查询数据
$oldData = $Model->where(['id'=>$id])->first();
$oldData['coupon_code'] = $oldData['coupon_id'] ? $Coupon->idToCode($oldData['coupon_id']) : '';
// 分配数据
$this->assign('crumbs','修改');
$this->assign('oldData',$oldData);
// 加载模板
return $this->fetch();
}
/**
* 删除
*
* */
public function set_status( Request $request,Model $Model ){
// 验证参数
$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'=>'查无数据']);
// 查询数据
$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'=>'']);
}
}