middleware('CheckLogin'); $this->middleware('Logs'); $this->middleware('CacheClear'); } const QUESTION_UPLOAD = 1; const QUESTION_UPLOAD_KEY = 'bsb_question_upload:'; //题目上传到redis的key /** * 活动配置——题库上传(题目) */ public function postActQuestion(Request $request){ try { $params = $request->all(); $keys = array('activity_id', 'corp_id', 'category_name'); foreach ($keys as $key) { if (empty($params[$key])) { return Api::json(config('code.params_error'), trans('params.'.$key) . trans('msg.params_not_null')); } } if ($request->isMethod("post")) { $activity_info = ActivityService::getActivityInfo($params['activity_id'], $params['corp_id']); if (!$activity_info) { return Api::json(config('code.activity_not_exist'), trans('msg.activity_not_exist')); } if (!empty($_FILES['question'])) { $filePath = $_FILES['question']['tmp_name']; Excel::load($filePath, function ($reader) { $reader = $reader->getSheet(0); $res = $reader->toArray(); //去掉无用表头 foreach ($res as $k=>$v) { unset($res[$k]); if(Api::trimAll($v[0]) == '题目标题' && Api::trimAll($v['1']) == 'A选项' && Api::trimAll($v[2]) == 'B选项' && Api::trimAll($v[3]) == 'C选项' && Api::trimAll($v['4']) == 'D选项' && Api::trimAll($v[5]) == '答案'){ break; } } //去掉excel表格的空数据 $res = collect($res)->filter(function($value){ return !is_null($value[0]) || !is_null($value[1]) || !is_null($value[2]) || !is_null($value[3]) || !is_null($value[4]) || !is_null($value[5]); }); if (empty($res)) { print_r(Api::json(config('code.fail'), trans('msg.question_not_exist'))); exit; } //存入redis Redis::set(self::QUESTION_UPLOAD_KEY.$_REQUEST['activity_id'], json_encode($res), 'EX', 3600); }); //从redis获取数据 $data = Redis::get(self::QUESTION_UPLOAD_KEY.$params['activity_id']); $result = QuestionService::questionUpload(json_decode($data, true), $params); if($result['code'] != 0){ return Api::json(config('code.upload_fail'), trans('msg.upload_fail')); } //文件保存 $newPath = base_path('storage/uploads/' . date('Ymd')); if (!is_dir($newPath)) { mkdir($newPath); } $file = $request->file('question'); $file->move($newPath, date('His') . $_FILES['question']['name']); //修改人员上传状态 $query_config['is_question_upload'] = self::QUESTION_UPLOAD; Configs::where('corp_id', $params['corp_id'])->where('activity_id', $params['activity_id'])->update($query_config); return Api::json($result['code'], $result['msg'], $result['data']); } else { return Api::json(config('code.params_error'), trans('msg.params_not_null')); } } }catch(\Exception $e) { return Api::serviceError($e); } } /** * 获取题库内的题目列表 * 2018-06-21 10:00:00 * @param Request $request * @return string */ public function getQuestionsList(Request $request){ try { $params = $request->all(); $keys = array('corp_id', 'category_id', 'activity_id'); foreach ($keys as $key) { if (empty($params[$key])) { return Api::json(config('code.params_error'), trans('params.'.$key) . trans('msg.params_not_null')); } } $category_info = QuestionService::getCategoryInfo($params['activity_id'], $params['corp_id'], $params['category_id']); if (!$category_info) { return Api::json(config('code.category_not_exist'), trans('msg.category_not_exist')); } $keyword = empty($params['keyword']) ? '' : $params['keyword']; $page_size = empty($params['page_size']) ? 10 : $params['page_size']; $sql = Questions::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->where('category_id', $params['category_id'])->where('status', 1); if (!empty($params['keyword'])) { $sql = $sql->where("question_title", "like", "%" . $keyword . "%"); } $questions = $sql->orderByDesc('create_time')->paginate($page_size); $data = Api::page($questions); //时间戳转换 foreach ($data['data'] as $key => $val) { $data['data'][$key] = Api::dateFormat($val); $data['data'][$key]['question_options'] = unserialize($val['question_options']); } return Api::json(config('code.success'), trans('msg.success'), $data); }catch(\Exception $e) { return Api::serviceError($e); } } /** * 获取题库列表 * 2018-06-21 10:00:00 * @param Request $request * @return string */ public function getCategoryList(Request $request){ try { $params = $request->all(); $keys = array('corp_id', 'activity_id'); foreach ($keys as $key) { if (empty($params[$key])) { return Api::json(config('code.params_error'), trans('params.'.$key) . trans('msg.params_not_null')); } } $res = QuestionService::getCategoryList($params); return Api::json($res['code'], $res['msg'], $res['data']); }catch(\Exception $e) { return Api::serviceError($e); } } /** * 修改单个题目 * @param Request $request * @return string */ public function postQuestionModify(Request $request){ try { $params = $request->all(); $keys = array('corp_id', 'activity_id', 'category_id'); foreach ($keys as $key) { if (empty($params[$key])) { return Api::json(config('code.params_error'), trans('params.'.$key) . trans('msg.params_not_null')); } } $res = QuestionService::questionModify($params); return Api::json($res['code'], $res['msg'], $res['data']); } catch (\Exception $e) { return Api::serviceError($e); } } /** * 修改题库名 * @param Request $request * @return string */ public function postSaveCategory(Request $request){ try { $params = $request->all(); $keys = array('corp_id', 'activity_id', 'category_id', 'category_name'); foreach ($keys as $key) { if (empty($params[$key])) { return Api::json(config('code.params_error'), trans('params.'.$key) . trans('msg.params_not_null')); } } $res = QuestionService::saveCategory($params); return Api::json($res['code'], $res['msg'], $res['data']); } catch (\Exception $e) { return Api::serviceError($e); } } /** * 删除题目 * 2018-06-22 10:24:00 * @param Request $request * @return string */ public function postDelQuestion(Request $request){ try { $params = $request->all(); $keys = array('corp_id', 'activity_id', 'category_id', 'question_id'); foreach ($keys as $key) { if (empty($params[$key])) { return Api::json(config('code.params_error'), trans('params.'.$key) . trans('msg.params_not_null')); } } $res = QuestionService::delQuestion($params); return Api::json($res['code'], $res['msg'], $res['data']); } catch (\Exception $e) { return Api::serviceError($e); } } } ?>