Version.php
4.0 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace app\common\model;
use think\Model;
class Version extends Model
{
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 定义字段类型
protected $type = [
];
/**
* 检测版本号
*
* @param string $version 客户端版本号
* @return array
*/
public static function check($version)
{
$versionlist = self::all(['status' => 'normal'], [], TRUE);
foreach ($versionlist as $k => $v)
{
// 版本正常且新版本号不等于验证的版本号且找到匹配的旧版本
if ($v['status'] == 'normal' && $v['newversion'] !== $version && self::inversion($version, $v['oldversion']))
{
$updateversion = $v;
break;
}
}
if (isset($updateversion))
{
$search = ['{version}', '{newversion}', '{downloadurl}', '{url}', '{packagesize}'];
$replace = [$version, $updateversion['newversion'], $updateversion['downloadurl'], $updateversion['downloadurl'], $updateversion['packagesize']];
$upgradetext = str_replace($search, $replace, $updateversion['content']);
return [
"enforce" => $updateversion['enforce'],
"version" => $version,
"newversion" => $updateversion['newversion'],
"downloadurl" => $updateversion['downloadurl'],
"packagesize" => $updateversion['packagesize'],
"upgradetext" => $upgradetext
];
}
return NULL;
}
/**
* 检测版本是否的版本要求的数据中
*
* @param string $version
* @param array $data
*/
public static function inversion($version, $data = [])
{
//版本号以.分隔
$data = is_array($data) ? $data : [$data];
if ($data)
{
if (in_array("*", $data) || in_array($version, $data))
{
return TRUE;
}
$ver = explode('.', $version);
if ($ver)
{
$versize = count($ver);
//验证允许的版本
foreach ($data as $m)
{
$c = explode('.', $m);
if (!$c || $versize != count($c))
continue;
$i = 0;
foreach ($c as $a => $k)
{
if (!self::compare($ver[$a], $k))
{
continue 2;
}
else
{
$i++;
}
}
if ($i == $versize)
return TRUE;
}
}
}
return FALSE;
}
/**
* 比较两个版本号
*
* @param string $v1
* @param string $v2
* @return boolean
*/
public static function compare($v1, $v2)
{
if ($v2 == "*" || $v1 == $v2)
{
return TRUE;
}
else
{
$values = [];
$k = explode(',', $v2);
foreach ($k as $v)
{
if (strpos($v, '-') !== FALSE)
{
list($start, $stop) = explode('-', $v);
for ($i = $start; $i <= $stop; $i++)
{
$values[] = $i;
}
}
else
{
$values[] = $v;
}
}
return in_array($v1, $values) ? TRUE : FALSE;
}
}
}