Version.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class Version extends Model
  5. {
  6. // 开启自动写入时间戳字段
  7. protected $autoWriteTimestamp = 'int';
  8. // 定义时间戳字段名
  9. protected $createTime = 'createtime';
  10. protected $updateTime = 'updatetime';
  11. // 定义字段类型
  12. protected $type = [
  13. ];
  14. /**
  15. * 检测版本号
  16. *
  17. * @param string $version 客户端版本号
  18. * @return array
  19. */
  20. public static function check($version)
  21. {
  22. $versionlist = self::all(['status' => 'normal'], [], TRUE);
  23. foreach ($versionlist as $k => $v)
  24. {
  25. // 版本正常且新版本号不等于验证的版本号且找到匹配的旧版本
  26. if ($v['status'] == 'normal' && $v['newversion'] !== $version && self::inversion($version, $v['oldversion']))
  27. {
  28. $updateversion = $v;
  29. break;
  30. }
  31. }
  32. if (isset($updateversion))
  33. {
  34. $search = ['{version}', '{newversion}', '{downloadurl}', '{url}', '{packagesize}'];
  35. $replace = [$version, $updateversion['newversion'], $updateversion['downloadurl'], $updateversion['downloadurl'], $updateversion['packagesize']];
  36. $upgradetext = str_replace($search, $replace, $updateversion['content']);
  37. return [
  38. "enforce" => $updateversion['enforce'],
  39. "version" => $version,
  40. "newversion" => $updateversion['newversion'],
  41. "downloadurl" => $updateversion['downloadurl'],
  42. "packagesize" => $updateversion['packagesize'],
  43. "upgradetext" => $upgradetext
  44. ];
  45. }
  46. return NULL;
  47. }
  48. /**
  49. * 检测版本是否的版本要求的数据中
  50. *
  51. * @param string $version
  52. * @param array $data
  53. */
  54. public static function inversion($version, $data = [])
  55. {
  56. //版本号以.分隔
  57. $data = is_array($data) ? $data : [$data];
  58. if ($data)
  59. {
  60. if (in_array("*", $data) || in_array($version, $data))
  61. {
  62. return TRUE;
  63. }
  64. $ver = explode('.', $version);
  65. if ($ver)
  66. {
  67. $versize = count($ver);
  68. //验证允许的版本
  69. foreach ($data as $m)
  70. {
  71. $c = explode('.', $m);
  72. if (!$c || $versize != count($c))
  73. continue;
  74. $i = 0;
  75. foreach ($c as $a => $k)
  76. {
  77. if (!self::compare($ver[$a], $k))
  78. {
  79. continue 2;
  80. }
  81. else
  82. {
  83. $i++;
  84. }
  85. }
  86. if ($i == $versize)
  87. return TRUE;
  88. }
  89. }
  90. }
  91. return FALSE;
  92. }
  93. /**
  94. * 比较两个版本号
  95. *
  96. * @param string $v1
  97. * @param string $v2
  98. * @return boolean
  99. */
  100. public static function compare($v1, $v2)
  101. {
  102. if ($v2 == "*" || $v1 == $v2)
  103. {
  104. return TRUE;
  105. }
  106. else
  107. {
  108. $values = [];
  109. $k = explode(',', $v2);
  110. foreach ($k as $v)
  111. {
  112. if (strpos($v, '-') !== FALSE)
  113. {
  114. list($start, $stop) = explode('-', $v);
  115. for ($i = $start; $i <= $stop; $i++)
  116. {
  117. $values[] = $i;
  118. }
  119. }
  120. else
  121. {
  122. $values[] = $v;
  123. }
  124. }
  125. return in_array($v1, $values) ? TRUE : FALSE;
  126. }
  127. }
  128. }