DeviceInfoUtils.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using JiaZhiQuan.Common.Messaging.Models;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Linq;
  5. namespace JiaZhiQuan.Common.Utils
  6. {
  7. public static class DeviceInfoUtils
  8. {
  9. /// <summary>
  10. /// 解析Device信息
  11. /// </summary>
  12. /// <param name="deviceInfo"></param>
  13. /// <returns>item1:系统,item2:APP版本,item3:机型,item4:市场来源</returns>
  14. public static (string, string, string, string) ExtractDevice(string deviceInfo, string channel)
  15. {
  16. string os = string.Empty, // 来源系统
  17. appVersion = string.Empty, // APP版本
  18. phoneModel = string.Empty, // 机型
  19. market = string.Empty;
  20. if (deviceInfo.Contains(UserInvite.ActiveChannel.Android.ToString()) && deviceInfo.Contains("soft_version"))
  21. {
  22. os = UserInvite.ActiveChannel.Android.ToString();
  23. AndroidDeviceInfo android = JsonConvert.DeserializeObject<AndroidDeviceInfo>(deviceInfo);
  24. // 版本号是v3.19.3_31这样的格式,数据库只保存3.19
  25. int startIndex = android.soft_version.IndexOf('v') + 1;
  26. int endIndex = android.soft_version.IndexOf('_');
  27. if (startIndex != -1 && endIndex != -1)
  28. {
  29. appVersion = android.soft_version.Substring(startIndex, endIndex - startIndex);
  30. }
  31. phoneModel = android.brand;
  32. market = channel switch
  33. {
  34. "xiaomi" => "小米市场",
  35. "huawei" => "华为市场",
  36. "yingyongbao" => "应用宝",
  37. "baidu" => "百度市场",
  38. "vivo" => "Vivo市场",
  39. "oppo" => "Oppo市场",
  40. "web" => "WEB (Android)",
  41. _ => ""
  42. };
  43. }
  44. else if (deviceInfo.Contains("iPhone") && deviceInfo.Contains("appVersoft_versionsion"))
  45. {
  46. os = UserInvite.ActiveChannel.iOS.ToString();
  47. iOSDeviceInfo ios = JsonConvert.DeserializeObject<iOSDeviceInfo>(deviceInfo);
  48. // 版本号是3.0.1
  49. appVersion = ios.appVersoft_versionsion;
  50. phoneModel = ios.os;
  51. market = "App Store";
  52. }
  53. return (os, appVersion, phoneModel, market);
  54. }
  55. }
  56. }