>// 歌词格式化
const parseLyric = (text) => {
let lines = text.split('\r\n'),
pattern = /\[\d{2}:\d{2}.\d{2}\]/g,
result = [];
// 去掉数组中最后的空白;
lines[lines.length - 1].length === 0 && lines.pop();
lines.map((ele) => {
let time = ele.match(pattern),
value = ele.replace(pattern, '');
time.map((e) => {
let t = e.slice(1, -1).split(':');
result.push([parseInt(t[0], 10) * 60 + parseFloat(t[1]), value]);
})
});
// 最后将结果数组中的元素按时间大小排序,以便保存之后正常显示歌词;
result.sort((a, b) => a[0] - b[0]);
return result;
};
// 时间转换秒数
const formatTime = (timeTemp) => {
let m = Math.floor(timeTemp / 60);
let s = Math.floor(timeTemp % 60);
return (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
};
// 时间戳转时间格式
const getLocalTime = (nS) => {
return new Date(parseInt(nS) * 1000).toLocaleString('chinese', {hour12: false}).replace(/\//g, '-');
};
// 数组去重
const unique = (arr) => {
const res = [];
const json = {};
for (let i = 0; i < arr.length; i++) {
if (!json[arr[i]]) {
res.push(arr[i]);
json[arr[i]] = 1;
}
}
return res;
};
export {parseLyric, formatTime, getLocalTime, unique};
>//FormData和Payload是浏览器传输数据给接口的其中两种格式,这两种方式浏览器是通过Content-Type来进行区分的,
// 如果是application/x-www-form-urlencoded,则为formdata方式,如果是application/json方式,则为payload方式。需要转换成formdata方式,则要下载qs进行转换
import qs from 'qs';
const defaultHeaders = {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
};
const parseJSONFilter = (res) => res.json();
// 使用 fetch 封装
const get = (uri) => {
return fetch(uri).then(parseJSONFilter).catch(err => ({ err }));
};
const post = (uri, body) => {
return fetch(uri, {
body: qs.stringify(body),
headers: defaultHeaders,
method: 'POST'
}).then(parseJSONFilter).catch(error => ({ error }));
};
// 使用 fetch + async + await 方式
// 需要引入 babel-polyfill ,并且babel-loader 需设置为es7 presets:['react', 'es2015','stage-0'];
const asyncGet = (uri) => {
return fetch(uri);
};
const asyncPost = (uri, body) => {
return fetch(uri, {
body: qs.stringify(body),
headers: defaultHeaders,
method: 'POST'
});
};
export default { get, post, asyncGet, asyncPost };
>const getItem = (key) => {
let value;
try {
value = localStorage.getItem(key);
} catch (ex) {
if (process.env.NODE_ENV === 'production') {
console.error('localStorage.getItem报错, ', ex.message)
}
} finally {
return value;
}
};
const setItem = (key, value) => {
try {
// ios safari 无痕模式下,直接使用 localStorage.setItem 会报错
localStorage.setItem(key, value)
} catch (ex) {
// 开发环境下提示 error
if (process.env.NODE_ENV === 'production') {
console.error('localStorage.setItem报错, ', ex.message)
}
}
};
export {getItem, setItem};
>const API = {
new_song: '?json=true',
song_play: 'plist/index&json=true', // 歌单
song_playlist: 'plist/list', // plist/list/125032?json=true
rank: 'rank/list&json=true',
rankid: 'rank/info/', // rank/info/?rankid=8888&page=1&json=true'
singer_category: 'singer/class&json=true',
singer_list: 'singer/list/', // singer/list/88?json=true
singer_detail: 'singer/info/', // singer/info/3060&json=true
song_detail: 'app/i/getSongInfo.php', // ?cmd=playInfo&hash=CB7EE97F4CC11C4EA7A1FA4B516A5D97
song_lyrics:'app/i/krc.php', // ?cmd=100&hash=2B8DA604EDFF833B06AD9DCEC3F8F28C&timelength=222000
searchHot: 'api/v3/search/hot',
searchResult: 'api/v3/search/song',
};
export default API;