Browse Source
feat: 添加购物车功能并优化代码结构
feat: 添加购物车功能并优化代码结构
refactor: 重构验证函数和API调用逻辑 fix: 修复z-index过高问题并优化样式 docs: 更新注释和文档说明 style: 统一代码格式和命名规范 chore: 更新.gitignore和package.json配置 test: 添加异步函数状态检查工具函数master
18 changed files with 482 additions and 229 deletions
-
1.gitignore
-
4components/navigation/navigation.vue
-
9eslint.config.mjs
-
31libs/api/index.js
-
8libs/utils/config.js
-
10libs/utils/crypto.js
-
51libs/utils/index.js
-
82libs/utils/isPending.js
-
12package.json
-
6pages/agreement/agreement.vue
-
2pages/classification/classification.scss
-
178pages/classification/classification.vue
-
86pages/home/home.vue
-
40pages/home/honest.vue
-
4pages/index/index.vue
-
123pages/login/login.vue
-
24pages/personalInformation/personalInformation.vue
-
2store/index.js
@ -1,5 +1,11 @@ |
|||
const SECRET_KEY = 'a1b2c3d4e5f678901234567890abcdefa1b2c3d4e5f678901234567890abcdef'; |
|||
/** |
|||
* 加密密钥 |
|||
*/ |
|||
const SECRET_KEY = "a1b2c3d4e5f678901234567890abcdefa1b2c3d4e5f678901234567890abcdef"; |
|||
|
|||
/** |
|||
* @property {string} SECRET_KEY 加密密钥 |
|||
*/ |
|||
export default { |
|||
SECRET_KEY, |
|||
}; |
|||
@ -0,0 +1,82 @@ |
|||
/** |
|||
* 检查目标是否为pending状态的Promise或异步函数 |
|||
* @param {*} target 要检查的目标,可接受Promise、异步函数或同步函数 |
|||
* @returns {Promise<boolean>} 一个Promise,解析为目标是否为pending状态 |
|||
*/ |
|||
async function isPending(target) { |
|||
// 如果是异步函数,执行后检查返回的Promise状态
|
|||
if (isAsyncFunction(target)) { |
|||
try { |
|||
const promise = target(); |
|||
return await checkPromisePending(promise); |
|||
} |
|||
// eslint-disable-next-line unused-imports/no-unused-vars
|
|||
catch (error) { |
|||
return false; // 异步函数执行出错,不是pending状态
|
|||
} |
|||
} |
|||
|
|||
// 如果是Promise对象,直接检查状态
|
|||
if (isPromise(target)) { |
|||
return await checkPromisePending(target); |
|||
} |
|||
|
|||
return false; // 既不是异步函数也不是Promise
|
|||
} |
|||
|
|||
/** |
|||
* 检查是否为异步函数 |
|||
* @param {*} fn 要检查的函数 |
|||
* @returns {boolean} 是否为异步函数 |
|||
*/ |
|||
function isAsyncFunction(fn) { |
|||
return typeof fn === "function" |
|||
&& (fn.constructor.name === "AsyncFunction" |
|||
|| fn.toString().includes("async")); |
|||
} |
|||
|
|||
/** |
|||
* 检查是否为Promise |
|||
* @param {*} obj 要检查的对象 |
|||
* @returns {boolean} 是否为Promise |
|||
*/ |
|||
function isPromise(obj) { |
|||
return obj instanceof Promise |
|||
|| (obj && typeof obj.then === "function"); |
|||
} |
|||
|
|||
/** |
|||
* 检查Promise是否为pending状态 |
|||
* @param {Promise} promise 要检查的Promise对象 |
|||
* @returns {Promise<boolean>} 一个Promise,解析为Promise是否为pending状态 |
|||
*/ |
|||
async function checkPromisePending(promise) { |
|||
const pending = Symbol("pending"); |
|||
|
|||
try { |
|||
const result = await Promise.race([ |
|||
promise.then( |
|||
() => "fulfilled", |
|||
() => "rejected", |
|||
), |
|||
Promise.resolve(pending), |
|||
]); |
|||
|
|||
return result === pending; |
|||
} |
|||
// eslint-disable-next-line unused-imports/no-unused-vars
|
|||
catch (error) { |
|||
return false; // Promise已经rejected
|
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 检查是否为Promise或异步函数 |
|||
* @param {*} target 要检查的目标 |
|||
* @returns {boolean} 是否为Promise或异步函数 |
|||
*/ |
|||
function isAsync(target) { |
|||
return isPromise(target) || isAsyncFunction(target); |
|||
} |
|||
|
|||
export { isAsync, isPending }; |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue