get({ url: "server/OrderServerList", data: query }, ({ data }) => { data.list.map((v) => { v.order_goods.map((g) => { g.goods = JSON.parse(g.goods); g.main_banner = app.globalData.imgUrl + g.main_banner; let str = ""; g.spce_name = JSON.parse(g.spce_name); Object.values(g.spce_name).forEach((f) => (str += f + " ")); g.spce_name = str; }); }); console.log(data.list); this.setData({ list: list.concat(data.list), total: data.count, }); }); 优化这段代码
时间: 2024-02-06 08:03:44 浏览: 74
graphql-client:使用React和Node.js使用Graphql的简单演示
首先,可以使用 async/await 来异步获取数据,这样代码会更加清晰易读。其次,可以使用 Promise.all() 来并行处理订单列表中的每一个商品,提高处理效率。最后,可以将获取到的数据进行缓存,避免重复请求。
下面是优化后的代码:
```
async function getOrderList(query) {
const { data } = await axios.get("server/OrderServerList", { params: query });
await Promise.all(
data.list.map(async (order) => {
await Promise.all(
order.order_goods.map(async (goods) => {
if (typeof goods.goods === "string") {
goods.goods = JSON.parse(goods.goods);
}
goods.main_banner = app.globalData.imgUrl + goods.main_banner;
let specName = JSON.parse(goods.spce_name);
goods.spce_name = Object.values(specName).join(" ");
})
);
})
);
return data;
}
// 在调用 getOrderList 函数时,可以将获取到的数据进行缓存
let list = [];
let total = 0;
async function fetchOrderList(query) {
const data = await getOrderList(query);
list = list.concat(data.list);
total = data.count;
console.log(list);
return { list, total };
}
fetchOrderList(query).then(({ list, total }) => {
this.setData({ list, total });
});
```
阅读全文