优化这段代码 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; }
时间: 2024-02-14 17:07:17 浏览: 25
可以将两个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 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); } });
这段代码已经比之前进行了优化,但是还有一些可以改进的地方。可以使用async/await语法,使代码更加简洁易读。代码如下:
```
async function fetchData(apiFunc, data) {
try {
const res = await apiFunc({...data, pageSize: this.pagination.pageSize, pageNum: this.pagination.pageNum});
this.loading = false;
if (res.code === 200) {
this.tbData = res.data.list;
this.pagination.totalCount = res.data.total;
} else {
this.$message.error(res.msg);
}
} catch (error) {
this.loading = false;
this.$message.error(error.message);
}
}
switch (this.form.productType) {
case 1:
await fetchData(BatchEndorseAPI.ticketList, this.ticketData());
break;
case 3:
await fetchData(BatchEndorseAPI.theatreList, this.theatreData());
break;
default:
return;
}
```
这样可以使用try/catch捕获异常,使代码更加健壮,同时也减少了冗余的重复代码。
from selenium import webdriver from time import sleep import pickle import os bilibili_url = 'https://www.bilibili.com/' login_url = 'https://www.bilibili.com/' targer_url = 'https://show.bilibili.com/platform/detail.html?id=73710&from=pc_ticketlist' class manzhan: def __init__(self): self.status = 0 self.login_method = 1 self.driver = webdriver.Chrome(executable_path='C:\\Users\\zplrp\\AppData\\Local\\Programs\\Python\\Python38\\chromedriver.exe') def set_cookies(self): self.driver.get(bilibili_url) print("###请点击登录###") while self.driver.find('登录') != -1: sleep(1) print("###扫码成功###") pickle.dump(self.driver.get_cookies(),open('cookies.pkl','wb')) print('###cookie保存成功###') self.driver.get(targer_url) def get_cookie(self): cookies = pickle.load(open('cookie.pkl', 'rb')) for cookie in cookies: cookie_dict = { 'domain': '.bilibili.com', 'name': cookie.get('name'), 'value': cookie.get('value') } self.driver.add_cookie(cookie_dict) print('###载入cookie###') def login(self): """登录""" if self.login_method == 0: self.driver.get(login_url) print('###开始登录###') elif self.login_method == 1: if not os.path.exists('cookies.pkl'): self.get_cookies() else: self.driver.get(targer_url) self.get_cookie() def enter_manzhan(self): print('###打开浏览器,进入B站###') self.login() self.driver.refresh() self.status = 2 print('###登录成功###') if __name__ == '__main__': man = manzhan() man.enter_manzhan
这段代码是使用Selenium自动化工具来实现对B站的登录和进入漫展页面的操作。在代码中,首先导入了必要的模块和库,然后定义了一个名为manzhan的类,该类包含了一些方法用于登录和进入漫展页面。
在初始化方法中,设置了一些初始变量,并初始化了一个Chrome浏览器的驱动。然后,定义了set_cookies方法用于获取登录后的cookie并保存到文件中。get_cookie方法用于将之前保存的cookie加载到浏览器中。login方法根据登录方式进行登录操作。enter_manzhan方法用于打开浏览器、登录并进入漫展页面。
最后,在主函数中创建了一个manzhan对象,并调用enter_manzhan方法来执行登录和进入漫展页面的操作。
请问您对这段代码有什么疑问吗?