Data.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Push\Lib;
  3. use Dever;
  4. class Data
  5. {
  6. public function get($key, $name, $total = 10, $col = '1,2,3', $function = '-1', $page = false, $limit = false, $cdate = 'Y-m-d H:i')
  7. {
  8. $info = Dever::db('push/info')->one(array('key' => $key));
  9. if (!$info) {
  10. $insert['key'] = $key;
  11. $insert['name'] = $name;
  12. $insert['num'] = $total;
  13. $insert['col'] = $col;
  14. $insert['function'] = $function;
  15. $info['id'] = Dever::db('push/info')->insert($insert);
  16. $info['num'] = $insert['num'];
  17. }
  18. $data = array();
  19. if ($info) {
  20. if (!$info['num']) {
  21. $info['num'] = 20;
  22. }
  23. if ($limit > 0) {
  24. $info['num'] = $limit;
  25. }
  26. $where = array();
  27. $where['info_id'] = $info['id'];
  28. if ($page) {
  29. $where['page'] = array($info['num'], 'list');
  30. $data = Dever::db('push/data')->getAllPage($where);
  31. } else {
  32. $where['limit'] = '0,' . $info['num'];
  33. $data = Dever::db('push/data')->getAll($where);
  34. }
  35. if ($data) {
  36. foreach ($data as $k => $v) {
  37. $data[$k] = $this->getOne($v, 2, $cdate);
  38. }
  39. }
  40. }
  41. return $data;
  42. }
  43. public function getOne($info, $state = 2, $cdate = 'Y-m-d H:i')
  44. {
  45. $data = is_array($info) ? $info : Dever::db('push/data')->one($info);
  46. $source = array();
  47. $info = array();
  48. $col = array();
  49. if ($data['type'] > 0) {
  50. $func = Dever::db('push/info')->config['func'];
  51. $func = $func();
  52. $method = false;
  53. if ($func) {
  54. foreach ($func as $k => $v) {
  55. if ($v['id'] == $data['type']) {
  56. $method = $v['api'];
  57. $col = Dever::array_decode($v['col']);
  58. break;
  59. }
  60. }
  61. }
  62. if ($method) {
  63. $source = Dever::load($method, $data['type_id']);
  64. }
  65. }
  66. if ($state == 1) {
  67. $result = array();
  68. } else {
  69. $result = $data;
  70. unset($result['data']);
  71. $result['cdate_string'] = date($cdate, $result['cdate']);
  72. }
  73. $text = $data['data'];
  74. $text = Dever::array_decode($text);
  75. foreach ($text as $k => $v) {
  76. foreach ($v as $k1 => $v1) {
  77. if (strstr($k1, 'col_')) {
  78. $id = str_replace('col_', '', $k1);
  79. $col_info = Dever::db('push/col')->one($id);
  80. if (!$v1 && $source && $col) {
  81. foreach ($col as $k2 => $v2) {
  82. if ($v2['col_id'] == $id && isset($source[$v2['name']]) && $source[$v2['name']]) {
  83. $v1 = $source[$v2['name']];
  84. }
  85. }
  86. }
  87. if ($state == 1) {
  88. if (strstr($v1, 'jpg') || strstr($v1, 'png') || strstr($v1, 'gif')) {
  89. $v1 = '<img src="'.$v1.'" width="150"/>';
  90. }
  91. if (!isset($result[$k])) {
  92. $result[$k] = array();
  93. }
  94. $result[$k][$col_info['name']] = $v1;
  95. } else {
  96. $result[$col_info['ename']] = $v1;
  97. }
  98. }
  99. }
  100. }
  101. return $result;
  102. }
  103. }