Skip to content

Commit 768fb63

Browse files
authored
feat: scan QRCode auto redeem coupon (#5616)
* feat: scan QRCode auto redeem coupon * feat: use hook to auto redeem, instead of modify auth
1 parent 635e606 commit 768fb63

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useEffect, useRef } from 'react';
2+
import { getCouponCode, removeCouponCode } from '@/web/support/marketing/utils';
3+
import type { UserType } from '@fastgpt/global/support/user/type.d';
4+
import { redeemCoupon } from '@/web/support/user/team/api';
5+
6+
export const useCheckCoupon = (userInfo: UserType | null) => {
7+
const hasCheckedCouponRef = useRef(false);
8+
9+
useEffect(() => {
10+
if (!userInfo || hasCheckedCouponRef.current) return;
11+
12+
const couponCode = getCouponCode();
13+
if (!couponCode) return;
14+
15+
hasCheckedCouponRef.current = true;
16+
17+
redeemCoupon(couponCode)
18+
.catch(() => {})
19+
.finally(removeCouponCode);
20+
}, [userInfo]);
21+
};
22+
23+
export default useCheckCoupon;

projects/app/src/components/Layout/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { useDebounceEffect, useMount } from 'ahooks';
1515
import { useTranslation } from 'next-i18next';
1616
import { useToast } from '@fastgpt/web/hooks/useToast';
1717
import WorkorderButton from './WorkorderButton';
18+
import { useCheckCoupon } from './hooks/checkCoupon';
1819

1920
const Navbar = dynamic(() => import('./navbar'));
2021
const NavbarPhone = dynamic(() => import('./navbarPhone'));
@@ -74,6 +75,8 @@ const Layout = ({ children }: { children: JSX.Element }) => {
7475
const { userInfo, isUpdateNotification, setIsUpdateNotification } = useUserStore();
7576
const { setUserDefaultLng } = useI18nLng();
7677

78+
useCheckCoupon(userInfo);
79+
7780
const isChatPage = useMemo(
7881
() => router.pathname === '/chat' && Object.values(router.query).join('').length !== 0,
7982
[router.pathname, router.query]

projects/app/src/web/context/useInitApp.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
setUtmWorkflow
1818
} from '../support/marketing/utils';
1919
import { type ShortUrlParams } from '@fastgpt/global/support/marketing/type';
20+
import { setCouponCode } from '@/web/support/marketing/utils';
2021

2122
type MarketingQueryParams = {
2223
hiId?: string;
@@ -29,6 +30,7 @@ type MarketingQueryParams = {
2930
utm_medium?: string;
3031
utm_content?: string;
3132
utm_workflow?: string;
33+
couponCode?: string;
3234
};
3335

3436
const MARKETING_PARAMS: (keyof MarketingQueryParams)[] = [
@@ -40,7 +42,8 @@ const MARKETING_PARAMS: (keyof MarketingQueryParams)[] = [
4042
'utm_source',
4143
'utm_medium',
4244
'utm_content',
43-
'utm_workflow'
45+
'utm_workflow',
46+
'couponCode'
4447
];
4548

4649
export const useInitApp = () => {
@@ -55,7 +58,8 @@ export const useInitApp = () => {
5558
utm_source,
5659
utm_medium,
5760
utm_content,
58-
utm_workflow
61+
utm_workflow,
62+
couponCode
5963
} = router.query as MarketingQueryParams;
6064

6165
const { loadGitStar, setInitd, feConfigs } = useSystemStore();
@@ -149,6 +153,10 @@ export const useInitApp = () => {
149153
}
150154
setFastGPTSem({ keyword: k, search, ...utmParams });
151155

156+
if (couponCode) {
157+
setCouponCode(couponCode);
158+
}
159+
152160
const newPath = getPathWithoutMarketingParams();
153161
router.replace(newPath);
154162
});

projects/app/src/web/support/marketing/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,16 @@ export const setSourceDomain = (sourceDomain?: string) => {
9090
if (!formatSourceDomain || getSourceDomain()) return;
9191
sessionStorage.setItem('sourceDomain', formatSourceDomain);
9292
};
93+
94+
export const setCouponCode = (couponCode?: string) => {
95+
if (!couponCode) return;
96+
localStorage.setItem('couponCode', couponCode);
97+
};
98+
99+
export const getCouponCode = () => {
100+
return localStorage.getItem('couponCode') || undefined;
101+
};
102+
103+
export const removeCouponCode = () => {
104+
localStorage.removeItem('couponCode');
105+
};

0 commit comments

Comments
 (0)