common.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 $columns 列标题
  79. * @param type $rows 内容
  80. * @param string $filename 文件名
  81. * @param type $sheetname sheet标题
  82. * @param type $saveurl 保存位置
  83. * @param type $author 作者
  84. */
  85. function export($columns, $rows, $filename = "jjrcw", $sheetname = "sheet1", $saveurl = "php://output", $author = "晋江人才网") {
  86. $datatype = new \PhpOffice\PhpSpreadsheet\Cell\DataType;
  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($sheetname);
  95. $filename .= "_" . time();
  96. $titleStartLine = 1;
  97. $rowStartLine = $titleStartLine + 1;
  98. $colCount = count($columns);
  99. $rowCount = count($rows);
  100. //设置表头
  101. for ($i = 0; $i < $colCount; $i++) {
  102. $objPHPExcel->setCellValue(getExcelColumnByIndex($i) . $titleStartLine, $columns[$i]);
  103. }
  104. //设置正文内容
  105. for ($i = 0; $i < $rowCount; $i++) {
  106. for ($n = 0; $n < $colCount; $n++) {
  107. if (is_numeric($rows[$i][$n]) && strlen($rows[$i][$n]) > 17) {
  108. //超过17位的数字转成文本方以免被转化为科学数
  109. $objPHPExcel->setCellValueExplicit(getExcelColumnByIndex($n) . ($rowStartLine + $i), $rows[$i][$n], $datatype::TYPE_STRING);
  110. } else {
  111. $objPHPExcel->setCellValue(getExcelColumnByIndex($n) . ($rowStartLine + $i), $rows[$i][$n]);
  112. }
  113. }
  114. }
  115. header('Content-Type: application/vnd.ms-excel');
  116. header('Content-Disposition: attachment;filename="' . $filename . '.xlsx"');
  117. $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
  118. $writer->save($saveurl);
  119. //删除临时的sheet
  120. $spreadsheet->disconnectWorksheets();
  121. unset($spreadsheet);
  122. exit;
  123. }
  124. /**
  125. * 根据传入的数值(游标),从26个英文字母的数组中查询,返回excel列标
  126. * @param int $index 游标从0开始
  127. * @return string 返回列标
  128. */
  129. function getExcelColumnByIndex(int $index) {
  130. $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');
  131. if ($letters[$index])
  132. return $letters[$index];
  133. $rowIndex = floor($index / 26) - 1;
  134. $colIndex = $index % 26;
  135. return $letters[$rowIndex] . $letters[$colIndex];
  136. }
  137. /**
  138. * 检查是不是excel格式,不确定是否都是可用文件,主要还是第一和最后一个比较常见
  139. * @param type $mime
  140. * @return type
  141. */
  142. function isExcelFile($mime) {
  143. return in_array($mime, [
  144. "application/vnd.ms-excel",
  145. "application/msexcel",
  146. "application/x-msexcel",
  147. "application/x-ms-excel",
  148. "application/x-excel",
  149. "application/x-dos_ms_excel",
  150. "application/xls",
  151. "application/x-xls",
  152. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  153. ]);
  154. }
  155. /**
  156. * 获得上传文件的路径
  157. * @param $path
  158. * @return string
  159. */
  160. function getStoragePath($path) {
  161. if ($path) {
  162. return "/storage/{$path}";
  163. }
  164. return "";
  165. }