From 4997334c376727f143e9d0ff849bdd77ea98f461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E5=BA=86=E6=89=8D=E4=B8=A8=E9=9D=99=E8=A7=85?= Date: Tue, 2 Jan 2024 14:55:36 +0800 Subject: [PATCH] Add Cookie of inviter id perist (#15) --- ...-656f97b1-d2bb-424f-8aff-9d5fcdc142eb.json | 7 +++++ src/App.vue | 19 +++++++++++- src/utils/cookies.ts | 31 +++++++++++++++++++ src/utils/index.ts | 1 + 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 change/@zhishuyun-hub-656f97b1-d2bb-424f-8aff-9d5fcdc142eb.json create mode 100644 src/utils/cookies.ts diff --git a/change/@zhishuyun-hub-656f97b1-d2bb-424f-8aff-9d5fcdc142eb.json b/change/@zhishuyun-hub-656f97b1-d2bb-424f-8aff-9d5fcdc142eb.json new file mode 100644 index 0000000..c833ef3 --- /dev/null +++ b/change/@zhishuyun-hub-656f97b1-d2bb-424f-8aff-9d5fcdc142eb.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "add inviter id cookies persist", + "packageName": "@zhishuyun/hub", + "email": "cqc@cuiqingcai.com", + "dependentChangeType": "patch" +} diff --git a/src/App.vue b/src/App.vue index 445070a..c3040eb 100644 --- a/src/App.vue +++ b/src/App.vue @@ -8,6 +8,7 @@ import { defineComponent } from 'vue'; import { ElConfigProvider } from 'element-plus'; import zhCn from 'element-plus/dist/locale/zh-cn.mjs'; +import { getDomain, getPath, setCookie } from './utils'; export default defineComponent({ components: { @@ -15,8 +16,24 @@ export default defineComponent({ }, data() { return { - locale: zhCn + locale: zhCn, + inviterId: this.$route.query.inviter_id?.toString() }; + }, + methods: { + onSetCookie() { + // set inviter to cookies to persist + if (this.inviterId) { + // current date + 7 days + const expiration = new Date(); + expiration.setDate(expiration.getDate() + 7); + setCookie('INVITER_ID', this.inviterId, { + domain: getDomain(), + path: getPath(), + expires: expiration + }); + } + } } }); diff --git a/src/utils/cookies.ts b/src/utils/cookies.ts new file mode 100644 index 0000000..7568674 --- /dev/null +++ b/src/utils/cookies.ts @@ -0,0 +1,31 @@ +import { removeCookie, setCookie as baseSetCookie } from 'typescript-cookie'; +import { CookieAttributes } from 'typescript-cookie/dist/types'; + +export const getDomain = () => { + const host = window.location.host; + // process test env and prod env, for example: + // auth.zhishuyun.com -> .zhishuyun.com + // auth.test.zhishuyun.com -> .zhishuyun.com + const domain = host.replace(/^\S+?\.(test\.|local\.)?/, '.'); + return domain; +}; + +export const getPath = () => { + return '/'; +}; + +export const setCookie = (key: string, value: string, attributes: CookieAttributes) => { + baseSetCookie(key, value, { + domain: attributes?.domain || getDomain(), + path: attributes?.path || getPath(), + expires: attributes?.expires, + sameSite: attributes?.sameSite, + secure: attributes?.secure + }); +}; + +export const resetCookies = () => { + // remove tokens from cookies + removeCookie('ACCESS_TOKEN', { domain: getDomain(), path: getPath() }); + removeCookie('REFRESH_TOKEN', { domain: getDomain(), path: getPath() }); +}; diff --git a/src/utils/index.ts b/src/utils/index.ts index c435920..88063ad 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,3 +1,4 @@ export * from './baseUrl'; export * from './mode'; export * from './log'; +export * from './cookies';