Skip to content

Commit

Permalink
🐞 fix: 修复 Cookie 登录状态过期过快
Browse files Browse the repository at this point in the history
  • Loading branch information
imsyy committed Oct 15, 2024
1 parent 57325bf commit 3a6f8e7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/components/Card/SongCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
:key="song.cover"
:src="song.path ? song.cover : song.coverSize?.s || song.cover"
class="cover"
@update:show.once="localCover"
@update:show="localCover"
/>
<!-- 信息 -->
<div class="info">
Expand Down
14 changes: 13 additions & 1 deletion src/components/Modal/Login/LoginCookie.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
</template>
可在官方的
<n-a href="https://music.163.com/" target="_blank">网页端</n-a>
和客户端的控制台中获取,详细步骤请自行搜索。
和客户端的控制台中获取,只需要 Cookie 中的 <code>MUSIC_U</code> 字段即可,例如:
<code>MUSIC_U=00C7...;</code><br />请注意:必须以 <code>;</code> 结束
</n-alert>
<n-input
v-model:value="cookie"
Expand Down Expand Up @@ -61,5 +62,16 @@ const login = async () => {
width: 100%;
margin-top: 20px;
}
code {
font-size: 12px;
display: inline-flex;
align-items: center;
justify-content: center;
background-color: var(--n-border-color);
padding: 4px 6px;
border-radius: 8px;
margin: 4px 0;
font-family: auto;
}
}
</style>
16 changes: 12 additions & 4 deletions src/utils/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ export const removeCookie = (key: string) => {

// 设置 Cookie
export const setCookies = (cookieValue: string) => {
const cookies = cookieValue.split(";;");
const cookies = cookieValue.split(";");
const date = new Date();
// 永不过期
date.setFullYear(date.getFullYear() + 50);
const expires = `expires=${date.toUTCString()}`;
// 写入
cookies.forEach((cookie) => {
document.cookie = cookie;
// document.cookie = cookie;
const cookieParts = cookie.split(";");
const nameValuePair = cookieParts[0].split("=");
const name = nameValuePair[0].trim();
const value = nameValuePair[1].trim();
const name = nameValuePair[0]?.trim();
const value = nameValuePair[1]?.trim();
console.info(`name: ${name}, value: ${value}`);
// 设置 cookie
document.cookie = `${name}=${value}; ${expires}; path=/`;
// 保存 cookie
localStorage.setItem(`cookie-${name}`, value);
});
Expand Down

0 comments on commit 3a6f8e7

Please sign in to comment.