Commit 7808430f by 张立顺

feat webview新增登录缓存

1 parent a2b3c672
// pages/webView/webView.js // pages/webView/webView.js
import commonApi from '~/api/api' import commonApi from '~/api/api'
const WEBVIEW_H5_TOKENS_KEY = 'dj_boutique_webview_h5_tokens'
let time let time
Page({ Page({
/** /**
...@@ -9,6 +12,26 @@ Page({ ...@@ -9,6 +12,26 @@ Page({
flag: true, flag: true,
isH5Bind: false 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) { getParams(str) {
let params = str.split('?') let params = str.split('?')
if (params.length < 2) return {} if (params.length < 2) return {}
...@@ -29,6 +52,18 @@ Page({ ...@@ -29,6 +52,18 @@ Page({
onLoad: function (options) { onLoad: function (options) {
wx.$util.showLoading('加载中...') wx.$util.showLoading('加载中...')
let decodeUrl = decodeURIComponent(options.url) 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) let param = this.getParams(decodeUrl)
if (param.urlParam) { if (param.urlParam) {
let query = JSON.parse(param.urlParam) let query = JSON.parse(param.urlParam)
...@@ -115,6 +150,19 @@ Page({ ...@@ -115,6 +150,19 @@ Page({
this.setData({ this.setData({
isH5Bind: true 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) {}
}
}
} }
}, },
......
...@@ -136,6 +136,7 @@ const removeStorage = () => { ...@@ -136,6 +136,7 @@ const removeStorage = () => {
wx.removeStorageSync('dj_boutique_sessionId') wx.removeStorageSync('dj_boutique_sessionId')
wx.removeStorageSync('dateStaffPop') wx.removeStorageSync('dateStaffPop')
wx.removeStorageSync('abnormalOrderPop') wx.removeStorageSync('abnormalOrderPop')
wx.removeStorageSync('dj_boutique_webview_h5_tokens')
} }
const showNoAccess = () => { const showNoAccess = () => {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!