Handle.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. $param = json_decode($info['param'], true);
  32. foreach ($param as $k => $v) {
  33. if (strstr($v, '|')) {
  34. $temp = explode('|', $v);
  35. $v = Dever::load($temp[1], $temp[0]);
  36. $param[$k] = $v;
  37. }
  38. }
  39. Dever::location($info['url'] . '?' . http_build_query($param));
  40. }
  41. }
  42. return 'error';
  43. }
  44. }