User.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Collections.Generic;
  2. namespace TapSDK.Core.Standalone.Internal {
  3. public class User {
  4. internal static readonly string USER_ID_KEY = "tapdb_played_duration_user_id";
  5. internal string Id {
  6. get => id;
  7. set {
  8. id = value;
  9. TapCoreStandalone.Prefs.Set(USER_ID_KEY, id);
  10. }
  11. }
  12. private string id;
  13. private readonly PlayRecorder playRecorder;
  14. internal User() {
  15. playRecorder = new PlayRecorder();
  16. }
  17. internal void Login(string userId, Dictionary<string, object> props = null) {
  18. // 先执行旧用户登出逻辑
  19. Id = TapCoreStandalone.Prefs.Get<string>(USER_ID_KEY);
  20. if (!string.IsNullOrWhiteSpace(Id)) {
  21. Logout();
  22. }
  23. // 再执行新用户登录逻辑
  24. Id = userId;
  25. TapEventStandalone.Tracker?.TrackEvent(Constants.USER_LOGIN, props, true);
  26. Dictionary<string, object> updateProps = new Dictionary<string, object> {
  27. { "has_user", true },
  28. };
  29. TapEventStandalone.Tracker?.TrackDeviceProperties(Constants.PROPERTY_UPDATE_TYPE, updateProps);
  30. playRecorder.Start();
  31. }
  32. internal void Logout() {
  33. playRecorder.Stop();
  34. Id = null;
  35. }
  36. }
  37. }