dingdanquanliucheng/frontend/mini-manager/pages/supplier-text.js
taiyi 50e1aed63c fix: 下发工厂页面文案为空时阻止下发并改善错误提示
文案生成失败时不再静默继续,改为 toast 提示错误信息,
并在点击下发前校验 textContent 非空,避免空内容触发后端 500。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-07 11:01:26 +08:00

159 lines
4.9 KiB
JavaScript

var badgeColorMap = {
approved: "badge-green",
pending_factory: "badge-blue",
pending_approve: "badge-orange",
canceled: "badge-red",
rejected: "badge-red",
};
Page({
data: {
statusBarHeight: 20,
loading: true,
actionLoading: false,
message: "",
orderRows: [],
showDrawer: false,
currentOrderId: null,
currentOrderNo: "",
currentSupplierId: null,
currentIsConfirmed: false,
textContent: "",
templateType: "default",
},
onShow: function () {
var app = getApp();
this.setData({ statusBarHeight: app.globalData.statusBarHeight || 20 });
this.loadOrders();
},
loadOrders: function () {
var that = this;
var app = getApp();
that.setData({ loading: true, message: "" });
Promise.all([
app.request({ url: "/api/orders?order_status=approved&page_no=1&page_size=50", method: "GET" }),
app.request({ url: "/api/orders?order_status=pending_factory&page_no=1&page_size=50", method: "GET" }),
]).then(function (results) {
var rows = [];
results.forEach(function (data) {
(data.list || []).forEach(function (item) {
rows.push({
orderId: item.order_id,
orderNo: item.order_no,
customerName: item.customer_name,
statusText: app.mapOrderStatus(item.order_status),
badgeClass: badgeColorMap[item.order_status] || "badge-gray",
isConfirmed: item.order_status === "pending_factory",
});
});
});
that.setData({ orderRows: rows });
}).catch(function (error) {
that.setData({ message: error.message || "加载失败" });
}).finally(function () {
that.setData({ loading: false });
});
},
handleTapOrder: function (event) {
var that = this;
var id = event.currentTarget.dataset.id;
var no = event.currentTarget.dataset.no;
var confirmed = event.currentTarget.dataset.confirmed;
var app = getApp();
that.setData({
showDrawer: true,
currentOrderId: id,
currentOrderNo: no,
currentIsConfirmed: !!confirmed,
currentSupplierId: null,
textContent: "",
templateType: "default",
message: "",
actionLoading: true,
});
app.request({ url: "/api/orders/" + id, method: "GET" }).then(function (detail) {
var supplierId = detail.factory_id || null;
that.setData({ currentSupplierId: supplierId });
if (!supplierId) {
that.setData({ textContent: "该订单未绑定工厂,无法生成文案", actionLoading: false });
return;
}
if (confirmed) {
that.setData({ textContent: "文案已下发,无需重复操作", actionLoading: false });
return;
}
return app.request({
url: "/api/orders/" + id + "/supplier-text",
method: "POST",
data: { supplier_id: Number(supplierId) },
});
}).then(function (data) {
if (data) {
that.setData({ textContent: data.text_content, templateType: data.template_type, actionLoading: false });
}
}).catch(function (error) {
that.setData({ textContent: "", actionLoading: false });
wx.showToast({ title: error.message || "加载失败", icon: "none" });
});
},
handleTextInput: function (event) {
this.setData({ textContent: event.detail.value });
},
handleCopyAndSend: function () {
var that = this;
if (!that.data.currentOrderId || !that.data.currentSupplierId) return;
if (!that.data.textContent || !that.data.textContent.trim()) {
wx.showToast({ title: "文案内容为空,无法下发", icon: "none" });
return;
}
wx.showModal({
title: "确认下发",
content: "即将复制文案到剪贴板,并将订单 " + that.data.currentOrderNo + " 状态变更为待发厂。",
confirmColor: "#2563eb",
success: function (res) {
if (!res.confirm) return;
that._doConfirmAndCopy();
},
});
},
_doConfirmAndCopy: function () {
var that = this;
var app = getApp();
that.setData({ actionLoading: true });
app.request({
url: "/api/orders/" + that.data.currentOrderId + "/supplier-text/confirm",
method: "POST",
data: {
supplier_id: Number(that.data.currentSupplierId),
text_content: that.data.textContent,
remark: "",
},
}).then(function () {
wx.setClipboardData({
data: that.data.textContent,
success: function () {
wx.showToast({ title: "已复制,请粘贴发送", icon: "none", duration: 2000 });
},
});
that.setData({ showDrawer: false, actionLoading: false });
that.loadOrders();
}).catch(function (error) {
that.setData({ actionLoading: false, message: error.message || "下发失败" });
});
},
handleCloseDrawer: function () {
this.setData({ showDrawer: false });
},
handleBack: function () {
wx.navigateBack({ delta: 1 });
},
});