methods: { async init () { queryEmployeeById(this.id).then(res => { console.log(res) if (String(res.code) === '1') { console.log(res.data) this.ruleForm = res.data this.ruleForm.sex = res.data.sex === '0' ? '女' : '男' // this.ruleForm.password = '' } else { this.$message.error(res.msg || '操作失败') } }) }
时间: 2023-06-02 15:04:28 浏览: 80
这个问题属于技术问题,我可以回答。这个方法是一个异步方法,用于初始化数据。首先通过id查询员工信息,如果查询成功并且返回code为1,则将查询到的数据赋值给ruleForm对象,并将性别从数字转换为文字。如果查询失败则输出错误提示信息。
相关问题
handleDelete(row){ this.$confirm('是否删除当前用户信息?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => { myAxios({ url:'/deleteUsergoods', params:{ user:row.user } }).then(res =>{ console.log(res); this.getList(); }).catch(err =>{ console.log(err); }) }).catch(() => {}); }优化这段代码
这段代码可以进行如下优化:
1. 将确认框的文本内容和类型定义为常量,避免多处使用时重复定义。
2. 将网络请求的操作封装为一个独立的函数,避免代码重复。
3. 将网络请求成功后的操作封装为一个独立的函数,方便代码重用。
4. 使用 async/await 代替 then/catch,使代码更加简洁易懂。
以下是优化后的代码:
```
const confirmText = '是否删除当前用户信息?';
const confirmType = 'warning';
async function deleteUsergoods(user) {
try {
const res = await myAxios({
url: '/deleteUsergoods',
params: { user },
});
return res;
} catch (err) {
console.log(err);
throw err;
}
}
function handleDelete(row) {
this.$confirm(confirmText, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: confirmType,
}).then(async () => {
await deleteUsergoods(row.user);
console.log('删除成功');
this.getList();
}).catch(() => {});
}
```
修改这段代码,使第一个定时任务先执行,第一个定时任务执行完之后,开始执行第二个定时任务,并且第二个定时任务执行时,第一个定时任务不再开启。 findBlue(){ var _this = this; wx.startBluetoothDevicesDiscovery({ allowDuplicatesKey: false, interval: 0, success:(res) => { console.log("搜索蓝牙设备成功...") setTimeout(function(){ _this.getBlue() },500) }, fail: (res) => { console.log('搜索附近的蓝牙设备失败') console.log(res) } }) }, // 3.搜索蓝牙设备之后,需要获取搜索到的蓝牙设备信息 getBlue(){ var _this = this; wx.getBluetoothDevices({ success: (res) => { console.log(res) if(res.devices.length > 0){ for (var i = 0; i < res.devices.length; i++){ // 判断里面是否有我想要的蓝牙设备 if (res.devices[i].name === this.data.inputValue || res.devices[i].localName === this.data.inputValue){ console.log("name:"+res.devices[i].name) this.setData({ isFound: true, deviceId: res.devices[i].deviceId }) //扫描到目标设备之后关闭蓝牙搜索 wx.stopBluetoothDevicesDiscovery({ success: (res) => { console.log('扫描到目标设备之后关闭蓝牙搜索'); setTimeout(function(){ _this.connetBlue(_this.data.deviceId);//4.0 },500) } }) // this.connetBlue(res.devices[i].deviceId);//4.0 return; } } } console.log("重新找....") var time = setTimeout(function(){ if(_this.data.isFound == false && _this.data.isTui == false && _this.data.isTip == false){ console.log("执行任务1==================") _this.findBlue() } },500) setTimeout(function(){ console.log("执行任务2---------------------") clearTimeout(time) console.log("清除time定时任务") console.log("isFound:",_this.data.isFound) console.log("isTui:",_this.data.isTui) console.log("isTip:",_this.data.isTip) if(_this.data.isFound == false && _this.data.isTui == false && _this.data.isTip == false){ console.log("重新打开适配器") wx.openBluetoothAdapter({ success: (res) => { console.log('初始化蓝牙设备成功') //获取适配器状态 wx.getBluetoothAdapterState({ success(res) { setTimeout(function(){ _this.findBlue();//2.0 },800) } }) } }) } }, 3000) }, fail: () =>{ console.log("搜索蓝牙设备失败") } }) },
修改后的代码如下,使用Promise和async/await实现任务的异步执行:
```
async function findAndConnectBlue() {
try {
// 1. 先执行任务1
await searchBlue();
// 2. 任务1执行完后,执行任务2
await connectBlue();
} catch (error) {
console.log(error);
}
}
function searchBlue() {
return new Promise((resolve, reject) => {
wx.startBluetoothDevicesDiscovery({
allowDuplicatesKey: false,
interval: 0,
success: (res) => {
console.log("搜索蓝牙设备成功...");
wx.getBluetoothDevices({
success: (res) => {
console.log(res);
if (res.devices.length > 0) {
for (var i = 0; i < res.devices.length; i++) {
// 判断里面是否有我想要的蓝牙设备
if (
res.devices[i].name === this.data.inputValue ||
res.devices[i].localName === this.data.inputValue
) {
console.log("name:" + res.devices[i].name);
this.setData({
isFound: true,
deviceId: res.devices[i].deviceId,
});
//扫描到目标设备之后关闭蓝牙搜索
wx.stopBluetoothDevicesDiscovery({
success: (res) => {
console.log("扫描到目标设备之后关闭蓝牙搜索");
resolve();
},
});
return;
}
}
}
console.log("重新找....");
setTimeout(() => {
if (
this.data.isFound == false &&
this.data.isTui == false &&
this.data.isTip == false
) {
console.log("执行任务1==================");
searchBlue().then(() => resolve());
} else {
resolve();
}
}, 500);
},
fail: () => {
console.log("搜索蓝牙设备失败");
reject();
},
});
},
fail: (res) => {
console.log("搜索附近的蓝牙设备失败");
console.log(res);
reject();
},
});
});
}
function connectBlue() {
return new Promise((resolve, reject) => {
console.log("执行任务2---------------------");
console.log("清除time定时任务");
console.log(
"isFound:",
this.data.isFound,
"isTui:",
this.data.isTui,
"isTip:",
this.data.isTip
);
if (
this.data.isFound == false &&
this.data.isTui == false &&
this.data.isTip == false
) {
console.log("重新打开适配器");
wx.openBluetoothAdapter({
success: (res) => {
console.log("初始化蓝牙设备成功");
//获取适配器状态
wx.getBluetoothAdapterState({
success: (res) => {
setTimeout(() => {
searchBlue().then(() => resolve());
}, 800);
},
});
},
});
} else {
resolve();
}
});
}
findAndConnectBlue();
```
相关推荐
![html](https://img-home.csdnimg.cn/images/20210720083451.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)