common.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. }