拼接搜索条件
{a: \'xx\', b: \'yyy\', c: \'zzz\'}
成
?a=xx&b=yyy&c=zzz
// 只保留有值的搜索条件,拼接成 ?a=xx&b=yyy&c=zzz (传入空对象会返回空字符串)searchObjToQueryString(searchObj) {// 默认约定搜索条件都是字符串let queryStr = \'\'Object.entries(searchObj).forEach((item, index) => {// 字段为空、所属范围是金东区,则不拼接到查询条件里if (item[1] === \'\') {// 仅是结束本轮循环,不是跳出整个循环return false} else if (item[0] == \'regional\' && item[1] == \'金东区\') {// 仅是结束本轮循环,不是跳出整个循环return false}// 根据第一位是不是 \"?\" 来决定如何拼接if (queryStr[0] != \'?\') {queryStr = `?${item[0]}=${item[1]}`} else {queryStr = queryStr + `&${item[0]}=${item[1]}`}})// console.log(queryStr, \'searchObjToQueryString queryStr\')return queryStr},
高级用法
// 只保留有值的搜索条件,拼接成 ?a=xx&b=yyy&c=zzz (传入空对象会返回空字符串)searchObjToQueryString(searchObj) {// 默认约定搜索条件都是字符串let queryStr = \'\'Object.entries(searchObj).forEach((item, index) => {// 字段为空、所属范围是金东区,则不拼接到查询条件里if (item[1] === \'\') {// 仅是结束本轮循环,不是跳出整个循环return false} else if (item[0] == \'regional\' && item[1] == \'金东区\') {// 仅是结束本轮循环,不是跳出整个循环return false}// 根据第一位是不是 \"?\" 来决定如何拼接if (queryStr[0] != \'?\') {queryStr = `?${item[0]}=${item[1]}`} else {queryStr = queryStr + `&${item[0]}=${item[1]}`}})// console.log(queryStr, \'searchObjToQueryString queryStr\')return queryStr},// 按搜索条件搜索地块searchDikuai(searchObj) {// { region, searchValue, minValue, maxValue, industry }console.log(searchObj, \'searchObj\')let that = thiswx.request({url: urlList.searchDikuai + this.searchObjToQueryString(searchObj),method: \"GET\",// header: {// \'content-type\': \'application/x-www-form-urlencoded\'// },success(res) {console.log(res, \'searchDikuai res\')if (res.data.msg == \'请求成功\') {let data = res.data.data// console.log(data, \'searchDikuai data\')that.setData({listData: data})}},fail(err) {console.log(err, \'err\')}})},// 调用搜索方法this.searchDikuai({regional: regional,searchValue: searchValue,minValue: minValue,maxValue: maxValue,industry: industry,})