common.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. // 应用公共文件
  3. /**
  4. *
  5. * @param type $algo
  6. * @param type $password
  7. * @param type $salt
  8. * @param type $hash_iterations
  9. * @return type
  10. */
  11. function simple_hash($algo = 'md5', $password = '', $salt = '', $hash_iterations = 2) {
  12. $res = '';
  13. $pass = $salt . $password;
  14. $encoded = hash($algo, $pass, true);
  15. $iteration = $hash_iterations - 1;
  16. if ($iteration > 0) {
  17. for ($i = 0; $i < $iteration; $i++) {
  18. $encoded = hash($algo, $encoded, true);
  19. }
  20. }
  21. $tmp = unpack('H*', $encoded);
  22. if (!empty($tmp) && !empty($tmp[1])) {
  23. $res = $tmp[1];
  24. }
  25. return $res;
  26. }
  27. /**
  28. * 检查权限
  29. * @param type $url
  30. * @param type $old_url
  31. * @return type
  32. */
  33. function chkCommission($url, $old_url) {
  34. return app\common\api\MenuApi::chkPermission($url, $old_url);
  35. }
  36. /**
  37. * 随机字符ID
  38. * @return type
  39. */
  40. function getStringId() {
  41. $day = random_int(10, 30);
  42. $time = strtotime("-4 years -6 months -" . $day . " days");
  43. $randnum = random_int(100000000, 999999999);
  44. $randnum = str_shuffle($randnum);
  45. return $time . $randnum;
  46. }
  47. function isNullOrEmpty($obj) {
  48. if (!$obj || $obj == "" || !isset($obj))
  49. return "";
  50. return $obj;
  51. }
  52. function getTreeList($array, $id_field = "id", $pid_field = "pid", $value = "0") {
  53. static $result = [];
  54. foreach ($array as $key => $item) {
  55. if ($value == $item[$pid_field]) {
  56. $result[] = $item;
  57. unset($array[$key]);
  58. getTreeList($array, $id_field, $pid_field, $item[$id_field]);
  59. }
  60. }
  61. return $result;
  62. }
  63. /**
  64. * 读取excel
  65. * @param type $filepath
  66. * @param type $sheetIndex
  67. * @return type
  68. */
  69. function getExcelDatas($filepath, $sheetIndex = 0) {
  70. $reader_type = \PHPExcel_IOFactory::identify($filepath);
  71. $reader = \PHPExcel_IOFactory::createReader($reader_type);
  72. $phpexcel = $reader->load($filepath);
  73. $sheet = $phpexcel->getSheet($sheetIndex);
  74. return $sheet->toArray();
  75. }
  76. /**
  77. * 导出excel
  78. * @param type $title 标题
  79. * @param type $columns 列标题
  80. * @param type $rows 内容
  81. * @param string $filename 文件名
  82. * @param type $saveurl 保存位置
  83. * @param type $author 作者
  84. */
  85. function export($title, $columns, $rows, $filename = "jjrcw", $saveurl = "php://output", $author = "晋江人才网") {
  86. $filename .= "_" . time();
  87. $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  88. $spreadsheet->getProperties()->setCreator($author)
  89. ->setLastModifiedBy($author)
  90. ->setTitle($filename)
  91. ->setSubject($filename);
  92. $spreadsheet->setActiveSheetIndex(0);
  93. $objPHPExcel = $spreadsheet->getActiveSheet();
  94. $objPHPExcel->setTitle($title);
  95. $titleStartLine = 1;
  96. $rowStartLine = $titleStartLine + 1;
  97. $colCount = count($columns);
  98. $rowCount = count($rows);
  99. //设置表头
  100. for ($i = 0; $i < $colCount; $i++) {
  101. $objPHPExcel->setCellValue(getExcelColumnByIndex($i) . $titleStartLine, $columns[$i]);
  102. }
  103. //设置正文内容
  104. for ($i = 0; $i < $rowCount; $i++) {
  105. for ($n = 0; $n < $colCount; $n++) {
  106. $objPHPExcel->setCellValue(getExcelColumnByIndex($n) . ($rowStartLine + $i), $rows[$i][$n]);
  107. }
  108. }
  109. header('Content-Type: application/vnd.ms-excel');
  110. header('Content-Disposition: attachment;filename="' . $filename . '.xlsx"');
  111. $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
  112. $writer->save($saveurl);
  113. //删除临时的sheet
  114. $spreadsheet->disconnectWorksheets();
  115. unset($spreadsheet);
  116. exit;
  117. }
  118. /**
  119. * 根据传入的数值(游标),从26个英文字母的数组中查询,返回excel列标
  120. * @param int $index 游标从0开始
  121. * @return string 返回列标
  122. */
  123. function getExcelColumnByIndex(int $index) {
  124. $letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
  125. if ($letters[$index])
  126. return $letters[$index];
  127. $rowIndex = floor($index / 26) - 1;
  128. $colIndex = $index % 26;
  129. return $letters[$rowIndex] . $letters[$colIndex];
  130. }
  131. /**
  132. * 检查是不是excel格式,不确定是否都是可用文件,主要还是第一和最后一个比较常见
  133. * @param type $mime
  134. * @return type
  135. */
  136. function isExcelFile($mime) {
  137. return in_array($mime, [
  138. "application/vnd.ms-excel",
  139. "application/msexcel",
  140. "application/x-msexcel",
  141. "application/x-ms-excel",
  142. "application/x-excel",
  143. "application/x-dos_ms_excel",
  144. "application/xls",
  145. "application/x-xls",
  146. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  147. ]);
  148. }
  149. /**
  150. * 获得上传文件的路径
  151. * @param $path
  152. * @return string
  153. */
  154. function getStoragePath($path) {
  155. if ($path) {
  156. return "/storage/{$path}";
  157. }
  158. return "";
  159. }