Handle.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 = Dever::json_decode($info['param']);
  33. if ($param) {
  34. foreach ($param as $k => $v) {
  35. if (strstr($v, '|')) {
  36. $temp = explode('|', $v);
  37. $v = Dever::load($temp[1], $temp[0]);
  38. $param[$k] = $v;
  39. }
  40. }
  41. if (strstr($info['url'], '?')) {
  42. $info['url'] .= '&' . http_build_query($param);
  43. } else {
  44. $info['url'] .= '?' . http_build_query($param);
  45. }
  46. }
  47. }
  48. Dever::location($info['url']);
  49. }
  50. }
  51. return 'error';
  52. }
  53. }