import { AppTracks } from "@scripts/app/AppTrack"; export interface IFBLoginRes { accessToken: string; /** SDK 返回的用户资料,仅用于展示;服务端仍需验证登录凭证。 */ userId?: string; name?: string; /** iOS Limited Login JWT; never use as a Graph API access token. */ authenticationToken?: string; tokenType?: "access" | "authentication"; nonce?: string; isCancel: boolean; status?: string; reason?: string; } export type IFBEventValue = string | number; export type IFBEventParams = Record; export interface IFBShareOptions { url: string; hashtag?: string; quote?: string; } export interface IFBShareRes { success: boolean; isCancel: boolean; postId?: string; reason?: string; } const FBEventNames = { AchievedLevel: "fb_mobile_level_achieved", ActivatedApp: "fb_mobile_activate_app", AddedPaymentInfo: "fb_mobile_add_payment_info", AddedToCart: "fb_mobile_add_to_cart", AddedToWishlist: "fb_mobile_add_to_wishlist", CompletedRegistration: "fb_mobile_complete_registration", CompletedTutorial: "fb_mobile_tutorial_completion", InitiatedCheckout: "fb_mobile_initiated_checkout", Purchased: "fb_mobile_purchase", Rated: "fb_mobile_rate", Searched: "fb_mobile_search", SpentCredits: "fb_mobile_spent_credits", UnlockedAchievement: "fb_mobile_achievement_unlocked", ViewedContent: "fb_mobile_content_view", } const FBEventParameterName = { ContentID: "fb_content_id", ContentType: "fb_content_type", Currency: "fb_currency", Description: "fb_description", Level: "fb_level", MaxRatingValue: "fb_max_rating_value", NumItems: "fb_num_items", PaymentInfoAvailable: "fb_payment_info_available", RegistrationMethod: "fb_registration_method", SearchString: "fb_search_string", Success: "fb_success", } const EventsMap = { [AppTracks.Registered]: FBEventNames.CompletedRegistration, [AppTracks.AddToWishlist]: FBEventNames.AddedToWishlist, [AppTracks.Addtocart]: FBEventNames.AddedToCart, } export abstract class FacebookImpl { private _isInitialized = false; public get isInitialized() { return this._isInitialized; } abstract init(): void; protected onInited() { this._isInitialized = true; this.activeApp(); } async login(): Promise { const res = await this.onLogin(); return res; } protected abstract onLogin(): Promise; abstract logOut(): Promise; abstract share(options: IFBShareOptions): Promise; activeApp() { this.logEvent(FBEventNames.ActivatedApp); } logEvent(eventName: string, parameters?: IFBEventParams, valueToSum?: number): Promise { const fbEvent = EventsMap[eventName] if (!!fbEvent) { return this.onLogEvent(fbEvent, parameters, valueToSum); } if (eventName === AppTracks.Purchased) { let currency = parameters?.currency || ""; if (typeof currency === 'number') currency = currency.toString(); const value = Number(parameters?.value || valueToSum); this.logPurchase(value, currency, { [FBEventParameterName.Currency]: currency }) } } abstract onLogEvent(eventName: string, parameters?: IFBEventParams, valueToSum?: number): Promise; abstract logPurchase( amount: number, currency: string, parameters?: IFBEventParams, ): Promise; abstract flushEvents(): Promise; }