Clockin.php 6.3 KB

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