ScoreClockin.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\ScoreClockin as Request;
  3. use App\Models\Score\Clockin as Model;
  4. use App\Models\Coupon as Coupon;
  5. /**
  6. * 打卡任务
  7. *
  8. * @author 刘相欣
  9. *
  10. */
  11. class ScoreClockin extends Auth{
  12. protected function _initialize(){
  13. parent::_initialize();
  14. $this->assign('breadcrumb1','任务打卡');
  15. $this->assign('breadcrumb2','打卡任务');
  16. }
  17. /**
  18. * 首页列表
  19. *
  20. * */
  21. public function index(Model $Model, Coupon $Coupon){
  22. $activeId = request('active_id',0);
  23. // 组合查询条件
  24. $map = [];
  25. if( $activeId ) $map[] = ['active_id','=',$activeId];
  26. // 查询数据
  27. $list = $Model->where($map)->orderBy('what_day')->orderBy('id')->paginate(request('limit',config('page_num',10)))->appends(request()->all());
  28. foreach ($list as $k=>$v){
  29. if ($v['coupon_id']) {
  30. $v['coupon_code'] = $Coupon->idToCode($v['coupon_id']);
  31. $coupon = $Coupon->getOne($v['coupon_id']);
  32. if ($coupon) $v['coupon_name'] = $coupon['name'];
  33. }
  34. $list[$k] = $v;
  35. }
  36. // 分配数据
  37. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  38. $this->assign('list', $list);
  39. $this->assign('active_id', $activeId);
  40. // 加载模板
  41. return $this->fetch();
  42. }
  43. /**
  44. * 添加
  45. *
  46. * */
  47. public function add( Request $request,Model $Model,Coupon $Coupon){
  48. if( request()->isMethod('post') ){
  49. // 验证参数
  50. $request->scene('add')->validate();
  51. // 组合数据
  52. $data['what_day'] = (int) request('what_day',0);
  53. $data['reward'] = (int) request('reward',0);
  54. $data['active_id'] = request('active_id',0);
  55. $data['status'] = 0;
  56. $coupon_code = request('coupon_code','');
  57. $data['coupon_id'] = $Coupon->codeToId($coupon_code);
  58. // 查询是否已经有
  59. $oldDId = $Model->query()->where([['what_day','=',$data['what_day']],['active_id','=',$data['active_id']]])->value('id');
  60. // 提示新增失败
  61. if( $oldDId ) return json_send(['code'=>'error','msg'=>'打卡天数已存在']);
  62. // 写入用户表
  63. $id = $Model->add($data);
  64. // 提示新增失败
  65. if( isset($id['error']) ) return json_send(['code'=>'error','msg'=>$id['error']]);
  66. // 记录行为
  67. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  68. // 告知结果
  69. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  70. }
  71. // 分配数据
  72. $this->assign('crumbs','新增');
  73. // 加载模板
  74. return $this->fetch();
  75. }
  76. /**
  77. * 编辑
  78. *
  79. * */
  80. public function edit( Request $request,Model $Model,Coupon $Coupon){
  81. // 接收参数
  82. $id = request('id',0);
  83. // 查询用户
  84. $oldData = $Model->where(['id'=>$id])->first();
  85. // 如果是post
  86. if(request()->isMethod('post')){
  87. // 验证参数
  88. $request->scene('edit')->validate();
  89. $data['what_day'] = (int) request('what_day',0);
  90. $data['reward'] = (int) request('reward',0);
  91. $coupon_code = request('coupon_code','');
  92. $data['coupon_id'] = $Coupon->codeToId($coupon_code);
  93. // 查询是否已经有
  94. $oldDId = $Model->query()->where([['what_day','=',$data['what_day']],['active_id','=',$oldData['active_id']]])->value('id');
  95. // 提示
  96. if( $oldDId && $id != $oldDId ) return json_send(['code'=>'error','msg'=>'打卡天数已存在']);
  97. // 写入用户表
  98. $result = $Model->edit($id,$data);
  99. // 提示新增失败
  100. if( isset($result['error']) ) return json_send(['code'=>'error','msg'=>$result['error']]);
  101. // 记录行为
  102. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  103. // 告知结果
  104. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  105. }
  106. // 如果是没有数据
  107. if( !$oldData ) return $this->error('查无数据');
  108. // 查询数据
  109. $oldData = $Model->where(['id'=>$id])->first();
  110. $oldData['coupon_code'] = $oldData['coupon_id'] ? $Coupon->idToCode($oldData['coupon_id']) : '';
  111. // 分配数据
  112. $this->assign('crumbs','修改');
  113. $this->assign('oldData',$oldData);
  114. // 加载模板
  115. return $this->fetch();
  116. }
  117. /**
  118. * 删除
  119. *
  120. * */
  121. public function set_status( Request $request,Model $Model ){
  122. // 验证参数
  123. $request->scene('set_status')->validate();
  124. // 接收参数
  125. $id = request('id',0);
  126. $status = request('status',0);
  127. // 查询用户
  128. $oldData = $Model->where(['id'=>$id])->first();
  129. // 如果是没有数据
  130. if( !$oldData ) return json_send(['code'=>'error','msg'=>'查无数据']);
  131. // 查询数据
  132. $result = $Model->edit($id,['status'=>$status]);
  133. // 提示新增失败
  134. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  135. // 记录行为
  136. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  137. // 告知结果
  138. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  139. }
  140. }