Active.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. $data = $Model->getOne($id);
  32. // 如果存在的话
  33. if( !$data ) return json_send(['code'=>'error','msg'=>'暂无活动','data'=>$data]);
  34. // 默认可以参加活动
  35. $data['allow_join'] = 1;
  36. // 判断是不是可以参与
  37. if( $data['tag_scope'] ) {
  38. // 解析数组
  39. $data['tag_scope'] = explode(',',$data['tag_scope']);
  40. // 查询用户标签
  41. $tags = $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']);
  42. // 标签范围限定时,默认不能参与
  43. $data['allow_join'] = 0;
  44. // 判断标签是不是存在
  45. foreach ($tags as $value) {
  46. // 标签范围内,允许参加
  47. if( in_array($value['name'],$data['tag_scope']) ) $data['allow_join'] = 1;
  48. }
  49. // 如果不能参与
  50. if( !$data['allow_join'] ) return json_send(['code'=>'error','msg'=>'账号不在标签范围','data'=>['error'=>'不在标签范围内']]);
  51. }
  52. // 判断是不是可以参与
  53. if( $data['city_ids'] ) {
  54. // 解析数组
  55. $data['city_ids'] = explode(',',$data['city_ids']);
  56. // 如果不在城市范围
  57. if( !in_array($custom['city_id'],$data['city_ids']) ) $data['allow_join'] = 0;
  58. // 如果不能参与
  59. if( !$data['allow_join'] ) return json_send(['code'=>'error','msg'=>'账号不在活动城市','data'=>['error'=>'账号不在活动城市']]);
  60. }
  61. // 获取参与次数
  62. $joinTotal = $ActiveRecord->query()->where([['active_id','=',$id],['custom_uid','=',$uid]])->count();
  63. // 获取答题次数
  64. $shareTotal = $ActiveShare->query()->where([['active_id','=',$id],['custom_uid','=',$uid]])->count();
  65. // 计算答题次数
  66. $data['join_total'] = $data['join_total'] + ( $shareTotal >= $data['join_share'] ? $data['join_share'] : $shareTotal);
  67. // 计算剩余参与次数
  68. $data['join_last'] = $data['join_total'] - $joinTotal;
  69. // 计算剩余分享次数
  70. $data['share_last'] = ($data['join_share'] - $shareTotal <= 0 ? 0 : $data['join_share'] - $shareTotal);
  71. // 删除不必要的数据
  72. unset($data['allow_join'],$data['tag_scope'],$data['city_ids']);
  73. // 返回结果
  74. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  75. }
  76. }