Data.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Push\Lib;
  3. use Dever;
  4. class Data
  5. {
  6. # 直接获取某个页面的内容
  7. public function getPage($path = 'module', $cdate = 'Y-m-d H:i')
  8. {
  9. $page = Dever::input('page');
  10. if ($page) {
  11. $uri = $page;
  12. } else {
  13. $uri = Dever::uri();
  14. if (!$uri) {
  15. $uri = 'home';
  16. }
  17. }
  18. $data = $this->getPageData($uri, $cdate);
  19. $result = array();
  20. if ($data && $data['info']) {
  21. foreach ($data['info'] as $k => $v) {
  22. if ($v['template_id'] > 0 && isset($v['template'])) {
  23. $result[] = Dever::render($path . '/' . $v['template']['key'], $v);
  24. }
  25. }
  26. }
  27. return implode("\r\n", $result);
  28. }
  29. # 获取某个页面的数据
  30. public function getPageData($page_id, $cdate = 'Y-m-d H:i')
  31. {
  32. $where = array();
  33. if (is_numeric($page_id)) {
  34. $where['id'] = $page_id;
  35. } else {
  36. $where['key'] = $page_id;
  37. }
  38. $data = Dever::db('push/page')->find($where);
  39. if ($data) {
  40. $data['info'] = Dever::db('push/info')->select(array('page_id' => $data['id']));
  41. if ($data['info']) {
  42. foreach ($data['info'] as $k => $v) {
  43. $where = array();
  44. $where['info_id'] = $v['id'];
  45. $where['limit'] = '0,' . $v['num'];
  46. $data['info'][$k]['data'] = Dever::db('push/data')->getAll($where);
  47. if ($data['info'][$k]['data']) {
  48. foreach ($data['info'][$k]['data'] as $k1 => $v1) {
  49. $data['info'][$k]['data'][$k1] = $this->getOne($v1, 2, $cdate);
  50. }
  51. }
  52. $data['info'][$k]['template'] = Dever::db('push/template')->find($v['template_id']);
  53. }
  54. }
  55. }
  56. return $data;
  57. }
  58. # 获取某个推送位的所有数据
  59. public function get($key, $name, $total = 10, $col = '1,2,3', $func = '-1', $page = false, $limit = false, $cdate = 'Y-m-d H:i')
  60. {
  61. $info = Dever::db('push/info')->one(array('key' => $key));
  62. if (!$info) {
  63. $insert['key'] = $key;
  64. $insert['name'] = $name;
  65. $insert['num'] = $total;
  66. $insert['col'] = $col;
  67. $insert['func'] = $func;
  68. $info['id'] = Dever::db('push/info')->insert($insert);
  69. $info['num'] = $insert['num'];
  70. }
  71. $data = array();
  72. if ($info) {
  73. if (!$info['num']) {
  74. $info['num'] = 20;
  75. }
  76. if ($limit > 0) {
  77. $info['num'] = $limit;
  78. }
  79. $where = array();
  80. $where['info_id'] = $info['id'];
  81. if ($page) {
  82. $where['page'] = array($info['num'], 'list');
  83. $data = Dever::db('push/data')->getAllPage($where);
  84. } else {
  85. $where['limit'] = '0,' . $info['num'];
  86. $data = Dever::db('push/data')->getAll($where);
  87. }
  88. if ($data) {
  89. foreach ($data as $k => $v) {
  90. $data[$k] = $this->getOne($v, 2, $cdate);
  91. }
  92. }
  93. }
  94. return $data;
  95. }
  96. # 获取某条推送位的数据
  97. public function getOne($info, $state = 2, $cdate = 'Y-m-d H:i')
  98. {
  99. $data = is_array($info) ? $info : Dever::db('push/data')->one($info);
  100. $source = array();
  101. $info = array();
  102. $col = array();
  103. if ($data['type'] > 0) {
  104. $func = Dever::db('push/func')->one($data['type']);
  105. if ($func) {
  106. if ($func['type'] == 1) {
  107. $method = $func['api'];
  108. $col = Dever::array_decode($func['col']);
  109. $source = Dever::load($method, $data['type_id']);
  110. } else {
  111. $data['type_id'] = $func['api'];
  112. }
  113. }
  114. }
  115. if ($state == 1) {
  116. $result = array();
  117. } else {
  118. $result = $data;
  119. $result['cdate_string'] = date($cdate, $result['cdate']);
  120. }
  121. $lang = Dever::getLang();
  122. if ($lang) {
  123. $key = $lang . '_col_';
  124. } else {
  125. $key = 'col_';
  126. }
  127. foreach ($data as $k => $v) {
  128. $s = strstr($k, $key);
  129. if ($lang && strstr($k, 'col_') && !$s) {
  130. $s = true;
  131. $key = 'col_';
  132. }
  133. if ($s) {
  134. $id = str_replace($key, '', $k);
  135. $col_info = Dever::db('push/col')->one($id);
  136. if (!$col_info) {
  137. continue;
  138. }
  139. if (!$v && $source && $col) {
  140. foreach ($col as $k1 => $v1) {
  141. if ($v1['col_id'] == $id && isset($source[$v1['name']]) && $source[$v1['name']]) {
  142. $v = $source[$v1['name']];
  143. }
  144. }
  145. }
  146. if ($state == 1) {
  147. if (strstr($v, 'jpg') || strstr($v, 'png') || strstr($v, 'gif')) {
  148. $v = '<img src="'.$v.'" width="150"/>';
  149. }
  150. if (!isset($result[$k])) {
  151. $result[$k] = array();
  152. }
  153. $result[$k][$col_info['name']] = $v;
  154. } else {
  155. $result[$col_info['key']] = $v;
  156. }
  157. }
  158. }
  159. return $result;
  160. }
  161. }