博客列表 >高效的编写后台代码

高效的编写后台代码

人傻钱少好说话
人傻钱少好说话原创
2020年09月10日 00:31:40855浏览
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\service\AdminBase;//权限
  4. use app\api\model\Answer as AnswerModel;
  5. /** thinkphp 5.24
  6. * 答题任务
  7. * auth:yaodunyuan
  8. * create 2020-09-01 13:44
  9. */
  10. class Answer extends AdminBase
  11. {
  12. var $answermodel ;
  13. var $list ;
  14. var $name;
  15. var $id ;
  16. function _initialize($pagesize = 15)
  17. {
  18. $this->answermodel = new AnswerModel();
  19. $name = $this->name = input('name');
  20. $id = $this->id = input('id');
  21. if (!$name) {
  22. $this->list = $this->answermodel->paginate($pagesize, false);
  23. }else{
  24. $this->list = $this->answermodel->where(['title' =>['like' ,"{$name}%"] ])->paginate($pagesize,false);
  25. }
  26. }
  27. /**
  28. * 题目列表
  29. */
  30. function index(){
  31. return view()
  32. ->assign([
  33. 'name' => $this->name,
  34. 'list' => $this->list ?? null ,
  35. 'page' => $this->list->render()
  36. ]);
  37. }
  38. /**
  39. * 上传文件
  40. */
  41. function uploadfile(){
  42. $file = $_FILES['file'];
  43. if(!in_array(explode('.',$file['name'])[1],['xls','xlsx'])){
  44. show(0,'上传类型错误');
  45. }
  46. $data = import_excel($file);
  47. if($data['count']){
  48. show(1,'上传成功',$data);
  49. }
  50. show(0,'上传失败');
  51. }
  52. /**
  53. * 编辑
  54. */
  55. function edit(){
  56. if (request()->isPost()) {
  57. $data = $_POST;
  58. $data['class_name'] = db('answer_class')->where('id',$data['class_id'])->field('name')->find()['name'];
  59. if(!$this->id){
  60. db('answer')->insert($data) ? show(1, '操作成功') : show(0, '操作失败');
  61. }else{
  62. db('answer')->where('id', $this->id)->update($data) ? show(1, '操作成功') : show(0, '操作失败');
  63. }
  64. }
  65. $data = $this->answermodel->where('id',$this->id)->find();
  66. return view()->assign([
  67. 'item' => $data,
  68. 'class_list' => $class_list = db('answer_class')->select(),
  69. 'sys_rw' => db('sys_reward')->where('type',1)->select(),//给予积分
  70. ]);
  71. }
  72. /**
  73. * 添加
  74. */
  75. function add(){
  76. $class_list = db('answer_class')->select();
  77. return view('edit')->assign([
  78. 'class_list' => $class_list,
  79. 'sys_rw' => db('sys_reward')->where('type',1)->select(),//给予积分
  80. ]);
  81. }
  82. /**
  83. * 删除
  84. */
  85. function delete(){
  86. if(!$this->id) show(0,'缺少ID');
  87. $this->answermodel->where(['id' => $this->id])->delete() ? show(1,'操作成功') : show(0,'操作失败');
  88. }
  89. /**
  90. * 下载模板文件
  91. */
  92. function downfile(){
  93. //echo 'dome.xls';die;
  94. $file = fopen(ROOT_PATH.'dome.xls',"rb");
  95. $title = 'dome.xls';
  96. header("Content-Type: application/force-download");
  97. header("Content-Type: application/octet-stream");
  98. header("Content-Type: application/download");
  99. header('Content-Disposition:inline;filename="'.$title.'"');
  100. while(!feof($file)){
  101. echo fread($file,8192);
  102. ob_flush();
  103. flush();
  104. }
  105. fclose();
  106. }
  107. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议