@mpxjs/api-proxy 2.8.23-alpha → 2.8.39

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.
@@ -1,154 +0,0 @@
1
- import { ToPromise, webHandleSuccess } from '../../../common/js'
2
- import '../../../common/stylus/Modal.styl'
3
- import { getClassStyle } from '@hummer/tenon-vue'
4
-
5
- // 汉字为两个字符,字母/数字为一个字符
6
- const _getLength = t => {
7
- let len = 0
8
- for (let i = 0; i < t.length; i++) {
9
- if (t.charCodeAt(i) > 127 || t.charCodeAt(i) === 94) {
10
- len += 2
11
- } else {
12
- len++
13
- }
14
- }
15
- return len
16
- }
17
- function createDom (Element, attrs = {}, children = []) {
18
- const dom = new Element()
19
- Object.keys(attrs).forEach(k => Reflect.set(dom, k, attrs[k]))
20
- children.length && children.forEach(child => dom.appendChild(child))
21
- return dom
22
- }
23
- export default class Modal extends ToPromise {
24
- constructor () {
25
- super()
26
- this.defaultOpts = {
27
- title: '',
28
- content: '',
29
- showCancel: true,
30
- cancelText: '取消',
31
- cancelColor: '#000000',
32
- confirmText: '确定',
33
- confirmColor: '#576B95',
34
- success: (...args) => {},
35
- fail: (...args) => {},
36
- complete: (...args) => {}
37
- }
38
- this.hideTimer = null
39
- }
40
-
41
- show (options = {}) {
42
- if (options.confirmText && _getLength(options.confirmText) > 8) {
43
- // eslint-disable-next-line
44
- return Promise.reject({
45
- errMsg:
46
- 'showModal:fail confirmText length should not larger than 4 Chinese characters'
47
- })
48
- }
49
- if (options.cancelText && _getLength(options.cancelText) > 8) {
50
- // eslint-disable-next-line
51
- return Promise.reject({
52
- errMsg:
53
- 'showModal:fail cancelText length should not larger than 4 Chinese characters'
54
- })
55
- }
56
- if (this.hideTimer) {
57
- clearTimeout(this.hideTimer)
58
- this.hideTimer = null
59
- }
60
-
61
- this.box = createDom(
62
- View,
63
- { style: getClassStyle(null, '__mpx_modal_box__') },
64
- [
65
- createDom(
66
- View,
67
- { style: getClassStyle(null, '__mpx_modal_title_view__') },
68
- [
69
- (this.title = createDom(Text, {
70
- style: getClassStyle(null, '__mpx_modal_title__')
71
- }))
72
- ]
73
- ),
74
- createDom(
75
- View,
76
- { style: getClassStyle(null, '__mpx_modal_content_view__') },
77
- [
78
- (this.content = createDom(Text, {
79
- style: getClassStyle(null, '__mpx_modal_content__')
80
- }))
81
- ]
82
- ),
83
- (this.btns = createDom(
84
- View,
85
- { style: getClassStyle(null, '__mpx_modal_btns__') },
86
- [
87
- (this.cancelBtn = createDom(Text, {
88
- style: getClassStyle(null, '__mpx_modal_cancel__')
89
- })),
90
- (this.confirmBtn = createDom(Text, {
91
- style: getClassStyle(null, '__mpx_modal_confirm__')
92
- }))
93
- ]
94
- ))
95
- ]
96
- )
97
-
98
- const opts = Object.assign({}, this.defaultOpts, options)
99
- this.title.text = opts.title
100
- this.content.text = opts.content
101
- this.dialog = new Dialog()
102
- this.dialog.cancelable = false
103
- this.cancelBtn.text = opts.cancelText
104
- this.confirmBtn.text = opts.confirmText
105
- this.confirmBtn.style = Object.assign(this.confirmBtn.style, { color: opts.confirmColor })
106
- if (opts.showCancel !== true) {
107
- this.cancelBtn.style = Object.assign(
108
- this.cancelBtn.style,
109
- getClassStyle(null, '__mpx_modal_hide__')
110
- )
111
- } else {
112
- this.cancelBtn.style = Object.assign(
113
- this.cancelBtn.style,
114
- { color: opts.cancelColor }
115
- )
116
- }
117
-
118
- this.cancelBtn.addEventListener('tap', event => {
119
- this.hide()
120
- const result = {
121
- errMsg: 'showModal:ok',
122
- cancel: true,
123
- confirm: false
124
- }
125
- webHandleSuccess(result, opts.success, opts.complete)
126
- this.toPromiseResolve(result)
127
- })
128
-
129
- this.confirmBtn.addEventListener('tap', event => {
130
- this.hide()
131
- const result = {
132
- errMsg: 'showModal:ok',
133
- cancel: false,
134
- confirm: true
135
- }
136
- webHandleSuccess(result, opts.success, opts.complete)
137
- this.toPromiseResolve(result)
138
- })
139
-
140
- this.dialog.custom(this.box)
141
- return this.toPromiseInitPromise()
142
- }
143
-
144
- hide () {
145
- if (this.hideTimer) {
146
- clearTimeout(this.hideTimer)
147
- this.hideTimer = null
148
- }
149
-
150
- this.hideTimer = setTimeout(() => {
151
- this.dialog.dismiss()
152
- }, 0)
153
- }
154
- }
@@ -1,12 +0,0 @@
1
- import Modal from './Modal'
2
-
3
- let modal = null
4
-
5
- function showModal (options = {}) {
6
- if (!modal) { modal = new Modal() }
7
- return modal.show(options)
8
- }
9
-
10
- export {
11
- showModal
12
- }
@@ -1,11 +0,0 @@
1
- function nextTick (cb) {
2
- if (typeof Promise !== 'undefined') {
3
- Promise.resolve().then(cb)
4
- } else {
5
- setTimeout(cb, 0)
6
- }
7
- }
8
-
9
- export {
10
- nextTick
11
- }
@@ -1,71 +0,0 @@
1
- import { webHandleSuccess, webHandleFail } from '../../../common/js'
2
- const { Request } = __GLOBAL__
3
-
4
- function request (options = { url: '' }) {
5
- let {
6
- data = {},
7
- method = 'GET',
8
- dataType = 'json',
9
- responseType = 'text',
10
- timeout = 60 * 1000,
11
- header = {},
12
- success = null,
13
- fail = null,
14
- complete = null
15
- } = options
16
-
17
- method = method.toUpperCase()
18
-
19
- if (
20
- method === 'POST' &&
21
- typeof data !== 'string' && // string 不做处理
22
- (header['Content-Type'] === 'application/x-www-form-urlencoded' ||
23
- header['content-type'] === 'application/x-www-form-urlencoded')
24
- ) {
25
- // 重新设置data
26
- data = Object.keys(data)
27
- .reduce((pre, curKey) => {
28
- return `${pre}&${encodeURIComponent(curKey)}=${encodeURIComponent(
29
- data[curKey]
30
- )}`
31
- }, '')
32
- .slice(1)
33
- }
34
-
35
- const requestFn = new Request()
36
-
37
- requestFn.url = options.url
38
- requestFn.method = method
39
- requestFn.timeout = timeout
40
- requestFn.header = header
41
- requestFn.params = data
42
- requestFn.send((response) => {
43
- let { status, header: resHeader, data: resData, error } = response
44
- // 返回的数据处理
45
-
46
- if (responseType === 'text' && dataType === 'json') {
47
- try {
48
- resData = JSON.parse(resData)
49
- } catch (e) {}
50
- }
51
-
52
- if (status >= 200 && status < 300) {
53
- const result = {
54
- errMsg: 'request:ok',
55
- data: resData,
56
- statusCode: status,
57
- header: resHeader
58
- }
59
- webHandleSuccess(result, success, complete)
60
- return result
61
- } else {
62
- const res = { errMsg: `request:fail ${error.msg}` }
63
- webHandleFail(res, fail, complete)
64
- if (!fail) {
65
- return Promise.reject(res)
66
- }
67
- }
68
- })
69
- }
70
-
71
- export { request }
@@ -1,102 +0,0 @@
1
- import { webHandleSuccess } from '../../../common/js'
2
- import { EventChannel } from '../event-channel'
3
- const { Navigator } = __GLOBAL__
4
-
5
- function redirectTo (options = {}) {
6
- if (Navigator) {
7
- Navigator.__mpxAction = { type: 'redirect' }
8
- return new Promise((resolve, reject) => {
9
- // 关闭本页面的跳转
10
- Navigator.openPage(
11
- {
12
- url: options.url,
13
- closeSelf: true
14
- },
15
- // 执行环境变了 得不到执行的机会 故回调无效
16
- () => {}
17
- )
18
- const res = { errMsg: 'redirectTo:ok' }
19
- webHandleSuccess(res, options.success, options.complete)
20
- resolve(res)
21
- })
22
- }
23
- }
24
-
25
- function navigateTo (options = {}) {
26
- if (Navigator) {
27
- const eventChannel = new EventChannel()
28
- Navigator.__mpxAction = {
29
- type: 'to',
30
- eventChannel
31
- }
32
- if (options.events) {
33
- eventChannel._addListeners(options.events)
34
- }
35
- return new Promise((resolve, reject) => {
36
- // 不关闭本页面的跳转
37
- Navigator.openPage(
38
- {
39
- url: options.url
40
- },
41
- // 执行环境变了 得不到执行的机会 故回调无效
42
- () => {}
43
- )
44
- const res = { errMsg: 'redirectTo:ok' }
45
- webHandleSuccess(res, options.success, options.complete)
46
- resolve(res)
47
- })
48
- }
49
- }
50
-
51
- function navigateBack (options = {}) {
52
- if (Navigator) {
53
- const delta = options.delta || 1
54
- Navigator.__mpxAction = {
55
- type: 'back',
56
- delta
57
- }
58
- // popBack方法
59
- Navigator.popBack(delta, { animated: true })
60
- const res = { errMsg: 'navigateBack:ok' }
61
- webHandleSuccess(res, options.success, options.complete)
62
- return Promise.resolve(res)
63
- }
64
- }
65
-
66
- function reLaunch (options = {}) {
67
- if (Navigator) {
68
- Navigator.__mpxAction = {
69
- type: 'reLaunch'
70
- }
71
- Navigator.popToRootPage()
72
-
73
- const res = { errMsg: 'reLaunch:ok' }
74
- webHandleSuccess(res, options.success, options.complete)
75
- return Promise.resolve(res)
76
- }
77
- }
78
-
79
- function switchTab (options = {}) {
80
- if (Navigator) {
81
- Navigator.__mpxAction = {
82
- type: 'switch',
83
- path: options.url,
84
- replaced: true
85
- }
86
- return new Promise((resolve, reject) => {
87
- Navigator.openPage(
88
- {
89
- url: options.url,
90
- closeSelf: true
91
- },
92
- // 执行环境变了 得不到执行的机会 故回调无效
93
- () => {}
94
- )
95
- const res = { errMsg: 'redirectTo:ok' }
96
- webHandleSuccess(res, options.success, options.complete)
97
- resolve(res)
98
- })
99
- }
100
- }
101
-
102
- export { redirectTo, navigateTo, navigateBack, reLaunch, switchTab }
@@ -1,105 +0,0 @@
1
- import { webHandleSuccess, webHandleFail, warn } from '../../../common/js'
2
- const { WebSocket } = __GLOBAL__
3
-
4
- class SocketTask {
5
- constructor (url, protocols) {
6
- this._openCb = null
7
- this._closeCb = null
8
- this._messageCb = null
9
- this._errorCb = null
10
- this._closeData = null
11
-
12
- WebSocket.connect(url)
13
- this.addListener(WebSocket)
14
- }
15
-
16
- get CONNECTING () {
17
- warn('不支持CONNECTING')
18
- }
19
-
20
- get OPEN () {
21
- warn('不支持OPEN')
22
- }
23
-
24
- get CLOSING () {
25
- warn('不支持CLOSING')
26
- }
27
-
28
- get CLOSED () {
29
- warn('不支持CLOSED')
30
- }
31
-
32
- get readyState () {
33
- warn('不支持readyState')
34
- }
35
-
36
- send (options) {
37
- // todo fail options needs tobe convert
38
- // const { data = '', success, fail, complete } = options
39
- const { data = '', success, complete } = options
40
- WebSocket.send(data)
41
- const res = { errMsg: 'sendSocketMessage:ok' }
42
- webHandleSuccess(res, success, complete)
43
- return Promise.resolve(res)
44
- }
45
-
46
- close (options) {
47
- const { code = 1000, reason = '', success, fail, complete } = options
48
- this._closeData = {
49
- code,
50
- reason
51
- }
52
- try {
53
- WebSocket.close()
54
- const res = { errMsg: 'closeSocket:ok' }
55
- webHandleSuccess(res, success, complete)
56
- return Promise.resolve(res)
57
- } catch (err) {
58
- const res = { errMsg: `closeSocket:fail ${err}` }
59
- webHandleFail(res, fail, complete)
60
- if (!fail) {
61
- return Promise.reject(res)
62
- }
63
- }
64
- }
65
-
66
- addListener (socket) {
67
- socket.onOpen((event) => {
68
- typeof this._openCb === 'function' && this._openCb(event)
69
- })
70
- socket.onMessage((event) => {
71
- typeof this._messageCb === 'function' && this._messageCb(event)
72
- })
73
- socket.onError((event) => {
74
- typeof this._errorCb === 'function' && this._errorCb(event)
75
- })
76
- socket.onClose((event) => {
77
- if (typeof this._closeCb !== 'function') {
78
- return
79
- }
80
- if (this._closeData) {
81
- this._closeCb(event)
82
- } else {
83
- this._closeCb({ code: 2000, reason: `${event}` })
84
- }
85
- })
86
- }
87
-
88
- onOpen (cb) {
89
- this._openCb = cb
90
- }
91
-
92
- onMessage (cb) {
93
- this._messageCb = cb
94
- }
95
-
96
- onError (cb) {
97
- this._errorCb = cb
98
- }
99
-
100
- onClose (cb) {
101
- this._closeCb = cb
102
- }
103
- }
104
-
105
- export default SocketTask
@@ -1,48 +0,0 @@
1
- import { warn, webHandleSuccess, webHandleFail } from '../../../common/js'
2
- import SocketTask from './SocketTask'
3
-
4
- function connectSocket (options = { url: '' }) {
5
- const { url, protocols, success, fail, complete } = options
6
-
7
- try {
8
- const socketTask = new SocketTask(url, protocols)
9
- webHandleSuccess({ errMsg: 'connectSocket:ok' }, success, complete)
10
- return socketTask
11
- } catch (e) {
12
- webHandleFail({ errMsg: `connectSocket:fail ${e}` }, fail, complete)
13
- }
14
- }
15
-
16
- function sendSocketMessage () {
17
- warn('sendSocketMessage 请使用 socketTask.send')
18
- }
19
-
20
- function closeSocket () {
21
- warn('closeSocket 请使用 socketTask.close')
22
- }
23
-
24
- function onSocketOpen () {
25
- warn('onSocketOpen 请使用 socketTask.onOpen')
26
- }
27
-
28
- function onSocketError () {
29
- warn('onSocketError 请使用 socketTask.onError')
30
- }
31
-
32
- function onSocketMessage () {
33
- warn('onSocketMessage 请使用 socketTask.onMessage')
34
- }
35
-
36
- function onSocketClose () {
37
- warn('onSocketClose 请使用 socketTask.onClose')
38
- }
39
-
40
- export {
41
- connectSocket,
42
- sendSocketMessage,
43
- closeSocket,
44
- onSocketOpen,
45
- onSocketError,
46
- onSocketMessage,
47
- onSocketClose
48
- }
@@ -1,143 +0,0 @@
1
- import { webHandleSuccess, webHandleFail, warn, hasOwn } from '../../../common/js'
2
- const { Storage } = __GLOBAL__
3
-
4
- function setStorage (options = {}) {
5
- const { key, data, success, fail, complete } = options
6
-
7
- try {
8
- setStorageSync(key, data)
9
-
10
- const res = { errMsg: 'setStorage:ok' }
11
- webHandleSuccess(res, success, complete)
12
- return Promise.resolve(res)
13
- } catch (err) {
14
- const res = { errMsg: `setStorage:fail ${err}` }
15
- webHandleFail(res, fail, complete)
16
- return Promise.reject(res)
17
- }
18
- }
19
-
20
- function setStorageSync (key = '', data) {
21
- let obj = {}
22
-
23
- if (typeof data === 'symbol') {
24
- obj = { data: '' }
25
- } else {
26
- obj = { data }
27
- }
28
- Storage.set(key, JSON.stringify(obj))
29
- }
30
-
31
- function getStorage (options = {}) {
32
- const { key, success, fail, complete } = options
33
- const { result, data } = getItem(key)
34
-
35
- if (result) {
36
- const res = { errMsg: 'getStorage:ok', data: data }
37
- webHandleSuccess(res, success, complete)
38
- return Promise.resolve(res)
39
- } else {
40
- const res = { errMsg: 'getStorage:fail', data: null }
41
- webHandleFail(res, fail, complete)
42
- return Promise.reject(res)
43
- }
44
- }
45
-
46
- function getStorageSync (key) {
47
- const res = getItem(key)
48
- if (res.result) return res.data
49
-
50
- return ''
51
- }
52
-
53
- function getItem (key) {
54
- let item
55
- try {
56
- item = JSON.parse(Storage.get(key))
57
- } catch (e) {}
58
-
59
- if (item && typeof item === 'object' && hasOwn(item, 'data')) {
60
- return { result: true, data: item.data }
61
- } else {
62
- return { result: false }
63
- }
64
- }
65
-
66
- function getStorageInfo (options = {}) {
67
- const { success, fail, complete } = options
68
-
69
- try {
70
- const info = getStorageInfoSync()
71
-
72
- const res = Object.assign({}, { errMsg: 'getStorageInfo:ok' }, info)
73
- webHandleSuccess(res, success, complete)
74
- return Promise.resolve(res)
75
- } catch (err) {
76
- const res = { errMsg: `getStorageInfo:fail ${err}` }
77
- webHandleFail(res, fail, complete)
78
- return Promise.reject(res)
79
- }
80
- }
81
-
82
- function getStorageInfoSync () {
83
- return {
84
- keys: Object.keys(Storage),
85
- limitSize: null,
86
- currentSize: null
87
- }
88
- }
89
-
90
- function removeStorage (options = { key: '' }) {
91
- const { key, success, fail, complete } = options
92
-
93
- try {
94
- removeStorageSync(key)
95
-
96
- const res = { errMsg: 'removeStorage:ok' }
97
- webHandleSuccess(res, success, complete)
98
- return Promise.resolve(res)
99
- } catch (err) {
100
- const res = { errMsg: `removeStorage:fail ${err}` }
101
- webHandleFail(res, fail, complete)
102
- return Promise.reject(res)
103
- }
104
- }
105
-
106
- function removeStorageSync (key) {
107
- Storage.remove(key)
108
- }
109
-
110
- function clearStorage (options = {}) {
111
- warn('不支持clearStorageSync')
112
- // const { success, fail, complete } = options
113
-
114
- // try {
115
- // clearStorageSync()
116
-
117
- // const res = { errMsg: 'clearStorage:ok' }
118
- // webHandleSuccess(res, success, complete)
119
- // return Promise.resolve(res)
120
- // } catch (err) {
121
- // const res = { errMsg: `clearStorage:fail ${err}` }
122
- // webHandleFail(res, fail, complete)
123
- // return Promise.reject(res)
124
- // }
125
- }
126
-
127
- function clearStorageSync () {
128
- // Storage.clear()
129
- warn('不支持clearStorageSync')
130
- }
131
-
132
- export {
133
- setStorage,
134
- setStorageSync,
135
- getStorage,
136
- getStorageSync,
137
- getStorageInfo,
138
- getStorageInfoSync,
139
- removeStorage,
140
- removeStorageSync,
141
- clearStorage,
142
- clearStorageSync
143
- }
@@ -1,52 +0,0 @@
1
- import { webHandleSuccess } from '../../../common/js'
2
- const { Hummer } = __GLOBAL__
3
-
4
- function getSystemInfoSync () {
5
- const {
6
- platform,
7
- osVersion,
8
- deviceWidth,
9
- deviceHeight,
10
- availableWidth,
11
- availableHeight,
12
- safeAreaBottom
13
- } = Hummer.env || {}
14
- return {
15
- brand: platform,
16
- model: platform,
17
- pixelRatio: null,
18
- screenWidth: deviceWidth,
19
- screenHeight: deviceHeight,
20
- windowWidth: availableWidth,
21
- windowHeight: availableHeight,
22
- statusBarHeight: null,
23
- language: null,
24
- version: null,
25
- system: platform + osVersion,
26
- platform: platform,
27
- fontSizeSetting: null,
28
- SDKVersion: null,
29
- benchmarkLevel: null,
30
- albumAuthorized: null,
31
- cameraAuthorized: null,
32
- locationAuthorized: null,
33
- microphoneAuthorized: null,
34
- notificationAlertAuthorized: null,
35
- notificationAuthorized: null,
36
- notificationBadgeAuthorized: null,
37
- notificationSoundAuthorized: null,
38
- bluetoothEnabled: null,
39
- locationEnabled: null,
40
- wifiEnabled: null,
41
- safeArea: safeAreaBottom
42
- }
43
- }
44
-
45
- function getSystemInfo (options = {}) {
46
- const info = getSystemInfoSync()
47
- const res = Object.assign({ errMsg: 'getSystemInfo:ok' }, info)
48
- webHandleSuccess(res, options.success, options.complete)
49
- return Promise.resolve(res)
50
- }
51
-
52
- export { getSystemInfo, getSystemInfoSync }