123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- /**
- * Created by PhpStorm.
- * User: lionared
- * Date: 2018/3/5
- * Time: 下午8:23
- */
- require_once(BASE_ROOT_PATH . '/helper/car/tpl_display.php');
- class car_helper
- {
- private $mCarsVersion;
- static private $stInstance = null;
- private function __construct()
- {
- $this->mCarsVersion = [];
- $this->init();
- }
- private function init()
- {
- $i = 0;
- while (true)
- {
- $start = $i * 1000;
- $items = Model()->table('car_base')->field('update_version,car_id')->order('car_id asc')->limit("{$start},1000")->select();
- if(empty($items)) {
- return;
- }
- $i++;
- foreach ($items as $item)
- {
- $version = intval($item['update_version']);
- $car_id = intval($item['car_id']);
- if($version <= 0) {
- $version = 1;
- }
- $this->mCarsVersion[$car_id] = $version;
- }
- }
- }
- static public function instance()
- {
- if (self::$stInstance == null) {
- self::$stInstance = new car_helper();
- }
- return self::$stInstance;
- }
- public function index_url($car_id)
- {
- $car_id = intval($car_id);
- if($car_id <= 0) return '';
- $file_name = "{$car_id}_index.html";
- $file_path = BASE_UPLOAD_PATH. DS. "car". DS. $file_name;
- if(!file_exists($file_path))
- {
- if($this->write_file($file_path, $this->index_file_content($car_id)) == false){
- return '';
- }
- }
- $version = $this->mCarsVersion[$car_id];
- $url = UPLOAD_SITE_URL. DS. "car". DS. "{$file_name}?{$version}";
- return $url;
- }
- public function detail_url($car_id)
- {
- $car_id = intval($car_id);
- if($car_id <= 0) return '';
- $file_name = "{$car_id}_detail.html";
- $file_path = BASE_UPLOAD_PATH. DS. "car". DS. $file_name;
- if(!file_exists($file_path))
- {
- if($this->write_file($file_path, $this->detail_file_content($car_id)) == false) {
- return '';
- }
- }
- $version = $this->mCarsVersion[$car_id];
- $url = UPLOAD_SITE_URL. DS. "car". DS. "{$file_name}?{$version}";
- return $url;
- }
- private function write_file($file_path, $content)
- {
- $write_success = false;
- $file = fopen($file_path,'w');
- if($file) {
- $write_success = fwrite($file, $content);
- fclose($file);
- }
- return $write_success;
- }
- private function index_file_content($car_id)
- {
- $mod_car = Model('car');
- $car_info = $mod_car->base_info([ 'car_id'=>$car_id ]);
- ob_start();
- Tpl::output('car_info', $car_info);
- Tpl::output('detail_url', $this->detail_url($car_id));
- Tpl::showpage("car/car_details");
- $content = ob_get_contents();
- ob_clean();
- return $content;
- }
- private function detail_file_content($car_id)
- {
- ob_start();
- Tpl::output('tpl', new tpl_display($car_id));
- Tpl::showpage("car/car_info");
- $content = ob_get_contents();
- ob_clean();
- return $content;
- }
- }
|