@mpxjs/api-proxy 2.8.40-test.1 → 2.8.40-test.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/api-proxy",
3
- "version": "2.8.40-test.1",
3
+ "version": "2.8.40-test.2",
4
4
  "description": "convert miniprogram API at each end",
5
5
  "module": "src/index.js",
6
6
  "types": "@types/index.d.ts",
@@ -31,7 +31,7 @@
31
31
  "promise"
32
32
  ],
33
33
  "author": "httpsxiao",
34
- "license": "ISC",
34
+ "license": "Apache-2.0",
35
35
  "bugs": {
36
36
  "url": "https://github.com/didi/mpx/issues"
37
37
  },
@@ -39,5 +39,5 @@
39
39
  "dependencies": {
40
40
  "axios": "^0.21.1"
41
41
  },
42
- "gitHead": "e636ed32cfc88787493c67c8bc27fd3af55f6f22"
42
+ "gitHead": "7b1911025a2eb47eddc046775f17b6f1405fdd54"
43
43
  }
@@ -129,6 +129,9 @@ function makeMap (arr) {
129
129
 
130
130
  const isBrowser = typeof window !== 'undefined'
131
131
 
132
+ function throwSSRWarning (info) {
133
+ console.error(`[Mpx runtime error]: Dangerous API! ${info}, It may cause some problems, please use this method with caution`)
134
+ }
132
135
  export {
133
136
  changeOpts,
134
137
  handleSuccess,
@@ -141,5 +144,6 @@ export {
141
144
  noop,
142
145
  makeMap,
143
146
  isBrowser,
144
- hasOwn
147
+ hasOwn,
148
+ throwSSRWarning
145
149
  }
@@ -26,13 +26,46 @@ function isTabBarPage (url, router) {
26
26
  function createDom (tag, attrs = {}, children = []) {
27
27
  const dom = document.createElement(tag)
28
28
  Object.keys(attrs).forEach(k => dom.setAttribute(k, attrs[k]))
29
- children.length && children.forEach(child => dom.appendChild(child))
29
+ children.length && children.forEach(child => dom.appendChild(typeof child === 'string' ? document.createTextNode(child) : child))
30
30
  return dom
31
31
  }
32
32
 
33
+ // 在H5中,直接绑定 click 可能出现延时问题,很多点击可以关闭的组件被唤出之后,有概率立马触发点击事件,导致组件被关闭。
34
+ // 使用该方法通过 touchstart 和 touchend 模拟 click 事件,解决延时问题。
35
+ function bindTap (dom, handler) {
36
+ let startTime = 0; let x = 0; let y = 0
37
+ const touchStart = (e) => {
38
+ startTime = Date.now()
39
+ x = e.touches[0].pageX
40
+ y = e.touches[0].pageY
41
+ }
42
+ const touchEnd = (e) => {
43
+ if (Date.now() - startTime < 300 && Math.abs(e.changedTouches[0].pageX - x) < 10 && Math.abs(e.changedTouches[0].pageY - y) < 10) {
44
+ handler(e)
45
+ }
46
+ }
47
+ dom.addEventListener('touchstart', touchStart)
48
+ dom.addEventListener('touchend', touchEnd)
49
+ return () => {
50
+ dom.removeEventListener('touchstart', touchStart)
51
+ dom.removeEventListener('touchend', touchEnd)
52
+ }
53
+ }
54
+
55
+ /**
56
+ * 获取弹窗应当挂载的根节点
57
+ * @returns dom
58
+ */
59
+ function getRootElement () {
60
+ const page = getCurrentPages().slice(-1)[0]?.$el
61
+ return page || document.body
62
+ }
63
+
33
64
  export {
34
65
  webHandleSuccess,
35
66
  webHandleFail,
36
67
  createDom,
68
+ bindTap,
69
+ getRootElement,
37
70
  isTabBarPage
38
71
  }
@@ -9,12 +9,14 @@
9
9
  z-index 1000
10
10
  .__mpx_preview_tip__
11
11
  position absolute
12
- top 0
12
+ top 7px
13
13
  left 50%
14
14
  transform translateX(-50%)
15
- color white
16
- font-size 48px
17
- letter-spacing 0.3em
15
+ color #fff
16
+ font-size 18px
17
+ letter-spacing .3em
18
+ text-align center
19
+ width 100%
18
20
  .__mpx_preview_images__
19
21
  display flex
20
22
  width 100%
@@ -1,8 +1,9 @@
1
- import { webHandleSuccess, webHandleFail } from '../../../common/js'
1
+ import { webHandleSuccess, webHandleFail, createDom, bindTap, getRootElement } from '../../../common/js'
2
2
  import '../../../common/stylus/ActionSheet.styl'
3
3
 
4
4
  export default class ActionSheet {
5
5
  constructor () {
6
+ // super()
6
7
  this.defaultOpts = {
7
8
  itemList: [],
8
9
  itemColor: '#000000',
@@ -11,37 +12,20 @@ export default class ActionSheet {
11
12
  complete: null
12
13
  }
13
14
  this.hideTimer = null
14
-
15
- const actionSheet = document.createElement('div')
16
- actionSheet.setAttribute('class', '__mpx_actionsheet__')
17
-
18
- const mask = document.createElement('div')
19
- mask.setAttribute('class', '__mpx_mask__')
20
-
21
- const box = document.createElement('div')
22
- box.setAttribute('class', '__mpx_actionsheet_box__')
23
-
24
- const list = document.createElement('div')
25
- list.setAttribute('class', '__mpx_actionsheet_list__')
26
-
27
- const cancelBtn = document.createElement('div')
28
- cancelBtn.setAttribute('class', '__mpx_actionsheet_cancel__')
29
- cancelBtn.textContent = '取消'
30
-
31
- box.appendChild(list)
32
- box.appendChild(cancelBtn)
33
- actionSheet.appendChild(mask)
34
- actionSheet.appendChild(box)
35
- document.body.appendChild(actionSheet)
36
-
37
- this.actionSheet = actionSheet
38
- this.mask = mask
39
- this.box = box
40
- this.list = list
41
- this.cancelBtn = cancelBtn
15
+ // 临时绑定事件的解绑方法数组,用于在 hide 时解绑
16
+ this.tempListeners = []
17
+
18
+ this.actionSheet = createDom('div', { class: '__mpx_actionsheet__' }, [
19
+ this.mask = createDom('div', { class: '__mpx_mask__' }),
20
+ this.box = createDom('div', { class: '__mpx_actionsheet_box__' }, [
21
+ this.list = createDom('div', { class: '__mpx_actionsheet_list__' }),
22
+ this.cancelBtn = createDom('div', { class: '__mpx_actionsheet_cancel__' }, ['取消'])
23
+ ])
24
+ ])
42
25
  }
43
26
 
44
27
  show (options) {
28
+ getRootElement().appendChild(this.actionSheet) // show 则挂载
45
29
  if (this.hideTimer) {
46
30
  clearTimeout(this.hideTimer)
47
31
  this.hideTimer = null
@@ -49,21 +33,20 @@ export default class ActionSheet {
49
33
 
50
34
  const opts = Object.assign({}, this.defaultOpts, options)
51
35
 
52
- const list = document.createElement('div')
53
- list.setAttribute('class', '__mpx_actionsheet_list__')
36
+ const list = createDom('div', { class: '__mpx_actionsheet_list__' })
54
37
 
38
+ // todo 使用事件代理
55
39
  opts.itemList.forEach((item, index) => {
56
- const sheet = document.createElement('div')
57
- sheet.setAttribute('class', '__mpx_actionsheet_sheet__')
58
- sheet.textContent = item
59
- sheet.onclick = () => {
40
+ const sheet = createDom('div', { class: '__mpx_actionsheet_sheet__' }, [item])
41
+ this.tempListeners.push(bindTap(sheet, () => {
60
42
  this.hide()
61
43
  const res = {
62
44
  errMsg: 'showActionSheet:ok',
63
45
  tapIndex: index
64
46
  }
65
47
  webHandleSuccess(res, opts.success, opts.complete)
66
- }
48
+ // this.toPromiseResolve(res)
49
+ }))
67
50
  list.appendChild(sheet)
68
51
  })
69
52
 
@@ -71,14 +54,20 @@ export default class ActionSheet {
71
54
  this.list = list
72
55
  this.list.style.color = opts.itemColor
73
56
 
74
- this.cancelBtn.onclick = () => {
57
+ this.tempListeners.push(bindTap(this.cancelBtn, () => {
75
58
  this.hide()
76
59
  const err = { errMsg: 'showActionSheet:fail cancel' }
77
60
  webHandleFail(err, opts.fail, opts.complete)
78
- }
79
-
80
- this.box.classList.add('show')
61
+ // !opts.fail && this.toPromiseReject(err)
62
+ }))
63
+ // make transition next frame
81
64
  this.actionSheet.classList.add('show')
65
+ // 如果使用 requestAnimationFrame,第一次展示不会有动画效果,原因待确认,这里先使用 setTimeout
66
+ setTimeout(() => {
67
+ this.box.classList.add('show')
68
+ }, 17)
69
+
70
+ // return this.toPromiseInitPromise()
82
71
  }
83
72
 
84
73
  hide () {
@@ -86,10 +75,12 @@ export default class ActionSheet {
86
75
  clearTimeout(this.hideTimer)
87
76
  this.hideTimer = null
88
77
  }
89
-
78
+ this.tempListeners.forEach(unbind => unbind())
79
+ this.tempListeners = []
80
+ this.box.classList.remove('show')
90
81
  this.hideTimer = setTimeout(() => {
91
- this.box.classList.remove('show')
92
82
  this.actionSheet.classList.remove('show')
93
- }, 0)
83
+ this.actionSheet.remove() // hide 则卸载
84
+ }, 300) // animation duration is 300ms
94
85
  }
95
86
  }
@@ -1,8 +1,13 @@
1
1
  import ActionSheet from './ActionSheet'
2
+ import { isBrowser, throwSSRWarning } from '../../../common/js'
2
3
 
3
4
  let actionSheet = null
4
5
 
5
6
  function showActionSheet (options = { itemList: [] }) {
7
+ if (!isBrowser) {
8
+ throwSSRWarning('showActionSheet API is running in non browser environments')
9
+ return
10
+ }
6
11
  if (!actionSheet) { actionSheet = new ActionSheet() }
7
12
  return actionSheet.show(options)
8
13
  }
@@ -1,3 +1,5 @@
1
+ import { isBrowser, throwSSRWarning } from '../../../common/js'
2
+
1
3
  class Animation {
2
4
  constructor (options) {
3
5
  this._actions = []
@@ -5,11 +7,15 @@ class Animation {
5
7
  this._options = options
6
8
  }
7
9
 
8
- _processSize (size) {
10
+ _processSize (size, type) {
9
11
  if (typeof size === 'number') {
10
12
  return `${size}px`
11
13
  } else {
12
14
  if (size.indexOf('rpx') !== -1) {
15
+ if (!isBrowser) {
16
+ throwSSRWarning(`Animation's ${type} API cannot use rpx in non browser environments`)
17
+ return
18
+ }
13
19
  // 计算rpx折算回px
14
20
  const rs = parseInt(size, 10)
15
21
  const width = window.screen.width
@@ -29,7 +35,7 @@ class Animation {
29
35
  case 'bottom':
30
36
  case 'width':
31
37
  case 'height':
32
- value = this._processSize(value)
38
+ value = this._processSize(value, type)
33
39
  this._propMaps[type] = {
34
40
  args: [type, value],
35
41
  type: 'style'
@@ -1,3 +1,4 @@
1
+ import { isBrowser } from '../../../common/js'
1
2
  global.__mpxAppCbs = global.__mpxAppCbs || {
2
3
  show: [],
3
4
  hide: [],
@@ -6,7 +7,9 @@ global.__mpxAppCbs = global.__mpxAppCbs || {
6
7
  }
7
8
 
8
9
  function onError (callback) {
9
- global.__mpxAppCbs.error.push(callback)
10
+ if (isBrowser) {
11
+ global.__mpxAppCbs.error.push(callback)
12
+ }
10
13
  }
11
14
 
12
15
  function offError (callback) {
@@ -16,7 +19,9 @@ function offError (callback) {
16
19
  }
17
20
 
18
21
  function onAppShow (callback) {
19
- global.__mpxAppCbs.show.push(callback)
22
+ if (isBrowser) {
23
+ global.__mpxAppCbs.show.push(callback)
24
+ }
20
25
  }
21
26
 
22
27
  function offAppShow (callback) {
@@ -26,7 +31,9 @@ function offAppShow (callback) {
26
31
  }
27
32
 
28
33
  function onAppHide (callback) {
29
- global.__mpxAppCbs.hide.push(callback)
34
+ if (isBrowser) {
35
+ global.__mpxAppCbs.hide.push(callback)
36
+ }
30
37
  }
31
38
 
32
39
  function offAppHide (callback) {
@@ -1,4 +1,9 @@
1
+ import { isBrowser, throwSSRWarning } from '../../../common/js'
1
2
  export const createInnerAudioContext = () => {
3
+ if (!isBrowser) {
4
+ throwSSRWarning('createInnerAudioContext API is running in non browser environments')
5
+ return
6
+ }
2
7
  // eslint-disable-next-line no-undef
3
8
  const audio = new Audio()
4
9
  const __audio = {}
@@ -1,6 +1,11 @@
1
1
  import WebIntersectionObserver from './IntersectionObserver'
2
+ import { isBrowser, throwSSRWarning } from '../../../common/js'
2
3
 
3
4
  function createIntersectionObserver (component, options) {
5
+ if (!isBrowser) {
6
+ throwSSRWarning('createIntersectionObserver API is running in non browser environments')
7
+ return
8
+ }
4
9
  return new WebIntersectionObserver(component, options)
5
10
  }
6
11
 
@@ -1,6 +1,11 @@
1
1
  import SelectQuery from './SelectQuery'
2
+ import { isBrowser, throwSSRWarning } from '../../../common/js'
2
3
 
3
4
  function createSelectorQuery () {
5
+ if (!isBrowser) {
6
+ throwSSRWarning('createSelectorQuery API is running in non browser environments')
7
+ return
8
+ }
4
9
  return new SelectQuery()
5
10
  }
6
11
 
@@ -1,11 +1,15 @@
1
- import { webHandleSuccess, webHandleFail } from '../../../../common/js'
1
+ import { webHandleSuccess, webHandleFail, isBrowser, throwSSRWarning } from '../../../../common/js'
2
2
 
3
3
  export function getNetworkType ({ success, fail = () => {}, complete = () => {} }) {
4
+ if (!isBrowser) {
5
+ throwSSRWarning('getNetworkType API is running in non browser environments')
6
+ return
7
+ }
4
8
  try {
5
9
  if (navigator.connection) {
6
10
  webHandleSuccess({ networkType: navigator.connection.effectiveType }, success, complete)
7
11
  } else {
8
- webHandleSuccess({ networkType: 'unknow' }, success, complete)
12
+ webHandleSuccess({ networkType: 'unknown' }, success, complete)
9
13
  }
10
14
  } catch (err) {
11
15
  webHandleFail(err, fail, complete)
@@ -1,4 +1,4 @@
1
- import { isBrowser } from '../../../../common/js/utils'
1
+ import { isBrowser, throwSSRWarning } from '../../../../common/js/utils'
2
2
  const fnMap = new Map()
3
3
 
4
4
  const oldObserveList = new Set()
@@ -13,6 +13,10 @@ if (isBrowser) {
13
13
  }
14
14
 
15
15
  export function onNetworkStatusChange (callbackFn) {
16
+ if (!isBrowser) {
17
+ throwSSRWarning('onNetworkStatusChange API is running in non browser environments')
18
+ return
19
+ }
16
20
  if (navigator.connection) {
17
21
  const proxyCallback = evt => {
18
22
  const isConnected = navigator.onLine
@@ -29,6 +33,10 @@ export function onNetworkStatusChange (callbackFn) {
29
33
  }
30
34
 
31
35
  export function offNetworkStatusChange (callbackFn) {
36
+ if (!isBrowser) {
37
+ throwSSRWarning('offNetworkStatusChange API is running in non browser environments')
38
+ return
39
+ }
32
40
  if (navigator.connection) {
33
41
  navigator.connection.removeEventListener('change', fnMap.get(callbackFn))
34
42
  } else {
@@ -1,4 +1,4 @@
1
- import { webHandleSuccess, webHandleFail, createDom, warn } from '../../../common/js'
1
+ import { webHandleSuccess, webHandleFail, createDom, warn, bindTap, getRootElement } from '../../../common/js'
2
2
  import '../../../common/stylus/Preview.styl'
3
3
  /**
4
4
  * Preview class for displaying images in a slideshow format.
@@ -12,7 +12,6 @@ export default class Preview {
12
12
  this.preview = createDom('div', { class: '__mpx_preview__' }, [
13
13
  this.textTip = createDom('div', { class: '__mpx_preview_tip__' })
14
14
  ])
15
- document.body.appendChild(this.preview)
16
15
  this.initEvent()
17
16
  }
18
17
 
@@ -41,9 +40,11 @@ export default class Preview {
41
40
  this.updateTextTip()
42
41
  })
43
42
  // click to close
44
- this.preview.addEventListener('click', () => {
43
+ bindTap(this.preview, () => {
44
+ this.currentIndex = 0
45
45
  this.preview.style.display = 'none'
46
46
  this.preview.querySelector('.__mpx_preview_images__').remove()
47
+ this.preview.remove()
47
48
  })
48
49
  }
49
50
 
@@ -57,6 +58,7 @@ export default class Preview {
57
58
  Object.keys(options).forEach(key => !supported.includes(key) && warn(`previewImage: 暂不支持选项 ${key} !`))
58
59
  const { urls, success, fail, complete } = options
59
60
  try {
61
+ getRootElement().appendChild(this.preview)
60
62
  this.preview.style.display = 'block'
61
63
  // create images with urls
62
64
  // append to preview
@@ -1,4 +1,4 @@
1
- import { webHandleSuccess } from '../../../common/js'
1
+ import { createDom, getRootElement, webHandleSuccess } from '../../../common/js'
2
2
  import '../../../common/stylus/Modal.styl'
3
3
  // import { forEach } from '@didi/mpx-fetch/src/util'
4
4
  // 汉字为两个字符,字母/数字为一个字符
@@ -27,52 +27,24 @@ export default class Modal {
27
27
  fail: (...args) => {},
28
28
  complete: (...args) => {}
29
29
  }
30
- this.hideTimer = null
31
-
32
- const modal = document.createElement('div')
33
- modal.setAttribute('class', '__mpx_modal__')
34
-
35
- const mask = document.createElement('div')
36
- mask.setAttribute('class', '__mpx_mask__')
37
-
38
- const box = document.createElement('div')
39
- box.setAttribute('class', '__mpx_modal_box__')
40
-
41
- const title = document.createElement('div')
42
- title.setAttribute('class', '__mpx_modal_title__')
43
30
 
44
- const content = document.createElement('div')
45
- content.setAttribute('class', '__mpx_modal_content__')
46
-
47
- const btns = document.createElement('div')
48
- btns.setAttribute('class', '__mpx_modal_btns__')
49
-
50
- const cancelBtn = document.createElement('div')
51
- cancelBtn.setAttribute('class', '__mpx_modal_cancel__')
52
-
53
- const confirmBtn = document.createElement('div')
54
- confirmBtn.setAttribute('class', '__mpx_modal_confirm__')
55
-
56
- btns.appendChild(cancelBtn)
57
- btns.appendChild(confirmBtn)
58
- box.appendChild(title)
59
- box.appendChild(content)
60
- box.appendChild(btns)
61
- modal.appendChild(mask)
62
- modal.appendChild(box)
63
- document.body.appendChild(modal)
31
+ this.hideTimer = null
64
32
 
65
- this.modal = modal
66
- this.mask = mask
67
- this.box = box
68
- this.title = title
69
- this.content = content
70
- this.btns = btns
71
- this.cancelBtn = cancelBtn
72
- this.confirmBtn = confirmBtn
33
+ this.modal = createDom('div', { class: '__mpx_modal__' }, [
34
+ this.mask = createDom('div', { class: '__mpx_mask__' }),
35
+ this.box = createDom('div', { class: '__mpx_modal_box__' }, [
36
+ this.title = createDom('div', { class: '__mpx_modal_title__' }),
37
+ this.content = createDom('div', { class: '__mpx_modal_content__' }),
38
+ this.btns = createDom('div', { class: '__mpx_modal_btns__' }, [
39
+ this.cancelBtn = createDom('div', { class: '__mpx_modal_cancel__' }),
40
+ this.confirmBtn = createDom('div', { class: '__mpx_modal_confirm__' })
41
+ ])
42
+ ])
43
+ ])
73
44
  }
74
45
 
75
46
  show (options = {}) {
47
+ getRootElement().appendChild(this.modal)
76
48
  if (options.confirmText && _getLength(options.confirmText) > 8) {
77
49
  // eslint-disable-next-line
78
50
  return Promise.reject({errMsg: 'showModal:fail confirmText length should not larger than 4 Chinese characters'})
@@ -131,6 +103,7 @@ export default class Modal {
131
103
 
132
104
  this.hideTimer = setTimeout(() => {
133
105
  this.modal.classList.remove('show')
106
+ this.modal.remove()
134
107
  }, 0)
135
108
  }
136
109
  }
@@ -1,8 +1,13 @@
1
1
  import Modal from './Modal'
2
+ import { isBrowser, throwSSRWarning } from '../../../common/js'
2
3
 
3
4
  let modal = null
4
5
 
5
6
  function showModal (options = {}) {
7
+ if (!isBrowser) {
8
+ throwSSRWarning('showModal API is running in non browser environments')
9
+ return
10
+ }
6
11
  if (!modal) { modal = new Modal() }
7
12
  return modal.show(options)
8
13
  }
@@ -1,7 +1,11 @@
1
- import { webHandleSuccess, webHandleFail } from '../../../common/js'
1
+ import { webHandleSuccess, webHandleFail, isBrowser, throwSSRWarning } from '../../../common/js'
2
2
  import { nextTick } from '../next-tick'
3
3
 
4
4
  export function pageScrollTo (options) {
5
+ if (!isBrowser) {
6
+ throwSSRWarning('pageScrollTo API is running in non browser environments')
7
+ return
8
+ }
5
9
  nextTick(() => {
6
10
  const ms = global.__ms
7
11
  const { success, fail, complete } = options
@@ -1,6 +1,10 @@
1
- import { webHandleSuccess, webHandleFail } from '../../../common/js'
1
+ import { webHandleSuccess, webHandleFail, throwSSRWarning, isBrowser } from '../../../common/js'
2
2
 
3
3
  function stopPullDownRefresh (options = {}) {
4
+ if (!isBrowser) {
5
+ throwSSRWarning('stopPullDownRefresh API is running in non browser environments')
6
+ return
7
+ }
4
8
  const router = global.__mpxRouter
5
9
  if (router) {
6
10
  let err
@@ -27,6 +31,10 @@ function stopPullDownRefresh (options = {}) {
27
31
  }
28
32
 
29
33
  function startPullDownRefresh (options = {}) {
34
+ if (!isBrowser) {
35
+ throwSSRWarning('startPullDownRefresh API is running in non browser environments')
36
+ return
37
+ }
30
38
  const router = global.__mpxRouter
31
39
  if (router) {
32
40
  let err
@@ -1,9 +1,13 @@
1
- import { webHandleSuccess, webHandleFail, isTabBarPage } from '../../../common/js'
1
+ import { webHandleSuccess, webHandleFail, isTabBarPage, throwSSRWarning, isBrowser } from '../../../common/js'
2
2
  import { EventChannel } from '../event-channel'
3
3
 
4
4
  let routeCount = 0
5
5
 
6
6
  function redirectTo (options = {}) {
7
+ if (!isBrowser) {
8
+ throwSSRWarning('redirectTo API is running in non browser environments')
9
+ return
10
+ }
7
11
  const router = global.__mpxRouter
8
12
  if (router) {
9
13
  if (isTabBarPage(options.url, router)) {
@@ -33,6 +37,10 @@ function redirectTo (options = {}) {
33
37
  }
34
38
 
35
39
  function navigateTo (options = {}) {
40
+ if (!isBrowser) {
41
+ throwSSRWarning('navigateTo API is running in non browser environments')
42
+ return
43
+ }
36
44
  const router = global.__mpxRouter
37
45
  if (router) {
38
46
  if (isTabBarPage(options.url, router)) {
@@ -69,6 +77,10 @@ function navigateTo (options = {}) {
69
77
  }
70
78
 
71
79
  function navigateBack (options = {}) {
80
+ if (!isBrowser) {
81
+ throwSSRWarning('navigateBack API is running in non browser environments')
82
+ return
83
+ }
72
84
  const router = global.__mpxRouter
73
85
  if (router) {
74
86
  let delta = options.delta || 1
@@ -87,6 +99,10 @@ function navigateBack (options = {}) {
87
99
  }
88
100
 
89
101
  function reLaunch (options = {}) {
102
+ if (!isBrowser) {
103
+ throwSSRWarning('reLaunch API is running in non browser environments')
104
+ return
105
+ }
90
106
  const router = global.__mpxRouter
91
107
  if (router) {
92
108
  if (routeCount === 0 && router.currentRoute.query.routeCount) routeCount = router.currentRoute.query.routeCount
@@ -125,6 +141,10 @@ function reLaunch (options = {}) {
125
141
  }
126
142
 
127
143
  function switchTab (options = {}) {
144
+ if (!isBrowser) {
145
+ throwSSRWarning('switchTab API is running in non browser environments')
146
+ return
147
+ }
128
148
  const router = global.__mpxRouter
129
149
  if (router) {
130
150
  const toRoute = router.match(options.url, router.history.current)
@@ -1,6 +1,10 @@
1
- import { webHandleSuccess } from '../../../common/js'
1
+ import { isBrowser, throwSSRWarning, webHandleSuccess } from '../../../common/js'
2
2
 
3
3
  function setNavigationBarTitle (options = {}) {
4
+ if (!isBrowser) {
5
+ throwSSRWarning('setNavigationBarTitle API is running in non browser environments')
6
+ return
7
+ }
4
8
  const { title, success, complete } = options
5
9
  if (document.title !== title) {
6
10
  document.title = title
@@ -10,6 +14,10 @@ function setNavigationBarTitle (options = {}) {
10
14
  }
11
15
 
12
16
  function setNavigationBarColor (options = {}) {
17
+ if (!isBrowser) {
18
+ throwSSRWarning('setNavigationBarColor API is running in non browser environments')
19
+ return
20
+ }
13
21
  const { backgroundColor, success, complete } = options
14
22
  const meta = document.createElement('meta')
15
23
  meta.setAttribute('name', 'theme-color')
@@ -1,7 +1,11 @@
1
- import { warn, webHandleSuccess, webHandleFail } from '../../../common/js'
1
+ import { warn, webHandleSuccess, webHandleFail, isBrowser, throwSSRWarning } from '../../../common/js'
2
2
  import SocketTask from './SocketTask'
3
3
 
4
4
  function connectSocket (options = { url: '' }) {
5
+ if (!isBrowser) {
6
+ throwSSRWarning('connectSocket API is running in non browser environments')
7
+ return
8
+ }
5
9
  const { url, protocols, success, fail, complete } = options
6
10
 
7
11
  try {
@@ -1,6 +1,10 @@
1
- import { webHandleSuccess, webHandleFail, hasOwn } from '../../../common/js'
1
+ import { webHandleSuccess, webHandleFail, hasOwn, isBrowser, throwSSRWarning } from '../../../common/js'
2
2
 
3
3
  function setStorage (options = {}) {
4
+ if (!isBrowser) {
5
+ throwSSRWarning('setStorage API is running in non browser environments')
6
+ return
7
+ }
4
8
  const { key, data, success, fail, complete } = options
5
9
 
6
10
  try {
@@ -15,6 +19,10 @@ function setStorage (options = {}) {
15
19
  }
16
20
 
17
21
  function setStorageSync (key = '', data) {
22
+ if (!isBrowser) {
23
+ throwSSRWarning('setStorageSync API is running in non browser environments')
24
+ return
25
+ }
18
26
  let obj = {}
19
27
 
20
28
  if (typeof data === 'symbol') {
@@ -26,6 +34,10 @@ function setStorageSync (key = '', data) {
26
34
  }
27
35
 
28
36
  function getStorage (options = {}) {
37
+ if (!isBrowser) {
38
+ throwSSRWarning('getStorage API is running in non browser environments')
39
+ return
40
+ }
29
41
  const { key, success, fail, complete } = options
30
42
  const { result, data } = getItem(key)
31
43
 
@@ -39,6 +51,10 @@ function getStorage (options = {}) {
39
51
  }
40
52
 
41
53
  function getStorageSync (key) {
54
+ if (!isBrowser) {
55
+ throwSSRWarning('getStorageSync API is running in non browser environments')
56
+ return
57
+ }
42
58
  const res = getItem(key)
43
59
  if (res.result) return res.data
44
60
 
@@ -60,6 +76,10 @@ function getItem (key) {
60
76
  }
61
77
 
62
78
  function getStorageInfo (options = {}) {
79
+ if (!isBrowser) {
80
+ throwSSRWarning('getStorageInfo API is running in non browser environments')
81
+ return
82
+ }
63
83
  const { success, fail, complete } = options
64
84
 
65
85
  try {
@@ -74,6 +94,10 @@ function getStorageInfo (options = {}) {
74
94
  }
75
95
 
76
96
  function getStorageInfoSync () {
97
+ if (!isBrowser) {
98
+ throwSSRWarning('getStorageInfoSync API is running in non browser environments')
99
+ return
100
+ }
77
101
  return {
78
102
  keys: Object.keys(window.localStorage),
79
103
  limitSize: null,
@@ -82,6 +106,10 @@ function getStorageInfoSync () {
82
106
  }
83
107
 
84
108
  function removeStorage (options = { key: '' }) {
109
+ if (!isBrowser) {
110
+ throwSSRWarning('removeStorage API is running in non browser environments')
111
+ return
112
+ }
85
113
  const { key, success, fail, complete } = options
86
114
 
87
115
  try {
@@ -96,10 +124,18 @@ function removeStorage (options = { key: '' }) {
96
124
  }
97
125
 
98
126
  function removeStorageSync (key) {
127
+ if (!isBrowser) {
128
+ throwSSRWarning('getStorageInfoSync API is running in non browser environments')
129
+ return
130
+ }
99
131
  window.localStorage.removeItem(key)
100
132
  }
101
133
 
102
134
  function clearStorage (options = {}) {
135
+ if (!isBrowser) {
136
+ throwSSRWarning('clearStorage API is running in non browser environments')
137
+ return
138
+ }
103
139
  const { success, fail, complete } = options
104
140
 
105
141
  try {
@@ -114,6 +150,10 @@ function clearStorage (options = {}) {
114
150
  }
115
151
 
116
152
  function clearStorageSync () {
153
+ if (!isBrowser) {
154
+ throwSSRWarning('clearStorageSync API is running in non browser environments')
155
+ return
156
+ }
117
157
  window.localStorage.clear()
118
158
  }
119
159
 
@@ -1,6 +1,10 @@
1
- import { webHandleSuccess } from '../../../common/js'
1
+ import { isBrowser, throwSSRWarning, webHandleSuccess } from '../../../common/js'
2
2
 
3
3
  function getSystemInfoSync () {
4
+ if (!isBrowser) {
5
+ throwSSRWarning('getSystemInfoSync API is running in non browser environments')
6
+ return
7
+ }
4
8
  const ua = navigator.userAgent.split('(')[1].split(')')[0]
5
9
  const phones = new Map([
6
10
  ['iPhone', /iPhone|iPad|iPod|iOS/i],
@@ -66,6 +70,10 @@ function getSystemInfoSync () {
66
70
  }
67
71
 
68
72
  function getSystemInfo (options = {}) {
73
+ if (!isBrowser) {
74
+ throwSSRWarning('getSystemInfo API is running in non browser environments')
75
+ return
76
+ }
69
77
  const info = getSystemInfoSync()
70
78
  const res = Object.assign({ errMsg: 'getSystemInfo:ok' }, info)
71
79
  webHandleSuccess(res, options.success, options.complete)
@@ -1,4 +1,4 @@
1
- import { webHandleSuccess, createDom } from '../../../common/js'
1
+ import { webHandleSuccess, createDom, getRootElement } from '../../../common/js'
2
2
  import '../../../common/stylus/Toast.styl'
3
3
  import '../../../common/stylus/Loading.styl'
4
4
 
@@ -30,11 +30,10 @@ export default class Toast {
30
30
  this.loading = createDom('div', { class: '__mpx_loading_wrapper__' }, Array.from({ length: 12 }, (_, i) => {
31
31
  return createDom('div', { class: `line${i + 1}` })
32
32
  }))
33
-
34
- document.body.appendChild(this.toast)
35
33
  }
36
34
 
37
35
  show (options, type) {
36
+ getRootElement().appendChild(this.toast) // show 则挂载
38
37
  if (this.hideTimer) {
39
38
  clearTimeout(this.hideTimer)
40
39
  this.hideTimer = null
@@ -90,7 +89,10 @@ export default class Toast {
90
89
  clearTimeout(this.hideTimer)
91
90
  this.hideTimer = null
92
91
  }
93
-
94
- this.hideTimer = setTimeout(() => { this.toast.classList.remove('show') }, duration)
92
+ this.hideTimer = setTimeout(() => {
93
+ this.toast.classList.remove('show')
94
+ this.toast.remove() // hide 则卸载
95
+ }, duration)
96
+ // return Promise.resolve({ errMsg }) todo 验证一下
95
97
  }
96
98
  }
@@ -1,8 +1,13 @@
1
1
  import Toast from './Toast'
2
+ import { isBrowser, throwSSRWarning } from '../../../common/js'
2
3
 
3
4
  let toast = null
4
5
 
5
6
  function showToast (options = { title: '' }) {
7
+ if (!isBrowser) {
8
+ throwSSRWarning('showToast API is running in non browser environments')
9
+ return
10
+ }
6
11
  if (!toast) { toast = new Toast() }
7
12
  return toast.show(options, 'toast')
8
13
  }
@@ -13,6 +18,10 @@ function hideToast (options = {}) {
13
18
  }
14
19
 
15
20
  function showLoading (options = { title: '' }) {
21
+ if (!isBrowser) {
22
+ throwSSRWarning('showLoading API is running in non browser environments')
23
+ return
24
+ }
16
25
  if (!toast) { toast = new Toast() }
17
26
  return toast.show(Object.assign({
18
27
  icon: 'loading',
@@ -1,5 +1,11 @@
1
+ import { isBrowser, throwSSRWarning } from '../../../common/js'
2
+
1
3
  const allowPlaybackRate = [0.5, 0.8, 1.0, 1.25, 1.5, 2.0]
2
4
  export const createVideoContext = (id, context) => {
5
+ if (!isBrowser) {
6
+ throwSSRWarning('createVideoContext API is running in non browser environments')
7
+ return
8
+ }
3
9
  if (!id) {
4
10
  throw new Error('id为必传参数')
5
11
  }
@@ -1,68 +0,0 @@
1
- // base64ToArrayBuffer, arrayBufferToBase64
2
- export * from './base'
3
-
4
- // createSelectorQuery
5
- export * from './create-selector-query'
6
-
7
- // showActionSheet
8
- export * from './action-sheet'
9
-
10
- // showToast, hideToast, showLoading, hideLoading
11
- export * from './toast'
12
-
13
- // previewImage
14
- export * from './preview-image'
15
-
16
- // showModal
17
- export * from './modal'
18
-
19
- // setStorage, setStorageSync, getStorage, getStorageSync
20
- // getStorageInfo, getStorageInfoSync, removeStorage, removeStorageSync
21
- // clearStorage, clearStorageSync
22
- export * from './storage'
23
-
24
- // getSystemInfo, getSystemInfoSync
25
- export * from './system'
26
-
27
- // getNetworkType, onNetworkStatusChange, offNetworkStatusChange
28
- export * from './device/network'
29
-
30
- // request
31
- export * from './request'
32
-
33
- // redirectTo, navigateTo, navigateBack, reLaunch, switchTab
34
- export * from './route'
35
-
36
- // connectSocket, sendSocketMessage, closeSocket
37
- // onSocketOpen, onSocketError, onSocketMessage, onSocketClose
38
- export * from './socket'
39
-
40
- // setNavigationBarTitle setNavigationBarColor
41
- export * from './set-navigation-bar'
42
-
43
- // nextTick
44
- export * from './next-tick'
45
-
46
- // onWindowResize offWindowResize
47
- export * from './window'
48
-
49
- // stopPullDownRefresh startPullDownRefresh
50
- export * from './pull-down'
51
-
52
- // createInnerAudioContext
53
- export * from './audio'
54
-
55
- // createVideoContext
56
- export * from './video'
57
-
58
- export * from './page-scroll-to'
59
-
60
- export * from './tab-bar'
61
-
62
- export * from './app'
63
-
64
- // createAnimation
65
- export * from './animation'
66
-
67
- // createIntersectionObserver
68
- export * from './create-intersection-observer'