From f26f29d84e0a68831a6af14dab3eec5500496d2e Mon Sep 17 00:00:00 2001 From: spring <2396852758@qq.com> Date: 星期三, 28 五月 2025 16:48:52 +0800 Subject: [PATCH] 初始化项目 --- uview-ui/libs/function/queryParams.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 58 insertions(+), 0 deletions(-) diff --git a/uview-ui/libs/function/queryParams.js b/uview-ui/libs/function/queryParams.js new file mode 100644 index 0000000..81c7e5e --- /dev/null +++ b/uview-ui/libs/function/queryParams.js @@ -0,0 +1,58 @@ +/** + * 瀵硅薄杞瑄rl鍙傛暟 + * @param {*} data,瀵硅薄 + * @param {*} isPrefix,鏄惁鑷姩鍔犱笂"?" + */ +function queryParams(data = {}, isPrefix = true, arrayFormat = 'brackets') { + let prefix = isPrefix ? '?' : '' + let _result = [] + if (['indices', 'brackets', 'repeat', 'comma'].indexOf(arrayFormat) == -1) arrayFormat = 'brackets'; + for (let key in data) { + let value = data[key] + // 鍘绘帀涓虹┖鐨勫弬鏁� + if (['', undefined, null].indexOf(value) >= 0) { + continue; + } + // 濡傛灉鍊间负鏁扮粍锛屽彟琛屽鐞� + if (value.constructor === Array) { + // e.g. {ids: [1, 2, 3]} + switch (arrayFormat) { + case 'indices': + // 缁撴灉: ids[0]=1&ids[1]=2&ids[2]=3 + for (let i = 0; i < value.length; i++) { + _result.push(key + '[' + i + ']=' + value[i]) + } + break; + case 'brackets': + // 缁撴灉: ids[]=1&ids[]=2&ids[]=3 + value.forEach(_value => { + _result.push(key + '[]=' + _value) + }) + break; + case 'repeat': + // 缁撴灉: ids=1&ids=2&ids=3 + value.forEach(_value => { + _result.push(key + '=' + _value) + }) + break; + case 'comma': + // 缁撴灉: ids=1,2,3 + let commaStr = ""; + value.forEach(_value => { + commaStr += (commaStr ? "," : "") + _value; + }) + _result.push(key + '=' + commaStr) + break; + default: + value.forEach(_value => { + _result.push(key + '[]=' + _value) + }) + } + } else { + _result.push(key + '=' + value) + } + } + return _result.length ? prefix + _result.join('&') : '' +} + +export default queryParams; -- Gitblit v1.9.3