InterviewController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. Log::write('工程师面试请求: step='.$step.";data=".json_encode($data));
  106. if ( !in_array($step, [1,2,3,4,5])) {
  107. return $this->fail('参数错误');
  108. }
  109. if ($step > 1 && (empty($data) || !is_array($data))) {
  110. return $this->fail('答案不可为空');
  111. }
  112. Log::write('工程师面试请求: step='.$step.";data=".json_encode($data));
  113. $worker_register_id = MasterWorkerRegister::where('id',$worker_register_id)->value('id');
  114. if (!$worker_register_id) {
  115. return $this->fail('ID有误');
  116. }
  117. $info = MasterWorkerInterview::where("worker_register_id", $worker_register_id)->findOrEmpty();
  118. if ($info->isEmpty()) {
  119. $info = new MasterWorkerInterview();
  120. $info->worker_register_id = $worker_register_id;
  121. } else if ($info->status > 0) {
  122. return $this->fail('您已提交过面试题,请勿重复提交',['has_next' => 0,'is_success' => $info->status == 1 ? 1 : 0, 'list' => []]);
  123. }
  124. $msg = '操作成功';
  125. $has_next = 1;
  126. $is_success = 0;
  127. $result = [];
  128. $list = $this->list;
  129. $list2 = $this->list2;
  130. $list3 = $this->list3;
  131. $tmplist = array_column($list, null, 'id');
  132. if ($step == 1 ) {
  133. $result = $list;
  134. } elseif ($step == 2) {
  135. foreach($data as $item) {
  136. if (empty($item['id']) || ($item['id'] != 4 && empty($item['options']))) {
  137. return $this->fail('参数错误');
  138. }
  139. if ($item['id'] == 5) {
  140. $options = $item['options'];
  141. }
  142. }
  143. if (empty($options)) {
  144. return $this->fail('请选择您具备的经验');
  145. }
  146. $key = array_search($options, $tmplist[5]['options']);
  147. // if ($key === false || !isset($list2[$key])) {
  148. // $has_next = 0;
  149. // $info->status = 2;
  150. // $msg = '很抱歉,您的测试未通过,感谢您的关注!';
  151. // } else {
  152. // //根据上一步的选项判断
  153. // $result[] = $list2[$key];
  154. // }
  155. switch($key) {
  156. case 0 :
  157. $info->type = 1;
  158. break;
  159. case 1 :
  160. $info->type = 2;
  161. break;
  162. default :
  163. $info->type = 0;
  164. }
  165. try{
  166. Db::startTrans();
  167. MasterWorkerRegisterLogic::createMasterWorker(['worker_register_id' => $worker_register_id,'is_rinse' => $info->type]);
  168. $info->content = json_encode([1 => $data]);
  169. $info->status = 1;
  170. $info->save();
  171. $has_next = 0;
  172. $is_success = 1;
  173. $msg = '恭喜您成功入驻众盾闪修平台!';
  174. Db::commit();
  175. } catch (\Exception $e) {
  176. Db::rollback();
  177. return $this->fail($e->getMessage());
  178. }
  179. } elseif ($step == 3) {
  180. $data = current($data);
  181. $id = empty($data['id']) ? 0 : $data['id'];
  182. if ($id == 6 || $id == 7) {
  183. $question = MasterWorkerLogic::getQuestion(['type' => $info->type, 'category' => $data['options']]);
  184. foreach($question['list'] as $key => $item) {
  185. $item['options'] = str_replace(";; ",";;",$item['options']);
  186. $question['list'][$key] = $item;
  187. $temp = [
  188. "id" => $item['id'],
  189. "title" => $item['title'],
  190. "options" => explode(";;",$item['options']),
  191. "type" => strpos($item['title'], '多选题') !== false ? 2 : 1,
  192. ];
  193. $result[] = $temp;
  194. }
  195. } else {
  196. return $this->fail('参数有误');
  197. }
  198. $content = json_decode($info->content, true);
  199. $content[2] = $data;
  200. $info->content = json_encode($content);
  201. $info->save();
  202. } elseif (($step == 4 && $info->type == 3) || $step == 5) {
  203. try{
  204. Db::startTrans();
  205. MasterWorkerRegisterLogic::createMasterWorker(['worker_register_id' => $worker_register_id,'is_rinse' => $info->type]);
  206. $content = json_decode($info->content, true);
  207. $content[$step - 1] = $data;
  208. $info->content = json_encode($content);
  209. $info->status = 1;
  210. $info->save();
  211. $has_next = 0;
  212. $is_success = 1;
  213. $msg = '恭喜您成功入驻众盾闪修平台!';
  214. Db::commit();
  215. } catch (\Exception $e) {
  216. Db::rollback();
  217. return $this->fail($e->getMessage());
  218. }
  219. } else {
  220. //校验
  221. $data = array_column($data, null, 'id');
  222. $ids = array_keys($data);
  223. $answers = MasterWorkerQuestion::where('type',$info->type)->whereIn('id',$ids)->column('answer','id');
  224. if (count($ids) != count($answers)) {
  225. return $this->fail('题目参数有误');
  226. }
  227. $rights = 0;
  228. foreach($data as &$item) {
  229. $answer = $answers[$item['id']];
  230. if (!in_array($item['options'],['对','错'])) {
  231. $item['options'] = preg_replace('/[^a-zA-Z]/', '', $item['options']);
  232. //用户选项排序
  233. $options = str_split($item['options']);
  234. sort($options);
  235. $item['options'] = implode('', $options);
  236. if (($answer == '对' || $answer == '错')) {
  237. $item['options'] = $item['options'] == 'A' ? '对' : '错';
  238. }
  239. }
  240. if ($item['options'] == $answer) {
  241. $rights ++;
  242. }
  243. $item['answer'] = $answer;
  244. }
  245. if ($rights / count($data) >= 0.6) {
  246. $result = $list3;
  247. } else {
  248. $has_next = 0;
  249. $info->status = 2;
  250. $msg = '很抱歉,您的测试未通过,感谢您的关注!';
  251. }
  252. $content = json_decode($info->content, true);
  253. $content[$step - 1] = $data;
  254. $info->content = json_encode($content);
  255. $info->save();
  256. }
  257. return $this->success($msg, ['has_next' => $has_next,'is_success' => $is_success, 'list' => $result], 1, 1);
  258. }
  259. }