@x-edu/live-player 0.0.21 → 0.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (299) hide show
  1. package/.browserslistrc +10 -10
  2. package/.eslintignore +8 -8
  3. package/.eslintrc.js +26 -26
  4. package/.gem-mine +11 -11
  5. package/.postcssrc.js +6 -6
  6. package/README.md +145 -145
  7. package/babel.config.js +33 -33
  8. package/doc/develop.md +23 -23
  9. package/gem-mine.config.js +86 -86
  10. package/jest.config.js +9 -9
  11. package/jsconfig.json +15 -15
  12. package/mock/helper.js +8 -8
  13. package/mock/index.js +34 -34
  14. package/package.json +99 -99
  15. package/public/3rd/ali-player/alipayercomponents-1.0.8.min.js +26 -26
  16. package/public/3rd/ali-player/aliplayer-hls-min.js +27 -27
  17. package/public/3rd/ali-player/aliplayer-min.js +1 -1
  18. package/public/3rd/im-web-push/index.umd.js +14 -14
  19. package/public/JsBridge.js +1602 -1602
  20. package/public/fish/editor/themes/default/css/ueditor.css +1905 -1905
  21. package/public/fish/editor/third-party/codemirror/codemirror.css +104 -104
  22. package/public/fish/editor/third-party/codemirror/codemirror.js +3581 -3581
  23. package/public/fish/editor/third-party/zeroclipboard/ZeroClipboard.js +1255 -1255
  24. package/public/fish/editor/ueditor.all.min.js +16 -16
  25. package/public/fish/editor/ueditor.config.js +467 -467
  26. package/public/fish/icon/umd-4.1.2.js +1 -1
  27. package/public/fish/video/videoplayer.min.css +6 -6
  28. package/public/fish/video/videoplayer.min.js +31 -31
  29. package/public/fish/video/zh-CN.js +82 -82
  30. package/public/icon/demo.css +539 -539
  31. package/public/icon/demo_index.html +2189 -2189
  32. package/public/icon/iconfont.css +363 -363
  33. package/public/icon/iconfont.json +618 -618
  34. package/public/index.html +50 -50
  35. package/script/image/readme.md +77 -77
  36. package/script/postcss.js +48 -48
  37. package/src/App.jsx +24 -24
  38. package/src/asset/img/course/playing.svg +21 -21
  39. package/src/asset/img/icon-course-white.svg +3 -3
  40. package/src/asset/img/icon-course.svg +3 -3
  41. package/src/asset/img/icon-eye-white.svg +4 -4
  42. package/src/asset/img/icon-eye.svg +4 -4
  43. package/src/asset/img/icon-school-white.svg +3 -3
  44. package/src/asset/img/icon-school.svg +3 -3
  45. package/src/asset/img/loading.svg +52 -52
  46. package/src/asset/img/video-play.svg +6 -6
  47. package/src/asset/style/background.less +19 -19
  48. package/src/asset/style/banner.less +36 -36
  49. package/src/asset/style/base.less +74 -74
  50. package/src/asset/style/index.less +114 -114
  51. package/src/asset/style/reset-theme.less +141 -141
  52. package/src/asset/style/theme/index.less +55 -55
  53. package/src/asset/style/theme/party/global.less +20 -20
  54. package/src/asset/style/theme/party/override.less +57 -57
  55. package/src/asset/style/theme/test/global.less +18 -18
  56. package/src/asset/style/theme/test/override.less +45 -45
  57. package/src/asset/style/variable.less +12 -12
  58. package/src/component/AliPlayer/index.jsx +49 -49
  59. package/src/component/Avatar/index.jsx +50 -50
  60. package/src/component/Empty/index.jsx +34 -34
  61. package/src/component/Empty/index.module.less +37 -37
  62. package/src/component/IMChatroom/component/status/403.jsx +13 -13
  63. package/src/component/IMChatroom/component/status/404.jsx +13 -13
  64. package/src/component/IMChatroom/component/status/Error.jsx +23 -23
  65. package/src/component/IMChatroom/component/status/Loading.jsx +5 -5
  66. package/src/component/IMChatroom/config/app/config.js +118 -118
  67. package/src/component/IMChatroom/config/app/index.js +60 -60
  68. package/src/component/IMChatroom/config/env.js +5 -5
  69. package/src/component/IMChatroom/config/index.js +3 -3
  70. package/src/component/IMChatroom/config/request/config/base.js +124 -124
  71. package/src/component/IMChatroom/config/request/config/chatroom.js +26 -26
  72. package/src/component/IMChatroom/config/request/config/imCoreApi.js +21 -21
  73. package/src/component/IMChatroom/config/request/index.js +21 -21
  74. package/src/component/IMChatroom/config/request/proxy.js +39 -39
  75. package/src/component/IMChatroom/config/url.js +7 -7
  76. package/src/component/IMChatroom/constants/errorCode.js +9 -9
  77. package/src/component/IMChatroom/constants/index.js +84 -84
  78. package/src/component/IMChatroom/constants/origin.js +27 -27
  79. package/src/component/IMChatroom/context/chatroom.js +154 -154
  80. package/src/component/IMChatroom/context/connect.js +28 -28
  81. package/src/component/IMChatroom/i18n/en/index.json +73 -73
  82. package/src/component/IMChatroom/i18n/index.jsx +14 -14
  83. package/src/component/IMChatroom/i18n/language.js +9 -9
  84. package/src/component/IMChatroom/i18n/zh-CN/index.json +73 -73
  85. package/src/component/IMChatroom/index.jsx +49 -49
  86. package/src/component/IMChatroom/page/chat/index.jsx +27 -27
  87. package/src/component/IMChatroom/page/chat/messageEditor/index.jsx +375 -375
  88. package/src/component/IMChatroom/page/chat/messageList/index.jsx +354 -354
  89. package/src/component/IMChatroom/page/chat/stickedMessage/index.jsx +59 -59
  90. package/src/component/IMChatroom/page/chat/stickedMessage/style/index.module.less +7 -7
  91. package/src/component/IMChatroom/page/chat/style/index.module.less +51 -51
  92. package/src/component/IMChatroom/page/home/connect/index.jsx +188 -188
  93. package/src/component/IMChatroom/page/home/connect/status.jsx +124 -124
  94. package/src/component/IMChatroom/page/home/context/cmd.jsx +37 -37
  95. package/src/component/IMChatroom/page/home/context/index.jsx +67 -67
  96. package/src/component/IMChatroom/page/home/index.jsx +164 -164
  97. package/src/component/IMChatroom/page/home/notice/index.jsx +123 -123
  98. package/src/component/IMChatroom/page/home/notice/style/index.module.less +49 -49
  99. package/src/component/IMChatroom/page/home/style/index.module.less +110 -110
  100. package/src/component/IMChatroom/page/member/index.jsx +15 -15
  101. package/src/component/IMChatroom/page/member/memberCount/index.jsx +44 -44
  102. package/src/component/IMChatroom/page/member/memberList/index.jsx +121 -121
  103. package/src/component/IMChatroom/page/member/style/index.module.less +2 -2
  104. package/src/component/IMChatroom/sdk/bridge.js +39 -39
  105. package/src/component/IMChatroom/sdk/cs/constants.js +20 -20
  106. package/src/component/IMChatroom/sdk/cs/index.js +101 -101
  107. package/src/component/IMChatroom/sdk/imChatroom.js +15 -15
  108. package/src/component/IMChatroom/sdk/imcore/constants.js +128 -128
  109. package/src/component/IMChatroom/sdk/imcore/emitters/chatroomDestroyed.js +5 -5
  110. package/src/component/IMChatroom/sdk/imcore/emitters/chatroomInfoGetFail.js +5 -5
  111. package/src/component/IMChatroom/sdk/imcore/emitters/chatroomKickedByAdmin.js +5 -5
  112. package/src/component/IMChatroom/sdk/imcore/emitters/chatroomKickedBySelf.js +5 -5
  113. package/src/component/IMChatroom/sdk/imcore/emitters/chatroomLoginFail.js +5 -5
  114. package/src/component/IMChatroom/sdk/imcore/emitters/chatroomLoginSuccess.js +5 -5
  115. package/src/component/IMChatroom/sdk/imcore/emitters/chatroomMemberLogin.js +5 -5
  116. package/src/component/IMChatroom/sdk/imcore/emitters/chatroomMemberLogout.js +5 -5
  117. package/src/component/IMChatroom/sdk/imcore/emitters/connectionConnected.js +5 -5
  118. package/src/component/IMChatroom/sdk/imcore/emitters/connectionConnecting.js +5 -5
  119. package/src/component/IMChatroom/sdk/imcore/emitters/connectionDisconnect.js +5 -5
  120. package/src/component/IMChatroom/sdk/imcore/emitters/index.js +57 -57
  121. package/src/component/IMChatroom/sdk/imcore/emitters/messageReceive.js +36 -36
  122. package/src/component/IMChatroom/sdk/imcore/index.js +178 -178
  123. package/src/component/IMChatroom/sdk/imcore/interceptors/request.js +7 -7
  124. package/src/component/IMChatroom/sdk/imcore/interceptors/response.js +5 -5
  125. package/src/component/IMChatroom/sdk/imcore/plugins/getAuth.js +64 -64
  126. package/src/component/IMChatroom/sdk/imcore/plugins/getConversationId.js +20 -20
  127. package/src/component/IMChatroom/sdk/imcore/plugins/getExtInfo.js +10 -10
  128. package/src/component/IMChatroom/sdk/imcore/plugins/getLoginInfo.js +48 -48
  129. package/src/component/IMChatroom/sdk/imcore/plugins/getPlatformType.js +12 -12
  130. package/src/component/IMChatroom/sdk/imcore/plugins/index.js +22 -22
  131. package/src/component/IMChatroom/sdk/imcore/util/format.js +177 -177
  132. package/src/component/IMChatroom/sdk/imcore/util/parser.js +35 -35
  133. package/src/component/IMChatroom/sdk/oms.js +18 -18
  134. package/src/component/IMChatroom/sdk/uc.js +3 -3
  135. package/src/component/IMChatroom/subscription/channel/app.js +5 -5
  136. package/src/component/IMChatroom/subscription/channel/ctl.js +22 -22
  137. package/src/component/IMChatroom/subscription/channel/member.js +40 -40
  138. package/src/component/IMChatroom/subscription/channel/message.js +13 -13
  139. package/src/component/IMChatroom/subscription/channel/ntf.js +86 -86
  140. package/src/component/IMChatroom/subscription/channel/tel.js +5 -5
  141. package/src/component/IMChatroom/subscription/channel/websocket.js +5 -5
  142. package/src/component/IMChatroom/subscription/event.js +37 -37
  143. package/src/component/IMChatroom/subscription/index.js +19 -19
  144. package/src/component/IMChatroom/util/account.js +8 -8
  145. package/src/component/IMChatroom/util/chatroom.js +255 -255
  146. package/src/component/IMChatroom/util/jsonBigInt.js +5 -5
  147. package/src/component/IMChatroom/util/location.js +12 -12
  148. package/src/component/Icon/index.jsx +16 -16
  149. package/src/component/Icon/index.module.less +2 -2
  150. package/src/component/Pagination/LocalPagination.jsx +27 -27
  151. package/src/component/Pagination/RemotePagination.jsx +32 -32
  152. package/src/component/Pagination/index.jsx +32 -31
  153. package/src/component/Pagination/index.module.less +117 -110
  154. package/src/component/PlayerTip/index.jsx +34 -34
  155. package/src/component/VideoBase/index.jsx +242 -242
  156. package/src/component/VideoBase/style.less +65 -65
  157. package/src/component/VideoBase/utils.js +81 -81
  158. package/src/component/mixin.less +9 -9
  159. package/src/component/status/403.jsx +15 -15
  160. package/src/component/status/404/index.jsx +73 -73
  161. package/src/component/status/404/index.module.less +36 -36
  162. package/src/component/status/Error/error.module.less +13 -13
  163. package/src/component/status/Error/index.jsx +47 -47
  164. package/src/component/status/Loading/img/loading.svg +52 -52
  165. package/src/component/status/Loading/index.jsx +31 -31
  166. package/src/component/status/Loading/index.less +56 -56
  167. package/src/component/status/None/index.jsx +20 -20
  168. package/src/component/status/None/index.module.less +35 -35
  169. package/src/component/status/Status/index.jsx +26 -26
  170. package/src/component/status/Status/index.module.less +8 -8
  171. package/src/config/constant/commonUrl.js +2 -2
  172. package/src/config/constant/user.js +6 -6
  173. package/src/config/context/site-config.js +48 -48
  174. package/src/config/env/index.js +5 -5
  175. package/src/config/env/local/index.js +13 -13
  176. package/src/config/env/ncet-xedu/api.js +175 -175
  177. package/src/config/env/ncet-xedu/app.js +12 -12
  178. package/src/config/env/ncet-xedu/cs.js +8 -8
  179. package/src/config/env/ncet-xedu/index.js +14 -14
  180. package/src/config/env/ncet-xedu/push.js +5 -5
  181. package/src/config/env/preproduction/api.js +144 -144
  182. package/src/config/env/preproduction/app.js +12 -12
  183. package/src/config/env/preproduction/cs.js +8 -8
  184. package/src/config/env/preproduction/index.js +14 -14
  185. package/src/config/env/preproduction/push.js +5 -5
  186. package/src/config/env/product/api.js +113 -113
  187. package/src/config/env/product/app.js +12 -12
  188. package/src/config/env/product/cs.js +5 -5
  189. package/src/config/env/product/index.js +14 -14
  190. package/src/config/env/product/push.js +5 -5
  191. package/src/config/live.js +80 -80
  192. package/src/config/publicLive.js +53 -53
  193. package/src/config/request/assessments.js +24 -24
  194. package/src/config/request/config-center.js +8 -8
  195. package/src/config/request/helper.js +166 -166
  196. package/src/config/request/im-broadcasts.js +12 -12
  197. package/src/config/request/live-activity-gateway.js +8 -8
  198. package/src/config/request/live-activity.js +13 -13
  199. package/src/config/request/raw.js +6 -6
  200. package/src/config/request/x-cloud-web.js +13 -13
  201. package/src/config/request/x-proxy.js +34 -34
  202. package/src/demo/Detail.jsx +11 -11
  203. package/src/demo/List.jsx +66 -64
  204. package/src/demo/index.jsx +17 -17
  205. package/src/detail/LineSwitch/index.jsx +71 -71
  206. package/src/detail/LiveCountDown/index.jsx +44 -44
  207. package/src/detail/LiveCountDown/index.module.less +31 -31
  208. package/src/detail/LiveOnlineCount/img/icon.svg +15 -15
  209. package/src/detail/LiveOnlineCount/index.jsx +68 -68
  210. package/src/detail/LiveOnlineCount/index.module.less +34 -34
  211. package/src/detail/LiveOnlineCount/util.js +74 -74
  212. package/src/detail/LiveStatus/AnchorOnTheWay.jsx +88 -88
  213. package/src/detail/LiveStatus/index.jsx +202 -202
  214. package/src/detail/LiveStatus/index.module.less +192 -192
  215. package/src/detail/LiveVideo/config.js +37 -37
  216. package/src/detail/LiveVideo/index.jsx +188 -188
  217. package/src/detail/LiveVideo/index.module.less +41 -41
  218. package/src/detail/NotLoginChatroom/index.jsx +37 -37
  219. package/src/detail/NotLoginChatroom/index.module.less +46 -46
  220. package/src/detail/RecordVideo/index.jsx +150 -150
  221. package/src/detail/RecordVideo/index.module.less +31 -31
  222. package/src/detail/ReplayVideo/index.jsx +93 -93
  223. package/src/detail/ReplayVideo/index.module.less +65 -65
  224. package/src/detail/const.js +20 -20
  225. package/src/detail/hook/usePullSource.js +82 -82
  226. package/src/detail/hook/usePullState.js +51 -51
  227. package/src/detail/index.jsx +482 -482
  228. package/src/detail/index.module.less +188 -188
  229. package/src/hook/useAsync.js +84 -84
  230. package/src/hook/useGoToPage.js +83 -83
  231. package/src/hook/useInterval.js +28 -28
  232. package/src/hook/useIsMounted.js +17 -17
  233. package/src/hook/useModuleByRole.js +53 -53
  234. package/src/hook/usePageScrollable.js +52 -52
  235. package/src/hook/usePortal.js +83 -83
  236. package/src/hook/useScrollRestore/index.js +32 -32
  237. package/src/hook/useScrollRestore/scrollContext.js +20 -20
  238. package/src/hook/useTrackRef.js +11 -11
  239. package/src/hook/useWindowSize.js +28 -28
  240. package/src/hook/useZoom.js +47 -47
  241. package/src/index.js +10 -10
  242. package/src/lib/getLiveOnlineCount.js +10 -10
  243. package/src/lib/getServiceConfig.js +102 -102
  244. package/src/list/Empty/index.jsx +20 -20
  245. package/src/list/Empty/index.module.less +23 -23
  246. package/src/list/ListItem/Action/index.jsx +100 -98
  247. package/src/list/ListItem/Action/index.module.less +19 -21
  248. package/src/list/ListItem/img/play.svg +15 -15
  249. package/src/list/ListItem/img/video.svg +3 -0
  250. package/src/list/ListItem/index.jsx +46 -28
  251. package/src/list/ListItem/index.module.less +152 -146
  252. package/src/list/index.module.less +35 -35
  253. package/src/service/common.js +73 -73
  254. package/src/service/content-mgr.js +73 -73
  255. package/src/service/im-group.js +12 -12
  256. package/src/service/im.js +7 -7
  257. package/src/service/imBroadcasts.js +33 -33
  258. package/src/service/like.js +134 -134
  259. package/src/service/manageApi.js +7 -7
  260. package/src/service/managePortal.js +6 -6
  261. package/src/service/myStudyRecord.js +93 -93
  262. package/src/service/navigation.js +47 -47
  263. package/src/service/ndMeeting.js +10 -10
  264. package/src/service/ndr.js +122 -122
  265. package/src/service/news.js +93 -93
  266. package/src/service/personalCenter.js +270 -270
  267. package/src/service/qualityCourse.js +31 -31
  268. package/src/service/recommend.js +81 -81
  269. package/src/service/school.js +323 -323
  270. package/src/service/search.js +170 -170
  271. package/src/service/site-config.js +7 -7
  272. package/src/service/socialCensor.js +45 -45
  273. package/src/service/specialEdu.js +31 -31
  274. package/src/service/suggestion.js +85 -85
  275. package/src/service/syncClassroom.js +321 -321
  276. package/src/service/tCourse.js +204 -204
  277. package/src/service/tchTraining.js +37 -37
  278. package/src/service/uc.js +45 -45
  279. package/src/util/app.js +1 -1
  280. package/src/util/array.js +3 -3
  281. package/src/util/auth/func.js +57 -57
  282. package/src/util/date.js +53 -53
  283. package/src/util/env.js +9 -9
  284. package/src/util/getTimestamp.js +10 -10
  285. package/src/util/hof.js +105 -105
  286. package/src/util/live/index.js +42 -42
  287. package/src/util/netUrl.js +35 -35
  288. package/src/util/object.js +13 -13
  289. package/src/util/push/index.js +58 -58
  290. package/src/util/request.js +227 -227
  291. package/src/util/type.js +17 -17
  292. package/src/util/url.js +203 -203
  293. package/src/util/video.js +26 -26
  294. package/test/setup.js +21 -21
  295. package/test/unit/component/header/logo.spec.jsx +8 -8
  296. package/test/unit/helper.jsx +25 -25
  297. package/test/unit/util/date.spec.js +40 -40
  298. package/dist/XEduLivePlayer.common.js +0 -72605
  299. package/dist/XEduLivePlayerPre.common.js +0 -72625
@@ -1,58 +1,58 @@
1
- import { Loader } from 'fish'
2
- import env from '@/config/env'
3
-
4
- const url = '/3rd/im-web-push/index.umd.js'
5
- const { loadJS } = Loader
6
- let instPromise
7
-
8
- // http://git.sdp.nd/component-h5/web-im-push/-/tree/master
9
- class ImPush {
10
- constructor() {
11
- this.listeners = []
12
- }
13
-
14
- async init() {
15
- if (instPromise) {
16
- return instPromise
17
- }
18
- instPromise = loadJS(url, 'ndiot_push').then(() => {
19
- window.ndiot_push.open({
20
- url: env.push.host,
21
- appId: env.app.appid,
22
- disableSharedWorker: true
23
- })
24
- return window.ndiot_push
25
- })
26
- return instPromise
27
- }
28
-
29
- getInstance() {
30
- return window.ndiot_push
31
- }
32
-
33
- addTopicListener(name, listener) {
34
- const inst = this.getInstance()
35
- this.listeners.push(listener)
36
- inst.subscribe(name)
37
- this.listen()
38
- }
39
-
40
- removeTopicListener(name, listener) {
41
- const inst = this.getInstance()
42
- this.listeners = this.listeners.filter((l) => l !== listener)
43
- inst.unsubscribe(name)
44
- this.listen()
45
- }
46
-
47
- listen() {
48
- const inst = this.getInstance()
49
- inst.onNewMessage = (data) => {
50
- console.log('im-push', data)
51
- this.listeners.forEach((l) => {
52
- l(data)
53
- })
54
- }
55
- }
56
- }
57
-
58
- export default new ImPush()
1
+ import { Loader } from 'fish'
2
+ import env from '@/config/env'
3
+
4
+ const url = '/3rd/im-web-push/index.umd.js'
5
+ const { loadJS } = Loader
6
+ let instPromise
7
+
8
+ // http://git.sdp.nd/component-h5/web-im-push/-/tree/master
9
+ class ImPush {
10
+ constructor() {
11
+ this.listeners = []
12
+ }
13
+
14
+ async init() {
15
+ if (instPromise) {
16
+ return instPromise
17
+ }
18
+ instPromise = loadJS(url, 'ndiot_push').then(() => {
19
+ window.ndiot_push.open({
20
+ url: env.push.host,
21
+ appId: env.app.appid,
22
+ disableSharedWorker: true
23
+ })
24
+ return window.ndiot_push
25
+ })
26
+ return instPromise
27
+ }
28
+
29
+ getInstance() {
30
+ return window.ndiot_push
31
+ }
32
+
33
+ addTopicListener(name, listener) {
34
+ const inst = this.getInstance()
35
+ this.listeners.push(listener)
36
+ inst.subscribe(name)
37
+ this.listen()
38
+ }
39
+
40
+ removeTopicListener(name, listener) {
41
+ const inst = this.getInstance()
42
+ this.listeners = this.listeners.filter((l) => l !== listener)
43
+ inst.unsubscribe(name)
44
+ this.listen()
45
+ }
46
+
47
+ listen() {
48
+ const inst = this.getInstance()
49
+ inst.onNewMessage = (data) => {
50
+ console.log('im-push', data)
51
+ this.listeners.forEach((l) => {
52
+ l(data)
53
+ })
54
+ }
55
+ }
56
+ }
57
+
58
+ export default new ImPush()
@@ -1,227 +1,227 @@
1
- import axios from 'axios'
2
- import queryString from 'query-string'
3
- import { isFunction } from './type'
4
-
5
- const STATUS = {
6
- pending: 1,
7
- finish: 2,
8
- error: 3
9
- }
10
-
11
- export default class Request {
12
- static create(config) {
13
- const cache = {}
14
- const _config = {
15
- withCredentials: false,
16
- loadingTime: 1500,
17
- shouldBlock(error) {
18
- const status = error?.response?.status
19
- return status === 429 || status === 503
20
- },
21
- block(_c) {
22
- return true
23
- },
24
- blockTime: 10 * 60 * 1000,
25
- shouldRetry(error) {
26
- return error?.response?.status >= 500
27
- },
28
- ...config
29
- }
30
- const instance = axios.create(_config)
31
-
32
- const originRequest = instance.request.bind(instance)
33
- let blockRequest = false
34
- let blockError = null
35
-
36
- const handleError = (error, c) => {
37
- // failover 对于一个请求只允许进行一次
38
- if (isFunction(c.failover)) {
39
- const newConfig = c.failover(error, c)
40
- if (typeof newConfig === 'object') {
41
- return instance.request({
42
- ...newConfig,
43
- failover: false
44
- })
45
- }
46
- }
47
-
48
- if (isFunction(c.shouldBlock)) {
49
- blockRequest = c.shouldBlock(error)
50
- blockError = error
51
- setTimeout(() => {
52
- blockRequest = false
53
- blockError = null
54
- }, c.blockTime)
55
- }
56
-
57
- if (c.retry > 0 && c._retry < Math.min(c.retry, 2)) {
58
- if (c.shouldRetry(error)) {
59
- c._retry += 1
60
- return instance.request(c)
61
- }
62
- }
63
- if (isFunction(c.error)) {
64
- c.error(error, c)
65
- }
66
- return Promise.reject(error)
67
- }
68
-
69
- const getKey = ({ key, params, url }) => {
70
- if (key) {
71
- return key
72
- }
73
- const query = queryString.stringify(params)
74
- const _key = `${url}${query ?? `?${query}`}`
75
- return _key
76
- }
77
-
78
- const clearLoadingTimer = (key) => {
79
- if (cache[key]) {
80
- const { timer } = cache[key]
81
- if (timer) {
82
- clearTimeout(timer)
83
- }
84
- }
85
- }
86
-
87
- instance.request = function request(c) {
88
- const key = getKey(c)
89
- c.key = key
90
- const exist = cache[key]
91
- if (exist) {
92
- // get 请求支持缓存
93
- if (c.method === 'get') {
94
- // 同 url 请求还在等待的直接返回,不发起请求
95
- if (exist.status === STATUS.pending) {
96
- return cache[key]._request
97
- }
98
- // 强制请求,直接发起,忽略缓存情况
99
- if (c.force) {
100
- return originRequest(c)
101
- }
102
- // 存在缓存数据,直接使用
103
- if (cache[key].response) {
104
- return Promise.resolve(cache[key].response)
105
- }
106
- }
107
- } else {
108
- const { loading = _config.loading, loadingTime = _config.loadingTime } = c
109
- cache[key] = {
110
- status: STATUS.pending,
111
- timer: setTimeout(() => {
112
- if (isFunction(loading)) {
113
- loading({
114
- ...c,
115
- key
116
- })
117
- }
118
- }, loadingTime)
119
- }
120
- }
121
-
122
- const req = originRequest(c)
123
- cache[key]._request = req
124
- return req
125
- }
126
-
127
- {
128
- const actions = ['delete', 'get', 'head', 'options']
129
- actions.forEach((method) => {
130
- instance[method] = function request(url, c) {
131
- return instance.request({
132
- ...c,
133
- method,
134
- url,
135
- data: (c || {}).data
136
- })
137
- }
138
- })
139
- }
140
-
141
- {
142
- const actions = ['post', 'put', 'patch']
143
- actions.forEach((method) => {
144
- instance[method] = function request(url, data, c) {
145
- return instance.request({
146
- ...c,
147
- method,
148
- url,
149
- data
150
- })
151
- }
152
- })
153
- }
154
-
155
- instance.interceptors.request.use(
156
- (c) => {
157
- if (blockRequest && c.block(c)) {
158
- return Promise.reject(blockError)
159
- }
160
- c._retry = c._retry ?? 0
161
- return c
162
- },
163
- (error) => handleError(error, error.config)
164
- )
165
-
166
- instance.interceptors.response.use(
167
- (response) => {
168
- const c = response.config
169
- const { ttl, method, key } = c
170
- if (key) {
171
- clearLoadingTimer(key)
172
- if (cache[key]) {
173
- cache[key].status = STATUS.finish
174
-
175
- if (method.toLocaleLowerCase() === 'get' && ttl > 0) {
176
- cache[key].response = response
177
- if (cache[key].clearCacheTimer) {
178
- clearTimeout(cache[key].clearCacheTimer)
179
- }
180
- cache[key].clearCacheTimer = setTimeout(() => {
181
- delete cache[key]
182
- }, ttl)
183
- } else {
184
- delete cache[key]
185
- }
186
- }
187
- }
188
- if (isFunction(_config.complete)) {
189
- _config.complete(c)
190
- }
191
- return response
192
- },
193
- (error) => {
194
- const { key } = error.config
195
- if (key) {
196
- clearLoadingTimer(key)
197
- if (cache[key]) {
198
- cache[key].status = STATUS.error
199
- delete cache[key]
200
- }
201
- }
202
- if (isFunction(_config.complete)) {
203
- _config.complete(error.config)
204
- }
205
- return handleError(error, error.config)
206
- }
207
- )
208
-
209
- instance.clear = function clear(url) {
210
- if (url) {
211
- const item = cache[url]
212
- if (item && item.status !== STATUS.pending) {
213
- delete cache[url]
214
- }
215
- } else {
216
- Object.keys(cache).forEach((key) => {
217
- const item = cache[key]
218
- if (item && item.status !== STATUS.pending) {
219
- delete cache[key]
220
- }
221
- })
222
- }
223
- }
224
-
225
- return instance
226
- }
227
- }
1
+ import axios from 'axios'
2
+ import queryString from 'query-string'
3
+ import { isFunction } from './type'
4
+
5
+ const STATUS = {
6
+ pending: 1,
7
+ finish: 2,
8
+ error: 3
9
+ }
10
+
11
+ export default class Request {
12
+ static create(config) {
13
+ const cache = {}
14
+ const _config = {
15
+ withCredentials: false,
16
+ loadingTime: 1500,
17
+ shouldBlock(error) {
18
+ const status = error?.response?.status
19
+ return status === 429 || status === 503
20
+ },
21
+ block(_c) {
22
+ return true
23
+ },
24
+ blockTime: 10 * 60 * 1000,
25
+ shouldRetry(error) {
26
+ return error?.response?.status >= 500
27
+ },
28
+ ...config
29
+ }
30
+ const instance = axios.create(_config)
31
+
32
+ const originRequest = instance.request.bind(instance)
33
+ let blockRequest = false
34
+ let blockError = null
35
+
36
+ const handleError = (error, c) => {
37
+ // failover 对于一个请求只允许进行一次
38
+ if (isFunction(c.failover)) {
39
+ const newConfig = c.failover(error, c)
40
+ if (typeof newConfig === 'object') {
41
+ return instance.request({
42
+ ...newConfig,
43
+ failover: false
44
+ })
45
+ }
46
+ }
47
+
48
+ if (isFunction(c.shouldBlock)) {
49
+ blockRequest = c.shouldBlock(error)
50
+ blockError = error
51
+ setTimeout(() => {
52
+ blockRequest = false
53
+ blockError = null
54
+ }, c.blockTime)
55
+ }
56
+
57
+ if (c.retry > 0 && c._retry < Math.min(c.retry, 2)) {
58
+ if (c.shouldRetry(error)) {
59
+ c._retry += 1
60
+ return instance.request(c)
61
+ }
62
+ }
63
+ if (isFunction(c.error)) {
64
+ c.error(error, c)
65
+ }
66
+ return Promise.reject(error)
67
+ }
68
+
69
+ const getKey = ({ key, params, url }) => {
70
+ if (key) {
71
+ return key
72
+ }
73
+ const query = queryString.stringify(params)
74
+ const _key = `${url}${query ?? `?${query}`}`
75
+ return _key
76
+ }
77
+
78
+ const clearLoadingTimer = (key) => {
79
+ if (cache[key]) {
80
+ const { timer } = cache[key]
81
+ if (timer) {
82
+ clearTimeout(timer)
83
+ }
84
+ }
85
+ }
86
+
87
+ instance.request = function request(c) {
88
+ const key = getKey(c)
89
+ c.key = key
90
+ const exist = cache[key]
91
+ if (exist) {
92
+ // get 请求支持缓存
93
+ if (c.method === 'get') {
94
+ // 同 url 请求还在等待的直接返回,不发起请求
95
+ if (exist.status === STATUS.pending) {
96
+ return cache[key]._request
97
+ }
98
+ // 强制请求,直接发起,忽略缓存情况
99
+ if (c.force) {
100
+ return originRequest(c)
101
+ }
102
+ // 存在缓存数据,直接使用
103
+ if (cache[key].response) {
104
+ return Promise.resolve(cache[key].response)
105
+ }
106
+ }
107
+ } else {
108
+ const { loading = _config.loading, loadingTime = _config.loadingTime } = c
109
+ cache[key] = {
110
+ status: STATUS.pending,
111
+ timer: setTimeout(() => {
112
+ if (isFunction(loading)) {
113
+ loading({
114
+ ...c,
115
+ key
116
+ })
117
+ }
118
+ }, loadingTime)
119
+ }
120
+ }
121
+
122
+ const req = originRequest(c)
123
+ cache[key]._request = req
124
+ return req
125
+ }
126
+
127
+ {
128
+ const actions = ['delete', 'get', 'head', 'options']
129
+ actions.forEach((method) => {
130
+ instance[method] = function request(url, c) {
131
+ return instance.request({
132
+ ...c,
133
+ method,
134
+ url,
135
+ data: (c || {}).data
136
+ })
137
+ }
138
+ })
139
+ }
140
+
141
+ {
142
+ const actions = ['post', 'put', 'patch']
143
+ actions.forEach((method) => {
144
+ instance[method] = function request(url, data, c) {
145
+ return instance.request({
146
+ ...c,
147
+ method,
148
+ url,
149
+ data
150
+ })
151
+ }
152
+ })
153
+ }
154
+
155
+ instance.interceptors.request.use(
156
+ (c) => {
157
+ if (blockRequest && c.block(c)) {
158
+ return Promise.reject(blockError)
159
+ }
160
+ c._retry = c._retry ?? 0
161
+ return c
162
+ },
163
+ (error) => handleError(error, error.config)
164
+ )
165
+
166
+ instance.interceptors.response.use(
167
+ (response) => {
168
+ const c = response.config
169
+ const { ttl, method, key } = c
170
+ if (key) {
171
+ clearLoadingTimer(key)
172
+ if (cache[key]) {
173
+ cache[key].status = STATUS.finish
174
+
175
+ if (method.toLocaleLowerCase() === 'get' && ttl > 0) {
176
+ cache[key].response = response
177
+ if (cache[key].clearCacheTimer) {
178
+ clearTimeout(cache[key].clearCacheTimer)
179
+ }
180
+ cache[key].clearCacheTimer = setTimeout(() => {
181
+ delete cache[key]
182
+ }, ttl)
183
+ } else {
184
+ delete cache[key]
185
+ }
186
+ }
187
+ }
188
+ if (isFunction(_config.complete)) {
189
+ _config.complete(c)
190
+ }
191
+ return response
192
+ },
193
+ (error) => {
194
+ const { key } = error.config
195
+ if (key) {
196
+ clearLoadingTimer(key)
197
+ if (cache[key]) {
198
+ cache[key].status = STATUS.error
199
+ delete cache[key]
200
+ }
201
+ }
202
+ if (isFunction(_config.complete)) {
203
+ _config.complete(error.config)
204
+ }
205
+ return handleError(error, error.config)
206
+ }
207
+ )
208
+
209
+ instance.clear = function clear(url) {
210
+ if (url) {
211
+ const item = cache[url]
212
+ if (item && item.status !== STATUS.pending) {
213
+ delete cache[url]
214
+ }
215
+ } else {
216
+ Object.keys(cache).forEach((key) => {
217
+ const item = cache[key]
218
+ if (item && item.status !== STATUS.pending) {
219
+ delete cache[key]
220
+ }
221
+ })
222
+ }
223
+ }
224
+
225
+ return instance
226
+ }
227
+ }
package/src/util/type.js CHANGED
@@ -1,17 +1,17 @@
1
- export function isFunction(v) {
2
- return typeof v === 'function'
3
- }
4
-
5
- export function isIE() {
6
- return /msie|trident/.test(window.navigator.userAgent.toLowerCase())
7
- }
8
-
9
- export function isWin7() {
10
- return navigator.userAgent.indexOf('Windows NT 6.1') >= 0
11
- }
12
-
13
- export function isH5() {
14
- return window.navigator.userAgent.match(
15
- /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
16
- )
17
- }
1
+ export function isFunction(v) {
2
+ return typeof v === 'function'
3
+ }
4
+
5
+ export function isIE() {
6
+ return /msie|trident/.test(window.navigator.userAgent.toLowerCase())
7
+ }
8
+
9
+ export function isWin7() {
10
+ return navigator.userAgent.indexOf('Windows NT 6.1') >= 0
11
+ }
12
+
13
+ export function isH5() {
14
+ return window.navigator.userAgent.match(
15
+ /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
16
+ )
17
+ }