create.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. const _Http = getApp().globalData.http;
  2. Page({
  3. data: {
  4. loading: false,
  5. disabled: false,
  6. showAll: false,
  7. activeTab: 0,
  8. productList: [],
  9. form: [{
  10. label: "客户",
  11. error: false,
  12. errMsg: "",
  13. type: "text",
  14. value: "",
  15. placeholder: "客户名称",
  16. valueName: "name",
  17. required: false,
  18. checking: "base",
  19. disabled: true
  20. },
  21. {
  22. label: "门店",
  23. error: false,
  24. errMsg: "",
  25. type: "text",
  26. value: "",
  27. placeholder: "门店名称",
  28. valueName: "storename",
  29. required: false,
  30. checking: "base",
  31. disabled: true
  32. },
  33. {
  34. label: "开单日期",
  35. error: false,
  36. errMsg: "",
  37. type: "date",
  38. value: "",
  39. placeholder: "请选择开单日期",
  40. valueName: "billdate",
  41. required: false,
  42. checking: "base"
  43. },
  44. {
  45. label: "订单数量",
  46. error: false,
  47. errMsg: "",
  48. type: "number",
  49. value: "0",
  50. placeholder: "订单数量",
  51. valueName: "orderCount",
  52. required: false,
  53. checking: "base",
  54. disabled: true
  55. },
  56. {
  57. label: "订单金额",
  58. error: false,
  59. errMsg: "",
  60. type: "number",
  61. value: "0",
  62. placeholder: "订单金额",
  63. valueName: "amount",
  64. required: false,
  65. checking: "base",
  66. disabled: true
  67. },
  68. {
  69. label: "备注",
  70. error: false,
  71. errMsg: "",
  72. type: "textarea",
  73. value: "",
  74. placeholder: "请输入备注",
  75. valueName: "remarks",
  76. required: false,
  77. checking: "base"
  78. }
  79. ],
  80. "content": {
  81. "sa_custorderid": 0,
  82. "sa_customersid": 0,
  83. }
  84. },
  85. onLoad(options) {
  86. // 如果传递了客户信息,填充到表单
  87. try {
  88. const customerInfo = getCurrentPages().find(v => v.__route__ == 'CRM/customer/detail').data.detail;
  89. let form = this.data.form;
  90. console.log("customerInfo", customerInfo)
  91. // 填充客户信息到表单
  92. form.forEach(item => {
  93. if (item.valueName === 'name' && customerInfo.name) {
  94. item.value = customerInfo.name;
  95. }
  96. if (item.valueName === 'storename' && customerInfo.storename) {
  97. item.value = customerInfo.storename;
  98. }
  99. });
  100. this.setData({
  101. form,
  102. "content.sa_storeid": customerInfo.sa_storeid,
  103. "content.storeno": customerInfo.storeno,
  104. "content.sa_customersid": customerInfo.sa_customersid,
  105. });
  106. } catch (error) {
  107. console.error("解析客户信息失败", error);
  108. }
  109. // 设置默认开单日期为当天
  110. const today = new Date().toISOString().split('T')[0];
  111. let form = this.data.form;
  112. form.forEach(item => {
  113. if (item.valueName === 'billdate') {
  114. item.value = today;
  115. this.setData({
  116. "content.billdate": today
  117. });
  118. }
  119. });
  120. this.setData({
  121. form
  122. });
  123. },
  124. submit() {
  125. // 检查是否有商品
  126. if (this.data.productList.length === 0) {
  127. this.addProduct();
  128. wx.showToast({
  129. title: '请先添加商品',
  130. icon: 'none'
  131. });
  132. return;
  133. }
  134. this.setData({
  135. loading: true
  136. });
  137. let formData = this.selectComponent("#Form").submit();
  138. if (!formData) {
  139. this.setData({ loading: false });
  140. return;
  141. }
  142. let content = {
  143. ...this.data.content,
  144. ...formData
  145. };
  146. // 先创建订单
  147. _Http.basic({
  148. "id": "2026031309441701", // 创建订单的接口ID
  149. content
  150. }).then(res => {
  151. console.log("保存订单", res);
  152. if (res.code == 1) {
  153. const orderId = res.data;
  154. console.log("订单ID", orderId);
  155. // 显示加载提示
  156. wx.showLoading({
  157. title: '正在绑定商品...',
  158. mask: true
  159. });
  160. // 使用Promise.all处理商品绑定
  161. this.bindProductsWithPromise(orderId).then(() => {
  162. // 绑定完成,跳转到订单详情
  163. this.navigateToOrderDetail(orderId);
  164. }).catch(() => {
  165. // 绑定失败,跳转到订单详情
  166. this.navigateToOrderDetail(orderId);
  167. });
  168. } else {
  169. this.setData({ loading: false });
  170. wx.showToast({
  171. title: res.msg || '保存失败',
  172. icon: 'none'
  173. });
  174. }
  175. }).catch(err => {
  176. this.setData({
  177. loading: false
  178. });
  179. console.error("保存订单失败", err);
  180. wx.showToast({
  181. title: '网络错误',
  182. icon: 'none'
  183. });
  184. });
  185. },
  186. // 使用Promise.all绑定商品
  187. bindProductsWithPromise(orderId) {
  188. const productList = this.data.productList;
  189. // 创建商品绑定的Promise数组
  190. const bindPromises = productList.map(product => {
  191. return new Promise((resolve, reject) => {
  192. _Http.basic({
  193. "id": "2026031415462301", // 绑定商品接口ID
  194. content: {
  195. sa_custorderid: orderId,
  196. sa_custorderitemsid: 0,
  197. sys_enterprise_itemid:product.sys_enterprise_itemid|| product.itemid,
  198. qty: product.qty,
  199. oldprice: product.originalPrice || product.price,
  200. discountrate: product.discount,
  201. price: product.price,
  202. amount: product.amount,
  203. remarks: product.remarks || ""
  204. }
  205. }).then(res => {
  206. if (res.code != 1) {
  207. console.error("绑定商品失败", res);
  208. }
  209. resolve(res);
  210. }).catch(err => {
  211. console.error("绑定商品失败", err);
  212. resolve(null); // 即使失败也继续处理
  213. });
  214. });
  215. });
  216. // 处理附件绑定(预留接口)
  217. const attachmentPromise = this.bindAttachmentsWithPromise(orderId);
  218. // 合并所有Promise
  219. const allPromises = [...bindPromises, attachmentPromise];
  220. // 使用Promise.all处理所有请求
  221. return Promise.all(allPromises);
  222. },
  223. // 绑定附件(预留接口)
  224. bindAttachmentsWithPromise(orderId) {
  225. return new Promise((resolve) => {
  226. // 这里预留附件绑定的逻辑
  227. // 目前暂时直接resolve
  228. resolve(null);
  229. });
  230. },
  231. // 跳转到订单详情
  232. navigateToOrderDetail(orderId) {
  233. // 隐藏加载提示
  234. wx.hideLoading();
  235. this.setData({ loading: false });
  236. wx.redirectTo({
  237. url: `/CRM/order/detail?id=${orderId}`,
  238. success: () => {
  239. // 跳转到订单详情页面
  240. wx.showToast({
  241. title: "开单成功",
  242. icon: "none",
  243. duration: 1500,
  244. });
  245. }
  246. });
  247. getCurrentPages().find(v => v.__route__ == 'CRM/customer/detail').partialRenewal(true);
  248. },
  249. interrupt({
  250. detail
  251. }) {
  252. // 处理中断逻辑,如果需要的话
  253. },
  254. /* 表单必填项是否完成 */
  255. onConfirm({
  256. detail
  257. }) {
  258. this.setData({
  259. disabled: detail
  260. });
  261. },
  262. onChange(e) {
  263. this.setData({
  264. showAll: e.detail
  265. });
  266. },
  267. closePage() {
  268. wx.navigateBack({
  269. delta: 1
  270. });
  271. },
  272. // tab切换
  273. onTabChange(e) {
  274. this.setData({
  275. activeTab: e.detail
  276. });
  277. },
  278. // 打开添加商品面板
  279. addProduct() {
  280. // 直接跳转到产品选择页面
  281. wx.navigateTo({
  282. url: `/select/product1/index?params=${JSON.stringify({
  283. "id": "2026031312441901",
  284. "content": {
  285. "pageNumber": 1,
  286. "pageSize": 20,
  287. "where": {
  288. "tablefilter": {
  289. "itemname": null,
  290. "itemno": null,
  291. "model": null,
  292. "guid_price": null,
  293. "guid_price_cus": null,
  294. "packageqty": null
  295. }
  296. }
  297. }
  298. })}&butText=添加商品`
  299. });
  300. // 设置全局回调函数
  301. getApp().globalData.handleSelect = this.handleSelect.bind(this);
  302. },
  303. // 处理选择商品回调
  304. handleSelect(detail) {
  305. if (detail && detail.list) {
  306. const existingItemIds = this.data.productList.map(item => item.sys_enterprise_itemid);
  307. // 过滤掉已经存在的商品
  308. const newProducts = detail.list.filter(item => !existingItemIds.includes(item.sys_enterprise_itemid)).map(item => {
  309. // 处理价格字段,去除格式化符号
  310. const priceStr = item.guid_price_cus || item.price || "0";
  311. const price = parseFloat(priceStr.toString().replace(/[¥,]/g, '')) || 0;
  312. const qty = parseInt(item.qty) || 1;
  313. return {
  314. itemid: item.itemid,
  315. sys_enterprise_itemid: item.sys_enterprise_itemid,
  316. itemname: item.itemname,
  317. itemno: item.itemno,
  318. model: item.model,
  319. originalPrice: price,
  320. price: price,
  321. discount: 1,
  322. qty: qty,
  323. amount: parseFloat((price * qty * 1).toFixed(2)),
  324. remarks: item.remarks || ''
  325. };
  326. });
  327. if (newProducts.length > 0) {
  328. this.setData({
  329. productList: [...this.data.productList, ...newProducts]
  330. });
  331. // 更新订单金额
  332. this.updateOrderAmount();
  333. } else {
  334. wx.showToast({
  335. title: '所选商品已在列表中',
  336. icon: 'none'
  337. });
  338. }
  339. // 返回页面
  340. wx.navigateBack();
  341. }
  342. },
  343. // 处理字段编辑
  344. onFieldBlur(e) {
  345. const index = e.currentTarget.dataset.index;
  346. const field = e.currentTarget.dataset.field;
  347. const value = e.detail.value;
  348. const productList = [...this.data.productList];
  349. const product = productList[index];
  350. // 处理不同字段的编辑
  351. switch (field) {
  352. case 'price':
  353. product.price = parseFloat(value) || 0;
  354. // 重新计算金额,保留两位小数,折扣1=100%
  355. product.amount = parseFloat((product.price * product.qty * product.discount).toFixed(2));
  356. break;
  357. case 'discount':
  358. product.discount = parseFloat(value) || 1;
  359. // 重新计算金额,保留两位小数,折扣1=100%
  360. product.amount = parseFloat((product.price * product.qty * product.discount).toFixed(2));
  361. break;
  362. case 'qty':
  363. let qty = parseInt(value) || 1;
  364. // 数量不能为0
  365. qty = Math.max(1, qty);
  366. product.qty = qty;
  367. // 重新计算金额,保留两位小数,折扣1=100%
  368. product.amount = parseFloat((product.price * product.qty * product.discount).toFixed(2));
  369. break;
  370. case 'amount':
  371. product.amount = parseFloat(value) || 0;
  372. // 重新计算单价(假设数量和折扣不变),保留两位小数,折扣1=100%
  373. if (product.qty > 0 && product.discount > 0) {
  374. product.price = parseFloat((product.amount / (product.qty * product.discount)).toFixed(2));
  375. }
  376. break;
  377. case 'remarks':
  378. product.remarks = value;
  379. break;
  380. }
  381. this.setData({
  382. productList
  383. });
  384. // 更新订单金额
  385. this.updateOrderAmount();
  386. },
  387. // 删除商品
  388. deleteProduct(e) {
  389. wx.showModal({
  390. title: '提示',
  391. content: '确定要删除这个商品吗?',
  392. confirmText: '确定',
  393. cancelText: '取消',
  394. success: (res) => {
  395. if (res.confirm) {
  396. const index = e.currentTarget.dataset.index;
  397. const productList = [...this.data.productList];
  398. productList.splice(index, 1);
  399. this.setData({
  400. productList
  401. });
  402. // 更新订单金额
  403. this.updateOrderAmount();
  404. }
  405. }
  406. });
  407. },
  408. // 更新订单金额
  409. updateOrderAmount() {
  410. // 计算总金额,确保不是NaN,默认0
  411. const totalAmount = parseFloat(this.data.productList.reduce((sum, item) => {
  412. const amount = parseFloat(item.amount) || 0;
  413. return sum + amount;
  414. }, 0)) || 0;
  415. // 计算总数量,所有商品数量的总计
  416. const totalQty = this.data.productList.reduce((sum, item) => {
  417. const qty = parseInt(item.qty) || 0;
  418. return sum + qty;
  419. }, 0);
  420. const form = [...this.data.form];
  421. form.forEach(item => {
  422. if (item.valueName === 'amount') {
  423. item.value = totalAmount.toString();
  424. this.setData({ "content.amount": totalAmount });
  425. }
  426. if (item.valueName === 'orderCount') {
  427. item.value = totalQty.toString();
  428. this.setData({ "content.orderCount": totalQty });
  429. }
  430. });
  431. this.setData({ form });
  432. }
  433. });