Skip to content

Commit

Permalink
feat(web): 增加网易云音乐卡片发送
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Jun 7, 2021
1 parent 4a47dc8 commit 9386655
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import { TMemo } from '@capital/shared/components/TMemo';
import { showToasts } from '@capital/shared/manager/ui';
import { Button, Divider, Empty, Input } from 'antd';
import {
fetchMusicDetail,
NeteaseMusicSongInfo,
searchMusicList,
} from '../../model/netease-music';
import { ModalWrapper } from '@capital/web/components/Modal';
import { t } from '@capital/shared/i18n';
import styled from 'styled-components';
import _get from 'lodash/get';
import { useMsgSend } from '@capital/shared/redux/hooks/useMsgSend';

const Search = Input.Search;

Expand All @@ -31,21 +33,26 @@ const SongItem = styled.div`
}
`;

export const NeteaseMusicSelector: React.FC = TMemo(() => {
export const NeteaseMusicSelector: React.FC<{
converseUUID: string;
onSendMusicCard?: () => void;
}> = TMemo((props) => {
const { converseUUID, onSendMusicCard } = props;
const [loading, setLoading] = useState(false);
const [searchedList, setSearchedList] =
useState<NeteaseMusicSongInfo[] | null>(null);
const { sendCardMsg } = useMsgSend(converseUUID);

const onSearch = useCallback(async (text: string) => {
try {
setLoading(true);
const res = await searchMusicList(text);

if (res.code === 200) {
setSearchedList(res.result.songs);
} else {
if (res.code !== 200) {
showToasts(t('搜索音乐失败'), 'error');
return;
}
setSearchedList(res.result.songs);
} catch (err) {
console.error(err);
showToasts(`${t('搜索失败')}: ${String(err)}`, 'error');
Expand All @@ -54,9 +61,46 @@ export const NeteaseMusicSelector: React.FC = TMemo(() => {
}
}, []);

const handleClick = useCallback((songId: number) => {
console.log('songId', songId);
}, []);
const handleClick = useCallback(
async (songId: number) => {
try {
setLoading(true);

const res = await fetchMusicDetail(songId);
if (res.code !== 200) {
showToasts(`${t('发送失败')}: 网络异常`, 'error');
return;
}

const detail = _get(res, ['data', 0]);
const url = detail.url;

if (typeof url === 'string' && url !== '') {
sendCardMsg('media', {
mediaType: 'audio',
mediaSource: 'netease',
mediaUrl: url,
neteaseSongId: songId,
});
showToasts(t('发送成功'), 'success');

if (typeof onSendMusicCard === 'function') {
onSendMusicCard();
}
} else {
showToasts(
`${t('发送失败')}: 该音乐可能是一个版权音乐, 无法获取播放地址`,
'error'
);
}
} catch (err) {
showToasts(`${t('请求失败')}: ${String(err)}`, 'error');
} finally {
setLoading(false);
}
},
[sendCardMsg, onSendMusicCard]
);

return (
<ModalWrapper title={t('网易云音乐')}>
Expand Down
47 changes: 47 additions & 0 deletions src/plugins/netease-music/src/model/netease-music.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ interface NeteaseMusicResponse<T = unknown> {
result: T;
}

interface NeteaseMusicResponseData<T = unknown> {
code: number; // 200
data: T;
}

interface NeteaseMusicSearchResult {
hasMore: boolean;
songCount: number;
Expand Down Expand Up @@ -51,6 +56,34 @@ interface NeteaseMusicSongAlbumArtist {
trans: unknown;
}

interface NeteaseMusicSongDetail {
br: number;
canExtend: boolean;
code: number;
encodeType: 'mp3'; // maybe have m4a?
expi: number;
fee: number;
flag: number;
freeTimeTrialPrivilege: {
resConsumable: boolean;
userConsumable: boolean;
type: number;
remainTime: number;
};
freeTrialInfo: unknown;
freeTrialPrivilege: { resConsumable: boolean; userConsumable: boolean };
gain: number;
id: number;
level: 'normal' | 'exhigh'; // normal, exhigh ...
md5: string;
payed: number;
size: number;
type: 'mp3';
uf: unknown;
url: string;
urlSource: number;
}

/**
* 搜索音乐
*/
Expand All @@ -63,3 +96,17 @@ export async function searchMusicList(keywords: string) {

return data;
}

/**
* 获取音乐Url
*/
export async function fetchMusicDetail(songId: number) {
const res = await fetch(`${neteaseMusicAPI}/song/url?id=${songId}`, {
credentials: 'include',
});
const data = (await res.json()) as NeteaseMusicResponseData<
NeteaseMusicSongDetail[]
>;

return data;
}
11 changes: 8 additions & 3 deletions src/plugins/netease-music/src/reg.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import React from 'react';
import { regChatSendBoxAddonAction } from '@capital/web/reg/regChatSendBoxAction';
import { openModal } from '@capital/web/components/Modal';
import { closeModal, openModal } from '@capital/web/components/Modal';
import { NeteaseMusicSelector } from './components/modals/NeteaseMusicSelector';
import { t } from '@capital/shared/i18n';

regChatSendBoxAddonAction({
label: t('发送网易云音乐'),
onClick: () => {
openModal(<NeteaseMusicSelector />);
onClick: ({ converseUUID }) => {
const key = openModal(
<NeteaseMusicSelector
converseUUID={converseUUID}
onSendMusicCard={() => closeModal(key)}
/>
);
},
});
10 changes: 10 additions & 0 deletions src/shared/i18n/langs/en-US/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"k1574405e": "Once it is determined that it cannot be revoked",
"k15d1c82c": "This component has been discarded. If it affects your use, please contact the developer",
"k16641ebe": "No information was found",
"k19a60a55": "搜索音乐失败",
"k1a47edf3": "Please Type Account Password",
"k1ba3a7e9": "Lawful evil",
"k1c93fc91": "Action Message",
Expand Down Expand Up @@ -135,6 +136,8 @@
"k6a365d01": "Operation failed",
"k6b8145c2": "Login Result",
"k6c2d3aa8": "Compact Mode",
"k6d4e5958": "发送网易云音乐",
"k6d58e46e": "发送失败",
"k6dfdc625": "Maximize",
"k6e23c48": "Other",
"k6ec17240": "Note Title",
Expand All @@ -149,6 +152,7 @@
"k77a44085": "Connecting...",
"k77ee6a43": "Member Manage",
"k78102887": "Group Members",
"k7862cd02": "发送成功",
"k7917d72c": "Export Actor Card",
"k7a89720": "Open in new window",
"k7a9587f0": "Search Result",
Expand All @@ -159,6 +163,7 @@
"k7ffd35dd": "Desktop notification permission is disabled",
"k8004f31b": "Tick",
"k802cc488": "Please enter 5-20 new passwords",
"k8040989e": "[卡片消息]",
"k81662255": "Create Invite Code",
"k81c7a6d6": "Request to add actor",
"k821df934": "No Account? Register Now",
Expand All @@ -180,12 +185,14 @@
"k8c717e61": "Lawful neutral",
"k8f17d40": "The application is successful, please wait for the approval of the group administrator",
"k8fd7fca5": "Update Actor",
"k901e9f6f": "请求失败",
"k921f1c92": "My Account",
"k93fb7d2d": "Group Not Found",
"k950183bc": "New password cannot be empty",
"k959638c1": "Welcome to TRPG World",
"k960aa491": "Search Chat Log",
"k96608ca7": "Group Sub Name",
"k97453f25": "网易云音乐",
"k97464d0d": "All Actor Card: {{groupActorNum}}",
"k98dadf2e": "Copy completed! Send to friend and let him entry!",
"k98dff103": "Clear Cache",
Expand Down Expand Up @@ -214,6 +221,7 @@
"ka7907771": "Save Success",
"ka7c99495": "Are you ensure to delete note {{title}}?",
"ka7f5a106": "Empty Username or Password Not Allowed",
"ka891a9ce": "消息不能为空",
"ka93a38e0": "Voice Channel",
"ka9a9cdc8": "Is Alpha User",
"kaa9fd559": "Voice setting changes cannot take effect when using mic",
Expand Down Expand Up @@ -251,6 +259,7 @@
"kc48f6f64": "All",
"kc522e12c": "Login Type",
"kc567b415": "Report Error",
"kc60e4ac2": "发送",
"kc77d6c87": "Note Loading Failed",
"kc79ba447": "Neutral good",
"kc7d58c4c": "Repeat Password Incorrect",
Expand Down Expand Up @@ -297,6 +306,7 @@
"ke456aed": "Agree",
"ke4d28dd6": "Ensure to Quit Group",
"ke4ed9d21": "Assign Members",
"ke5262837": "搜索失败",
"ke6ae638": "True neutral",
"ke8477173": "Yourself",
"ke88afa8f": "Bot List",
Expand Down
10 changes: 10 additions & 0 deletions src/shared/i18n/langs/zh-CN/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"k1574405e": "一旦确定无法撤销",
"k15d1c82c": "该组件已被弃用, 如果影响到您的使用请联系开发者",
"k16641ebe": "找不到相关信息",
"k19a60a55": "搜索音乐失败",
"k1a47edf3": "请输入账号密码",
"k1ba3a7e9": "守序中立",
"k1c93fc91": "行动信息",
Expand Down Expand Up @@ -135,6 +136,8 @@
"k6a365d01": "操作失败",
"k6b8145c2": "登录结果",
"k6c2d3aa8": "紧凑模式",
"k6d4e5958": "发送网易云音乐",
"k6d58e46e": "发送失败",
"k6dfdc625": "最大化",
"k6e23c48": "其他",
"k6ec17240": "笔记标题",
Expand All @@ -149,6 +152,7 @@
"k77a44085": "正在连接...",
"k77ee6a43": "成员管理",
"k78102887": "成员数",
"k7862cd02": "发送成功",
"k7917d72c": "导出人物卡",
"k7a89720": "在新窗口打开",
"k7a9587f0": "搜索结果",
Expand All @@ -159,6 +163,7 @@
"k7ffd35dd": "桌面通知权限已被禁止",
"k8004f31b": "踢出团",
"k802cc488": "请输入5~20位新密码",
"k8040989e": "[卡片消息]",
"k81662255": "创建邀请码",
"k81c7a6d6": "申请添加角色",
"k821df934": "没有账号?现在注册",
Expand All @@ -180,12 +185,14 @@
"k8c717e61": "守序中立",
"k8f17d40": "申请成功, 请等待团管理员审批",
"k8fd7fca5": "更新人物卡",
"k901e9f6f": "请求失败",
"k921f1c92": "我的账号",
"k93fb7d2d": "找不到该团",
"k950183bc": "新密码不能为空",
"k959638c1": "欢迎来到TRPG的世界",
"k960aa491": "搜索聊天记录",
"k96608ca7": "团副名",
"k97453f25": "网易云音乐",
"k97464d0d": "共 {{groupActorNum}} 张角色卡",
"k98dadf2e": "复制成功, 直接发送给好友让他加入吧",
"k98dff103": "清理缓存",
Expand Down Expand Up @@ -214,6 +221,7 @@
"ka7907771": "保存成功",
"ka7c99495": "确定要删除笔记 {{title}} 么",
"ka7f5a106": "用户名密码不能为空",
"ka891a9ce": "消息不能为空",
"ka93a38e0": "语音频道",
"ka9a9cdc8": "是否为内测用户",
"kaa9fd559": "语音设置变更在使用麦克风时无法立即生效",
Expand Down Expand Up @@ -251,6 +259,7 @@
"kc48f6f64": "所有",
"kc522e12c": "登录类型",
"kc567b415": "汇报错误",
"kc60e4ac2": "发送",
"kc77d6c87": "笔记加载失败",
"kc79ba447": "中立善良",
"kc7d58c4c": "重复密码不正确",
Expand Down Expand Up @@ -297,6 +306,7 @@
"ke456aed": "同意",
"ke4d28dd6": "是否要退出群",
"ke4ed9d21": "指定成员",
"ke5262837": "搜索失败",
"ke6ae638": "绝对中立",
"ke8477173": "你自己",
"ke88afa8f": "机器人列表",
Expand Down
Loading

0 comments on commit 9386655

Please sign in to comment.