笔者之前使用通义灵码,后来发现不知为何导致vscode经常挂,因此改用文心快码,记录一些个人觉得不太合理的地方
代码优化1
请优化下面的代码:
async postRepairTypeQuery() {
const res = await this.$requestAPI({
apiUrl: "/ly/busicen/se/wmsbasequerycontext/wms/repairTypeQuery",
variables: {},
});
if (res.result !== "1") return this.$message(res.msg);
const list = res.rows.map((item) => ({
label: item.maintainTypeName,
value: item.guaranteeTypeCode,
}));
const { ORG_TYPE } = this.$store.getters.orgInfo;
const guaranteeTypeCodeItem = this.formData0.find(o=> o.codeField === "guaranteeTypeCode");
guaranteeTypeCodeItem.dataOptions.dataSource.options = ORG_TYPE === '3' ? list.filter(o=>o.guaranteeTypeCode !== '3') : list;
}
问题与优化点
- 代码重复:在
postRepairTypeQuery
方法中,对guaranteeTypeCodeItem
的查找逻辑可以简化,避免重复查找。 - 错误处理:当
res.result
不等于”1″时,直接返回错误消息,但未对res
进行存在性检查,可能会导致运行时错误。
修复后的代码片段
async postRepairTypeQuery() {
const res = await this.$requestAPI({
apiUrl: "/ly/busicen/se/wmsbasequerycontext/wms/repairTypeQuery",
variables: {},
});
// 检查响应结果是否存在
if (!res || res.result !== "1") {
return this.$message(res ? res.msg : '请求失败,未获得响应');
}
const list = res.rows.map((item) => ({
label: item.maintainTypeName,
value: item.guaranteeTypeCode,
}));
const { ORG_TYPE } = this.$store.getters.orgInfo;
// 使用可选链简化查找逻辑
const guaranteeTypeCodeItem = this.formData0.find(o => o.codeField === "guaranteeTypeCode");
if (guaranteeTypeCodeItem) {
guaranteeTypeCodeItem.dataOptions.dataSource.options = ORG_TYPE === '3'
? list.filter(o => o.guaranteeTypeCode !== '3')
: list;
}
}
问题:list并没有guaranteeTypeCode这个字段,会导致过滤不成功,编码助手未发现
代码优化2
请优化下面的代码:
handleInput(e) {
if (!e || !e.target.value) return;
const dataItems = e.target.value.split(",");
// console.log(dataItems, "dataItems");
if(!dataItems.length === 7) return;
const isNewScanCode = dataItems[0].indexOf('#') > -1
&& (dataItems[6].length === 5) //新版本扫描数据
const isOldScanCode = dataItems[0].indexOf('#') < 0
&& (dataItems[6].slice(-10,-5) === dataItems[3].slice(0,5))//老版本扫描数据
if (isNewScanCode || isOldScanCode) {
const [dlrCode, dataSoure, maintainOrderCode, partNo, shouldReachNum, oldPartMonthStr, identificationCode] = dataItems.map((item, index) => index === 0 ? item.replace('#', '') : item);
const { isOut } = this.dto;
const variables = {
dlrCode, // 专营店编码
dataSoure, // 数据来源
maintainOrderCode, // 保修单号
partNo, // 旧件编码
shouldReachNum, // 应到数量
oldPartMonthStr, // 旧件月份
identificationCode // 标识码
};
this.loading = true;
// if( this.scanData.indexOf(dataItems.join(',')) === -1 ){ // 若当前数据项不在已扫描的数据列表中,则将其添加到列表末尾
// this.scanData += this.scanning+',';
// }
this.scanning = ''; // 清空输入框内容,以便下一次扫描时可以重新开始录入数据项
this.$requestAPI({
apiUrl: "/ly/busicen/se/wmsordercontext/wms/oldPartInOutStoreSave",
variables: {
t: new Date().getTime(),
...variables,
...this.formData0Fields,
isInOutType: isOut ? "2" : "1",
scanOutStoreFlag: "3",
},
})
.then((res) => {
this.loading = false;
let msg = ''
let duration = 1000;
// const testOk = identificationCode === '00002' // 测试标识码,仅为示例
if (res.result === "1") {
this.refresh = true;
msg = '成功'
// 替换已成功的数据项,使其不再显示在扫描数据列表中
// this.scanData = this.scanData.replace(dataItems.join(',') + ',', "");
} else {
msg = res.result === "10007" ? "已入库 勿重复扫码" : res.msg;
duration = 10000;
this.playSpeech(msg);
const tableData = this.$refs[this.tableRefs[0]].tableData;
const hasDefault = tableData.find(item => item.scanCode === dataItems.join(','));
if( !hasDefault )
{
this.$refs[this.tableRefs[0]].tableData.unshift({
scanCode:dataItems.join(','),
errorCode:res.msg
})
}
}
const h = this.$createElement;
this.$message({
type: res.result === "1" ? 'success' : "warning",
message: h('p', null, [
h('span', {
style: {
fontSize: '42pt',
color: 'white',
}
}, msg)
]),
duration,
});
})
.catch(() => {
this.loading = false;
this.$message({
type: "error",
message: "请求失败,请稍后重试",
duration: 5000,
});
});
}
},
问题与优化点
- 逻辑判断错误:
if(!dataItems.length === 7)
应改为if(dataItems.length !== 7)
。 - 代码注释:存在未使用的注释代码,应清理以提高代码可读性。
- 消息提示样式:消息提示中的字体大小过大(42pt),可能不适合所有显示环境,需调整。
- 错误处理重复代码:错误处理部分有重复代码,可以优化。
修复后完整的代码片段
handleInput(e) {
if (!e || !e.target.value) return;
const dataItems = e.target.value.split(",");
if (dataItems.length !== 7) return;
const isNewScanCode = dataItems[0].indexOf('#') > -1 && dataItems[6].length === 5; // 新版本扫描数据
const isOldScanCode = dataItems[0].indexOf('#') < 0 && dataItems[6].slice(-10, -5) === dataItems[3].slice(0, 5); // 老版本扫描数据
if (isNewScanCode || isOldScanCode) {
const [dlrCode, dataSoure, maintainOrderCode, partNo, shouldReachNum, oldPartMonthStr, identificationCode] = dataItems.map((item, index) => index === 0 ? item.replace('#', '') : item);
const { isOut } = this.dto;
const variables = {
dlrCode, // 专营店编码
dataSoure, // 数据来源
maintainOrderCode, // 保修单号
partNo, // 旧件编码
shouldReachNum, // 应到数量
oldPartMonthStr, // 旧件月份
identificationCode // 标识码
};
this.loading = true;
this.scanning = ''; // 清空输入框内容,以便下一次扫描时可以重新开始录入数据项
this.$requestAPI({
apiUrl: "/ly/busicen/se/wmsordercontext/wms/oldPartInOutStoreSave",
variables: {
t: new Date().getTime(),
...variables,
...this.formData0Fields,
isInOutType: isOut ? "2" : "1",
scanOutStoreFlag: "3",
},
})
.then((res) => {
this.loading = false;
let msg = '';
let duration = 1000;
let type = 'warning';
if (res.result === "1") {
this.refresh = true;
msg = '成功';
type = 'success';
} else {
msg = res.result === "10007" ? "已入库 勿重复扫码" : res.msg;
duration = 10000;
this.playSpeech(msg);
const tableData = this.$refs[this.tableRefs[0]].tableData;
const hasDefault = tableData.find(item => item.scanCode === dataItems.join(','));
if (!hasDefault) {
this.$refs[this.tableRefs[0]].tableData.unshift({
scanCode: dataItems.join(','),
errorCode: res.msg
});
}
}
this.$message({
type: type,
message: msg,
duration: duration,
});
})
.catch(() => {
this.loading = false;
this.$message({
type: "error",
message: "请求失败,请稍后重试",
duration: 5000,
});
});
}
},
问题:把自定义的message提示去掉了