car_helper.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: lionared
  5. * Date: 2018/3/5
  6. * Time: 下午8:23
  7. */
  8. require_once(BASE_ROOT_PATH . '/helper/car/tpl_display.php');
  9. class car_helper
  10. {
  11. private $mCarsVersion;
  12. static private $stInstance = null;
  13. private function __construct()
  14. {
  15. $this->mCarsVersion = [];
  16. $this->init();
  17. }
  18. private function init()
  19. {
  20. $i = 0;
  21. while (true)
  22. {
  23. $start = $i * 1000;
  24. $items = Model()->table('car_base')->field('update_version,car_id')->order('car_id asc')->limit("{$start},1000")->select();
  25. if(empty($items)) {
  26. return;
  27. }
  28. $i++;
  29. foreach ($items as $item)
  30. {
  31. $version = intval($item['update_version']);
  32. $car_id = intval($item['car_id']);
  33. if($version <= 0) {
  34. $version = 1;
  35. }
  36. $this->mCarsVersion[$car_id] = $version;
  37. }
  38. }
  39. }
  40. static public function instance()
  41. {
  42. if (self::$stInstance == null) {
  43. self::$stInstance = new car_helper();
  44. }
  45. return self::$stInstance;
  46. }
  47. public function index_url($car_id)
  48. {
  49. $car_id = intval($car_id);
  50. if($car_id <= 0) return '';
  51. $file_name = "{$car_id}_index.html";
  52. $file_path = BASE_UPLOAD_PATH. DS. "car". DS. $file_name;
  53. if(!file_exists($file_path))
  54. {
  55. if($this->write_file($file_path, $this->index_file_content($car_id)) == false){
  56. return '';
  57. }
  58. }
  59. $version = $this->mCarsVersion[$car_id];
  60. $url = UPLOAD_SITE_URL. DS. "car". DS. "{$file_name}?{$version}";
  61. return $url;
  62. }
  63. public function detail_url($car_id)
  64. {
  65. $car_id = intval($car_id);
  66. if($car_id <= 0) return '';
  67. $file_name = "{$car_id}_detail.html";
  68. $file_path = BASE_UPLOAD_PATH. DS. "car". DS. $file_name;
  69. if(!file_exists($file_path))
  70. {
  71. if($this->write_file($file_path, $this->detail_file_content($car_id)) == false) {
  72. return '';
  73. }
  74. }
  75. $version = $this->mCarsVersion[$car_id];
  76. $url = UPLOAD_SITE_URL. DS. "car". DS. "{$file_name}?{$version}";
  77. return $url;
  78. }
  79. private function write_file($file_path, $content)
  80. {
  81. $write_success = false;
  82. $file = fopen($file_path,'w');
  83. if($file) {
  84. $write_success = fwrite($file, $content);
  85. fclose($file);
  86. }
  87. return $write_success;
  88. }
  89. private function index_file_content($car_id)
  90. {
  91. $mod_car = Model('car');
  92. $car_info = $mod_car->base_info([ 'car_id'=>$car_id ]);
  93. ob_start();
  94. Tpl::output('car_info', $car_info);
  95. Tpl::output('detail_url', $this->detail_url($car_id));
  96. Tpl::showpage("car/car_details");
  97. $content = ob_get_contents();
  98. ob_clean();
  99. return $content;
  100. }
  101. private function detail_file_content($car_id)
  102. {
  103. ob_start();
  104. Tpl::output('tpl', new tpl_display($car_id));
  105. Tpl::showpage("car/car_info");
  106. $content = ob_get_contents();
  107. ob_clean();
  108. return $content;
  109. }
  110. }