Service.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 社区服务
  7. */
  8. class Service extends Api
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. //服务类型列表
  13. public function paper_list(){
  14. $list = Db::name('service_paper')->where(['status'=>1])->whereNull('deletetime')->order('weigh desc')->select();
  15. $this->success(1, $list);
  16. }
  17. //题目列表
  18. public function question_list(){
  19. $paper_id = input('paper_id');
  20. $list = Db::name('service_question')->where('paper_id',$paper_id)->where(['status'=>1])->whereNull('deletetime')->order('weigh desc, id desc')->select();
  21. foreach($list as $k=>$v){
  22. $options = json_decode($v['options_json'],true);
  23. foreach ($options as $key=>$value){
  24. $op = [
  25. 'key' => $key,
  26. 'value' => $value
  27. ];
  28. $list[$k]['options'][] = $op;
  29. }
  30. unset($list[$k]['options_json']);
  31. }
  32. $this->success(1, $list);
  33. }
  34. //提交
  35. public function submit(){
  36. $paper_id = input('paper_id');
  37. $answer_json = input('answer','','trim');
  38. $list = Db::name('service_question')->where('paper_id',$paper_id)->where(['status'=>1])->whereNull('deletetime')->order('weigh desc, id desc')->select();
  39. $question_ids = array_column($list, 'id');
  40. $answer = json_decode($answer_json,true);
  41. if(count($list) != count($answer)){
  42. $this->error();
  43. }
  44. if(array_column($answer,'id') != $question_ids){
  45. $this->error();
  46. }
  47. $data = [
  48. 'user_id' => $this->auth->id,
  49. 'paper_id' => $paper_id,
  50. 'question_ids' => implode(',',$question_ids),
  51. 'user_answers' => $answer_json,
  52. 'createtime' => time(),
  53. 'updatetime' => time(),
  54. ];
  55. $grade_id = Db::name('service_grade')->insert($data);
  56. $this->success('提交成功',$grade_id);
  57. }
  58. }