common.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. // 应用公共文件
  3. use app\common\service\AuthService;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  6. use think\db\exception\DataNotFoundException;
  7. use think\db\exception\DbException;
  8. use think\db\exception\ModelNotFoundException;
  9. use think\facade\Cache;
  10. if (!function_exists('__url')) {
  11. /**
  12. * 构建URL地址
  13. * @param string $url
  14. * @param array $vars
  15. * @param bool $suffix
  16. * @param bool $domain
  17. * @return string
  18. */
  19. function __url(string $url = '', array $vars = [], bool $suffix = true, bool $domain = false): string
  20. {
  21. if (filter_var($url, FILTER_VALIDATE_URL)) return $url;
  22. return url($url, $vars, $suffix, $domain)->build();
  23. }
  24. }
  25. if (!function_exists('password')) {
  26. /**
  27. * 密码加密算法
  28. * @param $value
  29. * @return string
  30. */
  31. function password($value): string
  32. {
  33. $value = sha1('blog_') . md5($value) . md5('_encrypt') . sha1($value);
  34. return sha1($value);
  35. }
  36. }
  37. if (!function_exists('sysConfig')) {
  38. /**
  39. * 获取系统配置信息
  40. * @param $group
  41. * @param $name
  42. * @return mixed
  43. */
  44. function sysConfig($group, $name = null): mixed
  45. {
  46. $where = ['group' => $group];
  47. $value = empty($name) ? Cache::get("sysConfig_{$group}") : Cache::get("sysConfig_{$group}_{$name}");
  48. if (empty($value)) {
  49. if (!empty($name)) {
  50. $where['name'] = $name;
  51. $value = \app\admin\model\SystemConfig::where($where)->value('value');
  52. if (!empty($value) || $value >= 0) Cache::tag('sysConfig')->set("sysConfig_{$group}_{$name}", $value, 3600);
  53. } else {
  54. $value = \app\admin\model\SystemConfig::where($where)->column('value', 'name');
  55. if (!empty($value) || $value >= 0) Cache::tag('sysConfig')->set("sysConfig_{$group}", $value, 3600);
  56. }
  57. }
  58. return $value;
  59. }
  60. }
  61. if (!function_exists('array_format_key')) {
  62. /**
  63. * 二位数组重新组合数据
  64. * @param $array
  65. * @param $key
  66. * @return array
  67. */
  68. function array_format_key($array, $key): array
  69. {
  70. $newArray = [];
  71. foreach ($array as $vo) {
  72. $newArray[$vo[$key]] = $vo;
  73. }
  74. return $newArray;
  75. }
  76. }
  77. if (!function_exists('auth')) {
  78. /**
  79. * auth权限验证
  80. * @param $node
  81. * @return bool
  82. * @throws DataNotFoundException
  83. * @throws DbException
  84. * @throws ModelNotFoundException
  85. */
  86. function auth($node = null): bool
  87. {
  88. $authService = new AuthService(session('admin.id'));
  89. return $authService->checkNode($node);
  90. }
  91. }
  92. /**
  93. * @param string|null $detail
  94. * @param string $name
  95. * @param string $placeholder
  96. * @return string
  97. */
  98. function editor_textarea(?string $detail, string $name = 'desc', string $placeholder = '请输入'): string
  99. {
  100. $editor_type = sysConfig('site', 'editor_type');
  101. return match ($editor_type) {
  102. 'ckeditor' => "<textarea name='{$name}' rows='20' class='layui-textarea editor' placeholder='{$placeholder}'>{$detail}</textarea>",
  103. 'ueditor' => "<script type='text/plain' id='{$name}' name='{$name}' class='editor' data-content='{$detail}'></script>",
  104. 'EasyMDE' => "<textarea id='{$name}' class='editor' name='{$name}'>{$detail}</textarea>",
  105. default => "<div class='wangEditor_div'><textarea name='{$name}' rows='20' class='layui-textarea editor layui-hide'>{$detail}</textarea><div id='editor_toolbar_{$name}'></div><div id='editor_{$name}' style='height: 500px'></div></div>",
  106. };
  107. }
  108. /**
  109. * @desc 导出excel
  110. * @tip 追求性能请使用 xlsWriter https://xlswriter-docs.viest.me/zh-cn
  111. * @param array $header
  112. * @param array $list
  113. * @param string $fileName
  114. * @return void
  115. * @throws Exception
  116. */
  117. function exportExcel(array $header = [], array $list = [], string $fileName = ''): void
  118. {
  119. if (empty($fileName)) $fileName = time();
  120. if (empty($header) || empty($list)) throw new \Exception('导出数据不能为空');
  121. $spreadsheet = new Spreadsheet();
  122. $sheet = $spreadsheet->getActiveSheet();
  123. $headers = array_column($header, 0) ?? array_keys($list[0]);
  124. $sheet->fromArray([$headers], null, 'A1');
  125. $rowIndex = 2;
  126. foreach ($list as $row) {
  127. $rowData = [];
  128. foreach ($header as $item) {
  129. $value = $row[$item[1]] ?? '';
  130. if ($value === null) {
  131. $rowData[] = '';
  132. continue;
  133. }
  134. $rowData[] = $value;
  135. }
  136. $sheet->fromArray([$rowData], null, "A{$rowIndex}");
  137. $rowIndex++;
  138. }
  139. foreach (range('A', $sheet->getHighestColumn()) as $col) {
  140. $sheet->getColumnDimension($col)->setAutoSize(true);
  141. }
  142. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  143. header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"');
  144. header('Cache-Control: max-age=0');
  145. $writer = new Xlsx($spreadsheet);
  146. $writer->save('php://output');
  147. die();
  148. }