InterviewController.php 9.3 KB

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