InterviewController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace app\workerapi\controller;
  3. use think\Request;
  4. use app\workerapi\logic\MasterWorkerLogic;
  5. use app\common\model\master_worker\MasterWorkerInterview;
  6. use app\common\model\master_worker\MasterWorkerQuestion;
  7. use app\common\model\master_worker_register\MasterWorkerRegister;
  8. class InterviewController extends BaseApiController
  9. {
  10. public array $notNeedLogin = ['lists'];
  11. private array $list = [
  12. [
  13. 'id' => 1,
  14. 'title' => '请问您目前的工作状态是全职工作还是自由工作?',
  15. 'options' => [
  16. '全职工作',
  17. '自由工作'
  18. ],
  19. 'type' => 1,
  20. ],
  21. [
  22. 'id' => 2,
  23. 'title' => '请问您是否有独立上门给客户提供家电维修/清洗/安装的经验?',
  24. 'options' => [
  25. '有',
  26. '没有'
  27. ],
  28. 'type' => 1,
  29. ],
  30. [
  31. 'id' => 3,
  32. 'title' => '请问您有没有在其他同类型平台的工作经验?',
  33. 'options' => [
  34. '有',
  35. '没有'
  36. ],
  37. 'type' => 1,
  38. ],
  39. [
  40. 'id' => 4,
  41. 'title' => '请简要说明平台名称、工作时间年限、所服务的类目',
  42. 'type' => 3,
  43. ],
  44. [
  45. 'id' => 5,
  46. 'title' => '请选择您具备的经验?',
  47. 'options' => [
  48. '家电维修',
  49. '家电清洗',
  50. '家电安装',
  51. '不具备以上经验'
  52. ],
  53. 'type' => 1,
  54. ]
  55. ];
  56. private array $list2 = [
  57. [
  58. 'id' => 6,
  59. 'title' => '请选择您会的项目?最多选5个,最后会根据您所选的项目进行答题。',
  60. 'options' => [
  61. '空调加氟',
  62. '空调维修',
  63. '冰箱','制冷设备','油烟机','洗衣机','甩干机','燃气热水器','电热水器','燃气灶','干洗机',
  64. ],
  65. 'type' => 2,
  66. ],
  67. [
  68. 'id' => 7,
  69. 'title' => '请选择您会的项目?最多选5个,最后会根据您所选的项目进行答题。',
  70. 'options' => [
  71. '天井机','风管机','家用中央空调','空调','油烟机','洗衣机','热水器','燃气灶','冰箱'
  72. ],
  73. 'type' => 2,
  74. ],
  75. [
  76. 'id' => 8,
  77. 'title' => '请根据您的实际情况选择会哪些家电安装?',
  78. 'options' => [
  79. '空调','其他'
  80. ],
  81. 'type' => 1,
  82. ],
  83. ];
  84. private array $list3 = [
  85. [
  86. 'id' => 9,
  87. 'title' => '请选择您持有的证书?',
  88. 'options' => [
  89. '低压电工作业',
  90. '高压电工作业',
  91. '高处作业',
  92. '制冷与空调作业',
  93. '焊接与热切割作业',
  94. '无',
  95. ],
  96. 'type' => 1,
  97. ],
  98. [
  99. 'id' => 10,
  100. 'title' => '请简单描述您具备的其他技能?',
  101. 'type' => 3,
  102. ]
  103. ];
  104. public function lists(Request $request)
  105. {
  106. // 获取单个 POST 参数
  107. $worker_register_id = $request->post('worker_register_id');
  108. $step = $request->post('step');
  109. // 获取所有 POST 参数
  110. $data = $request->post('data');
  111. if ( !in_array($step, [1,2,3,4,5])) {
  112. return $this->fail('参数错误');
  113. }
  114. if ($step > 1 && (empty($data) || !is_array($data))) {
  115. return $this->fail('答案不可为空');
  116. }
  117. $worker_register_id = MasterWorkerRegister::where('id',$worker_register_id)->value('id');
  118. if (!$worker_register_id) {
  119. return $this->fail('ID有误');
  120. }
  121. $info = MasterWorkerInterview::where("worker_register_id", $worker_register_id)->findOrEmpty();
  122. if ($info->isEmpty()) {
  123. $info = new MasterWorkerInterview();
  124. $info->worker_register_id = $worker_register_id;
  125. } else if ($info->status > 0) {
  126. return $this->fail('您已提交过面试题,请勿重复提交');
  127. }
  128. $msg = '操作成功';
  129. $has_next = 1;
  130. $is_success = 0;
  131. $result = [];
  132. $list = $this->list;
  133. $list2 = $this->list2;
  134. $list3 = $this->list3;
  135. $tmplist = array_column($list, null, 'id');
  136. if ($step == 1 ) {
  137. $result[] = $list;
  138. } elseif ($step == 2) {
  139. foreach($data as $item) {
  140. if (empty($item['id']) || ($item['id'] != 4 && empty($item['options']))) {
  141. return $this->fail('参数错误');
  142. }
  143. if ($item['id'] == 5) {
  144. $options = $item['options'];
  145. }
  146. }
  147. if (empty($options)) {
  148. return $this->fail('请选择您具备的经验');
  149. }
  150. $key = array_search($options, $tmplist[5]['options']);
  151. if ($key === false || !isset($list2[$key])) {
  152. $has_next = 0;
  153. $info->status = 2;
  154. $msg = '很抱歉,您的测试未通过,感谢您的关注!';
  155. } else {
  156. //根据上一步的选项判断
  157. $result[] = $list2[$key];
  158. }
  159. switch($key) {
  160. case 0 :
  161. $info->type = 1;
  162. break;
  163. case 1 :
  164. $info->type = 2;
  165. break;
  166. case 2 :
  167. $info->type = 3;
  168. }
  169. $info->content = json_encode([ 1 => $data]);
  170. $info->save();
  171. } elseif ($step == 3) {
  172. $data = current($data);
  173. $id = empty($data['id']) ? 0 : $data['id'];
  174. if ($id == 6 || $id == 7) {
  175. $question = MasterWorkerLogic::getQuestion(['type' => $info->type, 'category' => $data['options']]);
  176. foreach($question['list'] as $key => $item) {
  177. $temp = [
  178. "id" => $item['id'],
  179. "title" => $item['title'],
  180. "options" => explode(",",$item['options']),
  181. "type" => strpos($item['title'], '多选题') !== false ? 2 : 1,
  182. ];
  183. $result[] = $temp;
  184. }
  185. } elseif ($id == 8) {
  186. if ($data['options'] == '空调') {
  187. $result[] = $list3;
  188. } else {
  189. $has_next = 0;
  190. $info->status = 2;
  191. $msg = '很抱歉,您的测试未通过,感谢您的关注!';
  192. }
  193. } else {
  194. return $this->fail('参数有误');
  195. }
  196. $content = json_decode($info->content, true);
  197. $content[2] = $data;
  198. $info->content = json_encode($content);
  199. $info->save();
  200. } elseif (($step == 4 && $info->type == 3) || $step == 5) {
  201. try{
  202. Db::startTrans();
  203. MasterWorkerRegisterLogic::createMasterWorker(['worker_register_id' => $worker_register_id]);
  204. $content = json_decode($info->content, true);
  205. $content[$step - 1] = $data;
  206. $info->content = json_encode($content);
  207. $info->status = 1;
  208. $info->save();
  209. $has_next = 0;
  210. $is_success = 1;
  211. $msg = '恭喜您成功入驻众盾闪修平台!';
  212. Db::commit();
  213. } catch (\Exception $e) {
  214. Db::rollback();
  215. return $this->fail($e->getMessage());
  216. }
  217. } else {
  218. //校验
  219. $data = array_column($data, null, 'id');
  220. $ids = array_keys($data);
  221. $answers = MasterWorkerQuestion::where('type',$info->type)->whereIn('id',$ids)->column('answer','id');
  222. if (count($ids) != count($answers)) {
  223. return $this->fail('题目参数有误');
  224. }
  225. $rights = 0;
  226. foreach($data as &$item) {
  227. $answer = $answers[$item['id']];
  228. $item['options'] = preg_replace('/[^a-zA-Z]/', '', $item['options']);
  229. if ($answer == '对' || $answer == '错') {
  230. $item['options'] = $item['options'] == 'A' ? '对' : '错';
  231. }
  232. if ($item['options'] == $answer) {
  233. $rights ++;
  234. }
  235. }
  236. if ($rights / count($data) >= 0.8) {
  237. $result[] = $list3[0];
  238. } else {
  239. $has_next = 0;
  240. $info->status = 2;
  241. $msg = '很抱歉,您的测试未通过,感谢您的关注!';
  242. }
  243. $content = json_decode($info->content, true);
  244. $content[$step - 1] = $data;
  245. $info->content = json_encode($content);
  246. $info->save();
  247. }
  248. return $this->success($msg, ['has_next' => $has_next,'is_success' => $is_success, 'list' => $result], 1, 1);
  249. }
  250. }