这是后端代码: public WmsStoreInterOrder getLatestInterOrder() { QueryWrapper<WmsStoreInterOrder> queryWrapper = new QueryWrapper<>(); queryWrapper.select("*") .orderByDesc("inter_time") .last("limit 1"); return getOne(queryWrapper); } @PostMapping("/latestInterOrder") public GlobalResponse<WmsStoreInterOrder> getLatestInterOrder() { WmsStoreInterOrder latestInterOrder = wmsStoreInterOrderService.getLatestInterOrder(); return GlobalResponse.success(latestInterOrder); } 这是前端代码: <el-dialog :visible.sync="showInterOrderDialog" title="新货物入库提醒" width="30%" > <span>新货物已入库!</span> </el-dialog> 这是前端script:import { latest } from "@/api/wms/inter-order"; data() { return { showInterOrderDialog: false, latestInterOrder: null, lastInterOrderId: null }; }, mounted() { this.pollLatestInterOrder(); }, async pollLatestInterOrder() { while (true) { try { const response = await latest(); const interOrder = response.data; if (this.lastInterOrderId !== interOrder.id) { this.latestInterOrder = interOrder; this.showInterOrderDialog = true; this.lastInterOrderId = interOrder.id; } } catch (error) { console.error("报错信息:", error); } await new Promise((resolve) => setTimeout(resolve, 1000)); // 轮询间隔,这里设置为10秒 } }, 改一下前端代码和script, 使用axios.js,使其他用户在其他页面也能显示弹窗 import axios from "axios"; //代码省略 export default instance;
时间: 2024-02-14 22:09:19 浏览: 248
首先需要修改前端代码中的请求方法,使用 axios 替代原先的 latest 方法,代码如下:
```
async pollLatestInterOrder() {
while (true) {
try {
const response = await axios.get('/latestInterOrder');
const interOrder = response.data.data;
if (this.lastInterOrderId !== interOrder.id) {
this.latestInterOrder = interOrder;
this.showInterOrderDialog = true;
this.lastInterOrderId = interOrder.id;
}
} catch (error) {
console.error('报错信息:', error);
}
await new Promise((resolve) => setTimeout(resolve, 1000)); // 轮询间隔,这里设置为10秒
}
},
```
在引入 axios 的地方,需要将其作为默认导出的对象,即:
```
import axios from 'axios';
const instance = axios.create({
// 配置信息
});
export default instance;
```
最后,在发送请求时,需要使用该 axios 实例,即:
```
async getLatestInterOrder() {
try {
const response = await instance.post('/latestInterOrder');
return response.data;
} catch (error) {
console.error('报错信息:', error);
throw error;
}
},
```
阅读全文