Answer.php 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php namespace App\Http\Controllers\Api\Riddle;
  2. use App\Http\Controllers\Api\Api;
  3. use App\Models\Riddle\Answer as Model;
  4. use App\Models\Riddle\Active;
  5. use App\Models\Riddle\ActiveShare;
  6. use App\Models\Riddle\ActiveRecord;
  7. use App\Models\Lottery\RiddleUsable;
  8. use Illuminate\Support\Facades\DB;
  9. /**
  10. * 答题问答
  11. *
  12. * @author 刘相欣
  13. *
  14. * */
  15. class Answer extends Api{
  16. /**
  17. * 判断回答是否正确 /api/riddle_answer/check_answer
  18. *
  19. * */
  20. public function check_answer(Model $Model,Active $Active,ActiveRecord $ActiveRecord,ActiveShare $ActiveShare,RiddleUsable $RiddleUsable){
  21. // 接口验签
  22. // $this->verify_sign();
  23. // 检查登录
  24. $uid = $this->checkLogin();
  25. // 获取参数
  26. $activeId = request('active_id',0);
  27. $questionId = request('question_id',0);
  28. $answerId = request('answer_id',0);
  29. // 获取客户城市的数据
  30. $data = $Active->getOne($activeId);
  31. // 如果存在的话
  32. if( !$data ) return json_send(['code'=>'error','msg'=>'活动已下线或不存在','data'=>'']);
  33. // 查询条件
  34. $map = [];
  35. // 判断周期
  36. if( !empty($data['freq']) ) {
  37. if( $data['freq'] == 1 ) $map = [['insert_time','>=',now()->startOfDay()->getTimestamp()],['insert_time','<=',now()->endOfDay()->getTimestamp()]];
  38. if( $data['freq'] == 2 ) $map = [['insert_time','>=',now()->startOfWeek()->getTimestamp()],['insert_time','<=',now()->endOfWeek()->getTimestamp()]];
  39. if( $data['freq'] == 3 ) $map = [['insert_time','>=',now()->startOfMonth()->getTimestamp()],['insert_time','<=',now()->endOfMonth()->getTimestamp()]];
  40. }
  41. // 获取已参与次数
  42. $joinTotal = $ActiveRecord->query()->where([['active_id','=',$activeId],['custom_uid','=',$uid]])->where($map)->count();
  43. // 获取已分享次数
  44. $shareTotal = $ActiveShare->query()->where([['active_id','=',$activeId],['custom_uid','=',$uid]])->where($map)->count();
  45. // 计算总答题次数
  46. $data['join_total'] = $data['join_total'] + ( $shareTotal >= $data['join_share'] ? $data['join_share'] : $shareTotal);
  47. // 计算剩余参与次数
  48. $data['join_last'] = $data['join_total'] - $joinTotal;
  49. // 如果次数不够
  50. if( $data['join_last'] <= 0 ) return json_send(['code'=>'error','msg'=>'您已经没有参与次数了','data'=>'']);
  51. // 获取问题列表
  52. $answer = $Model->getOne($questionId,$answerId);
  53. // 组合数据,写入订单表,子表
  54. DB::beginTransaction();
  55. try {
  56. // 记录答题记录
  57. $result = $ActiveRecord->add(['active_id'=>$activeId,'custom_uid'=>$uid,'question_id'=>$questionId,'answer_id'=>$answerId,'is_answer'=>$answer['is_answer']]);
  58. // 如果记录失败
  59. if( !$result ) {
  60. // 回滚事务
  61. DB::rollBack();
  62. // 返回结果
  63. return json_send(['code'=>'error','msg'=>'提交失败','data'=>['error'=>'答题记录失败']]);
  64. }
  65. // 判断答案是否正确
  66. if( $answer['is_answer'] ) {
  67. /**
  68. * @todo
  69. * 对应的抽奖活动 为用户增加一次抽奖机会
  70. */
  71. $usable = $RiddleUsable->query()->where([['custom_uid','=',$uid],['lottery_id','=',$data['lottery_id']]])->first();
  72. if (!$usable) {
  73. $result = $RiddleUsable->query()->insert(['custom_uid'=>$uid,'lottery_id'=>$data['lottery_id'],'number'=>1,'insert_time'=>time(),'update_time'=>time()]);
  74. }else{
  75. $result = $RiddleUsable->query()->where([['custom_uid','=',$uid],['lottery_id','=',$data['lottery_id']]])->increment('number',1);
  76. }
  77. if( !$result ) {
  78. DB::rollBack();
  79. // 返回结果
  80. return json_send(['code'=>'error','msg'=>'提交失败','data'=>['error'=>'抽奖机会增加失败']]);
  81. }
  82. }
  83. // 提交事务
  84. DB::commit();
  85. // 返回结果
  86. return json_send(['code'=>'success','msg'=>'提交成功','data'=>['is_answer'=>$answer['is_answer']]]);
  87. } catch (\Throwable $th) {
  88. // 返回结果
  89. return json_send(['code'=>'success','msg'=>'提交失败','data'=>['error'=>$th->getMessage()]]);
  90. }
  91. }
  92. }