Handle.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Short\Lib;
  3. use Dever;
  4. class Handle
  5. {
  6. public function showUrl($id)
  7. {
  8. $info = Dever::db('short/info')->one($id);
  9. $type = Dever::db('short/type')->one($info['type']);
  10. return $type['host'] . Dever::uid($id);
  11. }
  12. public function get($type, $url, $param, $host = '')
  13. {
  14. $param = json_encode($param);
  15. $where['type'] = $type;
  16. $type = Dever::db('short/type')->one($type);
  17. $where['key'] = md5($url . '?' . $param);
  18. $info = Dever::db('short/info')->one($where);
  19. if ($info) {
  20. $id = $info['id'];
  21. } else {
  22. $where['url'] = $url;
  23. $where['param'] = $param;
  24. $id = Dever::db('short/info')->insert($where);
  25. }
  26. if ($host) {
  27. $type['host'] = $host;
  28. }
  29. return $type['host'] . Dever::uid($id);
  30. }
  31. public function show($str)
  32. {
  33. $id = Dever::uid($str, 'decode');
  34. if ($id && $id > 0) {
  35. $info = Dever::db('short/info')->one($id);
  36. if ($info && $info['url']) {
  37. if ($info['param']) {
  38. $param = Dever::json_decode($info['param']);
  39. if ($param) {
  40. foreach ($param as $k => $v) {
  41. if (strstr($v, '|')) {
  42. $temp = explode('|', $v);
  43. $v = Dever::load($temp[1], $temp[0]);
  44. $param[$k] = $v;
  45. }
  46. }
  47. if (strstr($info['url'], '?')) {
  48. $info['url'] .= '&' . http_build_query($param);
  49. } else {
  50. $info['url'] .= '?' . http_build_query($param);
  51. }
  52. }
  53. }
  54. Dever::location($info['url']);
  55. }
  56. }
  57. return 'error';
  58. }
  59. }