common.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 $mime
  79. * @return type
  80. */
  81. function isExcelFile($mime) {
  82. return in_array($mime, [
  83. "application/vnd.ms-excel",
  84. "application/msexcel",
  85. "application/x-msexcel",
  86. "application/x-ms-excel",
  87. "application/x-excel",
  88. "application/x-dos_ms_excel",
  89. "application/xls",
  90. "application/x-xls",
  91. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  92. ]);
  93. }