common.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 string $settings 样式批设置
  82. * @param type $sheetname sheet标题
  83. * @param type $saveurl 保存位置
  84. * @param type $author 作者
  85. */
  86. function export($columns, $rows, $filename = "jjrcw", $settings = [], $sheetname = "sheet1", $saveurl = "php://output", $author = "晋江人才网") {
  87. $datatype = new \PhpOffice\PhpSpreadsheet\Cell\DataType;
  88. $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  89. $spreadsheet->getProperties()->setCreator($author)
  90. ->setLastModifiedBy($author)
  91. ->setTitle($filename)
  92. ->setSubject($filename);
  93. $spreadsheet->setActiveSheetIndex(0);
  94. $objPHPExcel = $spreadsheet->getActiveSheet();
  95. $objPHPExcel->setTitle($sheetname);
  96. $filename .= "_" . time();
  97. $titleStartLine = 1;
  98. $rowStartLine = $titleStartLine + 1;
  99. $colCount = count($columns);
  100. $rowCount = count($rows);
  101. //设置表头
  102. for ($i = 0; $i < $colCount; $i++) {
  103. $objPHPExcel->setCellValue(getExcelColumnByIndex($i) . $titleStartLine, $columns[$i]);
  104. }
  105. //设置正文内容
  106. for ($i = 0; $i < $rowCount; $i++) {
  107. for ($n = 0; $n < $colCount; $n++) {
  108. if (is_numeric($rows[$i][$n]) && strlen($rows[$i][$n]) > 17) {
  109. //超过17位的数字转成文本方以免被转化为科学数
  110. $objPHPExcel->setCellValueExplicit(getExcelColumnByIndex($n) . ($rowStartLine + $i), $rows[$i][$n], $datatype::TYPE_STRING);
  111. } else {
  112. $objPHPExcel->setCellValue(getExcelColumnByIndex($n) . ($rowStartLine + $i), $rows[$i][$n]);
  113. }
  114. }
  115. }
  116. $objPHPExcel->getDefaultColumnDimension()->setWidth(16); //默认列宽
  117. $objPHPExcel->getDefaultRowDimension()->setRowHeight(30); //默认列高
  118. //样式设置
  119. foreach ($settings as $type => $cfg) {
  120. switch ($type) {
  121. case "width":
  122. for ($i = 0; $i < count($cfg); $i++) {
  123. $objPHPExcel->getColumnDimension($cfg[$i][0])->setWidth($cfg[$i][1]);
  124. }
  125. break;
  126. case "height":
  127. for ($i = 0; $i < count($cfg); $i++) {
  128. $objPHPExcel->getRowDimension($cfg[$i][0])->setRowHeight($cfg[$i][1]);
  129. }
  130. break;
  131. case "background-color":
  132. for ($i = 0; $i < count($cfg); $i++) {
  133. $objPHPExcel->getStyle($cfg[$i][0])->getFill()->setFillType(PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor($cfg[$i][1])->setARGB($cfg[$i][1]);
  134. }
  135. break;
  136. case "border":
  137. $objPHPExcel->getStyle($cfg)->applyFromArray(array(
  138. "borders" => array(
  139. "allBorders" => array(
  140. 'borderStyle' => PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN
  141. )
  142. )
  143. ));
  144. break;
  145. case "align":
  146. for ($i = 0; $i < count($cfg); $i++) {
  147. switch ($cfg[$i][1]) {
  148. case "left":
  149. $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT;
  150. break;
  151. case "right":
  152. $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT;
  153. break;
  154. case "hCenter":
  155. $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER;
  156. break;
  157. case "hJustify":
  158. $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY;
  159. break;
  160. case "top":
  161. $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP;
  162. break;
  163. case "bottom":
  164. $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_BOTTOM;
  165. break;
  166. case "vCenter":
  167. $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER;
  168. break;
  169. case "vJustify":
  170. $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_JUSTIFY;
  171. break;
  172. }
  173. $align = [];
  174. if ($hAlign) {
  175. $align["horizontal"] = $hAlign;
  176. }
  177. if ($vAlign) {
  178. $align["vertical"] = $vAlign;
  179. }
  180. $objPHPExcel->getStyle($cfg[$i][0])->applyFromArray([
  181. "alignment" => $align
  182. ]);
  183. }
  184. break;
  185. case "color":
  186. for ($i = 0; $i < count($cfg); $i++) {
  187. $objPHPExcel->getStyle($cfg[$i][0])->getFont()->getColor()->setARGB($cfg[$i][1]);
  188. }
  189. break;
  190. case "wrap":
  191. for ($i = 0; $i < count($cfg); $i++) {
  192. $objPHPExcel->getStyle($cfg[$i])->getAlignment()->setWrapText(true); //cellvalue包含\n设为自动换行
  193. }
  194. break;
  195. }
  196. }
  197. header('Content-Type: application/vnd.ms-excel');
  198. header('Content-Disposition: attachment;filename="' . $filename . '"');
  199. $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls($spreadsheet);
  200. $writer->save($saveurl);
  201. //删除临时的sheet
  202. $spreadsheet->disconnectWorksheets();
  203. unset($spreadsheet);
  204. exit;
  205. }
  206. /**
  207. * 根据传入的数值(游标),从26个英文字母的数组中查询,返回excel列标
  208. * @param int $index 游标从0开始
  209. * @return string 返回列标
  210. */
  211. function getExcelColumnByIndex(int $index) {
  212. $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');
  213. if ($letters[$index])
  214. return $letters[$index];
  215. $rowIndex = floor($index / 26) - 1;
  216. $colIndex = $index % 26;
  217. return $letters[$rowIndex] . $letters[$colIndex];
  218. }
  219. /**
  220. * 公共的excel设置
  221. * @return type
  222. */
  223. function getCommonExcelSetting($columns, $rows) {
  224. return $settings = [
  225. "background-color" => [[sprintf("A1:%s1", getExcelColumnByIndex($columns - 1)), "0066CC"]],
  226. "color" => [[sprintf("A1:%s1", getExcelColumnByIndex($columns - 1)), "FFFFFF"]],
  227. "border" => sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1),
  228. "wrap" => [sprintf("A2:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1)],
  229. "align" => [[sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1), "hCenter"], [sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1), "vCenter"]]
  230. ];
  231. }
  232. /**
  233. * 检查是不是excel格式,不确定是否都是可用文件,主要还是第一和最后一个比较常见
  234. * @param type $mime
  235. * @return type
  236. */
  237. function isExcelFile($mime) {
  238. return in_array($mime, [
  239. "application/vnd.ms-excel",
  240. "application/msexcel",
  241. "application/x-msexcel",
  242. "application/x-ms-excel",
  243. "application/x-excel",
  244. "application/x-dos_ms_excel",
  245. "application/xls",
  246. "application/x-xls",
  247. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  248. ]);
  249. }
  250. /**
  251. * 获得上传文件的路径
  252. * @param $path
  253. * @return string
  254. */
  255. function getStoragePath($path) {
  256. if ($path) {
  257. return "/storage/{$path}";
  258. }
  259. return "";
  260. }
  261. function getFileView($path) {
  262. $complete_path = "https://report.jinjianghc.com/" . getStoragePath($path);
  263. return "https://fileview.jinjianghc.com/onlinePreview?url=" . base64_encode($complete_path);
  264. }