useLogger.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 { RecentNotUsedType, SortTypeEnum, TagType, VideoLibraryType } 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. recentNotUsed?: RecentNotUsedType;
  18. sortType?: SortTypeEnum;
  19. tags?: TagType[];
  20. videoLibraryType?: VideoLibraryType;
  21. } & NormalLogParams;
  22. type VideoLogParams = {
  23. videoId: number;
  24. score?: number;
  25. tags?: TagType[];
  26. title?: string;
  27. cover?: string;
  28. libraryType?: VideoLibraryType;
  29. playTime?: number;
  30. idx?: number;
  31. collect?: number;
  32. } & NormalLogParams;
  33. interface PublishPlanLogParams extends NormalLogParams {
  34. videoList: VideoItem[];
  35. }
  36. const useLogger = () => {
  37. const token = sso.getUserInfo()?.token;
  38. const channel = sso.getUserInfo()?.channel;
  39. const userId = sso.getUserInfo()?.id;
  40. const timestamp = Date.now();
  41. const userAgent = navigator.userAgent;
  42. const page = window.location.pathname;
  43. const uploadLog = async ({ businessType, objectType, extParams }: { businessType: string, objectType?: string, extParams?: Record<string, any> }) => {
  44. const log = {
  45. token,
  46. channel,
  47. userId,
  48. page,
  49. userAgent,
  50. timestamp,
  51. businessType,
  52. objectType,
  53. extParams,
  54. };
  55. const response = await http.post(uploadLogApi, {
  56. key: 'content-platform-user-behavior',
  57. data: log,
  58. });
  59. return response;
  60. }
  61. const uploadLogVideoListQuery = ({
  62. traceId,
  63. requestId,
  64. planType,
  65. gzhAccountId,
  66. subChannel,
  67. category,
  68. title,
  69. recentNotUsed,
  70. sortType,
  71. tags,
  72. videoLibraryType,
  73. }:VideoListQueryLogParams) => {
  74. return uploadLog({
  75. businessType: 'video_list_query',
  76. objectType: 'video_list',
  77. extParams: {
  78. traceId,
  79. requestId,
  80. planType,
  81. gzhAccountId,
  82. subChannel,
  83. category,
  84. title,
  85. recentNotUsed,
  86. sortType,
  87. tags,
  88. videoLibraryType,
  89. },
  90. });
  91. }
  92. const uploadLogVideoPlay = ({videoId, traceId, requestId, planType, gzhAccountId, subChannel}: VideoLogParams) => {
  93. return uploadLog({
  94. businessType: 'video_play',
  95. objectType: 'video',
  96. extParams: {
  97. videoId,
  98. traceId,
  99. requestId,
  100. planType,
  101. gzhAccountId,
  102. subChannel,
  103. },
  104. });
  105. }
  106. const uploadLogVideoPlayEnd = ({videoId, playTime, traceId, requestId, planType, gzhAccountId, subChannel}: VideoLogParams) => {
  107. return uploadLog({
  108. businessType: 'video_play_end',
  109. objectType: 'video',
  110. extParams: {
  111. videoId,
  112. playTime,
  113. traceId,
  114. requestId,
  115. planType,
  116. gzhAccountId,
  117. subChannel,
  118. },
  119. });
  120. }
  121. const uploadLogVideoView = ({videoId, idx, score, tags, title, cover, libraryType, traceId, requestId, planType, gzhAccountId, subChannel}: VideoLogParams) => {
  122. return uploadLog({
  123. businessType: 'video_view',
  124. objectType: 'video',
  125. extParams: {
  126. videoId,
  127. idx,
  128. score,
  129. tags,
  130. title,
  131. cover,
  132. libraryType,
  133. traceId,
  134. requestId,
  135. planType,
  136. gzhAccountId,
  137. subChannel,
  138. },
  139. });
  140. }
  141. const uploadLogVideoCollect = ({videoId, traceId, requestId, collect, planType, gzhAccountId, subChannel}: VideoLogParams) => {
  142. return uploadLog({
  143. businessType: 'video_collect',
  144. objectType: 'video',
  145. extParams: {
  146. videoId,
  147. traceId,
  148. requestId,
  149. collect,
  150. planType,
  151. gzhAccountId,
  152. subChannel,
  153. },
  154. });
  155. }
  156. const uploadLogVideoCreatePublish = ({
  157. videoList,
  158. traceId,
  159. requestId,
  160. planType,
  161. gzhAccountId,
  162. subChannel
  163. }: PublishPlanLogParams) => {
  164. return uploadLog({
  165. businessType: 'publish_plan_create',
  166. objectType: 'publish_plan',
  167. extParams: {
  168. videoList,
  169. traceId,
  170. requestId,
  171. planType,
  172. gzhAccountId,
  173. subChannel,
  174. },
  175. });
  176. }
  177. return {
  178. uploadLog,
  179. uploadLogVideoPlay,
  180. uploadLogVideoView,
  181. uploadLogVideoPlayEnd,
  182. uploadLogVideoCollect,
  183. uploadLogVideoListQuery,
  184. uploadLogVideoCreatePublish,
  185. };
  186. };
  187. export default useLogger;