这是api: export const latest = data => { return axios.post("/wms/inter/order/latestInterOrder", data); }; 这是前端代码: <el-dialog :visible.sync="showInterOrderDialog"></el-dialog> 这是前端script: import { latest } from "@/api/wms/inter-order"; data() { return { }; }, mounted() { this.showInterOrderDialog = false; latest().then(response => { const interOrder = response.data; this.lastInterOrderId = interOrder.id; }).catch(error => { console.error("报错信息:", error); }); this.pollLatestInterOrder(); }, methods: { newMessage() { this.$message({ showClose: true, message: "有新货物入库!" }); }, async pollLatestInterOrder() { }, }, 登录之后,怎么将前端的userId传递给后端接口
时间: 2024-02-14 10:36:02 浏览: 21
可以在调用 latest 接口的时候,将 userId 作为参数传递给后端接口。修改 latest 函数如下:
```
export const latest = (userId, data) => {
return axios.post(`/wms/inter/order/latestInterOrder?userId=${userId}`, data);
};
```
然后在 mounted 方法中调用 latest 函数时,将 userId 传递给它:
```
mounted() {
this.showInterOrderDialog = false;
const userId = this.$store.state.user.id;
latest(userId).then(response => {
const interOrder = response.data;
this.lastInterOrderId = interOrder.id;
}).catch(error => {
console.error("报错信息:", error);
});
this.pollLatestInterOrder();
},
```
这样后端接口就可以拿到前端传递过来的 userId 参数了。
相关问题
修改代码 const add=()=>{ axios.post("/shelf/add",{ user_id:localStorage.getItem("id"), book_id:route.query.bookId }).then((res)=>{ const data = res.data console.log(data) }) }
这段代码是一个使用 axios 发送 POST 请求的函数,请求地址是 `/shelf/add`,请求参数是当前用户的 id 和书本的 id。当请求成功后,会将响应数据打印到控制台。如果要修改这个函数,可以根据实际需求修改请求地址和请求参数,如下所示:
```javascript
const add = () => {
axios.post("/api/add-to-cart", {
userId: localStorage.getItem("userId"),
productId: route.query.productId,
quantity: 1
}).then((res) => {
const data = res.data;
console.log(data);
});
};
```
这个例子中,我们将请求地址改为了 `/api/add-to-cart`,请求参数包括了用户的 id、产品的 id 和购买数量,用于向购物车添加商品。你可以根据具体的业务需求修改这些参数。
这是api: export const listByUserId = data => { return axios.post("/wms/message/listByUserId", data); }; 这是前端代码: <el-dialog :visible.sync="showInterOrderDialog"></el-dialog> 这是script: data() { return { showInterOrderDialog: false, listByUserId: null, lastInterOrderId: null }; }, mounted() { this.init(); this.showInterOrderDialog = false; listByUserId().then(response => { const interOrder = response.data; this.lastInterOrderId = interOrder.id; }).catch(error => { console.error("报错信息:", error); }); this.pollLatestInterOrder(); }, newMessage() { this.$notify({ title: "消息提醒", message: "有新货物入库!", position: "bottom-right", duration: 0 }); }, async pollLatestInterOrder() { this.timer = setInterval(async () => { // 将时器赋值给实例属性 listByUserId().then(response => { const interOrder = response.data; if (interOrder.id !== this.lastInterOrderId) { this.lastInterOrderId = interOrder.id; this.newMessage(); } }).catch(error => { console.error("报错信息:", error); }); }, 5000); // 轮询间隔,这里设置为5秒 }, 能帮我重新写个轮询吗,status值为1就显示,为0就不显示
当 status 值为 1 时,显示弹窗,为 0 时不显示弹窗,可以这样实现:
```
data() {
return {
showInterOrderDialog: false,
lastInterOrderId: null
};
},
mounted() {
this.init();
this.showInterOrderDialog = false;
this.pollLatestInterOrder();
},
methods: {
newMessage() {
this.$notify({
title: "消息提醒",
message: "有新货物入库!",
position: "bottom-right",
duration: 0
});
this.showInterOrderDialog = true; // 显示弹窗
},
async pollLatestInterOrder() {
this.timer = setInterval(async () => {
try {
const response = await listByUserId();
const interOrder = response.data;
if (interOrder.id !== this.lastInterOrderId && interOrder.status === 1) {
this.lastInterOrderId = interOrder.id;
this.newMessage();
}
} catch (error) {
console.error("报错信息:", error);
}
}, 5000);
}
}
```
在 `pollLatestInterOrder` 方法中,获取到数据后判断 `status` 值是否为 1,如果是则调用 `newMessage` 方法,并将 `showInterOrderDialog` 设置为 `true`,显示弹窗。如果 `status` 值不为 1,则不显示弹窗。