Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
middleLayer_for_JJ_F
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
template admin
middleLayer_for_JJ_F
Commits
8524ec1d
Commit
8524ec1d
authored
Mar 25, 2024
by
limingzhe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: debug
parent
00de0ce8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
129 additions
and
0 deletions
+129
-0
middleLayerBase.ts
assets/middleLayer_for_JJ_F/script/middleLayerBase.ts
+129
-0
No files found.
assets/middleLayer_for_JJ_F/script/middleLayerBase.ts
View file @
8524ec1d
...
...
@@ -487,6 +487,135 @@ export abstract class middleLayerBase extends cc.Component {
});
}
asyncCallNetworkApiGetNew
(
uri
,
data
):
Promise
<
any
>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
this
.
callNetworkApiGetNew
(
uri
,
data
,
(
res
=>
{
resolve
(
res
);
}));
});
}
callNetworkApiPostNew
(
uri
,
data
,
callBack
)
{
const
token
=
cc
.
sys
.
localStorage
.
getItem
(
'
student_token
'
);
data
=
{...
data
,
token
};
this
.
getEngineInfo
().
then
((
engineInfo
)
=>
{
const
xhr
=
new
XMLHttpRequest
();
const
url
=
`
${
engineInfo
[
"
apiBase
"
]}${
uri
}
`
;
xhr
.
open
(
"
POST
"
,
url
,
true
);
xhr
.
setRequestHeader
(
'
content-type
'
,
'
application/json
'
);
xhr
.
onreadystatechange
=
()
=>
{
if
(
xhr
.
readyState
==
4
)
{
callBack
(
JSON
.
parse
(
xhr
.
responseText
));
}
}
xhr
.
setRequestHeader
(
"
token
"
,
token
)
xhr
.
send
(
JSON
.
stringify
(
data
));
});
}
async
callNetworkApiGetNew
(
uri
,
data
,
callBack
)
{
const
token
=
cc
.
sys
.
localStorage
.
getItem
(
'
student_token
'
);
const
engineInfo
=
await
this
.
getEngineInfo
();
data
=
{...
data
};
let
queryStr
=
'
?
'
;
const
params
=
[];
for
(
const
key
in
data
)
{
if
(
Object
.
hasOwnProperty
.
call
(
data
,
key
))
{
params
.
push
(
`
${
key
}
=
${
data
[
key
]}
`
);
}
}
if
(
params
.
length
>
0
)
{
queryStr
+=
params
.
join
(
"
&
"
);
}
else
{
queryStr
=
''
;
}
const
url
=
`
${
engineInfo
[
"
apiBase
"
]}${
uri
}${
queryStr
}
`
;
// 如果请求失败 最多尝试5次重连
for
(
let
i
=
0
;
i
<
5
;
i
++
)
{
const
result
=
await
this
.
newXMLHttpRequest
(
url
,
token
)
if
(
result
!=
null
)
{
// 正常响应
callBack
(
result
)
break
}
else
{
// 5秒有没有响应 - 重新请求
console
.
log
(
`请求失败,重试第
${
i
+
1
}
次`
)
}
}
}
ENGINE_INFO
;
getEngineInfo
()
{
return
new
Promise
((
resolve
,
reject
)
=>
{
if
(
this
.
ENGINE_INFO
)
{
resolve
(
this
.
ENGINE_INFO
);
return
;
}
const
loopEngineInfo
=
()
=>
{
this
.
scheduleOnce
(()
=>
{
if
(
!
(
<
any
>
window
).
air
.
engineInfo
)
{
loopEngineInfo
();
return
;
}
if
(
this
.
ENGINE_INFO
)
{
resolve
(
this
.
ENGINE_INFO
);
return
;
}
const
engineInfo
=
JSON
.
parse
((
<
any
>
window
).
air
.
engineInfo
);
if
(
engineInfo
.
isDev
==
1
)
{
engineInfo
.
domain
=
"
http://staging-teach.cdn.ireadabc.com/
"
;
engineInfo
.
apiBase
=
'
http://staging-jianj.iteachabc.com
'
;
engineInfo
.
orgId
=
521
;
}
else
{
engineInfo
.
domain
=
"
http://teach.cdn.ireadabc.com/
"
;
engineInfo
.
apiBase
=
'
http://jianj.iteachabc.com
'
;
//'http://openapi.iteachabc.com';
engineInfo
.
orgId
=
519
;
}
this
.
ENGINE_INFO
=
engineInfo
;
resolve
(
engineInfo
);
},
0.05
);
};
loopEngineInfo
();
});
}
newXMLHttpRequest
(
url
,
token
=
null
)
{
return
new
Promise
((
resovle
,
reject
)
=>
{
const
xhr
=
new
XMLHttpRequest
();
// 设置定时器 5秒终止连接
let
timeoutId
=
setTimeout
(()
=>
{
xhr
.
abort
();
resovle
(
null
)
},
5000
);
xhr
.
onreadystatechange
=
()
=>
{
if
(
xhr
.
readyState
==
4
&&
(
xhr
.
status
>=
200
&&
xhr
.
status
<
400
))
{
// 如果有定时器, 清除定时器
if
(
timeoutId
)
{
clearTimeout
(
timeoutId
);
timeoutId
=
null
;
}
resovle
(
JSON
.
parse
(
xhr
.
responseText
));
}
};
console
.
log
(
'
url =
'
+
url
);
xhr
.
open
(
'
GET
'
,
url
,
true
);
if
(
token
)
{
xhr
.
setRequestHeader
(
"
token
"
,
token
)
}
xhr
.
send
();
})
}
onDestroy
()
{
this
.
isDestroy
=
true
;
}
...
...
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