generated from duanshuwen/webapp-vue-frontend
feat: 请求环境的配置
This commit is contained in:
@@ -5,5 +5,5 @@ VITE_ENV = development
|
|||||||
VITE_CONSOLE = 1
|
VITE_CONSOLE = 1
|
||||||
|
|
||||||
# 接口地址
|
# 接口地址
|
||||||
VITE_BASE_API =
|
VITE_BASE_API = http://8.138.234.141/ingress/hotelBiz
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ VITE_ENV = production
|
|||||||
VITE_CONSOLE = 0
|
VITE_CONSOLE = 0
|
||||||
|
|
||||||
# 接口地址
|
# 接口地址
|
||||||
VITE_BASE_API =
|
VITE_BASE_API = https://biz.nianxx.cn/hotelBiz
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ VITE_ENV = staging
|
|||||||
VITE_CONSOLE = 1
|
VITE_CONSOLE = 1
|
||||||
|
|
||||||
# 接口地址
|
# 接口地址
|
||||||
VITE_BASE_API =
|
VITE_BASE_API = http://8.138.234.141/ingress/hotelBiz
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,222 +0,0 @@
|
|||||||
import { bindEvent, last } from 'vtils'
|
|
||||||
|
|
||||||
const Track = {
|
|
||||||
app: null,
|
|
||||||
ready: false,
|
|
||||||
App: {
|
|
||||||
created() {
|
|
||||||
Track.app = this
|
|
||||||
},
|
|
||||||
watch: {
|
|
||||||
'$route.path': {
|
|
||||||
immediate: true,
|
|
||||||
handler: async (fromPath, toPath) => {
|
|
||||||
await Track.ensureReady()
|
|
||||||
Track.app.$nextTick(async () => {
|
|
||||||
const { id, name } = await Track.injectMark()
|
|
||||||
window.loadEventPush(
|
|
||||||
fromPath,
|
|
||||||
toPath || '',
|
|
||||||
Track.app.$route.query,
|
|
||||||
{
|
|
||||||
mark_id: id,
|
|
||||||
mark_name: name
|
|
||||||
}
|
|
||||||
)
|
|
||||||
Track.app.$nextTick(() => {
|
|
||||||
window.elementAddEventListener()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
ensureReady: async () => {
|
|
||||||
if (Track.ready) return
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
const timer = setInterval(() => {
|
|
||||||
if (
|
|
||||||
// @ts-ignore
|
|
||||||
typeof window.Countly !== 'undefined' &&
|
|
||||||
// @ts-ignore
|
|
||||||
typeof window.anyEventPush !== 'undefined' &&
|
|
||||||
Track.app !== null
|
|
||||||
) {
|
|
||||||
clearInterval(timer)
|
|
||||||
Track.ready = true
|
|
||||||
resolve()
|
|
||||||
}
|
|
||||||
}, 60)
|
|
||||||
})
|
|
||||||
},
|
|
||||||
prepareData(data) {
|
|
||||||
return {
|
|
||||||
...(data.id && data.name
|
|
||||||
? {
|
|
||||||
control_id: data.id,
|
|
||||||
control_name: data.name,
|
|
||||||
control_value: data.value || data.name
|
|
||||||
}
|
|
||||||
: {}),
|
|
||||||
...(data.goods
|
|
||||||
? data.goods.isUniGoods
|
|
||||||
? {
|
|
||||||
content_id: data.goods.id,
|
|
||||||
content_name: data.goods.name,
|
|
||||||
resource_id: data.goods.resourceId,
|
|
||||||
resource_name: data.goods.resourceName,
|
|
||||||
resource_position_id: data.goods.resourcePositionId,
|
|
||||||
resource_position_name: data.goods.resourcePositionName,
|
|
||||||
resource_position_no: data.index || 0
|
|
||||||
}
|
|
||||||
: {
|
|
||||||
content_id: data.goods.commodityId,
|
|
||||||
content_name:
|
|
||||||
data.goods.commodityTitle ||
|
|
||||||
// 兼容订单下的商品
|
|
||||||
data.goods.name,
|
|
||||||
...(data.goods.commodityResourceId
|
|
||||||
? {
|
|
||||||
resource_id: data.goods.commodityResourceId,
|
|
||||||
resource_name: data.goods.commodityTitle,
|
|
||||||
resource_position_id: '',
|
|
||||||
resource_position_name: data.name,
|
|
||||||
resource_position_no: data.index || 0
|
|
||||||
}
|
|
||||||
: {})
|
|
||||||
}
|
|
||||||
: {}),
|
|
||||||
...(data.resource
|
|
||||||
? {
|
|
||||||
resource_id: data.resource.id,
|
|
||||||
resource_name: data.resource.title || data.resource.name || '',
|
|
||||||
resource_position_id: data.resource.stateId || '',
|
|
||||||
resource_position_name: data.resource.stateName || '',
|
|
||||||
resource_position_no: data.index || 0
|
|
||||||
}
|
|
||||||
: {}),
|
|
||||||
...(data.resourcePosition
|
|
||||||
? {
|
|
||||||
resource_position_id: data.resourcePosition.id,
|
|
||||||
resource_position_name: data.resourcePosition.name,
|
|
||||||
resource_position_no: data.index || 0
|
|
||||||
}
|
|
||||||
: {}),
|
|
||||||
...(data.mark
|
|
||||||
? {
|
|
||||||
mark_id: data.mark.id,
|
|
||||||
mark_name: data.mark.name
|
|
||||||
}
|
|
||||||
: {}),
|
|
||||||
...(data.content
|
|
||||||
? {
|
|
||||||
content_name: data.content
|
|
||||||
}
|
|
||||||
: {})
|
|
||||||
}
|
|
||||||
},
|
|
||||||
pushEvent: async (data) => {
|
|
||||||
await Track.ensureReady()
|
|
||||||
const trackData = Track.prepareData(data)
|
|
||||||
if (!trackData.mark_id && !trackData.mark_name) {
|
|
||||||
const { id, name } = await Track.injectMark()
|
|
||||||
trackData.mark_id = id
|
|
||||||
trackData.mark_name = name
|
|
||||||
}
|
|
||||||
window.anyEventPush(data.type, data.path, data.name, trackData)
|
|
||||||
},
|
|
||||||
provideMark(cb) {
|
|
||||||
return {
|
|
||||||
methods: {
|
|
||||||
provideTrackMark: cb
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
injectMark: async () => {
|
|
||||||
const currentRoute = Track.app.$route
|
|
||||||
const matched = currentRoute.matched
|
|
||||||
const currentPage = last(matched)
|
|
||||||
const currentPageInstance = currentPage?.instances.default
|
|
||||||
const provideTrackMark = currentPageInstance?.provideTrackMark
|
|
||||||
const { id = 'NONE', name = 'NONE' } =
|
|
||||||
typeof provideTrackMark === 'function' ? await provideTrackMark() : {}
|
|
||||||
return {
|
|
||||||
id,
|
|
||||||
name
|
|
||||||
}
|
|
||||||
},
|
|
||||||
install(app, options) {
|
|
||||||
Track.ensureReady().then(() => {
|
|
||||||
// @ts-ignore
|
|
||||||
countlyConfig.baseContextPath = options.appid
|
|
||||||
})
|
|
||||||
|
|
||||||
async function applyTrack(el, payload) {
|
|
||||||
await Track.ensureReady()
|
|
||||||
let data = payload.value
|
|
||||||
// @ts-ignore
|
|
||||||
if (data && !data.disabled) {
|
|
||||||
data = {
|
|
||||||
...data,
|
|
||||||
name: `${options.prefix || ''}${data.name}`,
|
|
||||||
// @ts-ignore
|
|
||||||
path: el.__track_data__?.path || window.getRoutePath()
|
|
||||||
}
|
|
||||||
|
|
||||||
el.__track_data__ = data
|
|
||||||
|
|
||||||
// 兼容曝光事件
|
|
||||||
if (data.expose !== false) {
|
|
||||||
el.setAttribute('exposure-event', data.name)
|
|
||||||
el.setAttribute(
|
|
||||||
'extend_attr',
|
|
||||||
JSON.stringify(Track.prepareData(data))
|
|
||||||
)
|
|
||||||
if (data.value !== null) {
|
|
||||||
el.setAttribute('alt', data.value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const shouldListen = !el.dataset.track
|
|
||||||
if (shouldListen) {
|
|
||||||
el.dataset.track = '1'
|
|
||||||
el.__track_dispose__ = bindEvent(el)(
|
|
||||||
'click',
|
|
||||||
() => {
|
|
||||||
console.log(`埋点:点击「${data.name}」`)
|
|
||||||
Track.pushEvent({
|
|
||||||
type: 'click',
|
|
||||||
...el.__track_data__,
|
|
||||||
value: el.innerText.trim().replace(/\s+/g, '')
|
|
||||||
})
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// 必须使用捕获模式以在其他事件触发前处理(比如其他事件触发了页面跳转,就会导致问题)
|
|
||||||
capture: true,
|
|
||||||
// 使用被动模式告诉浏览器这不会产生阻塞行为
|
|
||||||
passive: true
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function disposeTrack(el) {
|
|
||||||
// 需延时一下,不然当遇到点击跳转时页面卸载把事件也卸载了就触发不了了
|
|
||||||
setTimeout(() => {
|
|
||||||
delete el.__track_data__
|
|
||||||
el.__track_dispose__?.()
|
|
||||||
delete el.__track_dispose__
|
|
||||||
}, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
app.directive('track', {
|
|
||||||
mounted: applyTrack,
|
|
||||||
updated: applyTrack,
|
|
||||||
beforeUpdate: applyTrack,
|
|
||||||
unmounted: disposeTrack
|
|
||||||
})
|
|
||||||
console.log(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export default Track
|
|
||||||
@@ -1,131 +1,47 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import {
|
import { tansParams } from '@utils/tansParams'
|
||||||
md5Hash,
|
import { Session } from '@utils/storage'
|
||||||
rsaEncrypt,
|
|
||||||
rsaDecrypt,
|
|
||||||
aesEncrypt,
|
|
||||||
aesDecrypt,
|
|
||||||
generateUUID,
|
|
||||||
dataSign,
|
|
||||||
generateRandomString,
|
|
||||||
getLocalStorage
|
|
||||||
} from '@dcb/utils'
|
|
||||||
|
|
||||||
const encryptionOff = Number(import.meta.env.VITE_ENCRYPTION)
|
// 获取.env中的服务地址
|
||||||
const env = import.meta.env.VITE_ENV === 'development'
|
const { VITE_BASE_API, VITE_ENV } = import.meta.env
|
||||||
const STORE_OFF = Number(import.meta.env.VITE_STORE_OFF)
|
console.log('🚀 ~ VITE_ENV:', VITE_ENV)
|
||||||
const RSA = import.meta.env.VITE_APP_RSA
|
console.log('🚀 ~ VITE_BASE_API:', VITE_BASE_API)
|
||||||
const DATA_RSA = STORE_OFF ? import.meta.env.VITE_DATA_RSA : RSA
|
|
||||||
const martId = import.meta.env.VITE_MART_ID
|
|
||||||
const appKey = import.meta.env.VITE_KEY
|
|
||||||
const appSecret = import.meta.env.VITE_SECRET
|
|
||||||
|
|
||||||
const request = async (method, url, data, config = {}) => {
|
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
|
||||||
const p = env ? import.meta.env.VITE_P : decodeURI(getLocalStorage('p'))
|
|
||||||
const options = Object.assign({}, config, {
|
|
||||||
url,
|
|
||||||
method
|
|
||||||
})
|
|
||||||
|
|
||||||
console.log('入参', data)
|
// 创建axios对象
|
||||||
|
const instance = axios.create({
|
||||||
|
baseURL: VITE_ENV === 'development' ? '/ingress/hotelBiz' : VITE_BASE_API,
|
||||||
|
// 超时
|
||||||
|
timeout: 10000
|
||||||
|
})
|
||||||
|
|
||||||
const uuid = generateUUID()
|
// 添加拦截器
|
||||||
const secret = '4RBA7^@0$@KM$5D2333'
|
instance.interceptors.request.use((config) => {
|
||||||
const title = md5Hash(uuid + secret)
|
const token = Session.getToken()
|
||||||
const aesKey = generateRandomString(16) // 生成16位随机数
|
config.headers['Authorization'] = `Bearer ${token}`
|
||||||
|
|
||||||
const init = rsaEncrypt(aesKey, RSA)
|
// get请求映射params参数
|
||||||
const base = aesEncrypt(JSON.stringify(data), aesKey)
|
if (config.method === 'get' && config.params) {
|
||||||
|
let url = config.url + '?' + tansParams(config.params)
|
||||||
|
console.log('🚀 ~ url:', url)
|
||||||
|
|
||||||
options.data = options.data || {}
|
url = url.slice(0, -1)
|
||||||
|
config.params = {}
|
||||||
/*
|
config.url = url
|
||||||
* @ 验签
|
|
||||||
* @wiki: http://wiki.gogpay.cn/pages/viewpage.action?pageId=75301563
|
|
||||||
* */
|
|
||||||
url = url.replace(/^https?:\/\/.+?\/([^/]+)/, '')
|
|
||||||
|
|
||||||
const timestamp = Date.now()
|
|
||||||
let asHex = ''
|
|
||||||
const header = Object.assign(
|
|
||||||
{ clientId: '03' },
|
|
||||||
STORE_OFF ? { martId, path: url, timestamp } : { path: url, timestamp }
|
|
||||||
)
|
|
||||||
|
|
||||||
for (const [key, value] of Object.entries(header)) {
|
|
||||||
asHex += key + value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log(asHex + appSecret)
|
return config
|
||||||
const sign = dataSign(header, appSecret)
|
})
|
||||||
|
|
||||||
if (method === 'post') {
|
// 添加响应拦截器
|
||||||
if (!config.white && encryptionOff) {
|
instance.interceptors.response.use(
|
||||||
options.data = {
|
(res) => {
|
||||||
timestamp: uuid,
|
return Promise.resolve(res.data.data)
|
||||||
title,
|
},
|
||||||
init,
|
(error) => {
|
||||||
base
|
return Promise.reject(error)
|
||||||
}
|
|
||||||
} else {
|
|
||||||
options.data = data
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
|
||||||
options.headers = Object.assign({}, options.headers, header, {
|
export default instance
|
||||||
sign,
|
|
||||||
appKey,
|
|
||||||
p
|
|
||||||
})
|
|
||||||
|
|
||||||
console.log(options.headers, STORE_OFF)
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
axios
|
|
||||||
.request(options)
|
|
||||||
.then((res) => {
|
|
||||||
let { data } = res
|
|
||||||
|
|
||||||
if (!data) {
|
|
||||||
return resolve(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (method === 'post' && encryptionOff && !config.white) {
|
|
||||||
data = JSON.parse(
|
|
||||||
aesDecrypt(data.base, rsaDecrypt(data.init, DATA_RSA))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('返回值', data)
|
|
||||||
resolve(data)
|
|
||||||
})
|
|
||||||
.catch((res) => {
|
|
||||||
reject(res)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export const ajax = {
|
|
||||||
request,
|
|
||||||
get(url, config) {
|
|
||||||
return request('get', url, null, config)
|
|
||||||
},
|
|
||||||
delete(url, config) {
|
|
||||||
return request('delete', url, null, config)
|
|
||||||
},
|
|
||||||
head(url, config) {
|
|
||||||
return request('head', url, null, config)
|
|
||||||
},
|
|
||||||
post(url, data, config) {
|
|
||||||
return request('post', url, data, config)
|
|
||||||
},
|
|
||||||
put(url, data, config) {
|
|
||||||
return request('put', url, data, config)
|
|
||||||
},
|
|
||||||
patch(url, data, config) {
|
|
||||||
return request('path', url, data, config)
|
|
||||||
},
|
|
||||||
setCommonHeader(key, value) {
|
|
||||||
window.axios.defaults.headers.common[key] = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
/* eslint-disable no-process-env */
|
|
||||||
import { initAjax } from '@dcb/ajax'
|
|
||||||
const debug = import.meta.env.VITE_ENV
|
|
||||||
const RSA = import.meta.env.VITE_APP_RSA
|
|
||||||
const appid = import.meta.env.VITE_APP_ID
|
|
||||||
const BASE_API = import.meta.env.VITE_BASE_API
|
|
||||||
const encryptionOff = import.meta.env.VITE_ENCRYPTION
|
|
||||||
|
|
||||||
export const useAjax = function () {
|
|
||||||
const ajax = initAjax(true)
|
|
||||||
const options = { RSA, appid, BASE_API, encryptionOff, debug }
|
|
||||||
|
|
||||||
ajax.config(options)
|
|
||||||
|
|
||||||
return ajax
|
|
||||||
}
|
|
||||||
70
src/utils/storage.ts
Normal file
70
src/utils/storage.ts
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
/**
|
||||||
|
* window.localStorage 浏览器永久缓存
|
||||||
|
* @method set 设置永久缓存
|
||||||
|
* @method get 获取永久缓存
|
||||||
|
* @method remove 移除永久缓存
|
||||||
|
* @method clear 移除全部永久缓存
|
||||||
|
*/
|
||||||
|
export const Local = {
|
||||||
|
// 查看 v2.4.3版本更新日志
|
||||||
|
setKey(key: string) {
|
||||||
|
// @ts-ignore
|
||||||
|
return `${__NEXT_NAME__}:${key}`
|
||||||
|
},
|
||||||
|
|
||||||
|
// 设置永久缓存
|
||||||
|
set<T>(key: string, val: T) {
|
||||||
|
window.localStorage.setItem(Local.setKey(key), JSON.stringify(val))
|
||||||
|
},
|
||||||
|
|
||||||
|
// 获取永久缓存
|
||||||
|
get(key: string) {
|
||||||
|
let json = <string>window.localStorage.getItem(Local.setKey(key))
|
||||||
|
return JSON.parse(json)
|
||||||
|
},
|
||||||
|
|
||||||
|
// 移除永久缓存
|
||||||
|
remove(key: string) {
|
||||||
|
window.localStorage.removeItem(Local.setKey(key))
|
||||||
|
},
|
||||||
|
|
||||||
|
// 移除全部永久缓存
|
||||||
|
clear() {
|
||||||
|
window.localStorage.clear()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* window.sessionStorage 浏览器临时缓存
|
||||||
|
* @method set 设置临时缓存
|
||||||
|
* @method get 获取临时缓存
|
||||||
|
* @method remove 移除临时缓存
|
||||||
|
* @method clear 移除全部临时缓存
|
||||||
|
*/
|
||||||
|
export const Session = {
|
||||||
|
// 设置临时缓存
|
||||||
|
set(key: string, val: any) {
|
||||||
|
window.sessionStorage.setItem(key, JSON.stringify(val))
|
||||||
|
},
|
||||||
|
|
||||||
|
// 获取临时缓存
|
||||||
|
get(key: string) {
|
||||||
|
let json = <string>window.sessionStorage.getItem(key)
|
||||||
|
return JSON.parse(json)
|
||||||
|
},
|
||||||
|
|
||||||
|
// 移除临时缓存
|
||||||
|
remove(key: string) {
|
||||||
|
window.sessionStorage.removeItem(key)
|
||||||
|
},
|
||||||
|
|
||||||
|
// 移除全部临时缓存
|
||||||
|
clear() {
|
||||||
|
window.sessionStorage.clear()
|
||||||
|
},
|
||||||
|
|
||||||
|
// 获取当前存储的 token
|
||||||
|
getToken() {
|
||||||
|
return this.get('token')
|
||||||
|
}
|
||||||
|
}
|
||||||
29
src/utils/tansParams.ts
Normal file
29
src/utils/tansParams.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
/**
|
||||||
|
* 参数处理
|
||||||
|
* @param {*} params 参数
|
||||||
|
*/
|
||||||
|
export function tansParams(params: any) {
|
||||||
|
let result = ''
|
||||||
|
|
||||||
|
for (const propName of Object.keys(params)) {
|
||||||
|
const value = params[propName]
|
||||||
|
|
||||||
|
var part = encodeURIComponent(propName) + "="
|
||||||
|
|
||||||
|
if (value !== null && value !== "" && typeof (value) !== "undefined") {
|
||||||
|
if (typeof value === 'object') {
|
||||||
|
for (const key of Object.keys(value)) {
|
||||||
|
if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
|
||||||
|
let params = propName + '[' + key + ']'
|
||||||
|
var subPart = encodeURIComponent(params) + "="
|
||||||
|
result += subPart + encodeURIComponent(value[key]) + "&"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
result += part + encodeURIComponent(value) + "&"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user