diff --git a/front-end/mall4v/src/components/verifition/Verify/VerifyPoints.vue b/front-end/mall4v/src/components/verifition/Verify/VerifyPoints.vue index 1a38f61..d46beec 100644 --- a/front-end/mall4v/src/components/verifition/Verify/VerifyPoints.vue +++ b/front-end/mall4v/src/components/verifition/Verify/VerifyPoints.vue @@ -1,61 +1,63 @@ + diff --git a/front-end/mall4v/src/components/verifition/api/index.js b/front-end/mall4v/src/components/verifition/api/index.js index 3a8624a..d34139e 100644 --- a/front-end/mall4v/src/components/verifition/api/index.js +++ b/front-end/mall4v/src/components/verifition/api/index.js @@ -1,24 +1,38 @@ /** - * 此处可直接引用自己项目封装好的 axios 配合后端联调 + * 此处可直接引用自己项目封装好的 axios 配合后端联调。 + * 使用项目内部封装的 axios 实例可以确保所有请求都遵循统一的配置, + * 例如默认的基础URL、请求拦截器、响应拦截器等,从而简化API调用。 */ +// 引入组件内部封装的axios实例 import request from './../utils/axios' // 组件内部封装的axios -// import request from "@/api/axios.js" //调用项目封装的axios +// 或者引入项目全局封装的axios实例(根据实际路径调整) +// import request from "@/api/axios.js" // 调用项目封装的axios -// 获取验证图片 以及token -export function reqGet (data) { +/** + * 获取验证图片及token。 + * + * @param {Object} data - 请求体数据,通常包含生成验证码所需的参数。 + * @returns {Promise} - 返回一个 Promise 对象,解析为服务器响应的数据。 + */ +export function reqGet(data) { return request({ - url: '/captcha/get', - method: 'post', - data - }) + url: '/captcha/get', // API endpoint for getting captcha image and token + method: 'post', // 使用POST方法发送请求 + data // 将传入的数据作为请求体发送 + }); } -// 滑动或者点选验证 -export function reqCheck (data) { +/** + * 滑动或点选验证。 + * + * @param {Object} data - 请求体数据,通常包含验证操作的结果和token。 + * @returns {Promise} - 返回一个 Promise 对象,解析为服务器响应的数据。 + */ +export function reqCheck(data) { return request({ - url: '/captcha/check', - method: 'post', - data - }) + url: '/captcha/check', // API endpoint for checking captcha validation + method: 'post', // 使用POST方法发送请求 + data // 将传入的数据作为请求体发送 + }); } diff --git a/front-end/mall4v/src/components/verifition/utils/ase.js b/front-end/mall4v/src/components/verifition/utils/ase.js index 31a18c6..ecb3096 100644 --- a/front-end/mall4v/src/components/verifition/utils/ase.js +++ b/front-end/mall4v/src/components/verifition/utils/ase.js @@ -1,11 +1,26 @@ -import CryptoJS from 'crypto-js' +// 引入 CryptoJS 库,用于提供加密功能 +import CryptoJS from 'crypto-js'; + /** - * @word 要加密的内容 - * @keyWord String 服务器随机返回的关键字 - * */ -export function aesEncrypt (word, keyWord = 'XwKsGlMcdPMEhR1B') { - const key = CryptoJS.enc.Utf8.parse(keyWord) - const srcs = CryptoJS.enc.Utf8.parse(word) - const encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }) - return encrypted.toString() + * 使用 AES 算法对给定内容进行加密。 + * + * @param {string} word - 需要加密的内容。 + * @param {string} [keyWord='XwKsGlMcdPMEhR1B'] - 用于加密的密钥,默认为 'XwKsGlMcdPMEhR1B'。 + * @returns {string} - 返回加密后的字符串,使用 Base64 编码。 + */ +export function aesEncrypt(word, keyWord = 'XwKsGlMcdPMEhR1B') { + // 将密钥转换为 UTF-8 编码的字节数组 + const key = CryptoJS.enc.Utf8.parse(keyWord); + + // 将要加密的内容转换为 UTF-8 编码的字节数组 + const srcs = CryptoJS.enc.Utf8.parse(word); + + // 使用 AES 加密算法,采用 ECB 模式和 PKCS7 填充方式对数据进行加密 + const encrypted = CryptoJS.AES.encrypt(srcs, key, { + mode: CryptoJS.mode.ECB, // 设置加密模式为 ECB (Electronic Codebook) + padding: CryptoJS.pad.Pkcs7 // 设置填充方式为 PKCS7 + }); + + // 返回加密后的内容,以 Base64 编码的字符串形式 + return encrypted.toString(); } diff --git a/front-end/mall4v/src/components/verifition/utils/axios.js b/front-end/mall4v/src/components/verifition/utils/axios.js index 8230cb9..693310d 100644 --- a/front-end/mall4v/src/components/verifition/utils/axios.js +++ b/front-end/mall4v/src/components/verifition/utils/axios.js @@ -1,27 +1,65 @@ -import axios from 'axios' +// 引入 axios 库,用于发起 HTTP 请求 +import axios from 'axios'; -axios.defaults.baseURL = import.meta.env.VITE_APP_BASE_API +// 设置默认的基础 URL,从环境变量中读取。 +// VITE_APP_BASE_API 是在构建时通过 Vite 配置提供的环境变量。 +axios.defaults.baseURL = import.meta.env.VITE_APP_BASE_API; +/** + * 创建一个自定义的 axios 实例,允许我们设置全局配置。 + */ const service = axios.create({ + // 设置请求超时时间为 40 秒(40000 毫秒) timeout: 40000, + + // 设置默认的请求头信息 headers: { - 'X-Requested-With': 'XMLHttpRequest', - 'Content-Type': 'application/json; charset=UTF-8' + 'X-Requested-With': 'XMLHttpRequest', // 标识这是一个 AJAX 请求 + 'Content-Type': 'application/json; charset=UTF-8' // 指定发送的数据类型为 JSON } -}) +}); + +/** + * 请求拦截器:在请求发送之前进行处理。 + * 可以用来添加认证令牌、修改请求配置等。 + */ service.interceptors.request.use( config => { - return config + // 在这里可以对 config 进行修改,例如添加 token 或其他头部信息 + // 示例:config.headers.Authorization = `Bearer ${token}`; + + // 返回配置对象或 Promise.resolve(config) + return config; }, error => { - Promise.reject(error) + // 请求错误处理 + // 如果请求出错,在这里可以做些事情,比如日志记录 + + // 返回错误或 Promise.reject(error) + return Promise.reject(error); } -) +); -// response interceptor +/** + * 响应拦截器:在接收到响应数据之后进行处理。 + * 可以用来统一处理响应错误、解析响应数据等。 + */ service.interceptors.response.use( response => { - return response.data + // 默认情况下只返回响应体中的 data 字段 + // 如果需要更多响应信息,可以直接返回整个 response 对象 + + return response.data; + }, + error => { + // 响应错误处理 + // 如果服务器返回错误状态码(如 4xx 或 5xx),在这里可以进行全局错误处理 + // 例如显示错误消息给用户或者跳转到错误页面 + + // 返回错误或 Promise.reject(error) + return Promise.reject(error); } -) -export default service +); + +// 导出 service 实例,以便其他模块可以引用它来发起 HTTP 请求 +export default service; diff --git a/front-end/mall4v/src/components/verifition/utils/util.js b/front-end/mall4v/src/components/verifition/utils/util.js index 82b5cc0..05cbecd 100644 --- a/front-end/mall4v/src/components/verifition/utils/util.js +++ b/front-end/mall4v/src/components/verifition/utils/util.js @@ -1,35 +1,68 @@ -export function resetSize (vm) { - let img_width, img_height, bar_width, bar_height // 图片的宽度、高度,移动条的宽度、高度 +/** + * 重置图片和移动条的尺寸。 + * + * @param {Object} vm - Vue 组件实例,包含 imgSize 和 barSize 属性。 + * imgSize 包含图片的宽度和高度信息。 + * barSize 包含移动条的宽度和高度信息。 + * @returns {Object} 返回一个对象,包含重置后的图片和移动条的尺寸信息。 + */ +export function resetSize(vm) { + // 定义变量来存储图片和移动条的宽度、高度 + let img_width, img_height, bar_width, bar_height; - const parentWidth = vm.$el.parentNode.offsetWidth || window.offsetWidth - const parentHeight = vm.$el.parentNode.offsetHeight || window.offsetHeight - if (vm.imgSize.width.indexOf('%') != -1) { - img_width = parseInt(vm.imgSize.width) / 100 * parentWidth + 'px' + // 获取父容器的宽度和高度,默认为窗口的宽度和高度 + const parentWidth = vm.$el.parentNode.offsetWidth || window.innerWidth; + const parentHeight = vm.$el.parentNode.offsetHeight || window.innerHeight; + + // 计算图片的宽度 + if (vm.imgSize.width.indexOf('%') !== -1) { + // 如果宽度是百分比,则根据父容器宽度计算实际像素值 + img_width = (parseInt(vm.imgSize.width) / 100 * parentWidth) + 'px'; } else { - img_width = vm.imgSize.width + // 否则直接使用提供的宽度值 + img_width = vm.imgSize.width; } - if (vm.imgSize.height.indexOf('%') != -1) { - img_height = parseInt(vm.imgSize.height) / 100 * parentHeight + 'px' + // 计算图片的高度 + if (vm.imgSize.height.indexOf('%') !== -1) { + // 如果高度是百分比,则根据父容器高度计算实际像素值 + img_height = (parseInt(vm.imgSize.height) / 100 * parentHeight) + 'px'; } else { - img_height = vm.imgSize.height + // 否则直接使用提供的高度值 + img_height = vm.imgSize.height; } - if (vm.barSize.width.indexOf('%') != -1) { - bar_width = parseInt(vm.barSize.width) / 100 * parentWidth + 'px' + // 计算移动条的宽度 + if (vm.barSize.width.indexOf('%') !== -1) { + // 如果宽度是百分比,则根据父容器宽度计算实际像素值 + bar_width = (parseInt(vm.barSize.width) / 100 * parentWidth) + 'px'; } else { - bar_width = vm.barSize.width + // 否则直接使用提供的宽度值 + bar_width = vm.barSize.width; } - if (vm.barSize.height.indexOf('%') != -1) { - bar_height = parseInt(vm.barSize.height) / 100 * parentHeight + 'px' + // 计算移动条的高度 + if (vm.barSize.height.indexOf('%') !== -1) { + // 如果高度是百分比,则根据父容器高度计算实际像素值 + bar_height = (parseInt(vm.barSize.height) / 100 * parentHeight) + 'px'; } else { - bar_height = vm.barSize.height + // 否则直接使用提供的高度值 + bar_height = vm.barSize.height; } - return { imgWidth: img_width, imgHeight: img_height, barWidth: bar_width, barHeight: bar_height } + // 返回包含重置后尺寸的对象 + return { imgWidth: img_width, imgHeight: img_height, barWidth: bar_width, barHeight: bar_height }; } -export const _code_chars = [1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] -export const _code_color1 = ['#fffff0', '#f0ffff', '#f0fff0', '#fff0f0'] -export const _code_color2 = ['#FF0033', '#006699', '#993366', '#FF9900', '#66CC66', '#FF33CC'] +// 验证码字符集,包含数字和字母(不包括0和O,避免混淆) +export const _code_chars = [ + 1, 2, 3, 4, 5, 6, 7, 8, 9, + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' +]; + +// 验证码背景颜色集合,浅色系 +export const _code_color1 = ['#fffff0', '#f0ffff', '#f0fff0', '#fff0f0']; + +// 验证码前景颜色集合,多种颜色以增加辨识度 +export const _code_color2 = ['#FF0033', '#006699', '#993366', '#FF9900', '#66CC66', '#FF33CC'];