diff --git a/src/utils/env.js b/src/utils/env.js index 641a51d..090c226 100644 --- a/src/utils/env.js +++ b/src/utils/env.js @@ -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.0.14:21907/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 中配置的代理一致 解决跨域问题 // if (ENV === 'development' && (platform === 'h5' || platform === 'windows')) { @@ -22,4 +22,4 @@ let BASE_URL = 'http://192.168.0.50:1907' // 孙亮 // BASE_URL = 'http://*****' // } -export default BASE_URL \ No newline at end of file +export default BASE_URL diff --git a/src/utils/http.js b/src/utils/http.js index 8e95a53..310b58f 100644 --- a/src/utils/http.js +++ b/src/utils/http.js @@ -1,24 +1,23 @@ import BASE_URL from './env' -const CryptoJS = require("@/utils/crypto-js"); -import { aqEnnable } from "@/api/index.js" +const CryptoJS = require('@/utils/crypto-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_iv = CryptoJS.enc.Utf8.parse("1234567812345678"); - +var cbc_iv = CryptoJS.enc.Utf8.parse('1234567812345678') // 加密 export function encryptCBC(word) { - if(!aqEnnable) { - return word - } - var srcs = CryptoJS.enc.Utf8.parse(word) - var encrypted = CryptoJS.AES.encrypt(srcs, cbc_key, { - iv: cbc_iv, - mode: CryptoJS.mode.CBC, - padding: CryptoJS.pad.Pkcs7 - }); - return encrypted.toString(); + if (!aqEnnable) { + return word + } + var srcs = CryptoJS.enc.Utf8.parse(word) + var encrypted = CryptoJS.AES.encrypt(srcs, cbc_key, { + iv: cbc_iv, + mode: CryptoJS.mode.CBC, + padding: CryptoJS.pad.Pkcs7, + }) + return encrypted.toString() } /** * 解密 @@ -26,129 +25,129 @@ export function encryptCBC(word) { * @returns {*} */ export function decryptCBC(word) { - if(!aqEnnable) { - return word - } - var encrypted = CryptoJS.AES.decrypt(word, cbc_key, { - iv: cbc_iv, - mode: CryptoJS.mode.CBC, - padding: CryptoJS.pad.Pkcs7 - }); - return encrypted.toString(CryptoJS.enc.Utf8); + if (!aqEnnable) { + return word + } + var encrypted = CryptoJS.AES.decrypt(word, cbc_key, { + iv: cbc_iv, + mode: CryptoJS.mode.CBC, + padding: CryptoJS.pad.Pkcs7, + }) + return encrypted.toString(CryptoJS.enc.Utf8) } function parseUrl(url) { - var json = {}; - if (url === undefined || typeof(url) != 'string' || url.indexOf("?") == -1 || url.indexOf("=") == -1) { - return json; - } else { - - let items = url.split('?')[1].split('&'); - for (var i = 0; i < items.length; i++) { - var item = items[i].split('='); - json[item[0]] = item[1]; - } - - } - return json; + var json = {} + if ( + url === undefined || + typeof url != 'string' || + url.indexOf('?') == -1 || + url.indexOf('=') == -1 + ) { + return json + } else { + let items = url.split('?')[1].split('&') + for (var i = 0; i < items.length; i++) { + var item = items[i].split('=') + json[item[0]] = item[1] + } + } + return json } // 是否空对象 function isEmptyObject(obj) { - return Object.keys(obj).length === 0; + return Object.keys(obj).length === 0 } - // 创建一个请求实例 console.log('当前平台信息', uni.getSystemInfoSync().platform) const request = (options) => { - // 设置默认配置 - const config = { - url: BASE_URL + options.url, // 拼接基础 URL - method: options.method || 'GET', // 默认为 GET 请求 - data: options.data || {}, // 请求数据 - header: options.header || { - 'Content-Type': 'application/json', - }, // 请求头 - timeout: options.timeout || 20000, // 请求超时时间,单位为毫秒 - } - // 获取平台类型 - const platform = uni.getSystemInfoSync().platform + // 设置默认配置 + const config = { + url: BASE_URL + options.url, // 拼接基础 URL + method: options.method || 'GET', // 默认为 GET 请求 + data: options.data || {}, // 请求数据 + header: options.header || { + 'Content-Type': 'application/json', + }, // 请求头 + timeout: options.timeout || 20000, // 请求超时时间,单位为毫秒 + } + // 获取平台类型 + const platform = uni.getSystemInfoSync().platform - // 安卓平台下自定义请求头或其他特殊配置 - if (platform === 'android') { - config.header['X-Android-Specific-Header'] = 'android-value' // Android 特定请求头 - } + // 安卓平台下自定义请求头或其他特殊配置 + if (platform === 'android') { + config.header['X-Android-Specific-Header'] = 'android-value' // Android 特定请求头 + } - // H5 平台下自定义请求头或其他特殊配置 - if (platform === 'h5') { - config.header['X-H5-Specific-Header'] = 'h5-value' // H5 特定请求头 - } + // H5 平台下自定义请求头或其他特殊配置 + if (platform === 'h5') { + config.header['X-H5-Specific-Header'] = 'h5-value' // H5 特定请求头 + } - // 获取存储的 Token(如果有) - const token = uni.getStorageSync('token') - console.log(token, 'token') - if (token) { - config.header['token'] = `${token}` - config.header['Authorization'] = `${token}` - } - - if(options.encry) { - 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)) - } - } - - } - } - - // 请求拦截 - uni.showLoading({ - title: '加载中...', - mask: true, - }) + // 获取存储的 Token(如果有) + const token = uni.getStorageSync('token') + console.log(token, 'token') + if (token) { + config.header['token'] = `${token}` + config.header['Authorization'] = `${token}` + } - // 发起请求 - return new Promise((resolve, reject) => { - uni.request({ - ...config, - success: (res) => { - // 请求成功的回调 - uni.hideLoading() // 隐藏加载中提示 + if (options.encry) { + 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)), + } + } + } + } - // 根据返回的状态码做处理 - 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: () => { - // 请求完成后的回调(不论成功失败都会调用) - }, - }) - }) + // 请求拦截 + uni.showLoading({ + title: '加载中...', + mask: true, + }) + + // 发起请求 + return new Promise((resolve, reject) => { + uni.request({ + ...config, + success: (res) => { + // 请求成功的回调 + 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: () => { + // 请求完成后的回调(不论成功失败都会调用) + }, + }) + }) } // 请求方法封装 @@ -167,4 +166,4 @@ const request = (options) => { // }, // } -export default request \ No newline at end of file +export default request