| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- const _Http = getApp().globalData.http;
- Page({
- data: {
- loading: false,
- disabled: false,
- showAll: false,
- activeTab: 0,
- productList: [],
- form: [{
- label: "客户",
- error: false,
- errMsg: "",
- type: "text",
- value: "",
- placeholder: "客户名称",
- valueName: "name",
- required: false,
- checking: "base",
- disabled: true
- },
- {
- label: "门店",
- error: false,
- errMsg: "",
- type: "text",
- value: "",
- placeholder: "门店名称",
- valueName: "storename",
- required: false,
- checking: "base",
- disabled: true
- },
- {
- label: "开单日期",
- error: false,
- errMsg: "",
- type: "date",
- value: "",
- placeholder: "请选择开单日期",
- valueName: "billdate",
- required: false,
- checking: "base"
- },
- {
- label: "订单数量",
- error: false,
- errMsg: "",
- type: "number",
- value: "0",
- placeholder: "订单数量",
- valueName: "orderCount",
- required: false,
- checking: "base",
- disabled: true
- },
- {
- label: "订单金额",
- error: false,
- errMsg: "",
- type: "number",
- value: "0",
- placeholder: "订单金额",
- valueName: "amount",
- required: false,
- checking: "base",
- disabled: true
- },
- {
- label: "备注",
- error: false,
- errMsg: "",
- type: "textarea",
- value: "",
- placeholder: "请输入备注",
- valueName: "remarks",
- required: false,
- checking: "base"
- }
- ],
- "content": {
- "sa_custorderid": 0,
- "sa_customersid": 0,
- }
- },
- onLoad(options) {
- // 如果传递了客户信息,填充到表单
- try {
- const customerInfo = getCurrentPages().find(v => v.__route__ == 'CRM/customer/detail').data.detail;
- let form = this.data.form;
- console.log("customerInfo", customerInfo)
- // 填充客户信息到表单
- form.forEach(item => {
- if (item.valueName === 'name' && customerInfo.name) {
- item.value = customerInfo.name;
- }
- if (item.valueName === 'storename' && customerInfo.storename) {
- item.value = customerInfo.storename;
- }
- });
- this.setData({
- form,
- "content.sa_storeid": customerInfo.sa_storeid,
- "content.storeno": customerInfo.storeno,
- "content.sa_customersid": customerInfo.sa_customersid,
- });
- } catch (error) {
- console.error("解析客户信息失败", error);
- }
- // 设置默认开单日期为当天
- const today = new Date().toISOString().split('T')[0];
- let form = this.data.form;
- form.forEach(item => {
- if (item.valueName === 'billdate') {
- item.value = today;
- this.setData({
- "content.billdate": today
- });
- }
- });
- this.setData({
- form
- });
- },
- submit() {
- // 检查是否有商品
- if (this.data.productList.length === 0) {
- this.addProduct();
- wx.showToast({
- title: '请先添加商品',
- icon: 'none'
- });
- return;
- }
-
- this.setData({
- loading: true
- });
- let formData = this.selectComponent("#Form").submit();
-
- if (!formData) {
- this.setData({ loading: false });
- return;
- }
- let content = {
- ...this.data.content,
- ...formData
- };
- // 先创建订单
- _Http.basic({
- "id": "2026031309441701", // 创建订单的接口ID
- content
- }).then(res => {
- console.log("保存订单", res);
- if (res.code == 1) {
- const orderId = res.data;
- console.log("订单ID", orderId);
-
- // 显示加载提示
- wx.showLoading({
- title: '正在绑定商品...',
- mask: true
- });
-
- // 使用Promise.all处理商品绑定
- this.bindProductsWithPromise(orderId).then(() => {
- // 绑定完成,跳转到订单详情
- this.navigateToOrderDetail(orderId);
- }).catch(() => {
- // 绑定失败,跳转到订单详情
- this.navigateToOrderDetail(orderId);
- });
- } else {
- this.setData({ loading: false });
- wx.showToast({
- title: res.msg || '保存失败',
- icon: 'none'
- });
- }
- }).catch(err => {
- this.setData({
- loading: false
- });
- console.error("保存订单失败", err);
- wx.showToast({
- title: '网络错误',
- icon: 'none'
- });
- });
- },
- // 使用Promise.all绑定商品
- bindProductsWithPromise(orderId) {
- const productList = this.data.productList;
- // 创建商品绑定的Promise数组
- const bindPromises = productList.map(product => {
- return new Promise((resolve, reject) => {
- _Http.basic({
- "id": "2026031415462301", // 绑定商品接口ID
- content: {
- sa_custorderid: orderId,
- sa_custorderitemsid: 0,
- sys_enterprise_itemid:product.sys_enterprise_itemid|| product.itemid,
- qty: product.qty,
- oldprice: product.originalPrice || product.price,
- discountrate: product.discount,
- price: product.price,
- amount: product.amount,
- remarks: product.remarks || ""
- }
- }).then(res => {
- if (res.code != 1) {
- console.error("绑定商品失败", res);
- }
- resolve(res);
- }).catch(err => {
- console.error("绑定商品失败", err);
- resolve(null); // 即使失败也继续处理
- });
- });
- });
- // 处理附件绑定(预留接口)
- const attachmentPromise = this.bindAttachmentsWithPromise(orderId);
- // 合并所有Promise
- const allPromises = [...bindPromises, attachmentPromise];
- // 使用Promise.all处理所有请求
- return Promise.all(allPromises);
- },
- // 绑定附件(预留接口)
- bindAttachmentsWithPromise(orderId) {
- return new Promise((resolve) => {
- // 这里预留附件绑定的逻辑
- // 目前暂时直接resolve
- resolve(null);
- });
- },
- // 跳转到订单详情
- navigateToOrderDetail(orderId) {
- // 隐藏加载提示
- wx.hideLoading();
- this.setData({ loading: false });
- wx.redirectTo({
- url: `/CRM/order/detail?id=${orderId}`,
- success: () => {
- // 跳转到订单详情页面
- wx.showToast({
- title: "开单成功",
- icon: "none",
- duration: 1500,
- });
- }
- });
- getCurrentPages().find(v => v.__route__ == 'CRM/customer/detail').partialRenewal(true);
- },
- interrupt({
- detail
- }) {
- // 处理中断逻辑,如果需要的话
- },
- /* 表单必填项是否完成 */
- onConfirm({
- detail
- }) {
- this.setData({
- disabled: detail
- });
- },
- onChange(e) {
- this.setData({
- showAll: e.detail
- });
- },
- closePage() {
- wx.navigateBack({
- delta: 1
- });
- },
- // tab切换
- onTabChange(e) {
- this.setData({
- activeTab: e.detail
- });
- },
- // 打开添加商品面板
- addProduct() {
- // 直接跳转到产品选择页面
- wx.navigateTo({
- url: `/select/product1/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 (detail && detail.list) {
- const existingItemIds = this.data.productList.map(item => item.sys_enterprise_itemid);
-
- // 过滤掉已经存在的商品
- const newProducts = detail.list.filter(item => !existingItemIds.includes(item.sys_enterprise_itemid)).map(item => {
- // 处理价格字段,去除格式化符号
- const priceStr = item.guid_price_cus || item.price || "0";
- const price = parseFloat(priceStr.toString().replace(/[¥,]/g, '')) || 0;
- const qty = parseInt(item.qty) || 1;
-
- return {
- itemid: item.itemid,
- sys_enterprise_itemid: item.sys_enterprise_itemid,
- itemname: item.itemname,
- itemno: item.itemno,
- model: item.model,
- originalPrice: price,
- price: price,
- discount: 1,
- qty: qty,
- amount: parseFloat((price * qty * 1).toFixed(2)),
- remarks: item.remarks || ''
- };
- });
-
- if (newProducts.length > 0) {
- this.setData({
- productList: [...this.data.productList, ...newProducts]
- });
-
- // 更新订单金额
- this.updateOrderAmount();
- } else {
- wx.showToast({
- title: '所选商品已在列表中',
- icon: 'none'
- });
- }
-
- // 返回页面
- wx.navigateBack();
- }
- },
- // 处理字段编辑
- onFieldBlur(e) {
- const index = e.currentTarget.dataset.index;
- const field = e.currentTarget.dataset.field;
- const value = e.detail.value;
- const productList = [...this.data.productList];
- const product = productList[index];
- // 处理不同字段的编辑
- switch (field) {
- case 'price':
- product.price = parseFloat(value) || 0;
- // 重新计算金额,保留两位小数,折扣1=100%
- product.amount = parseFloat((product.price * product.qty * product.discount).toFixed(2));
- break;
- case 'discount':
- product.discount = parseFloat(value) || 1;
- // 重新计算金额,保留两位小数,折扣1=100%
- product.amount = parseFloat((product.price * product.qty * product.discount).toFixed(2));
- break;
- case 'qty':
- let qty = parseInt(value) || 1;
- // 数量不能为0
- qty = Math.max(1, qty);
- product.qty = qty;
- // 重新计算金额,保留两位小数,折扣1=100%
- product.amount = parseFloat((product.price * product.qty * product.discount).toFixed(2));
- break;
- case 'amount':
- product.amount = parseFloat(value) || 0;
- // 重新计算单价(假设数量和折扣不变),保留两位小数,折扣1=100%
- if (product.qty > 0 && product.discount > 0) {
- product.price = parseFloat((product.amount / (product.qty * product.discount)).toFixed(2));
- }
- break;
- case 'remarks':
- product.remarks = value;
- break;
- }
- this.setData({
- productList
- });
- // 更新订单金额
- this.updateOrderAmount();
- },
- // 删除商品
- deleteProduct(e) {
- wx.showModal({
- title: '提示',
- content: '确定要删除这个商品吗?',
- confirmText: '确定',
- cancelText: '取消',
- success: (res) => {
- if (res.confirm) {
- const index = e.currentTarget.dataset.index;
- const productList = [...this.data.productList];
- productList.splice(index, 1);
- this.setData({
- productList
- });
- // 更新订单金额
- this.updateOrderAmount();
- }
- }
- });
- },
- // 更新订单金额
- updateOrderAmount() {
- // 计算总金额,确保不是NaN,默认0
- const totalAmount = parseFloat(this.data.productList.reduce((sum, item) => {
- const amount = parseFloat(item.amount) || 0;
- return sum + amount;
- }, 0)) || 0;
- // 计算总数量,所有商品数量的总计
- const totalQty = this.data.productList.reduce((sum, item) => {
- const qty = parseInt(item.qty) || 0;
- return sum + qty;
- }, 0);
- const form = [...this.data.form];
- form.forEach(item => {
- if (item.valueName === 'amount') {
- item.value = totalAmount.toString();
- this.setData({ "content.amount": totalAmount });
- }
- if (item.valueName === 'orderCount') {
- item.value = totalQty.toString();
- this.setData({ "content.orderCount": totalQty });
- }
- });
- this.setData({ form });
- }
- });
|