| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- const _Http = getApp().globalData.http,
- currency = require("../../../../utils/currency"),
- CNY = value => currency(value, {
- symbol: "¥",
- precision: 2
- }).format();
- Component({
- properties: {
- orderId: {
- type: String,
- value: ''
- },
- orderStatus: {
- type: String,
- value: ''
- }
- },
- data: {
- list: [],
- loading: false,
- content: {
- nocache: true,
- pageNumber: 1,
- pageSize: 20,
- pageTotal: 1,
- total: null,
- where: {
- tablefilter: {
- itemname: null,
- itemno: null,
- model: null,
- amount: ""
- }
- }
- }
- },
- methods: {
- /* 获取订单明细列表 */
- getList(id, init) {
- let content = this.data.content;
- content.sa_custorderid = id || this.data.orderId || this.data.sa_custorderid;
- if (init) {
- content.pageNumber = 1;
- this.setData({
- loading: true
- });
- }
- _Http.basic({
- "id": "2026031414243401",
- content
- }).then(res => {
- this.setData({
- loading: false
- });
- console.log("订单明细列表", res)
- if (res.code != 1) return wx.showToast({
- title: res.msg,
- icon: "none"
- })
- // 格式化金额数据
- const formattedData = res.data.map(item => {
- const amount = (item.price || 0) * (item.discountrate || 1) * (item.qty || 0);
- return {
- ...item,
- showPrice: CNY(item.price || 0),
- showAmount: CNY(amount)
- };
- })
- this.setData({
- list: res.pageNumber == 1 ? formattedData : this.data.list.concat(formattedData),
- "content.pageNumber": res.pageNumber + 1,
- "content.pageSize": res.pageSize,
- "content.pageTotal": res.pageTotal,
- "content.total": res.total,
- "sa_custorderid": content.sa_custorderid
- })
- })
- },
- // 打开添加商品面板
- addProduct() {
- // 已退单状态不能添加商品
- if (this.data.orderStatus === '已退单') {
- wx.showToast({
- title: '已退单订单不能添加商品',
- icon: 'none'
- });
- return;
- }
-
- // 直接跳转到产品选择页面
- wx.navigateTo({
- url: `/CRM/customer/modules/orderCreate/productSelect/index?params=${JSON.stringify({
- "id": "2026031312441901",
- "content": {
- "pageNumber": 1,
- "pageSize": 20,
- "where": {
- "tablefilter": {
- "itemname": null,
- "itemno": null,
- "model": null,
- "guid_price": null,
- "guid_price_cus": null,
- "packageqty": null
- }
- }
- }
- })}&butText=添加商品`
- });
- // 设置全局回调函数
- getApp().globalData.handleSelect = this.handleSelect.bind(this);
- },
- // 处理选择商品回调
- handleSelect(detail) {
- // 已退单状态不能添加商品
- if (this.data.orderStatus === '已退单') {
- wx.showToast({
- title: '已退单订单不能添加商品',
- icon: 'none'
- });
- return;
- }
-
- if (detail && detail.list) {
- const orderId = this.data.sa_custorderid;
- if (!orderId) {
- wx.showToast({
- title: '订单ID不存在',
- icon: 'none'
- });
- return;
- }
- // 显示加载提示
- wx.showLoading({
- title: '正在添加商品...',
- mask: true
- });
- // 准备绑定操作
- const promises = detail.list.map(item => {
- return new Promise((resolve, reject) => {
- // 处理价格字段,去除格式化符号
- const priceStr = item.guid_price_cus || item.price || "0";
- const price = parseFloat(priceStr.toString().replace(/[¥,]/g, '')) || 0;
- const qty = parseInt(item.qty) || 1;
- const amount = parseFloat((price * qty * 1).toFixed(2));
- // 直接调用绑定商品接口
- _Http.basic({
- "id": "2026031415462301", // 绑定商品接口ID
- content: {
- sa_custorderid: orderId,
- sa_custorderitemsid: 0,
- sys_enterprise_itemid: item.sys_enterprise_itemid || item.itemid,
- qty: qty,
- oldprice: price,
- discountrate: 1,
- price: price,
- amount: amount,
- remarks: item.remarks || ""
- }
- }).then(res => {
- if (res.code != 1) {
- console.error("绑定商品失败", res);
- }
- resolve(res);
- }).catch(err => {
- console.error("绑定商品失败", err);
- resolve(null); // 即使失败也继续处理
- });
- });
- });
- // 使用Promise.all处理所有绑定操作
- Promise.all(promises).then(() => {
- // 隐藏加载提示
- wx.hideLoading();
- // 刷新商品列表
- this.getList(orderId, true);
- // 显示成功提示
- setTimeout(() => {
- wx.showToast({
- title: '商品添加成功',
- icon: 'none'
- });
- });
- // 返回页面
- wx.navigateBack();
- }).catch(() => {
- // 隐藏加载提示
- wx.hideLoading();
- // 刷新商品列表
- this.getList(orderId, true);
- // 显示成功提示
- setTimeout(() => {
- wx.showToast({
- title: '商品添加成功',
- icon: 'none'
- });
- });
- // 返回页面
- wx.navigateBack();
- });
- }
- },
- // 处理字段编辑
- onFieldBlur(e) {
- // 已退单状态不能编辑商品
- if (this.data.orderStatus === '已退单') {
- wx.showToast({
- title: '已退单订单不能编辑商品',
- icon: 'none'
- });
- return;
- }
-
- const index = e.currentTarget.dataset.index;
- const field = e.currentTarget.dataset.field;
- const value = e.detail.value;
- const list = [...this.data.list];
- const item = list[index];
- // 处理不同字段的编辑
- switch (field) {
- case 'price':
- item.price = parseFloat(value) || 0;
- // 重新计算金额
- item.amount = parseFloat((item.price * (item.discountrate || 1) * item.qty).toFixed(2));
- break;
- case 'discountrate':
- // 确保折扣值有效,默认为1(100%)
- let discount = parseFloat(value) || 1;
- // 折扣不能小于0
- discount = Math.max(0, discount);
- item.discountrate = discount;
- // 重新计算金额
- item.amount = parseFloat((item.price * discount * item.qty).toFixed(2));
- break;
- case 'qty':
- let qty = parseInt(value) || 1;
- // 数量不能为0
- qty = Math.max(1, qty);
- item.qty = qty;
- // 重新计算金额
- item.amount = parseFloat((item.price * (item.discountrate || 1) * qty).toFixed(2));
- break;
- case 'amount':
- // 根据新金额计算单价
- const newAmount = parseFloat(value) || 0;
- item.amount = newAmount;
- if (item.qty > 0 && (item.discountrate || 1) > 0) {
- item.price = parseFloat((newAmount / item.qty / (item.discountrate || 1)).toFixed(2));
- }
- break;
- case 'remarks':
- item.remarks = value;
- break;
- }
- // 确保 discountrate 存在
- if (item.discountrate === undefined || item.discountrate === null) {
- item.discountrate = 1;
- }
- // 立即更新页面显示
- this.setData({
- list
- });
- // 调用接口更新商品信息
- this.updateProduct(item);
- },
- // 更新商品信息
- updateProduct(item) {
- const orderId = this.data.sa_custorderid;
- if (!orderId || !item.sa_custorderitemsid) return;
- // 显示加载提示
- wx.showLoading({
- title: '正在更新商品...',
- mask: true
- });
- // 调用绑定商品接口(用于更新)
- _Http.basic({
- "id": "2026031415462301", // 绑定商品接口ID
- content: {
- sa_custorderid: orderId,
- sa_custorderitemsid: item.sa_custorderitemsid,
- sys_enterprise_itemid: item.sys_enterprise_itemid || item.itemid,
- qty: item.qty,
- oldprice: item.price, // 使用当前价格作为原价
- discountrate: item.discountrate || 1,
- price: item.price,
- amount: item.amount,
- remarks: item.remarks || ""
- }
- }).then(res => {
- // 隐藏加载提示
- wx.hideLoading();
-
- if (res.code === 1) {
- // 显示成功提示
- wx.showToast({
- title: '更新商品成功',
- icon: 'none'
- });
- // 刷新商品列表
- this.getList(orderId, true);
- } else {
- console.error("更新商品失败", res);
- // 显示失败提示
- wx.showToast({
- title: '更新商品失败',
- icon: 'none'
- });
- }
- }).catch(err => {
- // 隐藏加载提示
- wx.hideLoading();
-
- console.error("更新商品失败", err);
- // 显示失败提示
- wx.showToast({
- title: '网络错误',
- icon: 'none'
- });
- });
- },
- // 删除商品
- deleteProduct(e) {
- // 已退单状态不能删除商品
- if (this.data.orderStatus === '已退单') {
- wx.showToast({
- title: '已退单订单不能删除商品',
- icon: 'none'
- });
- return;
- }
-
- const index = e.currentTarget.dataset.index;
- const item = this.data.list[index];
- if (!item.sa_custorderitemsid) {
- wx.showToast({
- title: '商品ID不存在',
- icon: 'none'
- });
- return;
- }
- wx.showModal({
- title: '提示',
- content: '确定要删除这个商品吗?',
- confirmText: '确定',
- cancelText: '取消',
- success: (res) => {
- if (res.confirm) {
- // 调用删除接口
- _Http.basic({
- "id": "2026031416083401", // 删除产品明细接口ID
- content: {
- sa_custorderid: item.sa_custorderid,
- sa_custorderitemsid: item.sa_custorderitemsid
- }
- }).then(res => {
- console.log('删除商品结果:', res);
- if (res.code === 1) {
- // 显示成功提示
- wx.showToast({
- title: '删除成功',
- icon: 'none'
- });
- // 刷新商品列表
- this.getList(this.data.sa_custorderid, true);
- } else {
- wx.showToast({
- title: res.msg || '删除失败',
- icon: 'none'
- });
- }
- }).catch(err => {
- console.error('删除商品失败:', err);
- wx.showToast({
- title: '网络错误',
- icon: 'none'
- });
- });
- }
- }
- });
- }
- }
- })
|