FacebookImpl.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { AppTracks } from "@scripts/app/AppTrack";
  2. export interface IFBLoginRes {
  3. accessToken: string;
  4. /** SDK 返回的用户资料,仅用于展示;服务端仍需验证登录凭证。 */
  5. userId?: string;
  6. name?: string;
  7. /** iOS Limited Login JWT; never use as a Graph API access token. */
  8. authenticationToken?: string;
  9. tokenType?: "access" | "authentication";
  10. nonce?: string;
  11. isCancel: boolean;
  12. status?: string;
  13. reason?: string;
  14. }
  15. export type IFBEventValue = string | number;
  16. export type IFBEventParams = Record<string, IFBEventValue>;
  17. export interface IFBShareOptions {
  18. url: string;
  19. hashtag?: string;
  20. quote?: string;
  21. }
  22. export interface IFBShareRes {
  23. success: boolean;
  24. isCancel: boolean;
  25. postId?: string;
  26. reason?: string;
  27. }
  28. const FBEventNames = {
  29. AchievedLevel: "fb_mobile_level_achieved",
  30. ActivatedApp: "fb_mobile_activate_app",
  31. AddedPaymentInfo: "fb_mobile_add_payment_info",
  32. AddedToCart: "fb_mobile_add_to_cart",
  33. AddedToWishlist: "fb_mobile_add_to_wishlist",
  34. CompletedRegistration: "fb_mobile_complete_registration",
  35. CompletedTutorial: "fb_mobile_tutorial_completion",
  36. InitiatedCheckout: "fb_mobile_initiated_checkout",
  37. Purchased: "fb_mobile_purchase",
  38. Rated: "fb_mobile_rate",
  39. Searched: "fb_mobile_search",
  40. SpentCredits: "fb_mobile_spent_credits",
  41. UnlockedAchievement: "fb_mobile_achievement_unlocked",
  42. ViewedContent: "fb_mobile_content_view",
  43. }
  44. const EventsMap = {
  45. [AppTracks.Registered]: FBEventNames.CompletedRegistration,
  46. [AppTracks.AddToWishlist]: FBEventNames.AddedToWishlist,
  47. [AppTracks.Addtocart]: FBEventNames.AddedToCart,
  48. }
  49. export abstract class FacebookImpl {
  50. private _isInitialized = false;
  51. public get isInitialized() { return this._isInitialized; }
  52. abstract init(): void;
  53. protected onInited() {
  54. this._isInitialized = true;
  55. this.activeApp();
  56. }
  57. async login(): Promise<IFBLoginRes> {
  58. const res = await this.onLogin();
  59. return res;
  60. }
  61. protected abstract onLogin(): Promise<IFBLoginRes>;
  62. abstract logOut(): Promise<boolean>;
  63. abstract share(options: IFBShareOptions): Promise<IFBShareRes>;
  64. activeApp() {
  65. this.logEvent(FBEventNames.ActivatedApp);
  66. }
  67. logEvent(eventName: string, parameters?: IFBEventParams, valueToSum?: number): Promise<boolean> {
  68. const fbEvent = EventsMap[eventName]
  69. if (!!fbEvent) {
  70. return this.onLogEvent(fbEvent, parameters, valueToSum);
  71. }
  72. }
  73. abstract onLogEvent(eventName: string, parameters?: IFBEventParams, valueToSum?: number): Promise<boolean>;
  74. abstract logPurchase(
  75. amount: number,
  76. currency: string,
  77. parameters?: IFBEventParams,
  78. ): Promise<boolean>;
  79. abstract flushEvents(): Promise<boolean>;
  80. }