接口加密
This commit is contained in:
parent
738f058281
commit
d9459b1bd0
19
App.vue
19
App.vue
|
|
@ -142,7 +142,9 @@
|
|||
*/
|
||||
initFromExtraData(extraData) {
|
||||
// 批量设置 Storage,减少 I/O 次数
|
||||
const storageData = {
|
||||
let storageData={}
|
||||
if(uni.getStorageSync('isTest')){
|
||||
storageData = {
|
||||
host: extraData.host,
|
||||
header: {
|
||||
"x-token": extraData['x-token'],
|
||||
|
|
@ -163,6 +165,18 @@
|
|||
encrypt: extraData['encrypt'],
|
||||
decrypt: extraData['decrypt']
|
||||
}
|
||||
}else{
|
||||
storageData = {
|
||||
host: extraData.host,
|
||||
header: {
|
||||
// #ifdef APP-PLUS
|
||||
"header": {
|
||||
"version": this.$version,
|
||||
}
|
||||
// #endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 批量写入
|
||||
Object.keys(storageData).forEach(key => {
|
||||
|
|
@ -171,6 +185,9 @@
|
|||
if (extraData['isCombo']) {
|
||||
uni.setStorageSync('isCombo', extraData['isCombo'])
|
||||
}
|
||||
if (extraData['paytype']) {
|
||||
uni.setStorageSync('isCombo', extraData['paytype'] == 'combo' ? 'ok' : '')
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
|||
Binary file not shown.
56
main.js
56
main.js
|
|
@ -28,9 +28,59 @@ export function createApp() {
|
|||
// #ifdef APP-PLUS
|
||||
app.config.globalProperties.$system = plus.os.name;
|
||||
// #endif
|
||||
app.config.globalProperties.$systemInfo = systemInfo
|
||||
uni.setStorageSync('version', '1.0.6.sp22')
|
||||
app.config.globalProperties.$version = uni.getStorageSync('version')
|
||||
app.config.globalProperties.$systemInfo = systemInfo;
|
||||
uni.setStorageSync('version', '1.0.6.sp22');
|
||||
app.config.globalProperties.$version = uni.getStorageSync('version');
|
||||
// 环境:development→测试,production→正式(跟构建环境走,无需手改)
|
||||
const isTest = process.env.NODE_ENV === 'development';
|
||||
app.config.globalProperties.$isTest = isTest;
|
||||
// #ifdef APP-PLUS
|
||||
const version = plus.runtime.version;
|
||||
// #endif
|
||||
|
||||
const compareVersion = (v1, v2) => {
|
||||
const a = String(v1).split('.')
|
||||
const b = String(v2).split('.')
|
||||
const len = Math.max(a.length, b.length)
|
||||
for (let i = 0; i < len; i++) {
|
||||
const n1 = parseInt(a[i] || '0', 10)
|
||||
const n2 = parseInt(b[i] || '0', 10)
|
||||
if (n1 > n2) return 1
|
||||
if (n1 < n2) return -1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
//安卓和ios
|
||||
// #ifdef APP-PLUS && !APP-HARMONY
|
||||
if (plus.os.name == 'iOS') {
|
||||
if (compareVersion(version, '1.9') >= 0 && !isTest) {
|
||||
uni.setStorageSync('isTest', false)
|
||||
} else {
|
||||
uni.setStorageSync('isTest', true)
|
||||
}
|
||||
} else {
|
||||
if (compareVersion(version, '2.2.0') >= 0 && !isTest) {
|
||||
uni.setStorageSync('isTest', false)
|
||||
} else {
|
||||
uni.setStorageSync('isTest', true)
|
||||
}
|
||||
}
|
||||
// #endif
|
||||
//鸿蒙
|
||||
// #ifdef APP-PLUS && APP-HARMONY
|
||||
if (isTest) {
|
||||
uni.setStorageSync('isTest', true)
|
||||
} else {
|
||||
uni.setStorageSync('isTest', false)
|
||||
}
|
||||
// #endif
|
||||
//h5环境
|
||||
// #ifndef APP-PLUS
|
||||
uni.setStorageSync('isTest', true)
|
||||
// #endif
|
||||
|
||||
|
||||
app.use(globalMethods);
|
||||
return {
|
||||
app
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
"name": "video-player 视频播放器 html5视频播放器-解决频层级、覆盖",
|
||||
"version": "2.0.0",
|
||||
"dependencies": {
|
||||
"@batiao/batiao-sdk-uniapp-unimp": "1.0.374",
|
||||
"crypto-js": "^4.2.0",
|
||||
"lottie-web": "^5.13.0",
|
||||
"uuid": "^13.0.0"
|
||||
|
|
@ -19,6 +20,12 @@
|
|||
"lint-staged": "^16.4.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@batiao/batiao-sdk-uniapp-unimp": {
|
||||
"version": "1.0.374",
|
||||
"resolved": "http://maven.batiao8.com/repository/mpnpm-releases/@batiao/batiao-sdk-uniapp-unimp/-/batiao-sdk-uniapp-unimp-1.0.374.tgz",
|
||||
"integrity": "sha512-jEQBgTwaZewB2ZzJHYQjWsA4UhZnd7xnSIzUdoYTLKJ3B+gXHmGWSNhJ5MzaXKH3Uxbot2mmWT+TSw8WSZN2Sg==",
|
||||
"license": "UNLICENSED"
|
||||
},
|
||||
"node_modules/@eslint-community/eslint-utils": {
|
||||
"version": "4.9.1",
|
||||
"resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.9.1.tgz",
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@
|
|||
]
|
||||
},
|
||||
"dependencies": {
|
||||
"@batiao/batiao-sdk-uniapp-unimp": "1.0.374",
|
||||
"crypto-js": "^4.2.0",
|
||||
"lottie-web": "^5.13.0",
|
||||
"uuid": "^13.0.0"
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import pageData from "@/static/json/page.json"
|
|||
import {
|
||||
jsonString
|
||||
} from "../uni_modules/uv-ui-tools/libs/function/test";
|
||||
import '@batiao/batiao-sdk-uniapp-unimp';
|
||||
export default {
|
||||
install(app, options) {
|
||||
|
||||
|
|
@ -156,48 +157,67 @@ export default {
|
|||
};
|
||||
|
||||
// 请求
|
||||
app.config.globalProperties.$requestPromise = async (data, isCode = true) => {
|
||||
//头部header
|
||||
let header = uni.getStorageSync('header')
|
||||
app.config.globalProperties.$requestPromise = async (data,isCode=true) => {
|
||||
let isTest = uni.getStorageSync('isTest')
|
||||
//头部header(浅拷贝,避免改到 storage 缓存对象)
|
||||
let header = { ...(uni.getStorageSync('header') || {}) }
|
||||
//请求地址
|
||||
let host = uni.getStorageSync('host') + data.url
|
||||
let host
|
||||
// #ifdef H5
|
||||
host = data.url
|
||||
// #endif
|
||||
// #ifndef H5
|
||||
host = uni.getStorageSync('host') + data.url
|
||||
// #endif
|
||||
// 签名
|
||||
if (isCode) {
|
||||
if(isCode){
|
||||
if(isTest){
|
||||
data = sign(data)
|
||||
} else {
|
||||
host = "http://10.3.0.24:9209/" + data.url
|
||||
header['x-token'] = "bdf87a98-eef0-47df-9faf-41d547b0457e"
|
||||
}
|
||||
|
||||
}else{
|
||||
host="http://10.3.0.24:9209/"+ data.url
|
||||
header['x-token']="bdf87a98-eef0-47df-9faf-41d547b0457e"
|
||||
}
|
||||
// 排序
|
||||
const sortedKeys = Object.keys(data.data).sort();
|
||||
let str = ''
|
||||
for (const key of sortedKeys) {
|
||||
if (!(data.url == 'api/image/segment/base64' && key == 'file')) {
|
||||
str += '&' + key + '=' + encodeURIComponent(data.data[key]);
|
||||
}
|
||||
str += '&' + key + '=' + encodeURIComponent(
|
||||
data.data[key] == null ? '' : String(data.data[key])
|
||||
);
|
||||
}
|
||||
str = str.replace('&', '?')
|
||||
// 拼接URL
|
||||
if(isTest){
|
||||
host = host + str
|
||||
console.log(host)
|
||||
console.log(data)
|
||||
}
|
||||
// 鸿蒙 GET/DELETE 若再传 data 会二次拼进 query,导致参数异常;参数已在 URL 中
|
||||
let requestData = data.data
|
||||
// #ifdef APP-HARMONY
|
||||
const method = (data.method || 'GET').toUpperCase()
|
||||
if (method === 'GET' || method === 'DELETE') {
|
||||
requestData = {}
|
||||
}
|
||||
// #endif
|
||||
// 测试环境用 uni.request,正式环境用宿主 nativeRequest
|
||||
const api = isTest
|
||||
? uni.request
|
||||
: (uni.nativeRequest || uni.request)
|
||||
const response = await new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
api({
|
||||
url: host,
|
||||
method: data.method,
|
||||
data: data.data,
|
||||
data: requestData,
|
||||
params: requestData,
|
||||
header,
|
||||
success: res => {
|
||||
console.log(res)
|
||||
try {
|
||||
if (isCode) {
|
||||
if(isCode&&isTest){
|
||||
res.data = deCode(res.data.data)
|
||||
console.log(res.data)
|
||||
} else {
|
||||
res.data = res.data.data
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
|
||||
}
|
||||
resolve(res.data);
|
||||
},
|
||||
|
|
|
|||
|
|
@ -4,7 +4,8 @@ import {
|
|||
} from "../uni_modules/uv-ui-tools/libs/function/index.js";
|
||||
import CryptoJS from "crypto-js";
|
||||
|
||||
// import pageData from "@/static/json/page.json"
|
||||
import '@batiao/batiao-sdk-uniapp-unimp';
|
||||
import pageData from "@/static/json/page.json";
|
||||
|
||||
async function apiUserEvent(type, adminData, uniData) {
|
||||
let uni_version = uni.getStorageSync("version")
|
||||
|
|
@ -118,33 +119,34 @@ function generateUUID() {
|
|||
|
||||
// 主请求函数
|
||||
const request = (method = 'GET', serverUrl, domainUrl, params, dataType = "json") => {
|
||||
let isTest = uni.getStorageSync('isTest')
|
||||
let currentUrl
|
||||
// try {
|
||||
// let pages = getCurrentPages();
|
||||
// let currentPage = pages[pages.length - 2];
|
||||
// currentUrl = currentPage.route;
|
||||
// currentUrl = pageData[currentUrl]
|
||||
// } catch (e) {
|
||||
try {
|
||||
let pages = getCurrentPages();
|
||||
let currentPage = pages[pages.length - 2];
|
||||
currentUrl = currentPage.route;
|
||||
currentUrl = pageData[currentUrl]
|
||||
} catch (e) {
|
||||
|
||||
// }
|
||||
let q = serverUrl
|
||||
}
|
||||
let q=serverUrl
|
||||
// 头部header
|
||||
let header = uni.getStorageSync('header') || {};
|
||||
// 请求地址
|
||||
serverUrl = uni.getStorageSync('host') || serverUrl;
|
||||
if (q == 'test') {
|
||||
serverUrl = "http://10.3.0.24:9209/"
|
||||
header['x-token'] = "bdf87a98-eef0-47df-9faf-41d547b0457e"
|
||||
if(q=='test'){
|
||||
serverUrl="http://10.3.0.24:9209/"
|
||||
header['x-token']="bdf87a98-eef0-47df-9faf-41d547b0457e"
|
||||
}
|
||||
let url = domianUrl(serverUrl, domainUrl);
|
||||
if (q != 'test') {
|
||||
if(q!='test'){
|
||||
params = deepClone(params);
|
||||
}
|
||||
|
||||
var key = uni.getStorageSync('encrypt') || "default_encrypt_key";
|
||||
let data = {};
|
||||
console.log("传参", method === "GET" || method === "DELETE" || q == 'q')
|
||||
if (method === "GET" || method === "DELETE" || q == 'q') {
|
||||
if (method === "GET" || method === "DELETE"||q=='q') {
|
||||
let urls = url
|
||||
if(isTest){
|
||||
params.timestamp = Math.floor(new Date().getTime() / 1000);
|
||||
params.nonce = generateUUID();
|
||||
let keys = Object.keys(params);
|
||||
|
|
@ -154,12 +156,16 @@ const request = (method = 'GET', serverUrl, domainUrl, params, dataType = "json"
|
|||
}
|
||||
let temp = keys.join("&") + "&" + CryptoJS.MD5(key).toString();
|
||||
params.signature = CryptoJS.MD5(temp).toString();
|
||||
urls = url + "?" + (isTest ? encodeParam(params): encodeParam(params));
|
||||
}
|
||||
data = {
|
||||
url: url + "?" + encodeParam(params),
|
||||
url:urls,
|
||||
method,
|
||||
data: params,
|
||||
header: header // setheader(dataType, header),
|
||||
};
|
||||
} else if (method === "POST" || method === "PUT") {
|
||||
if(isTest){
|
||||
let temp = decodeParam(url);
|
||||
temp.timestamp = Math.floor(new Date().getTime() / 1000);
|
||||
temp.nonce = generateUUID();
|
||||
|
|
@ -172,7 +178,9 @@ const request = (method = 'GET', serverUrl, domainUrl, params, dataType = "json"
|
|||
s += "&" + JSON.stringify(params);
|
||||
s += "&" + CryptoJS.MD5(key).toString();
|
||||
temp.signature = CryptoJS.MD5(s).toString();
|
||||
url = url.split("?")[0] + "?" + temp;
|
||||
url = url.split("?")[0] + "?" + encodeParam(temp);
|
||||
}
|
||||
|
||||
data = {
|
||||
url,
|
||||
|
|
@ -182,20 +190,21 @@ const request = (method = 'GET', serverUrl, domainUrl, params, dataType = "json"
|
|||
};
|
||||
}
|
||||
|
||||
console.log("传参详情", data)
|
||||
console.log("传参详情",data)
|
||||
// 测试环境用 uni.request,正式环境用宿主 nativeRequest
|
||||
const api = isTest
|
||||
? uni.request
|
||||
: (uni.nativeRequest || uni.request)
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request(data).then(res => {
|
||||
console.log(deCode(res.data.data))
|
||||
api(data).then(res => {
|
||||
if (res.statusCode == 200) {
|
||||
if (res.data.encrypt) {
|
||||
if (q != 'test') {
|
||||
if(q!='test'&&isTest){
|
||||
res.data = deCode(res.data.data);
|
||||
} else {
|
||||
res.data = res.data.data
|
||||
}
|
||||
|
||||
}
|
||||
console.log(res.data)
|
||||
// res.data = res.data.data
|
||||
}
|
||||
if (res.data.code == 1001003 || res.data.code == 1001004 || res.data.code == 1001005) {
|
||||
uni.hideLoading();
|
||||
uni.showToast({
|
||||
|
|
@ -205,7 +214,6 @@ const request = (method = 'GET', serverUrl, domainUrl, params, dataType = "json"
|
|||
return reject(res);
|
||||
} else if (res.data.code != 0) {
|
||||
if (data.method == 'POST' && domainUrl == 'api/order') {
|
||||
|
||||
apiUserEvent('all', {
|
||||
type: "event",
|
||||
key: "order_fail",
|
||||
|
|
@ -227,7 +235,6 @@ const request = (method = 'GET', serverUrl, domainUrl, params, dataType = "json"
|
|||
title: res.data.message
|
||||
})
|
||||
}
|
||||
|
||||
uni.hideLoading();
|
||||
return resolve(res);
|
||||
}
|
||||
|
|
@ -249,11 +256,11 @@ const request = (method = 'GET', serverUrl, domainUrl, params, dataType = "json"
|
|||
}
|
||||
})
|
||||
}
|
||||
console.log(123)
|
||||
|
||||
resolve(res.data);
|
||||
} else {
|
||||
// InnerApp.addRequestLog(url, data, res)
|
||||
reject(res);
|
||||
resolve(res);
|
||||
}
|
||||
}).catch(err => {
|
||||
if (err.errMsg == 'request:fail timeout') {
|
||||
|
|
@ -272,12 +279,26 @@ const request = (method = 'GET', serverUrl, domainUrl, params, dataType = "json"
|
|||
err: JSON.stringify(err)
|
||||
}
|
||||
})
|
||||
// uni.showModal({
|
||||
// title: '提示',
|
||||
// content: JSON.stringify(err),
|
||||
// success: function (res) {
|
||||
// if (res.confirm) {
|
||||
// console.log('用户点击确定');
|
||||
// } else if (res.cancel) {
|
||||
// console.log('用户点击取消');
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// uniToast('服务器繁忙');
|
||||
}
|
||||
// InnerApp.addRequestLog(url, data, err)
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* 处理请求头
|
||||
* @param dataType 是否json格式
|
||||
|
|
@ -330,16 +351,15 @@ const setheader = (dataType, baseHeader = {}) => {
|
|||
* @param domainUrl 接口路径
|
||||
*/
|
||||
const domianUrl = (serveUrl, domainUrl) => {
|
||||
let finalUrl=''
|
||||
// #ifdef H5
|
||||
// H5环境下使用相对路径,走 vite 代理,避免 CORS 跨域问题
|
||||
const finalUrl = domainUrl.startsWith('/') ? domainUrl : '/' + domainUrl;
|
||||
finalUrl = domainUrl.startsWith('/') ? domainUrl : '/' + domainUrl;
|
||||
console.log('[H5代理] 原始URL:', domainUrl, '→ 代理URL:', finalUrl);
|
||||
return finalUrl;
|
||||
// #endif
|
||||
|
||||
// #ifndef H5
|
||||
// 非H5环境(APP、小程序等)使用完整URL
|
||||
let finalUrl;
|
||||
if (serveUrl.endsWith('/') && domainUrl.startsWith('/')) {
|
||||
finalUrl = serveUrl + domainUrl.substring(1);
|
||||
} else if (!serveUrl.endsWith('/') && !domainUrl.startsWith('/')) {
|
||||
|
|
@ -348,8 +368,8 @@ const domianUrl = (serveUrl, domainUrl) => {
|
|||
finalUrl = serveUrl + domainUrl;
|
||||
}
|
||||
console.log('[非H5] 完整URL:', finalUrl);
|
||||
return finalUrl;
|
||||
// #endif
|
||||
return finalUrl;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue