@mpxjs/api-proxy 2.9.69 → 2.9.70-alpha.0
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 +3 -4
- package/src/common/js/ToPromise.js +25 -0
- package/src/common/js/index.js +1 -0
- package/src/common/js/utils.js +11 -1
- package/src/common/stylus/Modal.tenon.styl +42 -0
- package/src/common/stylus/Toast.tenon.styl +56 -0
- package/src/index.tenon.js +27 -0
- package/src/platform/api/animation/animation.tenon.js +225 -0
- package/src/platform/api/animation/index.tenon.js +89 -0
- package/src/platform/api/create-selector-query/rnNodesRef.js +1 -6
- package/src/platform/api/event-channel/index.tenon.js +52 -0
- package/src/platform/api/modal/index.tenon.js +12 -0
- package/src/platform/api/modal/tenonModal.js +154 -0
- package/src/platform/api/next-tick/index.tenon.js +11 -0
- package/src/platform/api/request/index.tenon.js +85 -0
- package/src/platform/api/request/tenonUtil.js +0 -0
- package/src/platform/api/route/index.tenon.js +121 -0
- package/src/platform/api/set-navigation-bar/index.tenon.js +17 -0
- package/src/platform/api/socket/SocketTask.tenon.js +105 -0
- package/src/platform/api/socket/index.tenon.js +48 -0
- package/src/platform/api/storage/index.tenon.js +144 -0
- package/src/platform/api/storage/index.web.js +1 -1
- package/src/platform/api/storage/rnStorage.js +1 -1
- package/src/platform/api/system/index.tenon.js +52 -0
- package/src/platform/api/system/rnSystem.js +3 -4
- package/src/platform/api/toast/Toast.tenon.js +101 -0
- package/src/platform/api/toast/index.tenon.js +36 -0
- package/LICENSE +0 -433
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { ToPromise, successHandle } 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
|
+
successHandle(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
|
+
successHandle(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
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { successHandle, failHandle } from '../../../common/js'
|
|
2
|
+
import { buildQueryStringUrl, parseHeader, tryJsonParse } from './tenonUtil'
|
|
3
|
+
const { Request } = __GLOBAL__
|
|
4
|
+
const requestFn = new Request()
|
|
5
|
+
|
|
6
|
+
function request (options = {}) {
|
|
7
|
+
let {
|
|
8
|
+
data = {},
|
|
9
|
+
method = 'GET',
|
|
10
|
+
dataType = 'form',
|
|
11
|
+
responseType = 'json',
|
|
12
|
+
timeout = 60 * 1000,
|
|
13
|
+
header = {},
|
|
14
|
+
success = null,
|
|
15
|
+
fail = null,
|
|
16
|
+
complete = null,
|
|
17
|
+
url = ''
|
|
18
|
+
} = options
|
|
19
|
+
|
|
20
|
+
method = method.toUpperCase()
|
|
21
|
+
|
|
22
|
+
if (['GET', 'PUT', 'DELETE'].indexOf(method) > -1) {
|
|
23
|
+
url = buildQueryStringUrl(data, url)
|
|
24
|
+
|
|
25
|
+
if (method === 'GET') {
|
|
26
|
+
data = {}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
switch (dataType) {
|
|
31
|
+
case 'form':
|
|
32
|
+
header = {
|
|
33
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
34
|
+
...header
|
|
35
|
+
}
|
|
36
|
+
break
|
|
37
|
+
|
|
38
|
+
case 'json':
|
|
39
|
+
header = {
|
|
40
|
+
'Content-Type': 'application/json',
|
|
41
|
+
...header
|
|
42
|
+
}
|
|
43
|
+
break
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
requestFn.url = url
|
|
47
|
+
requestFn.method = method
|
|
48
|
+
requestFn.timeout = timeout
|
|
49
|
+
requestFn.param = data
|
|
50
|
+
requestFn.header = header
|
|
51
|
+
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
requestFn.send((response) => {
|
|
54
|
+
let { status, header: resHeader, data: resData, error } = response
|
|
55
|
+
// 返回的数据处理
|
|
56
|
+
if (status >= 200 && status < 300) {
|
|
57
|
+
if (responseType === 'json' && typeof resData === 'string') {
|
|
58
|
+
try {
|
|
59
|
+
resData = JSON.parse(resData)
|
|
60
|
+
} catch (e) {
|
|
61
|
+
console.log('resDataType默认为"json", 尝试对返回内容进行JSON.parse, 但似乎出了些问题(若不希望对结果进行parse, 可传入resDataType: "text"): ', e)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const result = {
|
|
65
|
+
errMsg: 'request:ok',
|
|
66
|
+
data: resData,
|
|
67
|
+
statusCode: status,
|
|
68
|
+
header: resHeader
|
|
69
|
+
}
|
|
70
|
+
successHandle(result, success, complete)
|
|
71
|
+
resolve(result)
|
|
72
|
+
} else {
|
|
73
|
+
if (responseType === 'json') {
|
|
74
|
+
resData = tryJsonParse(resData)
|
|
75
|
+
}
|
|
76
|
+
header = parseHeader(header)
|
|
77
|
+
const res = { errMsg: `request:fail ${error.msg}`, data: resData, header }
|
|
78
|
+
failHandle(res, fail, complete)
|
|
79
|
+
reject(res)
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export { request }
|
|
File without changes
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { successHandle } from '../../../common/js'
|
|
2
|
+
import { EventChannel } from '../event-channel'
|
|
3
|
+
const { Navigator } = __GLOBAL__
|
|
4
|
+
|
|
5
|
+
function handleUrl (url) {
|
|
6
|
+
const [urlString, queryString] = url.split('?')
|
|
7
|
+
const queryObj = {}
|
|
8
|
+
|
|
9
|
+
if (!queryString) {
|
|
10
|
+
return {
|
|
11
|
+
query: queryObj,
|
|
12
|
+
url: urlString
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const paramsArray = queryString.split('&')
|
|
17
|
+
for (const pair of paramsArray) {
|
|
18
|
+
const [key, value] = pair.split('=')
|
|
19
|
+
queryObj[key] = decodeURIComponent(value)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
query: queryObj,
|
|
24
|
+
url: urlString
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function redirectTo (options = {}) {
|
|
29
|
+
const { url, query } = handleUrl(options.url || '')
|
|
30
|
+
if (Navigator) {
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
// 关闭本页面的跳转
|
|
33
|
+
Navigator.openPage(
|
|
34
|
+
{
|
|
35
|
+
url,
|
|
36
|
+
animated: false,
|
|
37
|
+
params: Object.assign({}, query, options.query || {}),
|
|
38
|
+
closeSelf: true
|
|
39
|
+
},
|
|
40
|
+
// 执行环境变了 得不到执行的机会 故回调无效
|
|
41
|
+
() => {}
|
|
42
|
+
)
|
|
43
|
+
const res = { errMsg: 'redirectTo:ok' }
|
|
44
|
+
successHandle(res, options.success, options.complete)
|
|
45
|
+
resolve(res)
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function navigateTo (options = {}) {
|
|
51
|
+
const { url, query } = handleUrl(options.url || '')
|
|
52
|
+
const events = options.events
|
|
53
|
+
|
|
54
|
+
if (Navigator) {
|
|
55
|
+
const eventChannel = new EventChannel()
|
|
56
|
+
if (events) {
|
|
57
|
+
eventChannel._addListeners(events)
|
|
58
|
+
}
|
|
59
|
+
return new Promise((resolve, reject) => {
|
|
60
|
+
// 不关闭本页面的跳转
|
|
61
|
+
Navigator.openPage({
|
|
62
|
+
url,
|
|
63
|
+
animated: true,
|
|
64
|
+
params: Object.assign({}, query, options.query || {})
|
|
65
|
+
})
|
|
66
|
+
const res = { errMsg: 'redirectTo:ok', eventChannel }
|
|
67
|
+
successHandle(res, options.success, options.complete)
|
|
68
|
+
resolve(res)
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function navigateBack (options = {}) {
|
|
74
|
+
if (Navigator) {
|
|
75
|
+
const delta = options.delta || 1
|
|
76
|
+
Navigator.popBack(delta, {
|
|
77
|
+
animated: true
|
|
78
|
+
})
|
|
79
|
+
const res = { errMsg: 'navigateBack:ok' }
|
|
80
|
+
successHandle(res, options.success, options.complete)
|
|
81
|
+
return Promise.resolve(res)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function reLaunch (options = {}) {
|
|
86
|
+
if (Navigator) {
|
|
87
|
+
Navigator.__mpxAction = {
|
|
88
|
+
type: 'reLaunch'
|
|
89
|
+
}
|
|
90
|
+
Navigator.popToRootPage()
|
|
91
|
+
|
|
92
|
+
const res = { errMsg: 'reLaunch:ok' }
|
|
93
|
+
successHandle(res, options.success, options.complete)
|
|
94
|
+
return Promise.resolve(res)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function switchTab (options = {}) {
|
|
99
|
+
if (Navigator) {
|
|
100
|
+
Navigator.__mpxAction = {
|
|
101
|
+
type: 'switch',
|
|
102
|
+
path: options.url,
|
|
103
|
+
replaced: true
|
|
104
|
+
}
|
|
105
|
+
return new Promise((resolve, reject) => {
|
|
106
|
+
Navigator.openPage(
|
|
107
|
+
{
|
|
108
|
+
url: options.url,
|
|
109
|
+
closeSelf: true
|
|
110
|
+
},
|
|
111
|
+
// 执行环境变了 得不到执行的机会 故回调无效
|
|
112
|
+
() => {}
|
|
113
|
+
)
|
|
114
|
+
const res = { errMsg: 'redirectTo:ok' }
|
|
115
|
+
successHandle(res, options.success, options.complete)
|
|
116
|
+
resolve(res)
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export { redirectTo, navigateTo, navigateBack, reLaunch, switchTab }
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { successHandle } from '../../../common/js'
|
|
2
|
+
const {
|
|
3
|
+
Hummer
|
|
4
|
+
} = __GLOBAL__
|
|
5
|
+
|
|
6
|
+
function setNavigationBarTitle (options = {}) {
|
|
7
|
+
const { title, success, complete } = options
|
|
8
|
+
Hummer.setTitle(title)
|
|
9
|
+
successHandle({ errMsg: 'setTitle:ok' }, success, complete)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function setNavigationBarColor (options = {}) {}
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
setNavigationBarTitle,
|
|
16
|
+
setNavigationBarColor
|
|
17
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { successHandle, failHandle, 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
|
+
successHandle(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
|
+
successHandle(res, success, complete)
|
|
56
|
+
return Promise.resolve(res)
|
|
57
|
+
} catch (err) {
|
|
58
|
+
const res = { errMsg: `closeSocket:fail ${err}` }
|
|
59
|
+
failHandle(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
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { warn, successHandle, failHandle } 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
|
+
successHandle({ errMsg: 'connectSocket:ok' }, success, complete)
|
|
10
|
+
return socketTask
|
|
11
|
+
} catch (e) {
|
|
12
|
+
failHandle({ 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
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { successHandle, failHandle, warn } from '../../../common/js'
|
|
2
|
+
import { hasOwn } from '@mpxjs/utils'
|
|
3
|
+
const { Storage } = __GLOBAL__
|
|
4
|
+
|
|
5
|
+
function setStorage (options = {}) {
|
|
6
|
+
const { key, data, success, fail, complete } = options
|
|
7
|
+
|
|
8
|
+
try {
|
|
9
|
+
setStorageSync(key, data)
|
|
10
|
+
|
|
11
|
+
const res = { errMsg: 'setStorage:ok' }
|
|
12
|
+
successHandle(res, success, complete)
|
|
13
|
+
return Promise.resolve(res)
|
|
14
|
+
} catch (err) {
|
|
15
|
+
const res = { errMsg: `setStorage:fail ${err}` }
|
|
16
|
+
failHandle(res, fail, complete)
|
|
17
|
+
return Promise.reject(res)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function setStorageSync (key = '', data) {
|
|
22
|
+
let obj = {}
|
|
23
|
+
|
|
24
|
+
if (typeof data === 'symbol') {
|
|
25
|
+
obj = { data: '' }
|
|
26
|
+
} else {
|
|
27
|
+
obj = { data }
|
|
28
|
+
}
|
|
29
|
+
Storage.set(key, JSON.stringify(obj))
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function getStorage (options = {}) {
|
|
33
|
+
const { key, success, fail, complete } = options
|
|
34
|
+
const { result, data } = getItem(key)
|
|
35
|
+
|
|
36
|
+
if (result) {
|
|
37
|
+
const res = { errMsg: 'getStorage:ok', data: data }
|
|
38
|
+
successHandle(res, success, complete)
|
|
39
|
+
return Promise.resolve(res)
|
|
40
|
+
} else {
|
|
41
|
+
const res = { errMsg: 'getStorage:fail', data: null }
|
|
42
|
+
failHandle(res, fail, complete)
|
|
43
|
+
return Promise.reject(res)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function getStorageSync (key) {
|
|
48
|
+
const res = getItem(key)
|
|
49
|
+
if (res.result) return res.data
|
|
50
|
+
|
|
51
|
+
return ''
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function getItem (key) {
|
|
55
|
+
let item
|
|
56
|
+
try {
|
|
57
|
+
item = JSON.parse(Storage.get(key))
|
|
58
|
+
} catch (e) {}
|
|
59
|
+
|
|
60
|
+
if (item && typeof item === 'object' && hasOwn(item, 'data')) {
|
|
61
|
+
return { result: true, data: item.data }
|
|
62
|
+
} else {
|
|
63
|
+
return { result: false }
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function getStorageInfo (options = {}) {
|
|
68
|
+
const { success, fail, complete } = options
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
const info = getStorageInfoSync()
|
|
72
|
+
|
|
73
|
+
const res = Object.assign({}, { errMsg: 'getStorageInfo:ok' }, info)
|
|
74
|
+
successHandle(res, success, complete)
|
|
75
|
+
return Promise.resolve(res)
|
|
76
|
+
} catch (err) {
|
|
77
|
+
const res = { errMsg: `getStorageInfo:fail ${err}` }
|
|
78
|
+
failHandle(res, fail, complete)
|
|
79
|
+
return Promise.reject(res)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function getStorageInfoSync () {
|
|
84
|
+
return {
|
|
85
|
+
keys: Object.keys(Storage),
|
|
86
|
+
limitSize: null,
|
|
87
|
+
currentSize: null
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function removeStorage (options = { key: '' }) {
|
|
92
|
+
const { key, success, fail, complete } = options
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
removeStorageSync(key)
|
|
96
|
+
|
|
97
|
+
const res = { errMsg: 'removeStorage:ok' }
|
|
98
|
+
successHandle(res, success, complete)
|
|
99
|
+
return Promise.resolve(res)
|
|
100
|
+
} catch (err) {
|
|
101
|
+
const res = { errMsg: `removeStorage:fail ${err}` }
|
|
102
|
+
failHandle(res, fail, complete)
|
|
103
|
+
return Promise.reject(res)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function removeStorageSync (key) {
|
|
108
|
+
Storage.remove(key)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function clearStorage (options = {}) {
|
|
112
|
+
warn('不支持clearStorageSync')
|
|
113
|
+
// const { success, fail, complete } = options
|
|
114
|
+
|
|
115
|
+
// try {
|
|
116
|
+
// clearStorageSync()
|
|
117
|
+
|
|
118
|
+
// const res = { errMsg: 'clearStorage:ok' }
|
|
119
|
+
// successHandle(res, success, complete)
|
|
120
|
+
// return Promise.resolve(res)
|
|
121
|
+
// } catch (err) {
|
|
122
|
+
// const res = { errMsg: `clearStorage:fail ${err}` }
|
|
123
|
+
// failHandle(res, fail, complete)
|
|
124
|
+
// return Promise.reject(res)
|
|
125
|
+
// }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function clearStorageSync () {
|
|
129
|
+
// Storage.clear()
|
|
130
|
+
warn('不支持clearStorageSync')
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export {
|
|
134
|
+
setStorage,
|
|
135
|
+
setStorageSync,
|
|
136
|
+
getStorage,
|
|
137
|
+
getStorageSync,
|
|
138
|
+
getStorageInfo,
|
|
139
|
+
getStorageInfoSync,
|
|
140
|
+
removeStorage,
|
|
141
|
+
removeStorageSync,
|
|
142
|
+
clearStorage,
|
|
143
|
+
clearStorageSync
|
|
144
|
+
}
|