This commit is contained in:
BianLzhaoMin 2025-02-17 14:49:12 +08:00
parent a21a7cd967
commit 5921d123cf
2 changed files with 124 additions and 125 deletions

View File

@ -3,7 +3,7 @@ const platform = uni.getSystemInfoSync().platform
// let BASE_URL = 'http://192.168.2.188:21907/gz_att_wechat' // 本地 // let BASE_URL = 'http://192.168.2.188:21907/gz_att_wechat' // 本地
// let BASE_URL = 'http://192.168.0.14:21907/gz_att_wechat' // 测试服务地址 // let BASE_URL = 'http://192.168.0.14:21907/gz_att_wechat' // 测试服务地址
// let BASE_URL = 'https://jj.jypxks.com/gz_att_wechat' // 生产服务地址 // let BASE_URL = 'https://jj.jypxks.com/gz_att_wechat' // 生产服务地址
let BASE_URL = 'http://192.168.0.50:1907' // 孙亮 let BASE_URL = 'http://192.168.0.50:1907/gz_att_wechat' // 孙亮
// 如果是浏览器调试会产生跨域 则配置请求基地址为 api 或其他 必须与vue.config.js 中配置的代理一致 解决跨域问题 // 如果是浏览器调试会产生跨域 则配置请求基地址为 api 或其他 必须与vue.config.js 中配置的代理一致 解决跨域问题
// if (ENV === 'development' && (platform === 'h5' || platform === 'windows')) { // if (ENV === 'development' && (platform === 'h5' || platform === 'windows')) {

View File

@ -1,24 +1,23 @@
import BASE_URL from './env' import BASE_URL from './env'
const CryptoJS = require("@/utils/crypto-js"); const CryptoJS = require('@/utils/crypto-js')
import { aqEnnable } from "@/api/index.js" import { aqEnnable } from '@/api/index.js'
var cbc_key = CryptoJS.enc.Utf8.parse("zhst@bonus@zhst@bonus@1234567890"); var cbc_key = CryptoJS.enc.Utf8.parse('zhst@bonus@zhst@bonus@1234567890')
// var cbc_key = CryptoJS.enc.Utf8.parse("zhst@bonus@zhst@"); // var cbc_key = CryptoJS.enc.Utf8.parse("zhst@bonus@zhst@");
var cbc_iv = CryptoJS.enc.Utf8.parse("1234567812345678"); var cbc_iv = CryptoJS.enc.Utf8.parse('1234567812345678')
// 加密 // 加密
export function encryptCBC(word) { export function encryptCBC(word) {
if(!aqEnnable) { if (!aqEnnable) {
return word return word
} }
var srcs = CryptoJS.enc.Utf8.parse(word) var srcs = CryptoJS.enc.Utf8.parse(word)
var encrypted = CryptoJS.AES.encrypt(srcs, cbc_key, { var encrypted = CryptoJS.AES.encrypt(srcs, cbc_key, {
iv: cbc_iv, iv: cbc_iv,
mode: CryptoJS.mode.CBC, mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7 padding: CryptoJS.pad.Pkcs7,
}); })
return encrypted.toString(); return encrypted.toString()
} }
/** /**
* 解密 * 解密
@ -26,129 +25,129 @@ export function encryptCBC(word) {
* @returns {*} * @returns {*}
*/ */
export function decryptCBC(word) { export function decryptCBC(word) {
if(!aqEnnable) { if (!aqEnnable) {
return word return word
} }
var encrypted = CryptoJS.AES.decrypt(word, cbc_key, { var encrypted = CryptoJS.AES.decrypt(word, cbc_key, {
iv: cbc_iv, iv: cbc_iv,
mode: CryptoJS.mode.CBC, mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7 padding: CryptoJS.pad.Pkcs7,
}); })
return encrypted.toString(CryptoJS.enc.Utf8); return encrypted.toString(CryptoJS.enc.Utf8)
} }
function parseUrl(url) { function parseUrl(url) {
var json = {}; var json = {}
if (url === undefined || typeof(url) != 'string' || url.indexOf("?") == -1 || url.indexOf("=") == -1) { if (
return json; url === undefined ||
} else { typeof url != 'string' ||
url.indexOf('?') == -1 ||
let items = url.split('?')[1].split('&'); url.indexOf('=') == -1
for (var i = 0; i < items.length; i++) { ) {
var item = items[i].split('='); return json
json[item[0]] = item[1]; } else {
} let items = url.split('?')[1].split('&')
for (var i = 0; i < items.length; i++) {
} var item = items[i].split('=')
return json; json[item[0]] = item[1]
}
}
return json
} }
// 是否空对象 // 是否空对象
function isEmptyObject(obj) { function isEmptyObject(obj) {
return Object.keys(obj).length === 0; return Object.keys(obj).length === 0
} }
// 创建一个请求实例 // 创建一个请求实例
console.log('当前平台信息', uni.getSystemInfoSync().platform) console.log('当前平台信息', uni.getSystemInfoSync().platform)
const request = (options) => { const request = (options) => {
// 设置默认配置 // 设置默认配置
const config = { const config = {
url: BASE_URL + options.url, // 拼接基础 URL url: BASE_URL + options.url, // 拼接基础 URL
method: options.method || 'GET', // 默认为 GET 请求 method: options.method || 'GET', // 默认为 GET 请求
data: options.data || {}, // 请求数据 data: options.data || {}, // 请求数据
header: options.header || { header: options.header || {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, // 请求头 }, // 请求头
timeout: options.timeout || 20000, // 请求超时时间,单位为毫秒 timeout: options.timeout || 20000, // 请求超时时间,单位为毫秒
} }
// 获取平台类型 // 获取平台类型
const platform = uni.getSystemInfoSync().platform const platform = uni.getSystemInfoSync().platform
// 安卓平台下自定义请求头或其他特殊配置 // 安卓平台下自定义请求头或其他特殊配置
if (platform === 'android') { if (platform === 'android') {
config.header['X-Android-Specific-Header'] = 'android-value' // Android 特定请求头 config.header['X-Android-Specific-Header'] = 'android-value' // Android 特定请求头
} }
// H5 平台下自定义请求头或其他特殊配置 // H5 平台下自定义请求头或其他特殊配置
if (platform === 'h5') { if (platform === 'h5') {
config.header['X-H5-Specific-Header'] = 'h5-value' // H5 特定请求头 config.header['X-H5-Specific-Header'] = 'h5-value' // H5 特定请求头
} }
// 获取存储的 Token如果有 // 获取存储的 Token如果有
const token = uni.getStorageSync('token') const token = uni.getStorageSync('token')
console.log(token, 'token') console.log(token, 'token')
if (token) { if (token) {
config.header['token'] = `${token}` config.header['token'] = `${token}`
config.header['Authorization'] = `${token}` config.header['Authorization'] = `${token}`
} }
if(options.encry) { if (options.encry) {
if (config.method == 'get') { if (config.method == 'get') {
console.log('parseUrl', parseUrl(config.url))
let obj = parseUrl(config.url)
config.url = `${config.url.split('?')[0]}?encryptedData=${encodeURIComponent(
encryptCBC(JSON.stringify(obj)),
)}`
console.log('config.url', config.url)
} else if (config.method == 'post') {
if (!isEmptyObject(config.data)) {
config.data = {
encryptedData: encryptCBC(JSON.stringify(config.data)),
}
}
}
}
console.log('parseUrl',parseUrl(config.url)) // 请求拦截
let obj = parseUrl(config.url) uni.showLoading({
config.url = `${config.url.split('?')[0]}?encryptedData=${encodeURIComponent(encryptCBC(JSON.stringify(obj)))}` title: '加载中...',
console.log('config.url',config.url) mask: true,
})
}else if(config.method == 'post') { // 发起请求
return new Promise((resolve, reject) => {
uni.request({
...config,
success: (res) => {
// 请求成功的回调
uni.hideLoading() // 隐藏加载中提示
if(!isEmptyObject(config.data)) { // 根据返回的状态码做处理
config.data = { if (res.statusCode === 200) {
encryptedData: encryptCBC(JSON.stringify(config.data)) resolve(res.data) // 返回成功的数据
} } else {
} reject(res.data || '请求失败')
uni.showToast({
} title: res.data.message || '请求失败',
} icon: 'none',
})
// 请求拦截 }
uni.showLoading({ },
title: '加载中...', fail: (err) => {
mask: true, uni.hideLoading() // 隐藏加载中提示
}) reject(err)
uni.showToast({
// 发起请求 title: err.message || '网络错误,请稍后再试',
return new Promise((resolve, reject) => { icon: 'none',
uni.request({ })
...config, },
success: (res) => { complete: () => {
// 请求成功的回调 // 请求完成后的回调(不论成功失败都会调用)
uni.hideLoading() // 隐藏加载中提示 },
})
// 根据返回的状态码做处理 })
if (res.statusCode === 200) {
resolve(res.data) // 返回成功的数据
} else {
reject(res.data || '请求失败')
uni.showToast({
title: res.data.message || '请求失败',
icon: 'none',
})
}
},
fail: (err) => {
uni.hideLoading() // 隐藏加载中提示
reject(err)
uni.showToast({
title: err.message || '网络错误,请稍后再试',
icon: 'none',
})
},
complete: () => {
// 请求完成后的回调(不论成功失败都会调用)
},
})
})
} }
// 请求方法封装 // 请求方法封装