res.code==200
时间: 2023-10-22 09:07:10 浏览: 57
This expression checks whether the response code of a server's response is equal to 200, which is a standard HTTP code indicating a successful response. If the expression evaluates to true, it means that the server responded successfully.
相关问题
优化这段代码 switch (this.form.productType) { case 1: data = this.ticketData(); BatchEndorseAPI.ticketList({ ...data, pageSize: this.pagination.pageSize, pageNum: this.pagination.pageNum, }).then((res: any) => { this.loading = false; if (res.code == 200) { this.tbData = res.data.list; this.pagination.totalCount = res.data.total; } else { this.$message.error(res.msg); } }); break; case 3: data = this.theatreData(); BatchEndorseAPI.theatreList({ ...data, pageSize: this.pagination.pageSize, pageNum: this.pagination.pageNum, }).then((res: any) => { this.loading = false; if (res.code == 200) { this.tbData = res.data.list; this.pagination.totalCount = res.data.total; } else { this.$message.error(res.msg); } }); break; default: break; }
可以将两个case合并,提取出共同的部分,减少重复代码。代码如下:
```
let apiFunc, data;
switch (this.form.productType) {
case 1:
apiFunc = BatchEndorseAPI.ticketList;
data = this.ticketData();
break;
case 3:
apiFunc = BatchEndorseAPI.theatreList;
data = this.theatreData();
break;
default:
return;
}
apiFunc({...data, pageSize: this.pagination.pageSize, pageNum: this.pagination.pageNum}).then((res: any) => {
this.loading = false;
if (res.code == 200) {
this.tbData = res.data.list;
this.pagination.totalCount = res.data.total;
} else {
this.$message.error(res.msg);
}
});
```
这样可以使代码更加简洁易读,并且减少了冗余的重复代码。
优化这段代码let dataType = uni.getStorageSync("dataType"); let wm_id = uni.getStorageSync("id"); if (dataType == 2) { const { data, code } = await $daskDetail({ wm_id }); if (code == 200) { const res = await $startData({ wl_id: data.count.wl_id }); if (res.code == 200) { this.status = 2; let endTools = JSON.parse(uni.getStorageSync("tools")); } } }
可以尝试以下优化:
1. 使用 async/await 简化异步请求代码。
2. 使用解构赋值提取需要的数据。
3. 缓存请求结果,避免重复请求。
优化后的代码如下:
```
try {
const dataType = await uni.getStorageSync("dataType");
if (dataType == 2) {
const wm_id = await uni.getStorageSync("id");
const { data, code } = await $daskDetail({ wm_id });
if (code == 200) {
const res = await $startData({ wl_id: data.count.wl_id });
if (res.code == 200) {
this.status = 2;
const endTools = JSON.parse(uni.getStorageSync("tools"));
// 缓存请求结果
uni.setStorageSync("endTools", endTools);
}
}
}
} catch (error) {
console.error(error);
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)