Clockin.php 5.5 KB

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