Clockin.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php namespace App\Http\Controllers\Api\Score;
  2. use App\Http\Controllers\Api\Api;
  3. use App\Models\Custom as Custom;
  4. use App\Models\Score\Clockin as Model;
  5. use App\Models\Score\ClockinActive as ClockinActive;
  6. use App\Models\WeiBan\Tags as WeiBanTags;
  7. use App\Models\CustomClockinRecord;
  8. use App\Models\CustomCoupon;
  9. use http\Env\Request;
  10. use Illuminate\Support\Carbon;
  11. /**
  12. * 任务-打卡
  13. *
  14. * @author 刘相欣
  15. *
  16. * */
  17. class Clockin extends Api
  18. {
  19. /**
  20. * 列表 /api/score_clockin/get_list
  21. *
  22. */
  23. public function get_list(Model $Model,CustomCoupon $CustomCoupon, Custom $Custom, ClockinActive $ClockinActive, WeiBanTags $WeiBanTags, CustomClockinRecord $CustomClockinRecord)
  24. {
  25. // 接口验签
  26. // $this->verify_sign();
  27. // 验证登录
  28. $uid = $this->checkLogin();
  29. // 获取客户信息
  30. $custom = $Custom->getOne($uid);
  31. //客户的城市ID 如果不存在的话
  32. if( !$custom['city_id'] ) return json_send(['code'=>'error','msg'=>'请选择所在城市','data'=>['error'=>'请选择所在城市']]);
  33. $cityId = $custom['city_id'];
  34. // 查询用户标签
  35. $tags = $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']);
  36. //获取打卡活动列表
  37. $activeList = $ClockinActive->getList();
  38. if (!$activeList) return json_send(['code'=>'error','msg'=>'暂无签到活动','data'=>['error'=>'暂无签到活动']]);
  39. $activeInfo = [];
  40. foreach ($activeList as $value){
  41. $city_ids = $value['city_ids'] ? explode(',',$value['city_ids']) : [];
  42. $tag_scope = $value['tag_scope'] ? explode(',',$value['tag_scope']) : [];
  43. if( !$city_ids || in_array($cityId,$city_ids) ) {
  44. if ($tag_scope){
  45. foreach ($tags as $tag) {
  46. // 标签范围内
  47. if( in_array($tag['name'],$tag_scope) ) {
  48. $activeInfo = $value;
  49. break;
  50. }
  51. }
  52. }else{
  53. $activeInfo = $value;
  54. break;
  55. }
  56. }
  57. }
  58. if (!$activeInfo) return json_send(['code'=>'error','msg'=>'暂无签到活动','data'=>['error'=>'暂无签到活动']]);
  59. // 获取列表
  60. $list = $Model->getActiveList($activeInfo['id']);
  61. $isMark = $CustomClockinRecord->isMarkClock($uid,$activeInfo['id']);
  62. $isMark['finish_day'] = empty($isMark['finish_day'])?0:$isMark['finish_day'];
  63. // 循环处理
  64. foreach ( $list as $key => $value ) {
  65. // 是否已经有该优惠券
  66. if( $value['coupon_id'] ) {
  67. // 是否已经有该优惠券
  68. $result = $CustomCoupon->isHaveCoupon([$value['coupon_id']],$uid);
  69. // 如果有,优惠券隐藏
  70. if( $result ) $value['coupon_id'] = 0;
  71. }
  72. // 是否已打卡,打卡天数内(含)为已打卡
  73. $value['is_finish'] = $isMark['finish_day'] >= $value['what_day'] ? 1 : 0;
  74. // 重新赋值
  75. $list[$key] = $value;
  76. }
  77. // 去除主键
  78. $list = array_values($list);
  79. $list = array_chunk($list,28);
  80. $data['list'] = $list;
  81. $data['is_mark'] = $isMark;
  82. $data['active_id'] = $activeInfo['id'];
  83. $data['start_time'] = $activeInfo['start_time'];
  84. $data['end_time'] = $activeInfo['end_time'];
  85. $data['active_rule'] = $activeInfo['active_rule'];
  86. // 返回结果
  87. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  88. }
  89. /**
  90. * 打卡 /api/score_clockin/finish
  91. *
  92. */
  93. public function finish(Model $Model,Custom $Custom,ClockinActive $ClockinActive,CustomClockinRecord $CustomClockinRecord)
  94. {
  95. // 接口验签
  96. // $this->verify_sign();
  97. // 验证登录
  98. $uid = $this->checkLogin();
  99. $activeId = request('active_id',0);
  100. // 获取客户信息
  101. $custom = $uid ? $Custom->getOne($uid) : [];
  102. //客户的城市ID 如果不存在的话
  103. if( !$custom['city_id'] ) return json_send(['code'=>'error','msg'=>'请选择所在城市','data'=>['error'=>'请选择所在城市']]);
  104. if( !$activeId ) return json_send(['code'=>'error','msg'=>'缺少活动id','data'=>['error'=>'缺少活动id']]);
  105. $activeInfo = $ClockinActive::query()->where(['id'=>$activeId])->first();
  106. if (!$activeInfo) return json_send(['code'=>'error','msg'=>'暂无签到活动','data'=>['error'=>'暂无签到活动']]);
  107. // 获取打卡次数
  108. $result = $CustomClockinRecord->finish($uid,$activeInfo['id']);
  109. // 失败结束
  110. if( isset($result['error']) ) return json_send(['code'=>'error','msg'=>$result['error'],'data'=>'']);
  111. // 返回结果
  112. return json_send(['code'=>'success','msg'=>'打卡成功','data'=>$result]);
  113. }
  114. }