Active.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php namespace App\Http\Controllers\Api\Riddle;
  2. use App\Http\Controllers\Api\Api;
  3. use App\Models\Riddle\Active as Model;
  4. use App\Models\Riddle\ActiveShare;
  5. use App\Models\Riddle\ActiveRecord;
  6. use App\Models\Custom;
  7. use App\Models\WeiBan\Tags as WeiBanTags;
  8. /**
  9. * 灯谜活动
  10. *
  11. * @author 刘相欣
  12. *
  13. * */
  14. class Active extends Api{
  15. /**
  16. * 获取抽奖配置 /api/riddle_active/get_detail
  17. *
  18. * */
  19. public function get_detail(Model $Model,Custom $Custom,WeiBanTags $WeiBanTags,ActiveShare $ActiveShare,ActiveRecord $ActiveRecord){
  20. // 接口验签
  21. // $this->verify_sign();
  22. // 检查登录
  23. $uid = $this->checkLogin();
  24. // 获取客户信息
  25. $custom = $Custom->getOne($uid);
  26. // 如果存在的话
  27. if( !$custom ) return json_send(['code'=>'no_login','msg'=>'请登录','data'=>['error'=>'无对应客户']]);
  28. // 接收参数
  29. $id = request('id',0);
  30. // 如果存在的话
  31. if( !$id ) return json_send(['code'=>'error','msg'=>'未知的活动ID','data'=>['error'=>'未知的活动ID']]);
  32. // 获取客户城市的数据
  33. $data = $Model->getOne($id);
  34. // 如果存在的话
  35. if( !$data ) return json_send(['code'=>'error','msg'=>'暂无活动','data'=>['error'=>'暂无活动']]);
  36. // 默认可以参加活动
  37. $data['allow_join'] = 1;
  38. // 判断是不是可以参与
  39. if( $data['tag_scope'] ) {
  40. // 解析数组
  41. $data['tag_scope'] = explode(',',$data['tag_scope']);
  42. // 查询用户标签
  43. $tags = $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']);
  44. // 标签范围限定时,默认不能参与
  45. $data['allow_join'] = 0;
  46. // 判断标签是不是存在
  47. foreach ($tags as $value) {
  48. // 标签范围内,允许参加
  49. if( in_array($value['name'],$data['tag_scope']) ) $data['allow_join'] = 1;
  50. }
  51. // 如果不能参与
  52. if( !$data['allow_join'] ) return json_send(['code'=>'error','msg'=>'账号不在标签范围','data'=>['error'=>'不在标签范围内']]);
  53. }
  54. // 判断是不是可以参与
  55. if( $data['city_ids'] ) {
  56. // 解析数组
  57. $data['city_ids'] = explode(',',$data['city_ids']);
  58. // 如果不在城市范围
  59. if( !in_array($custom['city_id'],$data['city_ids']) ) $data['allow_join'] = 0;
  60. // 如果不能参与
  61. if( !$data['allow_join'] ) return json_send(['code'=>'error','msg'=>'账号不在活动城市','data'=>['error'=>'账号不在活动城市']]);
  62. }
  63. // 获取参与次数
  64. $joinTotal = $ActiveRecord->query()->where([['active_id','=',$id],['custom_uid','=',$uid]])->count();
  65. // 获取答题次数
  66. $shareTotal = $ActiveShare->query()->where([['active_id','=',$id],['custom_uid','=',$uid]])->count();
  67. // 计算答题次数
  68. $data['join_total'] = $data['join_total'] + ( $shareTotal >= $data['join_share'] ? $data['join_share'] : $shareTotal);
  69. // 计算剩余参与次数
  70. $data['join_last'] = $data['join_total'] - $joinTotal;
  71. // 计算剩余分享次数
  72. $data['share_last'] = ($data['join_share'] - $shareTotal <= 0 ? 0 : $data['join_share'] - $shareTotal);
  73. // 删除不必要的数据
  74. unset($data['allow_join'],$data['tag_scope'],$data['city_ids']);
  75. // 返回结果
  76. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  77. }
  78. }