Settings.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. *------Create thems Controller------
  4. *------SCWPHP Version 1.0.0------
  5. *------Dev Model Jions------
  6. *------Create Time 2017-06-21 07:25:34------
  7. */
  8. namespace App\Api\Controller;
  9. //ini_set('display_errors', 1);
  10. //error_reporting(E_ALL);
  11. use Biz\Game\GameQuery;
  12. define('SETTINGS_REMOTE_KEY', '14e1b600b1fd579f47433b88e8d85291');
  13. class Settings extends BaseController {
  14. function init() {
  15. }
  16. function Query() {
  17. $game = isset($_GET['game']) && !empty($_GET['game']) ? $_GET['game'] : null;
  18. $playtype = isset($_GET['playtype']) && !empty($_GET['playtype']) ? $_GET['playtype'] : null;
  19. if (!$game) {
  20. Render('', -3216); //查询游戏设置,游戏名不能为空
  21. }
  22. $key = empty($playtype) ? $game : $game . $playtype;
  23. if (C()->get('cache')->has('odds-' . $key)) {
  24. $items = C()->get('cache')->get('odds-' . $key);
  25. } else {
  26. $cls = "\\Biz\\Game\\Settings\\" . ucfirst($game) . "Settings";
  27. $settings = new $cls();
  28. $items = $settings->get($playtype);
  29. // dump($items);
  30. if ($items < 0) {
  31. Render('', $items); //查询游戏设置,游戏名不能为空
  32. }
  33. C()->get('cache')->set('odds-' . $key, $items, 60);
  34. }
  35. Render($items);
  36. }
  37. //更新缓存
  38. public function odds() {
  39. $clear = isset($_REQUEST['clear']) && ($_REQUEST['clear'] == '-99999') ? 1 : 0;
  40. // $odds=C()->get('cache')->get('odds-list');
  41. $lastTime = C()->get('cache')->get('odds-lastUpdateTime');
  42. $times = $this->getUpdateTime();
  43. if (empty($lastTime) || $clear) {
  44. $this->_buildOdds();
  45. // header('location:/Cache/odds.json');
  46. //Render($odds,1,strtotime($lastTime) .'='.$times);
  47. }
  48. if (empty($times)) {
  49. $times = strtotime($lastTime);
  50. }
  51. if (strtotime($lastTime) < $times) {
  52. $this->_buildOdds();
  53. //返回所有最新赔率
  54. }
  55. // header('location:/Cache/odds.json');
  56. $json = file_get_contents(ROOT_PATH . '/Cache/odds.json');
  57. file_put_contents(ROOT_PATH . '/Cache/odds2.json',$json);
  58. if (!$json) {
  59. $this->_buildOdds();
  60. $json = file_get_contents(ROOT_PATH . '/Cache/odds.json');
  61. }
  62. $data=json_decode($json,1);
  63. Render($data, 1, strtotime($lastTime) . '+' . $times);
  64. }
  65. /**
  66. * 得到系统更新的赔率修改时间
  67. *
  68. * @return void
  69. */
  70. private function getUpdateTime() {
  71. $time = lm('Setinfo', 'commons')->getType(1911);
  72. return strtotime($time);
  73. }
  74. /**
  75. * 生成赔率根据数据
  76. *
  77. * @param [type] $oddsList 存储赔率生成结果数组
  78. * @param [type] $game 游戏名称
  79. * @param [type] $table_data 指定游戏的数据表的赔率记录信息
  80. * @return void
  81. */
  82. private function _build_odds(&$oddsList, $game, $table_data) {
  83. $cls = "\\Biz\\Game\\Settings\\" . ucfirst($game) . "Settings";
  84. if (!class_exists($cls)) {
  85. return -1;
  86. }
  87. $settings = new $cls(0);
  88. $settings->setOddsSettings($table_data);
  89. $items = $settings->get('');
  90. if ($items < 0) {
  91. return -2;
  92. }
  93. $oddsList[] = array('game_type' => $game, 'odds' => $items);
  94. }
  95. private function _buildOdds() {
  96. //本地缓存和服务器比较
  97. $allOdds = lm('Game_odds', 'Commons')->get(['game_type', 'odds', 'update_time']);
  98. if (!$allOdds) {
  99. Render('', -7029); //赔率数据不存在
  100. }
  101. $oddsList = array();
  102. foreach ($allOdds as $odds_item) {
  103. $this->_build_odds($oddsList, $odds_item->game_type, $odds_item);
  104. }
  105. $time = date('Y-m-d H:i:s', time());
  106. C()->get('cache')->set('odds-lastUpdateTime', $time, 31536000); //最后更新时间
  107. C()->get('cache')->set('odds-list', $oddsList, 31536000);
  108. file_put_contents(ROOT_PATH . '/Cache/odds.json', json_encode($oddsList));
  109. return $oddsList;
  110. }
  111. }