useLogger.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import http from '@src/http';
  2. import { uploadLogApi } from '@src/http/api';
  3. import sso from '@src/http/sso';
  4. import { VideoItem } from '@src/views/publishContent/types';
  5. import { VideoSortType } from '@src/views/publishContent/weCom/components/videoSelectModal';
  6. import { VideoSearchPlanType } from '@src/views/publishContent/weCom/type';
  7. type NormalLogParams = {
  8. traceId: number;
  9. requestId: number;
  10. planType?: VideoSearchPlanType;
  11. gzhAccountId?: number;
  12. subChannel?: string;
  13. }
  14. type VideoListQueryLogParams = {
  15. category?: string;
  16. title?: string;
  17. sortType?: VideoSortType;
  18. } & NormalLogParams;
  19. type VideoLogParams = {
  20. videoId: number;
  21. playTime?: number;
  22. collect?: number;
  23. } & NormalLogParams;
  24. interface PublishPlanLogParams extends NormalLogParams {
  25. videoList: VideoItem[];
  26. }
  27. const useLogger = () => {
  28. const token = sso.getUserInfo()?.token;
  29. const channel = sso.getUserInfo()?.channel;
  30. const userId = sso.getUserInfo()?.id;
  31. const timestamp = Date.now();
  32. const userAgent = navigator.userAgent;
  33. const page = window.location.pathname;
  34. const uploadLog = async ({ businessType, objectType, extParams }: { businessType: string, objectType?: string, extParams?: Record<string, any> }) => {
  35. const log = {
  36. token,
  37. channel,
  38. userId,
  39. page,
  40. userAgent,
  41. timestamp,
  42. businessType,
  43. objectType,
  44. extParams,
  45. };
  46. const response = await http.post(uploadLogApi, {
  47. key: 'content-platform-user-behavior',
  48. data: log,
  49. });
  50. return response;
  51. }
  52. const uploadLogVideoListQuery = ({
  53. traceId,
  54. requestId,
  55. planType,
  56. subChannel,
  57. category,
  58. title,
  59. sortType,
  60. }:VideoListQueryLogParams) => {
  61. return uploadLog({
  62. businessType: 'video_list_query',
  63. objectType: 'video_list',
  64. extParams: {
  65. traceId,
  66. requestId,
  67. planType,
  68. subChannel,
  69. category,
  70. title,
  71. sortType,
  72. },
  73. });
  74. }
  75. const uploadLogVideoPlay = ({videoId, traceId, requestId, planType, subChannel}: VideoLogParams) => {
  76. return uploadLog({
  77. businessType: 'video_play',
  78. objectType: 'video',
  79. extParams: {
  80. videoId,
  81. traceId,
  82. requestId,
  83. planType,
  84. subChannel,
  85. },
  86. });
  87. }
  88. const uploadLogVideoPlayEnd = ({videoId, playTime, traceId, requestId, planType, subChannel}: VideoLogParams) => {
  89. return uploadLog({
  90. businessType: 'video_play_end',
  91. objectType: 'video',
  92. extParams: {
  93. videoId,
  94. playTime,
  95. traceId,
  96. requestId,
  97. planType,
  98. subChannel,
  99. },
  100. });
  101. }
  102. const uploadLogVideoCollect = ({videoId, traceId, requestId, collect, planType, subChannel}: VideoLogParams) => {
  103. return uploadLog({
  104. businessType: 'video_collect',
  105. objectType: 'video',
  106. extParams: {
  107. videoId,
  108. traceId,
  109. requestId,
  110. collect,
  111. planType,
  112. subChannel,
  113. },
  114. });
  115. }
  116. const uploadLogVideoCreatePublish = ({
  117. videoList,
  118. traceId,
  119. requestId,
  120. planType,
  121. gzhAccountId,
  122. subChannel
  123. }: PublishPlanLogParams) => {
  124. return uploadLog({
  125. businessType: 'publish_plan_create',
  126. objectType: 'publish_plan',
  127. extParams: {
  128. videoList,
  129. traceId,
  130. requestId,
  131. planType,
  132. gzhAccountId,
  133. subChannel,
  134. },
  135. });
  136. }
  137. return {
  138. uploadLog,
  139. uploadLogVideoPlay,
  140. uploadLogVideoPlayEnd,
  141. uploadLogVideoCollect,
  142. uploadLogVideoListQuery,
  143. uploadLogVideoCreatePublish,
  144. };
  145. };
  146. export default useLogger;