common.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. *
  78. * @param type $title 标题
  79. * @param type $data 数据
  80. * @param type $detail 抬头内容 array(array(),array())
  81. * @param string $filename 文件名
  82. * @param type $format 数字格式化
  83. * @param type $saveurl 输出方式
  84. * @param type $author 作者
  85. */
  86. function export($title, $data, $detail = null, $filename = "g2beauty", $freeze = null, $format = [], $saveurl = "php://output", $other_config = null, $author = "g2beauty.cn") {
  87. }
  88. /**
  89. * 检查是不是excel格式,不确定是否都是可用文件,主要还是第一和最后一个比较常见
  90. * @param type $mime
  91. * @return type
  92. */
  93. function isExcelFile($mime) {
  94. return in_array($mime, [
  95. "application/vnd.ms-excel",
  96. "application/msexcel",
  97. "application/x-msexcel",
  98. "application/x-ms-excel",
  99. "application/x-excel",
  100. "application/x-dos_ms_excel",
  101. "application/xls",
  102. "application/x-xls",
  103. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  104. ]);
  105. }
  106. /**
  107. * 获得上传文件的路径
  108. * @param $path
  109. * @return string
  110. */
  111. function getStoragePath($path){
  112. if($path){
  113. return "/storage/{$path}";
  114. }
  115. return "";
  116. }