12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace Ad\Lib;
- use Dever;
- class Data
- {
- public function get($key, $data = array(), $cache = true)
- {
- $cache = 'ad_' . md5($key);
- //$data = $cache ? Dever::cache($cache) : $cache;
- $data = false;
- if ($data) {
- return $this->getData($data);
- } elseif (!$data) {
- $page = Dever::db('ad/page')->one(array('key' => $key));
- if ($page) {
- $info = Dever::db('ad/info')->getAll(array('page_id' => $page['id']));
- if ($info) {
- foreach ($info as $k => $v) {
- $where['info_id'] = $v['id'];
- if ($v['order_type'] == 2) {
- $where['order'] = array('id' => 'asc');
- }
- $info[$k]['data'] = Dever::db('ad/data')->getAll($where);
- if ($info[$k]['data'] && $v['order_type'] == 3) {
- $order = array(array_rand($info[$k]['data'], 1));
- $temp = $info[$k]['data'];
- unset($info[$k]['data']);
- foreach ($order as $k1 => $v1) {
- $info[$k]['data'][$k1] = $temp[$v1];
- }
- }
- }
- Dever::cache($cache, $info);
- return $this->getData($info);
- }
- }
- }
- return false;
- }
- private function getData($data)
- {
- if ($data) {
- $time = time();
- $source_type = $this->config(Dever::input('source_type', 'h5'));
- foreach ($data as $k => $v) {
- $child = $v['data'];
- $data[$k]['data'] = array();
- $i = 0;
- foreach ($child as $k1 => $v1) {
- if ($v1['type'] == 2 && ($time < $v1['sdate'] || $time > $v1['edate'])) {
- //unset($data[$k]['data'][$k1]);
- } elseif ($v1['system'] > 0 && $source_type != $v1['system']) {
- //unset($data[$k]['data'][$k1]);
- } else {
- $data[$k]['data'][$i] = $v1;
- $i++;
- }
- }
- }
- }
- return $data;
- }
- private function config($type)
- {
- if ($type == 'ios') {
- return 3;
- }
- if ($type == 'android') {
- return 2;
- }
- if ($type == 'applet') {
- return 4;
- }
- if ($type == 'h5') {
- return 1;
- }
- return -1;
- }
- }
|