common.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. if(!function_exists('get_this_class_methods'))
  12. {
  13. /**获取当前类方法
  14. * @param $class
  15. * @return array
  16. */
  17. function get_this_class_methods($class,$unarray = []) {
  18. $arrayall = get_class_methods($class);
  19. if ($parent_class = get_parent_class($class)) {
  20. $arrayparent = get_class_methods($parent_class);
  21. $arraynow = array_diff($arrayall, $arrayparent);//去除父级的
  22. } else {
  23. $arraynow = $arrayall;
  24. }
  25. return array_diff($arraynow, $unarray);//去除无用的
  26. }
  27. }
  28. if(!function_exists('get_tree_children'))
  29. {
  30. /**
  31. * tree 子菜单
  32. * @param $data
  33. * @param int $pid
  34. * @return array
  35. */
  36. function get_tree_children($data, $childrenname = 'children')
  37. {
  38. $list = array();
  39. foreach($data as $value){
  40. $list[$value['id']] = $value;
  41. }
  42. static $tree = array(); //格式化好的树
  43. foreach ($list as $item){
  44. if (isset($list[$item['pid']])){
  45. $list[$item['pid']][$childrenname][] = &$list[$item['id']];
  46. }
  47. else{
  48. $tree[] = &$list[$item['id']];
  49. }
  50. }
  51. return $tree;
  52. }
  53. }
  54. if (!function_exists('attr_format')) {
  55. /**
  56. * 格式化属性
  57. * @param $arr
  58. * @return array
  59. */
  60. function attr_format($arr)
  61. {
  62. $data = [];
  63. $res = [];
  64. $count = count($arr);
  65. if ($count > 1) {
  66. for ($i = 0; $i < $count - 1; $i++) {
  67. if ($i == 0) $data = $arr[$i]['detail'];
  68. //替代变量1
  69. $rep1 = [];
  70. foreach ($data as $v) {
  71. foreach ($arr[$i + 1]['detail'] as $g) {
  72. //替代变量2
  73. $rep2 = ($i != 0 ? '' : $arr[$i]['value'] . '_$_') . $v . '-$-' . $arr[$i + 1]['value'] . '_$_' . $g;
  74. $tmp[] = $rep2;
  75. if ($i == $count - 2) {
  76. foreach (explode('-$-', $rep2) as $k => $h) {
  77. //替代变量3
  78. $rep3 = explode('_$_', $h);
  79. //替代变量4
  80. $rep4['detail'][$rep3[0]] = isset($rep3[1]) ? $rep3[1] : '';
  81. }
  82. if($count == count($rep4['detail']))
  83. $res[] = $rep4;
  84. }
  85. }
  86. }
  87. $data = isset($tmp) ? $tmp : [];
  88. }
  89. } else {
  90. $dataArr = [];
  91. foreach ($arr as $k => $v) {
  92. foreach ($v['detail'] as $kk => $vv) {
  93. $dataArr[$kk] = $v['value'] . '_' . $vv;
  94. $res[$kk]['detail'][$v['value']] = $vv;
  95. }
  96. }
  97. $data[] = implode('-', $dataArr);
  98. }
  99. return [$data, $res];
  100. }
  101. }
  102. if (!function_exists('verify_domain')) {
  103. /**
  104. * 验证域名是否合法
  105. * @param string $domain
  106. * @return bool
  107. */
  108. function verify_domain(string $domain): bool
  109. {
  110. $res = "/^(?=^.{3,255}$)(http(s)?:\/\/)(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/\w+\.\w+)*$/";
  111. if (preg_match($res, $domain))
  112. return true;
  113. else
  114. return false;
  115. }
  116. }