@mpxjs/api-proxy 2.6.106 → 2.6.114-alpha.7

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.
@@ -0,0 +1,101 @@
1
+ import { webHandleSuccess } from '../../../common/js'
2
+ import '../../../common/stylus/Toast.styl'
3
+
4
+ import { getClassStyle } from '@hummer/tenon-vue'
5
+
6
+ function createDom (Element, attrs = {}, children = []) {
7
+ let dom = new Element()
8
+ Object.keys(attrs).forEach(k => Reflect.set(dom, k, attrs[k]))
9
+ children.length && children.forEach(child => dom.appendChild(child))
10
+ return dom
11
+ }
12
+
13
+ export default class Toast {
14
+ constructor () {
15
+ this.defaultOpts = {
16
+ title: '',
17
+ icon: 'success',
18
+ image: '',
19
+ duration: 1500,
20
+ mask: false,
21
+ success: () => {},
22
+ fail: () => {},
23
+ complete: () => {}
24
+ }
25
+ this.dialog = null
26
+ this.hideTimer = null
27
+ this.type = null
28
+
29
+ // create & combine toast
30
+ this.toast = createDom(View, { style: getClassStyle(null, '__mpx_toast__') }, [
31
+ // todo 暂不支持 mask隐藏
32
+ // this.mask = createDom(View, { style: getClassStyle(null, '__mpx_mask__') }),
33
+ this.content = createDom(View, { style: getClassStyle(null, '__mpx_toast_box__') }, [
34
+ this.icon = createDom(View, { style: getClassStyle(null, '__mpx_toast_icon__') }),
35
+ this.title = createDom(Text, { style: getClassStyle(null, '__mpx_toast_title__') })
36
+ ])
37
+ ])
38
+ }
39
+
40
+ show (options, type) {
41
+ if (this.hideTimer) {
42
+ clearTimeout(this.hideTimer)
43
+ this.hideTimer = null
44
+ }
45
+
46
+ const opts = Object.assign({}, this.defaultOpts, options)
47
+
48
+ this.type = type
49
+
50
+ // if (opts.mask) {
51
+ // this.mask.class += 'show'
52
+ // } else {
53
+ // this.mask.classList.remove('show')
54
+ // }
55
+
56
+ // const defaultIconClass = '__mpx_toast_icon__'
57
+
58
+ const iconClass = opts.image
59
+ ? '' // image
60
+ : opts.icon === 'none'
61
+ ? '__hide_icon' // none
62
+ : opts.icon === 'error'
63
+ ? '__error_icon'
64
+ : '__success_icon' // default
65
+
66
+ // 在Hummer环境,Object.assign 有一些问题,需要重新进行一次赋值
67
+ this.icon.style = Object.assign(this.icon.style, getClassStyle(null, `${iconClass}`))
68
+
69
+ opts.image && (this.icon.style = Object.assign(this.icon.style, { backgroundImage: opts.image }))
70
+
71
+ this.title.text = opts.title || ''
72
+
73
+ this.dialog = new Dialog()
74
+ this.dialog.cancelable = false
75
+ opts.icon === 'loading'
76
+ ? this.dialog.loading(opts.title || 'loading...') // 空字符串也被过滤
77
+ : this.dialog.custom(this.toast)
78
+
79
+ opts.duration >= 0 && this.hide({ duration: opts.duration }, type)
80
+
81
+ const errMsg = type === 'loading' ? 'showLoading:ok' : 'showToast:ok'
82
+ webHandleSuccess({ errMsg }, opts.success, opts.complete)
83
+ return Promise.resolve({ errMsg })
84
+ }
85
+
86
+ hide (options = {}, type) {
87
+ if (this.type !== type) return
88
+
89
+ const duration = options.duration || 0
90
+ const errMsg = type === 'loading' ? 'hideLoading:ok' : 'hideToast:ok'
91
+ webHandleSuccess({ errMsg }, options.success, options.complete)
92
+
93
+ if (this.hideTimer) {
94
+ clearTimeout(this.hideTimer)
95
+ this.hideTimer = null
96
+ }
97
+
98
+ this.hideTimer = setTimeout(() => { this.dialog.dismiss() }, duration)
99
+ return Promise.resolve({ errMsg })
100
+ }
101
+ }
@@ -0,0 +1,36 @@
1
+ import Toast from './Toast'
2
+
3
+ let toast = null
4
+
5
+ function showToast (options = { title: '' }) {
6
+ if (!toast) { toast = new Toast() }
7
+ return toast.show(options, 'toast')
8
+ }
9
+
10
+ function hideToast (options = {}) {
11
+ if (!toast) { return }
12
+ return toast.hide(Object.assign({ duration: 0 }, options), 'toast')
13
+ }
14
+
15
+ function showLoading (options = { title: '' }) {
16
+ if (!toast) { toast = new Toast() }
17
+ return toast.show(Object.assign({
18
+ icon: 'loading',
19
+ duration: -1
20
+ }, options), 'loading')
21
+ }
22
+
23
+ function hideLoading (options = {}) {
24
+ if (!toast) { return }
25
+ return toast.hide(Object.assign({
26
+ icon: 'loading',
27
+ duration: 0
28
+ }, options), 'loading')
29
+ }
30
+
31
+ export {
32
+ showToast,
33
+ hideToast,
34
+ showLoading,
35
+ hideLoading
36
+ }
@@ -1,9 +1,14 @@
1
- import { webHandleSuccess, webHandleFail } from '../../../common/js'
1
+ import { webHandleSuccess, webHandleFail, isTabBarPage } from '../../../common/js'
2
2
  import { EventChannel } from '../event-channel'
3
3
 
4
4
  function redirectTo (options = {}) {
5
5
  const router = global.__mpxRouter
6
6
  if (router) {
7
+ if (isTabBarPage(options.url, router)) {
8
+ const res = { errMsg: 'redirectTo:fail can not redirectTo a tabBar page' }
9
+ webHandleFail(res, options.fail, options.complete)
10
+ return Promise.reject(res)
11
+ }
7
12
  router.__mpxAction = { type: 'redirect' }
8
13
  return new Promise((resolve, reject) => {
9
14
  router.replace(
@@ -28,6 +33,11 @@ function redirectTo (options = {}) {
28
33
  function navigateTo (options = {}) {
29
34
  const router = global.__mpxRouter
30
35
  if (router) {
36
+ if (isTabBarPage(options.url, router)) {
37
+ const res = { errMsg: 'navigateTo:fail can not navigateTo a tabBar page' }
38
+ webHandleFail(res, options.fail, options.complete)
39
+ return Promise.reject(res)
40
+ }
31
41
  const eventChannel = new EventChannel()
32
42
  router.__mpxAction = {
33
43
  type: 'to',
@@ -71,11 +81,13 @@ function navigateBack (options = {}) {
71
81
  }
72
82
  }
73
83
 
84
+ let reLaunchCount = 0
85
+
74
86
  function reLaunch (options = {}) {
75
87
  const router = global.__mpxRouter
76
88
  if (router) {
89
+ if (reLaunchCount === 0 && router.currentRoute.query.reLaunchCount) reLaunchCount = router.currentRoute.query.reLaunchCount
77
90
  const delta = router.stack.length - 1
78
- let reLaunchCount = router.currentRoute.query.reLaunchCount || 0
79
91
  router.__mpxAction = {
80
92
  type: 'reLaunch',
81
93
  path: options.url,
@@ -120,7 +132,7 @@ function switchTab (options = {}) {
120
132
  const toRoute = router.match(options.url, router.history.current)
121
133
  const currentRoute = router.currentRoute
122
134
  if (toRoute.path !== currentRoute.path) {
123
- if (toRoute.redirectedFrom) {
135
+ if (!isTabBarPage(options.url, router)) {
124
136
  const res = { errMsg: 'switchTab:fail can not switch to no-tabBar page!' }
125
137
  webHandleFail(res, options.fail, options.complete)
126
138
  return Promise.reject(res)