Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
saas_plus
/
dj-car-boutique-work-wechat
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
1
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 7808430f
authored
Mar 23, 2026
by
张立顺
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat webview新增登录缓存
1 parent
a2b3c672
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
0 deletions
pages/webView/webView.js
utils/util.js
pages/webView/webView.js
View file @
7808430
// pages/webView/webView.js
import
commonApi
from
'~/api/api'
const
WEBVIEW_H5_TOKENS_KEY
=
'dj_boutique_webview_h5_tokens'
let
time
Page
({
/**
...
...
@@ -9,6 +12,26 @@ Page({
flag
:
true
,
isH5Bind
:
false
},
/** 从完整 URL 提取 base(协议+域名+路径,不含查询串) */
getBaseUrl
(
url
)
{
if
(
!
url
||
typeof
url
!==
'string'
)
return
''
const
idx
=
url
.
indexOf
(
'?'
)
return
idx
>=
0
?
url
.
slice
(
0
,
idx
)
:
url
},
/** 将对象转为 query 并追加到 URL */
appendParamsToUrl
(
url
,
params
)
{
if
(
!
params
||
typeof
params
!==
'object'
||
Object
.
keys
(
params
).
length
===
0
)
return
url
const
searchParams
=
Object
.
entries
(
params
)
.
filter
(([,
v
])
=>
v
!=
null
&&
v
!==
''
)
.
map
(([
k
,
v
])
=>
`
${
encodeURIComponent
(
k
)}
=
${
encodeURIComponent
(
String
(
v
))}
`
)
.
join
(
'&'
)
if
(
!
searchParams
)
return
url
const
separator
=
url
.
includes
(
'?'
)
?
'&'
:
'?'
return
`
${
url
}${
separator
}${
searchParams
}
`
},
getParams
(
str
)
{
let
params
=
str
.
split
(
'?'
)
if
(
params
.
length
<
2
)
return
{}
...
...
@@ -29,6 +52,18 @@ Page({
onLoad
:
function
(
options
)
{
wx
.
$util
.
showLoading
(
'加载中...'
)
let
decodeUrl
=
decodeURIComponent
(
options
.
url
)
// 检查是否有该 URL 对应的 H5 token 缓存,有则追加到链接
const
baseUrl
=
this
.
getBaseUrl
(
decodeUrl
)
try
{
const
stored
=
wx
.
getStorageSync
(
WEBVIEW_H5_TOKENS_KEY
)
if
(
stored
)
{
const
tokens
=
JSON
.
parse
(
stored
)
const
storedData
=
tokens
[
baseUrl
]
if
(
storedData
&&
typeof
storedData
===
'object'
)
{
decodeUrl
=
this
.
appendParamsToUrl
(
decodeUrl
,
storedData
)
}
}
}
catch
(
e
)
{}
let
param
=
this
.
getParams
(
decodeUrl
)
if
(
param
.
urlParam
)
{
let
query
=
JSON
.
parse
(
param
.
urlParam
)
...
...
@@ -115,6 +150,19 @@ Page({
this
.
setData
({
isH5Bind
:
true
})
// 保存 H5 传入的 token 等数据,按 baseUrl 维度存储
const
msgData
=
data
[
0
]
if
(
msgData
&&
typeof
msgData
===
'object'
&&
msgData
!==
null
)
{
const
baseUrl
=
this
.
getBaseUrl
(
this
.
data
.
url
)
if
(
baseUrl
)
{
try
{
const
stored
=
wx
.
getStorageSync
(
WEBVIEW_H5_TOKENS_KEY
)
const
tokens
=
stored
?
JSON
.
parse
(
stored
)
:
{}
tokens
[
baseUrl
]
=
msgData
wx
.
setStorageSync
(
WEBVIEW_H5_TOKENS_KEY
,
JSON
.
stringify
(
tokens
))
}
catch
(
err
)
{}
}
}
}
},
...
...
utils/util.js
View file @
7808430
...
...
@@ -136,6 +136,7 @@ const removeStorage = () => {
wx
.
removeStorageSync
(
'dj_boutique_sessionId'
)
wx
.
removeStorageSync
(
'dateStaffPop'
)
wx
.
removeStorageSync
(
'abnormalOrderPop'
)
wx
.
removeStorageSync
(
'dj_boutique_webview_h5_tokens'
)
}
const
showNoAccess
=
()
=>
{
...
...
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