123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php namespace App\Http\Controllers\Admin;
- use App\Http\Requests\Admin\ScoreClockin as Request;
- use App\Models\Score\Clockin as Model;
- use App\Models\Coupon as Coupon;
- /**
- * 打卡任务
- *
- * @author 刘相欣
- *
- */
- class ScoreClockin extends Auth{
- protected function _initialize(){
- parent::_initialize();
- $this->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', '<tr><td colspan="20">~~暂无数据</td></tr>');
- $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'=>'']);
- }
- }
|