Clockin.php 4.5 KB

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