QuestionController.php 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Models\Configs;
  4. use Illuminate\Http\Request;
  5. use App\Http\Requests;
  6. use App\Http\Controllers\Controller;
  7. use App\Common\Api;
  8. use App\Models\Questions;
  9. use DB;
  10. use Excel;
  11. use Storage;
  12. use Illuminate\Support\Facades\Redis;
  13. use App\Services\ActivityService;
  14. use App\Services\QuestionService;
  15. class QuestionController extends Controller
  16. {
  17. public function __construct(){
  18. //加载中间键
  19. $this->middleware('CheckLogin');
  20. $this->middleware('Logs');
  21. $this->middleware('CacheClear');
  22. }
  23. const QUESTION_UPLOAD = 1;
  24. const QUESTION_UPLOAD_KEY = 'bsb_question_upload:'; //题目上传到redis的key
  25. /**
  26. * 活动配置——题库上传(题目)
  27. */
  28. public function postActQuestion(Request $request){
  29. try {
  30. $params = $request->all();
  31. $keys = array('activity_id', 'corp_id', 'category_name');
  32. foreach ($keys as $key) {
  33. if (empty($params[$key])) {
  34. return Api::json(config('code.params_error'), trans('params.'.$key) . trans('msg.params_not_null'));
  35. }
  36. }
  37. if ($request->isMethod("post")) {
  38. $activity_info = ActivityService::getActivityInfo($params['activity_id'], $params['corp_id']);
  39. if (!$activity_info) {
  40. return Api::json(config('code.activity_not_exist'), trans('msg.activity_not_exist'));
  41. }
  42. if (!empty($_FILES['question'])) {
  43. $filePath = $_FILES['question']['tmp_name'];
  44. Excel::load($filePath, function ($reader) {
  45. $reader = $reader->getSheet(0);
  46. $res = $reader->toArray();
  47. //去掉无用表头
  48. foreach ($res as $k=>$v) {
  49. unset($res[$k]);
  50. 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]) == '答案'){
  51. break;
  52. }
  53. }
  54. //去掉excel表格的空数据
  55. $res = collect($res)->filter(function($value){
  56. 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]);
  57. });
  58. if (empty($res)) {
  59. print_r(Api::json(config('code.fail'), trans('msg.question_not_exist')));
  60. exit;
  61. }
  62. //存入redis
  63. Redis::set(self::QUESTION_UPLOAD_KEY.$_REQUEST['activity_id'], json_encode($res), 'EX', 3600);
  64. });
  65. //从redis获取数据
  66. $data = Redis::get(self::QUESTION_UPLOAD_KEY.$params['activity_id']);
  67. $result = QuestionService::questionUpload(json_decode($data, true), $params);
  68. if($result['code'] != 0){
  69. return Api::json(config('code.upload_fail'), trans('msg.upload_fail'));
  70. }
  71. //文件保存
  72. $newPath = base_path('storage/uploads/' . date('Ymd'));
  73. if (!is_dir($newPath)) {
  74. mkdir($newPath);
  75. }
  76. $file = $request->file('question');
  77. $file->move($newPath, date('His') . $_FILES['question']['name']);
  78. //修改人员上传状态
  79. $query_config['is_question_upload'] = self::QUESTION_UPLOAD;
  80. Configs::where('corp_id', $params['corp_id'])->where('activity_id', $params['activity_id'])->update($query_config);
  81. return Api::json($result['code'], $result['msg'], $result['data']);
  82. } else {
  83. return Api::json(config('code.params_error'), trans('msg.params_not_null'));
  84. }
  85. }
  86. }catch(\Exception $e) {
  87. return Api::serviceError($e);
  88. }
  89. }
  90. /**
  91. * 获取题库内的题目列表
  92. * 2018-06-21 10:00:00
  93. * @param Request $request
  94. * @return string
  95. */
  96. public function getQuestionsList(Request $request){
  97. try {
  98. $params = $request->all();
  99. $keys = array('corp_id', 'category_id', 'activity_id');
  100. foreach ($keys as $key) {
  101. if (empty($params[$key])) {
  102. return Api::json(config('code.params_error'), trans('params.'.$key) . trans('msg.params_not_null'));
  103. }
  104. }
  105. $category_info = QuestionService::getCategoryInfo($params['activity_id'], $params['corp_id'], $params['category_id']);
  106. if (!$category_info) {
  107. return Api::json(config('code.category_not_exist'), trans('msg.category_not_exist'));
  108. }
  109. $keyword = empty($params['keyword']) ? '' : $params['keyword'];
  110. $page_size = empty($params['page_size']) ? 10 : $params['page_size'];
  111. $sql = Questions::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->where('category_id', $params['category_id'])->where('status', 1);
  112. if (!empty($params['keyword'])) {
  113. $sql = $sql->where("question_title", "like", "%" . $keyword . "%");
  114. }
  115. $questions = $sql->orderByDesc('create_time')->paginate($page_size);
  116. $data = Api::page($questions);
  117. //时间戳转换
  118. foreach ($data['data'] as $key => $val) {
  119. $data['data'][$key] = Api::dateFormat($val);
  120. $data['data'][$key]['question_options'] = unserialize($val['question_options']);
  121. }
  122. return Api::json(config('code.success'), trans('msg.success'), $data);
  123. }catch(\Exception $e) {
  124. return Api::serviceError($e);
  125. }
  126. }
  127. /**
  128. * 获取题库列表
  129. * 2018-06-21 10:00:00
  130. * @param Request $request
  131. * @return string
  132. */
  133. public function getCategoryList(Request $request){
  134. try {
  135. $params = $request->all();
  136. $keys = array('corp_id', 'activity_id');
  137. foreach ($keys as $key) {
  138. if (empty($params[$key])) {
  139. return Api::json(config('code.params_error'), trans('params.'.$key) . trans('msg.params_not_null'));
  140. }
  141. }
  142. $res = QuestionService::getCategoryList($params);
  143. return Api::json($res['code'], $res['msg'], $res['data']);
  144. }catch(\Exception $e) {
  145. return Api::serviceError($e);
  146. }
  147. }
  148. /**
  149. * 修改单个题目
  150. * @param Request $request
  151. * @return string
  152. */
  153. public function postQuestionModify(Request $request){
  154. try {
  155. $params = $request->all();
  156. $keys = array('corp_id', 'activity_id', 'category_id');
  157. foreach ($keys as $key) {
  158. if (empty($params[$key])) {
  159. return Api::json(config('code.params_error'), trans('params.'.$key) . trans('msg.params_not_null'));
  160. }
  161. }
  162. $res = QuestionService::questionModify($params);
  163. return Api::json($res['code'], $res['msg'], $res['data']);
  164. } catch (\Exception $e) {
  165. return Api::serviceError($e);
  166. }
  167. }
  168. /**
  169. * 修改题库名
  170. * @param Request $request
  171. * @return string
  172. */
  173. public function postSaveCategory(Request $request){
  174. try {
  175. $params = $request->all();
  176. $keys = array('corp_id', 'activity_id', 'category_id', 'category_name');
  177. foreach ($keys as $key) {
  178. if (empty($params[$key])) {
  179. return Api::json(config('code.params_error'), trans('params.'.$key) . trans('msg.params_not_null'));
  180. }
  181. }
  182. $res = QuestionService::saveCategory($params);
  183. return Api::json($res['code'], $res['msg'], $res['data']);
  184. } catch (\Exception $e) {
  185. return Api::serviceError($e);
  186. }
  187. }
  188. /**
  189. * 删除题目
  190. * 2018-06-22 10:24:00
  191. * @param Request $request
  192. * @return string
  193. */
  194. public function postDelQuestion(Request $request){
  195. try {
  196. $params = $request->all();
  197. $keys = array('corp_id', 'activity_id', 'category_id', 'question_id');
  198. foreach ($keys as $key) {
  199. if (empty($params[$key])) {
  200. return Api::json(config('code.params_error'), trans('params.'.$key) . trans('msg.params_not_null'));
  201. }
  202. }
  203. $res = QuestionService::delQuestion($params);
  204. return Api::json($res['code'], $res['msg'], $res['data']);
  205. } catch (\Exception $e) {
  206. return Api::serviceError($e);
  207. }
  208. }
  209. }
  210. ?>