File tree Expand file tree Collapse file tree 4 files changed +49
-2
lines changed Expand file tree Collapse file tree 4 files changed +49
-2
lines changed Original file line number Diff line number Diff line change
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 ;
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import { useDebounceEffect, useMount } from 'ahooks';
15
15
import { useTranslation } from 'next-i18next' ;
16
16
import { useToast } from '@fastgpt/web/hooks/useToast' ;
17
17
import WorkorderButton from './WorkorderButton' ;
18
+ import { useCheckCoupon } from './hooks/checkCoupon' ;
18
19
19
20
const Navbar = dynamic ( ( ) => import ( './navbar' ) ) ;
20
21
const NavbarPhone = dynamic ( ( ) => import ( './navbarPhone' ) ) ;
@@ -74,6 +75,8 @@ const Layout = ({ children }: { children: JSX.Element }) => {
74
75
const { userInfo, isUpdateNotification, setIsUpdateNotification } = useUserStore ( ) ;
75
76
const { setUserDefaultLng } = useI18nLng ( ) ;
76
77
78
+ useCheckCoupon ( userInfo ) ;
79
+
77
80
const isChatPage = useMemo (
78
81
( ) => router . pathname === '/chat' && Object . values ( router . query ) . join ( '' ) . length !== 0 ,
79
82
[ router . pathname , router . query ]
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ import {
17
17
setUtmWorkflow
18
18
} from '../support/marketing/utils' ;
19
19
import { type ShortUrlParams } from '@fastgpt/global/support/marketing/type' ;
20
+ import { setCouponCode } from '@/web/support/marketing/utils' ;
20
21
21
22
type MarketingQueryParams = {
22
23
hiId ?: string ;
@@ -29,6 +30,7 @@ type MarketingQueryParams = {
29
30
utm_medium ?: string ;
30
31
utm_content ?: string ;
31
32
utm_workflow ?: string ;
33
+ couponCode ?: string ;
32
34
} ;
33
35
34
36
const MARKETING_PARAMS : ( keyof MarketingQueryParams ) [ ] = [
@@ -40,7 +42,8 @@ const MARKETING_PARAMS: (keyof MarketingQueryParams)[] = [
40
42
'utm_source' ,
41
43
'utm_medium' ,
42
44
'utm_content' ,
43
- 'utm_workflow'
45
+ 'utm_workflow' ,
46
+ 'couponCode'
44
47
] ;
45
48
46
49
export const useInitApp = ( ) => {
@@ -55,7 +58,8 @@ export const useInitApp = () => {
55
58
utm_source,
56
59
utm_medium,
57
60
utm_content,
58
- utm_workflow
61
+ utm_workflow,
62
+ couponCode
59
63
} = router . query as MarketingQueryParams ;
60
64
61
65
const { loadGitStar, setInitd, feConfigs } = useSystemStore ( ) ;
@@ -149,6 +153,10 @@ export const useInitApp = () => {
149
153
}
150
154
setFastGPTSem ( { keyword : k , search, ...utmParams } ) ;
151
155
156
+ if ( couponCode ) {
157
+ setCouponCode ( couponCode ) ;
158
+ }
159
+
152
160
const newPath = getPathWithoutMarketingParams ( ) ;
153
161
router . replace ( newPath ) ;
154
162
} ) ;
Original file line number Diff line number Diff line change @@ -90,3 +90,16 @@ export const setSourceDomain = (sourceDomain?: string) => {
90
90
if ( ! formatSourceDomain || getSourceDomain ( ) ) return ;
91
91
sessionStorage . setItem ( 'sourceDomain' , formatSourceDomain ) ;
92
92
} ;
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
+ } ;
You can’t perform that action at this time.
0 commit comments