Html.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace Heanup\Frame\Helper;
  3. /**
  4. * 格式转换
  5. */
  6. class Html extends \Heanup\Frame\Helper
  7. {
  8. /**
  9. * @param null $msg
  10. * @param null $url
  11. * @param array $get
  12. * @param int $wait
  13. */
  14. public static function location($msg = null, $url = null,$isParent=FALSE, $get = Array(), $wait = 1)
  15. {
  16. $url = self::getSiteUrl($url, $get);
  17. $target=$isParent?".parent":"";
  18. $str = "<script type='text/javascript'>function flash(){window".$target.".location.href='$url';}setTimeout('flash()','" . intval($wait) * 500 . "');</script>";
  19. if (!is_string($msg)) {
  20. $msg = $msg ? "操作成功!" : "操作失败!";
  21. }
  22. echo $str . $msg . "&nbsp;&nbsp;正在<a href='$url'>跳转中</a>.....";
  23. die();
  24. }
  25. /**
  26. * 取得当前页地址
  27. * @return mixed
  28. */
  29. public static function self()
  30. {
  31. return $_SERVER['REQUEST_URI'];
  32. }
  33. /**
  34. * 调整url自动生成规则
  35. * @param string $action
  36. * @param array $get
  37. * @param bool $mergeGet
  38. * @return string
  39. */
  40. public static function getSiteUrl($action = '', Array $get = array(), $mergeGet = false)
  41. {
  42. $url = '/' . trim($action, '/');
  43. if ($mergeGet) {
  44. $get = array_merge($_GET, $get);
  45. }
  46. if ($get) {
  47. $get = urldecode(http_build_query($get));
  48. }
  49. if ($get) {
  50. $url .= '?' . $get;
  51. }
  52. return $url;
  53. }
  54. /**
  55. * 链接生成器
  56. * @param $url
  57. * @param array $get
  58. * @param null $text
  59. * @param null $attributes
  60. * @return string
  61. */
  62. public static function anchor($url, Array $get = array(), $text = null, $attributes = null)
  63. {
  64. $url = '/' . trim($url, "/") . '/?' . http_build_query($get);
  65. $text = strlen($text) <= 0 ? $url : $text;
  66. $string = "<a href='$url' $attributes>$text</a>";
  67. return $string;
  68. }
  69. /**
  70. * 简易分页函数
  71. * @param string $url 跳转地址
  72. * @param int $totalPage 总页数
  73. * @param int $current 当前页
  74. * @param int $records 总记录数
  75. * @param int $displayCount 总共显示的页数
  76. * @return string
  77. */
  78. public static function pagination($url, $totalPage = 10, $current = 1, $records = 100, $displayCount = 10)
  79. {
  80. $current = ($current < $totalPage) ? $current : $totalPage;
  81. $current = ($current >= 1) ? $current : 1;
  82. $first = 1;
  83. $half = ceil($displayCount / 2);
  84. $end = $totalPage;
  85. $prev = $current - 1;
  86. $next = $current + 1;
  87. foreach ($_GET as $key => $value) {
  88. if($value === ''){
  89. unset($_GET[$key]);
  90. }
  91. }
  92. $linkSum = "<li><span class='sum'>总数:$records</span></li>";
  93. $linkFirst = '<li>' . self::anchor($url, array_merge($_GET, array('page' => $first)), '&laquo;') . '</li>';
  94. $linkPrev = '<li>' . self::anchor($url, array_merge($_GET, array('page' => $prev)), '&#8249;') . '</li>';
  95. $linkNext = '<li>' . self::anchor($url, array_merge($_GET, array('page' => $next)), '&#8250;') . '</li>';
  96. $linkEnd = '<li>' . self::anchor($url, array_merge($_GET, array('page' => $end)), '&raquo;') . '</li>';
  97. if ($current <= $half) {
  98. $linkFirst = '';
  99. $last = $displayCount;
  100. if ($last > $end) {
  101. $last = $end;
  102. }
  103. if ($current == 1) {
  104. $linkPrev = '';
  105. }
  106. if ($current == $end) {
  107. $linkNext = '';
  108. }
  109. } elseif ($current > ($end - $half) AND $current <= $end) {
  110. $first = $current - $half + 1;
  111. $last = $end;
  112. $linkEnd = '';
  113. if ($current == $end) {
  114. $linkNext = '';
  115. }
  116. } else {
  117. $first = $current - $half + 1;
  118. $last = $current + $half;
  119. }
  120. $string = null;
  121. for ($i = $first; $i <= $last; $i++) {
  122. if ($i == $current) {
  123. $string .= '<li class="active">' . self::anchor($url, array_merge($_GET, array('page' => $i)), "$i", "class='active'") . '</li>';
  124. } else {
  125. $string .= '<li>' . self::anchor($url, array_merge($_GET, array('page' => $i)), "$i") . '</li>';
  126. }
  127. }
  128. return $linkSum . $linkFirst . $linkPrev . $string . $linkNext . $linkEnd;
  129. }
  130. /**
  131. * 下拉菜单生成器
  132. * @param $list
  133. * @param $key
  134. * @param $text
  135. * @param null $attributes
  136. * @return null|string
  137. */
  138. public static function loopOutputSelect($list, $key = '', $text = '', $attributes = null)
  139. {
  140. if (!is_array($list) OR empty($list)) {
  141. return null;
  142. }
  143. $string = null;
  144. foreach ($list as $k => $v) {
  145. $selected = null;
  146. if ($k == $key) {
  147. $selected = "selected='selected'";
  148. }
  149. $string .= "<option value='$k' $selected >$v</option>\n";
  150. }
  151. if (!empty($text)) {
  152. $string = "<select name='$text' id='$text' $attributes>" . $string . "</select>";
  153. }
  154. return $string;
  155. }
  156. /**
  157. * 单选按钮生成器
  158. * @param $list
  159. * @param $key
  160. * @param $text
  161. * @param null $attributes
  162. * @return null|string
  163. */
  164. public static function loopOutputRadio($list, $key = '', $text = '', $attributes = null)
  165. {
  166. if (!is_array($list) OR empty($list)) {
  167. return null;
  168. }
  169. $i = 0;
  170. $string = null;
  171. foreach ($list as $k => $v) {
  172. $checked = null;
  173. if ($k == $key) {
  174. $checked = "checked='checked'";
  175. }
  176. $string .= "<input type='radio' name='$text' value='$k' id='$text-$i' $checked $attributes /><label for='$text-$i'>$v</label>\n";
  177. $i++;
  178. }
  179. return $string;
  180. }
  181. /**
  182. * 多选框生成器
  183. * @param $list
  184. * @param string $key
  185. * @param string $text
  186. * @param string $attributes
  187. * @param string $separator
  188. * @return bool|string
  189. */
  190. public static function loopOutputCheckbox($list, $key = "", $text = '', $attributes = "", $separator = "\n")
  191. {
  192. if (!is_array($list) || empty($list)) {
  193. return false;
  194. }
  195. $string = '';
  196. if (!is_array($key)) {
  197. $key = array($key);
  198. }
  199. foreach ($list as $k => $v) {
  200. $string .= "<input type='checkbox' name='$text" . "[]' value='$k' ";
  201. $string .= in_array($k, $key) ? " checked ='checked'" : "";
  202. $string .= $attributes;
  203. $string .= " />$v" . $separator;
  204. }
  205. return $string;
  206. }
  207. /**
  208. * 获取强密码
  209. * @param int $length
  210. * @return string
  211. */
  212. public static function getStrongCode($length = 8)
  213. {
  214. $length = $length > 8 ? intval($length) : 8;
  215. $arr = array();
  216. $arr[] = self::getRandoms($length - 4);
  217. $arr[] = strtoupper(self::getRandoms(1, 'char'));
  218. $arr[] = strtolower(self::getRandoms(1, 'char'));
  219. $arr[] = mt_rand(0, 9);
  220. $special = '!@#$%^*()';
  221. $arr[] = $special{mt_rand(0, 11)};
  222. shuffle($arr);
  223. return implode('', $arr);
  224. }
  225. /**
  226. * 获取随机数
  227. *
  228. * @param int $length
  229. * @param string $type (可选)
  230. * @return string
  231. */
  232. public static function getRandoms($length = 4, $type = '')
  233. {
  234. $returnStr = '';
  235. $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
  236. if ($type == 'char') {
  237. $start = 10;
  238. $end = 61;
  239. } elseif ($type == 'num') {
  240. $start = 0;
  241. $end = 9;
  242. } else {
  243. $start = 0;
  244. $end = 61;
  245. }
  246. for ($i = 0; $i < $length; $i++) {
  247. $returnStr .= $pattern{mt_rand($start, $end)}; // 生成php随机数
  248. }
  249. return $returnStr;
  250. }
  251. }