Data.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Ad\Lib;
  3. use Dever;
  4. class Data
  5. {
  6. public function get($key, $data = array(), $cache = true)
  7. {
  8. $cache = 'ad_' . md5($key);
  9. //$data = $cache ? Dever::cache($cache) : $cache;
  10. $data = false;
  11. if ($data) {
  12. return $this->getData($data);
  13. } elseif (!$data) {
  14. $page = Dever::db('ad/page')->one(array('key' => $key));
  15. if ($page) {
  16. $info = Dever::db('ad/info')->getAll(array('page_id' => $page['id']));
  17. if ($info) {
  18. foreach ($info as $k => $v) {
  19. $where['info_id'] = $v['id'];
  20. if ($v['order_type'] == 2) {
  21. $where['order'] = array('id' => 'asc');
  22. }
  23. $info[$k]['data'] = Dever::db('ad/data')->getAll($where);
  24. if ($info[$k]['data'] && $v['order_type'] == 3) {
  25. $order = array(array_rand($info[$k]['data'], 1));
  26. $temp = $info[$k]['data'];
  27. unset($info[$k]['data']);
  28. foreach ($order as $k1 => $v1) {
  29. $info[$k]['data'][$k1] = $temp[$v1];
  30. }
  31. }
  32. }
  33. Dever::cache($cache, $info);
  34. return $this->getData($info);
  35. }
  36. }
  37. }
  38. return false;
  39. }
  40. private function getData($data)
  41. {
  42. if ($data) {
  43. $time = time();
  44. $source_type = $this->config(Dever::input('source_type', 'h5'));
  45. foreach ($data as $k => $v) {
  46. $child = $v['data'];
  47. $data[$k]['data'] = array();
  48. $i = 0;
  49. foreach ($child as $k1 => $v1) {
  50. if ($v1['type'] == 2 && ($time < $v1['sdate'] || $time > $v1['edate'])) {
  51. //unset($data[$k]['data'][$k1]);
  52. } elseif ($v1['system'] > 0 && $source_type != $v1['system']) {
  53. //unset($data[$k]['data'][$k1]);
  54. } else {
  55. $data[$k]['data'][$i] = $v1;
  56. $i++;
  57. }
  58. }
  59. }
  60. }
  61. return $data;
  62. }
  63. private function config($type)
  64. {
  65. if ($type == 'ios') {
  66. return 3;
  67. }
  68. if ($type == 'android') {
  69. return 2;
  70. }
  71. if ($type == 'applet') {
  72. return 4;
  73. }
  74. if ($type == 'h5') {
  75. return 1;
  76. }
  77. return -1;
  78. }
  79. }