Answer.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 Illuminate\Support\Facades\DB;
  8. /**
  9. * 灯谜问答
  10. *
  11. * @author 刘相欣
  12. *
  13. * */
  14. class Answer extends Api{
  15. /**
  16. * 判断回答是否正确 /api/riddle_answer/check_answer
  17. *
  18. * */
  19. public function check_answer(Model $Model,Active $Active,ActiveRecord $ActiveRecord,ActiveShare $ActiveShare){
  20. // 接口验签
  21. // $this->verify_sign();
  22. // 检查登录
  23. $uid = $this->checkLogin();
  24. // 获取参数
  25. $activeId = request('active_id',0);
  26. $questionId = request('question_id',0);
  27. $answerId = request('answer_id',0);
  28. // 获取客户城市的数据
  29. $data = $Active->getOne($activeId);
  30. // 如果存在的话
  31. if( !$data ) return json_send(['code'=>'error','msg'=>'活动已下线或不存在','data'=>'']);
  32. // 获取参与次数
  33. $joinTotal = $ActiveRecord->query()->where([['active_id','=',$activeId],['custom_uid','=',$uid]])->count();
  34. // 获取答题次数
  35. $shareTotal = $ActiveShare->query()->where([['active_id','=',$activeId],['custom_uid','=',$uid]])->count();
  36. // 计算答题次数
  37. $data['join_total'] = $data['join_total'] + ( $shareTotal >= $data['join_share'] ? $data['join_share'] : $shareTotal);
  38. // 计算剩余参与次数
  39. $data['join_last'] = $data['join_total'] - $joinTotal;
  40. // 如果次数不够
  41. if( $data['join_last'] <= 0 ) return json_send(['code'=>'error','msg'=>'您已经没有参与次数了','data'=>'']);
  42. // 获取问题列表
  43. $answer = $Model->getOne($questionId,$answerId);
  44. // 组合数据,写入订单表,子表
  45. DB::beginTransaction();
  46. try {
  47. // 记录答题记录
  48. $result = $ActiveRecord->add(['active_id'=>$activeId,'custom_uid'=>$uid,'question_id'=>$questionId,'answer_id'=>$answerId,'is_answer'=>$answer['is_answer']]);
  49. // 如果记录失败
  50. if( !$result ) {
  51. // 回滚事务
  52. DB::rollBack();
  53. // 返回结果
  54. return json_send(['code'=>'error','msg'=>'提交失败','data'=>['error'=>'答题记录失败']]);
  55. }
  56. // 判断答案是否正确
  57. if( $answer['is_answer'] ) {
  58. /**
  59. * @todo
  60. * 对应的抽奖活动 为用户增加一次抽奖机会
  61. */
  62. }
  63. // 提交事务
  64. DB::commit();
  65. // 返回结果
  66. return json_send(['code'=>'success','msg'=>'提交成功','data'=>['is_answer'=>$answer['is_answer']]]);
  67. } catch (\Throwable $th) {
  68. // 返回结果
  69. return json_send(['code'=>'success','msg'=>'提交失败','data'=>['error'=>$th->getMessage()]]);
  70. }
  71. }
  72. }