Handle.php 2.0 KB

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