123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace Push\Lib;
- use Dever;
- class Data
- {
- # 直接获取某个页面的内容
- public function getPage($path = 'module', $cdate = 'Y-m-d H:i')
- {
- $page = Dever::input('page');
- if ($page) {
- $uri = $page;
- } else {
- $uri = Dever::uri();
- if (!$uri) {
- $uri = 'home';
- }
- }
-
- $data = $this->getPageData($uri, $cdate);
- $result = array();
- if ($data && $data['info']) {
- foreach ($data['info'] as $k => $v) {
- if ($v['template_id'] > 0 && isset($v['template'])) {
- $result[] = Dever::render($path . '/' . $v['template']['key'], $v);
- }
- }
- }
- return implode("\r\n", $result);
- }
- # 获取某个页面的数据
- public function getPageData($page_id, $cdate = 'Y-m-d H:i')
- {
- $where = array();
- if (is_numeric($page_id)) {
- $where['id'] = $page_id;
- } else {
- $where['key'] = $page_id;
- }
- $data = Dever::db('push/page')->find($where);
- if ($data) {
- $data['info'] = Dever::db('push/info')->select(array('page_id' => $data['id']));
- if ($data['info']) {
- foreach ($data['info'] as $k => $v) {
- $where = array();
- $where['info_id'] = $v['id'];
- $where['limit'] = '0,' . $v['num'];
- $data['info'][$k]['data'] = Dever::db('push/data')->getAll($where);
- if ($data['info'][$k]['data']) {
- foreach ($data['info'][$k]['data'] as $k1 => $v1) {
- $data['info'][$k]['data'][$k1] = $this->getOne($v1, 2, $cdate);
- }
- }
- $data['info'][$k]['template'] = Dever::db('push/template')->find($v['template_id']);
- }
- }
- }
- return $data;
- }
- # 获取某个推送位的所有数据
- public function get($key, $name, $total = 10, $col = '1,2,3', $func = '-1', $page = false, $limit = false, $cdate = 'Y-m-d H:i')
- {
- $info = Dever::db('push/info')->one(array('key' => $key));
- if (!$info) {
- $insert['key'] = $key;
- $insert['name'] = $name;
- $insert['num'] = $total;
- $insert['col'] = $col;
- $insert['func'] = $func;
- $info['id'] = Dever::db('push/info')->insert($insert);
- $info['num'] = $insert['num'];
- }
- $data = array();
- if ($info) {
- if (!$info['num']) {
- $info['num'] = 20;
- }
- if ($limit > 0) {
- $info['num'] = $limit;
- }
- $where = array();
- $where['info_id'] = $info['id'];
- if ($page) {
- $where['page'] = array($info['num'], 'list');
- $data = Dever::db('push/data')->getAllPage($where);
- } else {
- $where['limit'] = '0,' . $info['num'];
- $data = Dever::db('push/data')->getAll($where);
- }
- if ($data) {
- foreach ($data as $k => $v) {
- $data[$k] = $this->getOne($v, 2, $cdate);
- }
- }
- }
- return $data;
- }
- # 获取某条推送位的数据
- public function getOne($info, $state = 2, $cdate = 'Y-m-d H:i')
- {
- $data = is_array($info) ? $info : Dever::db('push/data')->one($info);
- $source = array();
- $info = array();
- $col = array();
- if ($data['type'] > 0) {
- $func = Dever::db('push/func')->one($data['type']);
- if ($func) {
- if ($func['type'] == 1) {
- $method = $func['api'];
- $col = Dever::array_decode($func['col']);
- $source = Dever::load($method, $data['type_id']);
- } else {
- $data['type_id'] = $func['api'];
- }
- }
- }
- if ($state == 1) {
- $result = array();
- } else {
- $result = $data;
- $result['cdate_string'] = date($cdate, $result['cdate']);
- }
- $lang = Dever::getLang();
- if ($lang) {
- $key = $lang . '_col_';
- } else {
- $key = 'col_';
- }
- foreach ($data as $k => $v) {
- $s = strstr($k, $key);
- if ($lang && strstr($k, 'col_') && !$s) {
- $s = true;
- $key = 'col_';
- }
- if ($s) {
- $id = str_replace($key, '', $k);
- $col_info = Dever::db('push/col')->one($id);
- if (!$col_info) {
- continue;
- }
- if (!$v && $source && $col) {
- foreach ($col as $k1 => $v1) {
- if ($v1['col_id'] == $id && isset($source[$v1['name']]) && $source[$v1['name']]) {
- $v = $source[$v1['name']];
- }
- }
- }
- if ($state == 1) {
- if (strstr($v, 'jpg') || strstr($v, 'png') || strstr($v, 'gif')) {
- $v = '<img src="'.$v.'" width="150"/>';
- }
- if (!isset($result[$k])) {
- $result[$k] = array();
- }
- $result[$k][$col_info['name']] = $v;
- } else {
- $result[$col_info['key']] = $v;
- }
- }
- }
- return $result;
- }
- }
|