chengkun
2025-05-26 8f3df543230cd4403368b39b9bbe5726d11a0284
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
class version_controller extends wxapp_controller{
    
    function index_action()
    {
        include(DATA_PATH.'api/wxapp/app.config.php');
        $update = false;
        $platform = strtolower($_POST['platform']);
        
        if ($platform == 'ios' && !empty($appconfig['iosversion'])){
            
            $vcode  = $this->versionCompare($appconfig['iosversion'], $_POST['version']);
            
            if ($vcode == 1){
                
                $update = true;
                
                $data['version']     =  $appconfig['iosversion'];
                $data['title']       =  $appconfig['apptitle'];
                $data['note']        =  $appconfig['appcontent'];
                $data['url']         =  $appconfig['iosurl'];
                $data['forceUpdate'] =  !empty($appconfig['sy_app_upforce']) ? $appconfig['sy_app_upforce'] : 0;
            }
            
        }elseif ($platform == 'android' && !empty($appconfig['androidversion'])){
            
            $vcode  = $this->versionCompare($appconfig['androidversion'], $_POST['version']);
            
            if ($vcode == 1){
                
                $update = true;
                
                $data['version']     =  $appconfig['androidversion'];
                $data['title']       =  $appconfig['apptitle'];
                $data['note']        =  $appconfig['appcontent'];
                $data['url']         =  $appconfig['androidurl'];
                $data['forceUpdate'] =  !empty($appconfig['sy_app_upforce']) ? $appconfig['sy_app_upforce'] : 0;
            }
        }
        
        if ($update){
            
            $this->render_json(0,'ok',$data);
            
        }else{
            
            $this->render_json(1,'no update');
        }
    }
    /**
     * 老版app更新配置
     */
    function oldIndex()
    {
        include(DATA_PATH.'api/wxapp/app.config.php');
        
        if (!empty($appconfig['iosversion'])){
            $data['iOS']['version'] = $appconfig['iosversion'];
            $data['iOS']['title'] = $appconfig['apptitle'];
            $data['iOS']['note'] = $appconfig['appcontent'];
            $data['iOS']['url'] = $appconfig['iosurl'];
        }else{
            $data['iOS']['version'] = $this->config['iosversion'];
            $data['iOS']['title'] = $this->config['apptitle'];
            $data['iOS']['note'] = $this->config['appcontent'];
            $data['iOS']['url'] = $this->config['iosurl'];
        }
        
        if (!empty($appconfig['androidversion'])){
            $data['Android']['version'] = $appconfig['androidversion'];
            $data['Android']['title'] = $appconfig['apptitle'];
            $data['Android']['note'] = $appconfig['appcontent'];
            $data['Android']['url'] = $appconfig['androidurl'];
        }else{
            $data['Android']['version'] = $this->config['androidversion'];
            $data['Android']['title'] = $this->config['apptitle'];
            $data['Android']['note'] = $this->config['appcontent'];
            $data['Android']['url'] = $this->config['androidurl'];
        }
        
        echo json_encode($data);die;
    }
    private function reg($str){
        return preg_replace('/[^0-9]/','',$str);
    }
    //根据length的长度进行补0的操作,$length的值为两个版本号中最长的那个
    private function add($str,$length){
        return str_pad($str,$length,"0");
    }
    // 版本号比较
    private function versionCompare($v1,$v2){
        $length = strlen($this->reg($v1))>strlen($this->reg($v2)) ? strlen($this->reg($v1)): strlen($this->reg($v2));
        $v1 = $this->add($this->reg($v1),$length);
        $v2 = $this->add($this->reg($v2),$length);
        if($v1 == $v2) {
            return 0;
        }else{
            return $v1>$v2?1:-1;
        }
    }
}
?>