|
@@ -0,0 +1,67 @@
|
|
|
+export interface OtaItem {
|
|
|
+ fileName: string
|
|
|
+ ossUrl: string
|
|
|
+}
|
|
|
+
|
|
|
+export interface OtaOption {
|
|
|
+ label: string
|
|
|
+ value: string
|
|
|
+ version: string | null
|
|
|
+ rawFile: string
|
|
|
+}
|
|
|
+
|
|
|
+// 比较两个版本号大小(升序用)
|
|
|
+export function compareVersions(a: string, b: string): number {
|
|
|
+ const pa = a.split('.').map((n) => parseInt(n, 10))
|
|
|
+ const pb = b.split('.').map((n) => parseInt(n, 10))
|
|
|
+ const len = Math.max(pa.length, pb.length)
|
|
|
+ for (let i = 0; i < len; i++) {
|
|
|
+ const na = pa[i] || 0
|
|
|
+ const nb = pb[i] || 0
|
|
|
+ if (na > nb) return 1
|
|
|
+ if (na < nb) return -1
|
|
|
+ }
|
|
|
+ return 0
|
|
|
+}
|
|
|
+
|
|
|
+// 判断字符串是否是合法版本号
|
|
|
+export function isValidVersion(v: string): boolean {
|
|
|
+ return /^\d+(\.\d+)*$/.test(v)
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 过滤并排序 OTA 列表
|
|
|
+ * @param data OTA 列表数据
|
|
|
+ * @param currentVersion 当前版本号
|
|
|
+ * @returns 过滤后的 OTA 选项列表
|
|
|
+ * @example
|
|
|
+ * const currentVersion = '2.1.1'
|
|
|
+ * const filteredList = filterAndSortOtaList(otaList, currentVersion);
|
|
|
+ */
|
|
|
+export function filterAndSortOtaList(data: OtaItem[], currentVersion?: string): OtaOption[] {
|
|
|
+ return (
|
|
|
+ data
|
|
|
+ .map((item) => {
|
|
|
+ // 提取版本(例: LNRadar500M-V2.1.4-RPM.bin → 2.1.4-RPM)
|
|
|
+ const match = item.fileName.match(/V([\d.]+(?:-[A-Za-z0-9]+)?)(?:\.bin|\.hex|\.fw)$/)
|
|
|
+ const version = match ? match[1] : null
|
|
|
+
|
|
|
+ return {
|
|
|
+ rawFile: item.fileName,
|
|
|
+ label: version ?? item.fileName,
|
|
|
+ value: item.ossUrl,
|
|
|
+ version,
|
|
|
+ ...item,
|
|
|
+ }
|
|
|
+ })
|
|
|
+ // 只保留有解析到版本号的
|
|
|
+ .filter((item) => item.version)
|
|
|
+ // 如果 currentVersion 有效,则只保留比它高的
|
|
|
+ .filter((item) => {
|
|
|
+ if (!currentVersion || !isValidVersion(currentVersion)) return true
|
|
|
+ return compareVersions(item.version!.split('-')[0], currentVersion) > 0
|
|
|
+ })
|
|
|
+ // 升序排序
|
|
|
+ .sort((a, b) => compareVersions(a.version!.split('-')[0], b.version!.split('-')[0]))
|
|
|
+ )
|
|
|
+}
|