<?php
/**
 * 更新
 */

defined('InShopNC') or exit('Access Invalid!');

require_once(BASE_ROOT_PATH . '/helper/area/area_upgrade.php');
require_once(BASE_ROOT_PATH . '/helper/area/area_check.php');

class app_updateControl extends mobileHomeControl
{
    public function __construct()
    {
        parent::__construct();
    }

    public function check_versionOp()
    {
        $ver_code = $_GET['ver_code'];
        if($_SESSION['client_type'] == 'ios')
        {
            $cur_ver = $GLOBALS['setting_config']['mobile_ios_version'];

            $cur_ver  = intval($cur_ver * 100 + 0.5);
            $ver_code = intval($ver_code * 100 + 0.5);

            if($ver_code < $cur_ver) {
                $url  = $GLOBALS['setting_config']['mobile_ios'];
                $tips = $GLOBALS['setting_config']['mobile_update_tips'];
                $artips = explode('#',$tips);
                $tips = implode("\n",$artips);
                return self::outsuccess(array("open_url" => $url,'tip' => $tips));
            } else {
                return self::outsuccess(null);
            }
        }
        elseif($_SESSION['client_type'] == 'android')
        {
            $cur_ver = $GLOBALS['setting_config']['mobile_apk_version'];

            $cur_ver  = intval($cur_ver * 100 + 0.5);
            $ver_code = intval($ver_code * 100 + 0.5);

            if($ver_code < $cur_ver) {
                $url  = $GLOBALS['setting_config']['mobile_apk'];
                $tips = $GLOBALS['setting_config']['mobile_update_tips'];
                $artips = explode('#',$tips);
                $tips = implode("\n",$artips);
                return self::outsuccess(array("open_url" => $url,'tip' => $tips,'force' => false));
            } else {
                return self::outsuccess(null);
            }
        }
        else {
            return self::outerr(errcode::ErrApptype);
        }
    }

    public function areaOp()
    {
        global $config;

        $ver_code = intval($_GET['version']);
        if($ver_code < $config['area_version'])
        {
            static $data = null;
            if($data == null)
            {
                $items = rcache('area', 'mb_');
                if(empty($items)) {
                    $area = new area\area_check();
                    $data = $area->export();
                    wcache('area', array('data' => serialize($data)), 'mb_');
                } else {
                    $data = unserialize($items['data']);
                }
            }

            return self::outsuccess(array('version' => $config['area_version'],'areas' => $data));
        }
        else {
            return self::outsuccess(null);
        }
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public function check_verOp()
    {
        $ver_code = $_GET['ver_code'];
        if($_SESSION['client_type'] == 'ios')
        {
            $cur_ver = $GLOBALS['setting_config']['mobile_ios_version'];
            
            $cur_ver  = intval($cur_ver * 100 + 0.5);
            $ver_code = intval($ver_code * 100 + 0.5);

            if($ver_code < $cur_ver) {
                $url  = $GLOBALS['setting_config']['mobile_ios'];
                $tips = $GLOBALS['setting_config']['mobile_update_tips'];
                return self::outsuccess(array("open_url" => $url,'tip' => $tips));
            } else {
                return self::outsuccess(null);
            }
        }
        elseif($_SESSION['client_type'] == 'android') {
            return $this->checkVersionOp();
        }
        else {
            return self::outerr(errcode::ErrApptype);
        }
    }

    public function checkVersionOp()
    {
        $ver_code = $_GET['ver_code'];
        if($this->android() == false || !isset($ver_code) || empty($ver_code) || intval($ver_code) <= 0) {
            return self::outerr(errcode::ErrParamter,"需要带入手机及版本信息");
        }

        $verinfo = rkcache('android_version',true);
        $result = array();
        if ($ver_code == $verinfo['ver_code']) {
            $result['latest'] = 0; //设计协议设计反了,0 表示目前是最新版本,1 表示不是最新版本
        }
        else
        {
            $result['latest'] = 1;
            $lowest_compatible_version = intval($verinfo['lowest_compatible_version']);
            if ($lowest_compatible_version > intval($ver_code)) {
                $result['force_update'] = 1;
            } else {
                $result['force_update'] = 0;
            }

            $update_info = array();
            $update_info['ver_code'] = $verinfo['ver_code'];
            $update_info['app_path'] = $verinfo['app_path'];
            $update_info['release_note'] = $verinfo['release_note'];
            $update_info['remind_time']  = $verinfo['remind_time'];
            $update_info['md5_file'] = $verinfo['md5_file'];
            $update_info['app_path'] = BASE_SITE_URL . $verinfo['app_path'];

            $result['update_info'] = $update_info;
        }

        self::outsuccess($result);
    }
}
