Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
npm
/
glsx-common-sdk
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 048b9cb0
authored
Aug 24, 2023
by
liimin
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
v0.3.0
1 parent
8cc0b323
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
96 additions
and
32 deletions
dist/glsx-common-sdk.js
package.json
src/function/version/helper.js
src/function/version/index.js
src/request/helper.js
src/request/index.js
src/request/responseHandler.js
dist/glsx-common-sdk.js
View file @
048b9cb
This diff is collapsed.
Click to expand it.
package.json
View file @
048b9cb
{
{
"name"
:
"glsx-common-sdk"
,
"name"
:
"glsx-common-sdk"
,
"version"
:
"0.
2.8
"
,
"version"
:
"0.
3.0
"
,
"description"
:
"glsx-common-sdk"
,
"description"
:
"glsx-common-sdk"
,
"main"
:
"./dist/glsx-common-sdk.js"
,
"main"
:
"./dist/glsx-common-sdk.js"
,
"private"
:
false
,
"private"
:
false
,
...
...
src/function/version/helper.js
View file @
048b9cb
import
{
storediff
}
from
'../../helper/index'
import
{
storediff
}
from
'../../helper/index'
import
{
GLSX_COMMON_VERSION_STORAGE_VERSION
}
from
'./constants'
import
{
GLSX_COMMON_VERSION_STORAGE_VERSION
}
from
'./constants'
const
webwx
=
{
const
webwx
=
{
getResult
:
(
data
=
{})
=>
{
getResult
:
function
()
{
const
v
=
storediff
.
get
(
GLSX_COMMON_VERSION_STORAGE_VERSION
)
const
{
store_version_key
,
version
:
data
}
=
this
const
v
=
storediff
.
get
(
store_version_key
)
const
compare
=
compareVersions
(
v
,
data
.
version
)
const
compare
=
compareVersions
(
v
,
data
.
version
)
return
{
compare
,
origin
:
v
,
isNew
:
compare
===
-
1
,
...
data
}
return
{
compare
,
origin
:
v
,
isNew
:
compare
===
-
1
,
...
data
}
},
},
setVersion
:
version
=>
{
setVersion
:
function
(
version
)
{
const
v
=
storediff
.
get
(
GLSX_COMMON_VERSION_STORAGE_VERSION
)
const
v
=
storediff
.
get
(
this
.
store_version_key
)
if
(
v
===
version
)
return
if
(
v
===
version
)
return
storediff
.
set
(
GLSX_COMMON_VERSION_STORAGE_VERSION
,
version
)
storediff
.
set
(
this
.
store_version_key
,
version
)
}
}
}
}
function
compareVersions
(
version1
,
version2
)
{
function
compareVersions
(
version1
,
version2
)
{
...
@@ -31,5 +32,32 @@ function compareVersions(version1, version2) {
...
@@ -31,5 +32,32 @@ function compareVersions(version1, version2) {
}
}
return
0
// version1 == version2
return
0
// version1 == version2
}
}
function
getParams
(
path
,
attr
)
{
const
_params
=
''
if
(
path
.
includes
(
'?'
)){
_params
=
path
.
split
(
'?'
)[
1
]
}
else
{
return
''
}
return
new
Map
(
_params
.
split
(
'&'
)
.
map
(
kv
=>
kv
.
split
(
'='
))
).
get
(
attr
)
}
export
function
setProduct
(
data
)
{
const
{
moduleUrl
}
=
data
let
mod
=
getParams
(
this
.
options
.
request
.
baseUrl
,
'moduleUrl'
)
if
(
!
mod
)
{
mod
=
moduleUrl
}
if
(
!
mod
)
{
mod
=
this
.
options
.
product
Object
.
assign
(
data
,
{
moduleUrl
:
mod
})
}
this
.
store_version_key
=
`
${
GLSX_COMMON_VERSION_STORAGE_VERSION
}
_
${
mod
.
toUpperCase
()}
`
}
export
default
webwx
export
default
webwx
src/function/version/index.js
View file @
048b9cb
import
Request
from
'../../request/index'
import
Request
from
'../../request/index'
import
webwx
from
'./helper'
import
webwx
,
{
setProduct
}
from
'./helper'
class
Version
{
class
Version
{
constructor
(
options
=
{})
{
constructor
(
options
=
{})
{
options
.
alert
=
options
.
alert
||
function
()
{}
options
.
alert
=
options
.
alert
||
function
()
{}
options
.
request
.
returnAll
=
false
options
.
request
.
returnAll
=
false
this
.
options
=
options
this
.
request
=
new
Request
(
options
)
this
.
request
=
new
Request
(
options
)
}
}
async
get
(
data
)
{
async
get
(
data
=
{})
{
setProduct
.
call
(
this
,
data
)
const
versionInfo
=
await
this
.
request
.
get
(
data
)
const
versionInfo
=
await
this
.
request
.
get
(
data
)
if
(
!
versionInfo
||
!
versionInfo
.
version
)
{
throw
new
Error
(
'未获取到版本信息,请检查配置'
)
}
this
.
version
=
versionInfo
this
.
version
=
versionInfo
return
this
.
compare
()
return
this
.
compare
()
}
}
set
(
version
=
this
.
version
.
version
)
{
set
(
version
=
this
.
version
.
version
)
{
webwx
.
setVersion
(
version
)
webwx
.
setVersion
.
call
(
this
,
version
)
return
this
.
compare
()
return
this
.
compare
()
}
}
compare
()
{
compare
()
{
return
webwx
.
getResult
(
this
.
version
)
return
webwx
.
getResult
.
call
(
this
)
}
}
}
}
export
default
Version
export
default
Version
src/request/helper.js
View file @
048b9cb
...
@@ -26,15 +26,17 @@ const createSign = function (params, secret, noSignAttr = [], way = 'sha1') {
...
@@ -26,15 +26,17 @@ const createSign = function (params, secret, noSignAttr = [], way = 'sha1') {
return
collect
return
collect
}
}
const
webRequest
=
async
({
method
,
obj
=
{},
baseUrl
,
headers
=
{}
},
returnAll
,
logoutCallback
,
alert
,
initResponse
)
=>
{
const
webRequest
=
async
function
(
method
,
obj
=
{},
returnAll
)
{
const
baseUrl
=
this
.
request
.
baseUrl
let
requestAddrss
=
baseUrl
let
requestAddrss
=
baseUrl
const
headers
=
this
.
headers
const
settings
=
{
const
settings
=
{
method
method
}
}
if
(
method
.
toUpperCase
()
===
RequestMethods
.
GET
)
{
if
(
method
.
toUpperCase
()
===
RequestMethods
.
GET
)
{
const
params
=
[]
const
params
=
[]
Object
.
keys
(
obj
).
map
(
key
=>
params
.
push
(
`
${
key
}
=
${
encodeURIComponent
(
obj
[
key
])}
`
))
Object
.
keys
(
obj
).
map
(
key
=>
params
.
push
(
`
${
key
}
=
${
encodeURIComponent
(
obj
[
key
])}
`
))
requestAddrss
=
baseUrl
+
(
params
.
length
?
`
?
${
params
.
join
(
'&'
)}
`
:
''
)
requestAddrss
=
baseUrl
+
(
params
.
length
?
`
${[
'?'
,
'&'
][
+
baseUrl
.
includes
(
'?'
)]}
${
params
.
join
(
'&'
)}
`
:
''
)
settings
.
headers
=
headers
settings
.
headers
=
headers
}
else
{
}
else
{
settings
.
headers
=
Object
.
assign
({
'Content-Type'
:
'application/json'
},
headers
)
settings
.
headers
=
Object
.
assign
({
'Content-Type'
:
'application/json'
},
headers
)
...
@@ -43,20 +45,19 @@ const webRequest = async ({ method, obj = {}, baseUrl, headers = {} }, returnAll
...
@@ -43,20 +45,19 @@ const webRequest = async ({ method, obj = {}, baseUrl, headers = {} }, returnAll
try
{
try
{
const
res
=
await
window
.
fetch
(
requestAddrss
,
settings
)
const
res
=
await
window
.
fetch
(
requestAddrss
,
settings
)
const
data
=
await
res
.
json
()
const
data
=
await
res
.
json
()
return
responseHandler
(
data
,
returnAll
,
initResponse
,
alert
,
logoutCallback
)
return
responseHandler
.
call
(
this
,
data
,
returnAll
)
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
`接口异常:`
,
error
)
console
.
error
(
`接口异常:`
,
error
)
throw
error
throw
error
}
}
}
}
const
wxRequest
=
async
({
method
,
obj
=
{},
baseUrl
},
returnAll
,
logoutCallback
,
alert
,
initResponse
)
=>
{
const
wxRequest
=
async
function
(
method
,
obj
=
{},
returnAll
)
{
wx
.
showNavigationBarLoading
()
const
header
=
method
.
toUpperCase
()
===
RequestMethods
.
POST
?
{
'content-type'
:
'application/json'
}
:
{}
const
header
=
method
.
toUpperCase
()
===
RequestMethods
.
POST
?
{
'content-type'
:
'application/json'
}
:
{}
try
{
try
{
const
res
=
await
new
Promise
((
resolve
,
reject
)
=>
{
const
res
=
await
new
Promise
((
resolve
,
reject
)
=>
{
wx
.
request
({
wx
.
request
({
url
:
baseUrl
,
url
:
this
.
request
.
baseUrl
,
data
:
obj
,
data
:
obj
,
method
,
method
,
header
,
header
,
...
@@ -68,18 +69,18 @@ const wxRequest = async ({ method, obj = {}, baseUrl }, returnAll, logoutCallbac
...
@@ -68,18 +69,18 @@ const wxRequest = async ({ method, obj = {}, baseUrl }, returnAll, logoutCallbac
})
})
const
{
statusCode
,
data
,
errMsg
}
=
res
const
{
statusCode
,
data
,
errMsg
}
=
res
if
(
statusCode
==
'200'
)
{
if
(
statusCode
==
'200'
)
{
return
responseHandler
(
data
,
returnAll
,
initResponse
,
alert
,
logoutCallback
)
return
responseHandler
.
call
(
this
,
data
,
returnAll
)
}
}
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
`接口异常:`
,
error
)
console
.
error
(
`接口异常:`
,
error
)
alert
(
'系统异常'
)
this
.
alert
&&
this
.
alert
(
'系统异常'
)
throw
error
throw
error
}
finally
{
wx
.
hideNavigationBarLoading
()
}
}
}
}
function
responseHandler
(
res
,
returnAll
,
initResponse
,
alert
,
logoutCallback
)
{
function
responseHandler
(
res
,
returnAll
)
{
const
{
code_key
,
errId
,
code_success_value
,
data_key
,
errId_logout_value
,
logout_function
}
=
initResponse
const
{
code_key
,
errId
,
code_success_value
,
data_key
,
errId_logout_value
,
logout_function
}
=
this
.
response
const
alert
=
this
.
alert
const
logoutCallback
=
this
.
logoutCallback
let
codekey
=
Array
.
isArray
(
code_key
)
?
code_key
.
find
(
key
=>
res
.
hasOwnProperty
(
key
))
:
code_key
let
codekey
=
Array
.
isArray
(
code_key
)
?
code_key
.
find
(
key
=>
res
.
hasOwnProperty
(
key
))
:
code_key
let
code
=
res
[
codekey
]
let
code
=
res
[
codekey
]
let
errValue
=
res
[
errId
]
let
errValue
=
res
[
errId
]
...
@@ -112,15 +113,11 @@ function logout(logout_function, logoutCallback) {
...
@@ -112,15 +113,11 @@ function logout(logout_function, logoutCallback) {
window
.
history
.
pushState
({
page
:
window
.
location
.
href
},
''
,
'/login'
)
window
.
history
.
pushState
({
page
:
window
.
location
.
href
},
''
,
'/login'
)
}
}
}
}
const
curl
=
({
data
,
method
,
request
,
headers
,
noSignAttr
,
returnAll
,
logoutCallback
,
isMiniprogram
,
alert
,
initResponse
,
sign
})
=>
{
//{ data, method, request, headers, noSignAttr, returnAll, logoutCallback, isMiniprogram, alert, initResponse, sign }
const
curl
=
function
(
data
,
method
,
noSignAttr
,
returnAll
)
{
const
request
=
this
.
request
// 调用getSign方法,传入请求数据和请求路径,得到签名
// 调用getSign方法,传入请求数据和请求路径,得到签名
const
signData
=
request
.
isSign
?
createSign
(
Object
.
assign
({},
request
.
sign
,
data
),
request
.
secret
,
noSignAttr
,
request
.
signWay
)
:
data
const
signData
=
request
.
isSign
?
createSign
(
Object
.
assign
({},
request
.
sign
,
data
),
request
.
secret
,
noSignAttr
,
request
.
signWay
)
:
data
const
options
=
{
return
[
webRequest
,
wxRequest
][
this
.
isMiniprogram
].
call
(
this
,
method
,
signData
,
returnAll
)
obj
:
signData
,
method
,
baseUrl
:
request
.
baseUrl
,
headers
}
return
[
webRequest
,
wxRequest
][
isMiniprogram
](
options
,
returnAll
,
logoutCallback
,
alert
,
initResponse
)
}
}
export
{
curl
}
export
{
curl
}
src/request/index.js
View file @
048b9cb
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
* @Author: limin
* @Author: limin
* @Date: 2023-07-04 17:25:31
* @Date: 2023-07-04 17:25:31
* @LastEditors: limin
* @LastEditors: limin
* @LastEditTime: 2023-08-
18 16:29:02
* @LastEditTime: 2023-08-
24 09:56:28
* @FilePath: \glsx-common-sdk\src\request\index.js
* @FilePath: \glsx-common-sdk\src\request\index.js
*/
*/
import
{
curl
}
from
'./helper'
import
{
curl
}
from
'./helper'
...
@@ -32,7 +32,8 @@ class Request {
...
@@ -32,7 +32,8 @@ class Request {
}
}
/**封装的请求方法 */
/**封装的请求方法 */
fetch
({
data
,
noSignAttr
=
[],
method
=
RequestMethods
.
POST
,
returnAll
=
false
})
{
fetch
({
data
,
noSignAttr
=
[],
method
=
RequestMethods
.
POST
,
returnAll
=
false
})
{
return
curl
({
data
,
method
,
request
:
this
.
request
,
headers
:
this
.
headers
,
noSignAttr
,
returnAll
,
logoutCallback
:
this
.
logoutCallback
,
isMiniprogram
:
this
.
isMiniprogram
,
alert
:
this
.
alert
,
initResponse
:
this
.
response
})
// return curl({ data, method, request: this.request, headers: this.headers, noSignAttr, returnAll, logoutCallback: this.logoutCallback, isMiniprogram: this.isMiniprogram, alert: this.alert, initResponse: this.response })
return
curl
.
call
(
this
,
data
,
method
,
noSignAttr
,
returnAll
)
}
}
}
}
export
default
Request
export
default
Request
src/request/responseHandler.js
0 → 100644
View file @
048b9cb
export
default
function
handler
(
data
,
returnAll
)
{
const
{
response
:
{
code_key
,
errId
=
'errId'
,
code_success_value
=
'0'
,
data_key
=
'data'
,
errId_logout_value
,
logout_function
=
'logout'
},
alert
}
=
this
.
customer
// if (returnAll || !alert) {
// return res
// }
let
codekey
=
Array
.
isArray
(
code_key
)
?
code_key
.
find
(
key
=>
res
.
hasOwnProperty
(
key
))
:
code_key
let
code
=
res
[
codekey
]
let
errValue
=
res
[
errId
]
if
(
returnAll
||
!
alert
)
{
if
((
code
&&
code
==
errId_logout_value
)
||
(
errValue
&&
errValue
==
errId_logout_value
))
{
logout
(
logout_function
)
}
return
res
}
// 特殊的code处理,可以增加
switch
(
code
+
''
)
{
case
code_success_value
+
''
:
break
case
errId_logout_value
+
''
:
logout
(
logout_function
)
break
default
:
if
(
errValue
&&
errValue
==
errId_logout_value
)
{
return
logout
(
logout_function
)
}
alert
(
res
.
message
||
res
.
msg
||
'出错了,请联系管理员'
)
break
}
return
res
[
data_key
]
}
Write
Preview
Markdown
is supported
Attach a file
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 post a comment