Handle.php 1.6 KB

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