LevelController.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 南京Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Http\Controllers;
  12. use App\Exports\Export;
  13. use App\Models\LevelModel;
  14. use App\Services\LevelService;
  15. use Maatwebsite\Excel\Facades\Excel;
  16. /**
  17. * 职级管理-控制器
  18. * @author wesmiler
  19. * @since 2020/11/11
  20. * Class LevelController
  21. * @package App\Http\Controllers
  22. */
  23. class LevelController extends Backend
  24. {
  25. /**
  26. * 构造函数
  27. * @author wesmiler
  28. * @since 2020/11/11
  29. * LevelController constructor.
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. $this->model = new LevelModel();
  35. $this->service = new LevelService();
  36. }
  37. /**
  38. * 获取职级列表
  39. * @return mixed
  40. * @since 2020/11/11
  41. * @author wesmiler
  42. */
  43. public function getLevelList()
  44. {
  45. $result = $this->service->getLevelList();
  46. return $result;
  47. }
  48. /**
  49. * 导出Excel
  50. * @author wesmiler
  51. * @since 2021/4/10
  52. */
  53. public function exportExcel()
  54. {
  55. // 参数
  56. $param = request()->all();
  57. // 文件名称
  58. $fileName = date('YmdHis') . '.xlsx';
  59. // 表格头
  60. $header = ['职级ID', '职级名称', '职级状态', '排序'];
  61. // 获取数据源
  62. $result = $this->model->where("mark", "=", 1)->get()->toArray();
  63. $list = [];
  64. if (!empty($result)) {
  65. foreach ($result as $key => $val) {
  66. $data = [];
  67. $data['id'] = $val['id'];
  68. $data['name'] = $val['name'];
  69. $data['status'] = $val['status'] == 1 ? "在用" : "停用";
  70. $data['sort'] = $val['sort'];
  71. $list[] = $data;
  72. }
  73. }
  74. // 保存文件
  75. if (!Excel::store(new Export($list, $header, "职级列表"), "" . $fileName)) {
  76. return message(MESSAGE_FAILED, false);
  77. }
  78. // 移动文件
  79. copy(storage_path("app") . "/" . $fileName, UPLOAD_TEMP_PATH . "/" . $fileName);
  80. // 下载地址
  81. $fileUrl = get_image_url(str_replace(ATTACHMENT_PATH, "", UPLOAD_TEMP_PATH) . "/" . $fileName);
  82. return message(MESSAGE_OK, true, $fileUrl);
  83. }
  84. }