|
| 1 | +import { songDetail } from "@/api/song"; |
| 2 | +import { formatSongsList } from "@/utils/format"; |
| 3 | +import { usePlayer } from "@/utils/player"; |
| 4 | + |
| 5 | +class OrpheusData { |
| 6 | + constructor(type: string, id: number, cmd: string) { |
| 7 | + this.type = type; |
| 8 | + this.id = id; |
| 9 | + this.cmd = cmd; |
| 10 | + } |
| 11 | + |
| 12 | + type: string; |
| 13 | + id: number; |
| 14 | + cmd: string; |
| 15 | +} |
| 16 | + |
| 17 | +export const handleProtocolUrl = (url: string) => { |
| 18 | + switch (true) { |
| 19 | + case url.startsWith("orpheus://"): |
| 20 | + handleOpenOrpheus(url); |
| 21 | + break; |
| 22 | + default: |
| 23 | + break; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +export const handleOpenOrpheus = async (url: string) => { |
| 30 | + const data = parseOrpheus(url); |
| 31 | + if (!data) return; |
| 32 | + console.log("🚀 Open Orpheus:", data); |
| 33 | + |
| 34 | + if (data.cmd === "play" && data.type === "song") { |
| 35 | + const player = usePlayer(); |
| 36 | + const result = await songDetail(data.id); |
| 37 | + const song = formatSongsList(result.songs)[0]; |
| 38 | + player.addNextSong(song, true); |
| 39 | + } else { |
| 40 | + console.log("❌ Unsupported Command or Type:", data); |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +const parseOrpheus = (url: string): OrpheusData | undefined => { |
| 45 | + // 这里的协议是从网页端打开官方客户端的协议 |
| 46 | + // 形如 `orpheus://eyJ0eXBlIjoic29uZyIsImlkIjoiMTgyNjM2MTcxMiIsImNtZCI6InBsYXkifQ==` |
| 47 | + // URI 的 Path 部分是 Base64 编码过的,解码后得到 Json |
| 48 | + // 形如 `{"type":"song","id":"1826361712","cmd":"play"}` |
| 49 | + |
| 50 | + if (!url.startsWith("orpheus://")) return; |
| 51 | + const path = url.replace("orpheus://", ""); |
| 52 | + const jsonString = atob(path); |
| 53 | + let data: OrpheusData; |
| 54 | + try { |
| 55 | + const json = JSON.parse(jsonString); |
| 56 | + data = new OrpheusData(json.type, json.id, json.cmd); |
| 57 | + } catch (e) { |
| 58 | + console.error("❌ Invalid Data:", e); |
| 59 | + return; |
| 60 | + } |
| 61 | + return data; |
| 62 | +} |
0 commit comments