InterviewController.php 9.9 KB

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