Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
scilla-components
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
劳工
scilla-components
Commits
ccb9cd2a
Commit
ccb9cd2a
authored
Apr 12, 2019
by
rockyl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
引擎启动参数改为engineConfig和customConfig
parent
524b14e4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
182 additions
and
191 deletions
+182
-191
webService.ts
src/net/webService.ts
+182
-191
No files found.
src/net/webService.ts
View file @
ccb9cd2a
/**
* Created by hwj on 2018/11/22.
* 网络组件
*/
import
{
utils
}
from
"scilla"
;
// 请求路径 root
let
baseUrl
:
string
;
/**
* 调用接口
* @param uri
* @param params
* @param method
* @param responseType
* @param ignoreSuccessField
*/
export
function
callApi
(
uri
:
string
,
params
:
any
=
null
,
method
:
string
=
'post'
,
responseType
=
'json'
,
ignoreSuccessField
=
false
):
Promise
<
any
>
{
let
ts
=
Date
.
now
()
+
Math
.
floor
(
Math
.
random
()
*
9999999
);
let
url
=
uri
.
indexOf
(
'//'
)
===
0
?
uri
:
`
${
uri
}
?_=
${
ts
}
`
;
params
=
params
||
{};
let
options
:
any
=
{
method
,
};
if
(
!
baseUrl
)
{
options
.
credentials
=
'include'
;
}
let
temp
=
typeof
params
===
'string'
?
params
:
utils
.
objectStringify
(
params
);
switch
(
method
.
toUpperCase
())
{
case
'GET'
:
if
(
temp
&&
temp
.
length
>
0
){
url
+=
(
url
.
indexOf
(
'?'
)
<
0
?
'?'
:
''
)
+
'&'
+
temp
;
}
break
;
case
'POST'
:
options
.
body
=
temp
;
options
.
headers
=
{
'Content-Type'
:
'application/x-www-form-urlencoded;charset=UTF-8'
,
};
break
;
}
const
fetchMethod
=
responseType
==
'jsonp'
?
window
[
'fetchJsonp'
]
:
fetch
;
url
=
baseUrl
?
baseUrl
+
url
:
url
return
fetchMethod
(
url
,
options
)
.
then
((
response
)
=>
{
if
(
response
.
type
===
'opaque'
)
{
return
null
;
}
return
response
.
text
();
})
.
then
((
response
)
=>
{
//console.log('fetch ==>', url, response);
if
(
response
)
{
let
data
;
switch
(
responseType
)
{
case
'json'
:
try
{
/*console.log('debug', GameConfig.debug);
console.log(url, options);
console.log(response);*/
data
=
JSON
.
parse
(
response
);
}
catch
(
e
)
{
console
.
log
(
'decode json failed: '
+
url
);
return
Promise
.
reject
({});
}
if
(
ignoreSuccessField
||
data
.
success
)
{
return
{
data
:
data
.
hasOwnProperty
(
'data'
)
?
data
.
data
:
data
,
origin
:
data
,
};
}
else
{
return
Promise
.
reject
(
data
.
code
);
}
case
'html'
:
let
html
=
null
;
//DOMParser.parseFromString(response, 'text/html');
return
html
;
case
'txt'
:
return
response
;
}
}
return
Promise
.
reject
();
})
}
/**
* 轮训请求
* @param successFunc
* @param maxTimes
* @param delay
* @param uri
* @param params
* @param method
* @param responseType
*/
export
function
polling
(
successFunc
,
uri
,
params
,
maxTimes
=
10
,
delay
=
500
,
method
=
'POST'
,
responseType
=
'json'
):
Promise
<
any
>
{
let
p
=
Promise
.
resolve
();
for
(
let
i
=
0
;
i
<
maxTimes
;
i
++
)
{
p
=
p
.
then
(
func
);
p
=
p
.
then
(()
=>
{
return
utils
.
waitPromise
(
delay
)
})
}
let
lastData
;
return
p
.
then
(
()
=>
{
return
Promise
.
reject
(
null
);
},
(
e
)
=>
{
if
(
e
===
'success'
)
{
return
Promise
.
resolve
(
lastData
);
}
return
Promise
.
reject
(
e
);
}
);
function
func
()
{
return
callApi
(
uri
,
params
,
method
,
responseType
).
then
(
(
data
)
=>
{
if
(
successFunc
(
data
))
{
lastData
=
data
;
return
Promise
.
reject
(
'success'
);
}
},
(
e
)
=>
{
return
Promise
.
reject
(
e
);
}
)
}
}
/**
* 获取token并发送
* @param uri
* @param params
* @param method
* @param responseType
*/
export
async
function
getToken
(
uri
:
string
,
params
:
any
,
method
:
string
=
'POST'
,
responseType
:
string
=
'json'
)
{
if
(
window
[
'getDuibaToken'
])
{
window
[
'getDuibaToken'
](
async
(
tokenObj
:
any
)
=>
{
params
.
token
=
tokenObj
.
token
;
await
this
.
send
(
uri
,
params
,
method
,
responseType
);
},
()
=>
{
});
}
else
{
await
this
.
send
(
uri
,
params
,
method
,
responseType
);
}
}
// /**
// * 通讯底层错误
// */
// protected onError(key,msgObj): void {
// const msg = `${key}:${msgObj}`
// this.dispatchEvent('Error', msg );
// }
/**
* 生成签名
* @param {number} ticketId
* @param {number} score
* @param {any} gameData
* @param {string} submitToken 提交
* @returns {string} 签名
*/
export
function
createSgin
(
ticketId
:
number
,
score
:
number
,
gameData
:
any
,
submitToken
:
string
):
string
{
return
window
[
'duiba_md5'
](
ticketId
+
''
+
score
+
''
+
gameData
+
''
+
submitToken
);
}
/**
* @desc 请求root补全
* @param {string} u
*/
export
function
enjectBaseUrl
(
u
)
{
baseUrl
=
u
;
}
\ No newline at end of file
/**
* Created by hwj on 2018/11/22.
* 网络组件
*/
import
{
utils
,
customConfig
}
from
"scilla"
;
/**
* 调用接口
* @param uri
* @param params
* @param method
* @param responseType
* @param ignoreSuccessField
*/
export
function
callApi
(
uri
:
string
,
params
:
any
=
null
,
method
:
string
=
'post'
,
responseType
=
'json'
,
ignoreSuccessField
=
false
):
Promise
<
any
>
{
let
ts
=
Date
.
now
()
+
Math
.
floor
(
Math
.
random
()
*
9999999
);
let
url
=
uri
.
indexOf
(
'//'
)
===
0
?
uri
:
`
${
uri
}
?_=
${
ts
}
`
;
params
=
params
||
{};
let
options
:
any
=
{
method
,
};
let
baseUrl
=
customConfig
.
webServiceUrl
;
if
(
!
baseUrl
)
{
options
.
credentials
=
'include'
;
}
let
temp
=
typeof
params
===
'string'
?
params
:
utils
.
objectStringify
(
params
);
switch
(
method
.
toUpperCase
())
{
case
'GET'
:
if
(
temp
&&
temp
.
length
>
0
){
url
+=
(
url
.
indexOf
(
'?'
)
<
0
?
'?'
:
''
)
+
'&'
+
temp
;
}
break
;
case
'POST'
:
options
.
body
=
temp
;
options
.
headers
=
{
'Content-Type'
:
'application/x-www-form-urlencoded;charset=UTF-8'
,
};
break
;
}
const
fetchMethod
=
responseType
==
'jsonp'
?
window
[
'fetchJsonp'
]
:
fetch
;
url
=
baseUrl
?
baseUrl
+
url
:
url
return
fetchMethod
(
url
,
options
)
.
then
((
response
)
=>
{
if
(
response
.
type
===
'opaque'
)
{
return
null
;
}
return
response
.
text
();
})
.
then
((
response
)
=>
{
//console.log('fetch ==>', url, response);
if
(
response
)
{
let
data
;
switch
(
responseType
)
{
case
'json'
:
try
{
/*console.log('debug', GameConfig.debug);
console.log(url, options);
console.log(response);*/
data
=
JSON
.
parse
(
response
);
}
catch
(
e
)
{
console
.
log
(
'decode json failed: '
+
url
);
return
Promise
.
reject
({});
}
if
(
ignoreSuccessField
||
data
.
success
)
{
return
{
data
:
data
.
hasOwnProperty
(
'data'
)
?
data
.
data
:
data
,
origin
:
data
,
};
}
else
{
return
Promise
.
reject
(
data
.
code
);
}
case
'html'
:
let
html
=
null
;
//DOMParser.parseFromString(response, 'text/html');
return
html
;
case
'txt'
:
return
response
;
}
}
return
Promise
.
reject
();
})
}
/**
* 轮训请求
* @param successFunc
* @param maxTimes
* @param delay
* @param uri
* @param params
* @param method
* @param responseType
*/
export
function
polling
(
successFunc
,
uri
,
params
,
maxTimes
=
10
,
delay
=
500
,
method
=
'POST'
,
responseType
=
'json'
):
Promise
<
any
>
{
let
p
=
Promise
.
resolve
();
for
(
let
i
=
0
;
i
<
maxTimes
;
i
++
)
{
p
=
p
.
then
(
func
);
p
=
p
.
then
(()
=>
{
return
utils
.
waitPromise
(
delay
)
})
}
let
lastData
;
return
p
.
then
(
()
=>
{
return
Promise
.
reject
(
null
);
},
(
e
)
=>
{
if
(
e
===
'success'
)
{
return
Promise
.
resolve
(
lastData
);
}
return
Promise
.
reject
(
e
);
}
);
function
func
()
{
return
callApi
(
uri
,
params
,
method
,
responseType
).
then
(
(
data
)
=>
{
if
(
successFunc
(
data
))
{
lastData
=
data
;
return
Promise
.
reject
(
'success'
);
}
},
(
e
)
=>
{
return
Promise
.
reject
(
e
);
}
)
}
}
/**
* 获取token并发送
* @param uri
* @param params
* @param method
* @param responseType
*/
export
async
function
getToken
(
uri
:
string
,
params
:
any
,
method
:
string
=
'POST'
,
responseType
:
string
=
'json'
)
{
if
(
window
[
'getDuibaToken'
])
{
window
[
'getDuibaToken'
](
async
(
tokenObj
:
any
)
=>
{
params
.
token
=
tokenObj
.
token
;
await
this
.
send
(
uri
,
params
,
method
,
responseType
);
},
()
=>
{
});
}
else
{
await
this
.
send
(
uri
,
params
,
method
,
responseType
);
}
}
// /**
// * 通讯底层错误
// */
// protected onError(key,msgObj): void {
// const msg = `${key}:${msgObj}`
// this.dispatchEvent('Error', msg );
// }
/**
* 生成签名
* @param {number} ticketId
* @param {number} score
* @param {any} gameData
* @param {string} submitToken 提交
* @returns {string} 签名
*/
export
function
createSgin
(
ticketId
:
number
,
score
:
number
,
gameData
:
any
,
submitToken
:
string
):
string
{
return
window
[
'duiba_md5'
](
ticketId
+
''
+
score
+
''
+
gameData
+
''
+
submitToken
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment