123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
-
- namespace App\Http\Controllers\Api;
-
- use App\Models\Configs;
- use Illuminate\Http\Request;
- use App\Http\Requests;
- use App\Http\Controllers\Controller;
- use App\Common\Api;
-
- use App\Models\Questions;
-
- use DB;
- use Excel;
- use Storage;
- use Illuminate\Support\Facades\Redis;
- use App\Services\ActivityService;
- use App\Services\QuestionService;
-
- class QuestionController extends Controller
- {
-
- public function __construct(){
- //加载中间键
- $this->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);
- }
- }
- }
- ?>
|