create.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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({
  140. loading: false
  141. });
  142. return;
  143. }
  144. let content = {
  145. ...this.data.content,
  146. ...formData
  147. };
  148. // 先创建订单
  149. _Http.basic({
  150. "id": "2026031309441701", // 创建订单的接口ID
  151. content
  152. }).then(res => {
  153. console.log("保存订单", res);
  154. if (res.code == 1) {
  155. const orderId = res.data;
  156. console.log("订单ID", orderId);
  157. // 显示加载提示
  158. wx.showLoading({
  159. title: '正在处理...',
  160. mask: true
  161. });
  162. // 准备绑定操作
  163. const promises = [];
  164. // 绑定商品
  165. if (this.data.productList.length > 0) {
  166. promises.push(this.bindProductsWithPromise(orderId));
  167. }
  168. // 绑定附件
  169. const attachmentids = this.selectComponent("#Yl_files").getFiles().attachmentids;
  170. if (attachmentids && attachmentids.length > 0) {
  171. promises.push(this.bindAttachmentsWithPromise(orderId, attachmentids));
  172. }
  173. // 使用Promise.all处理所有绑定操作
  174. Promise.all(promises).then(() => {
  175. // 所有绑定完成,跳转到订单详情
  176. this.navigateToOrderDetail(orderId);
  177. }).catch(() => {
  178. // 绑定失败,跳转到订单详情
  179. this.navigateToOrderDetail(orderId);
  180. });
  181. } else {
  182. this.setData({
  183. loading: false
  184. });
  185. wx.showToast({
  186. title: res.msg || '保存失败',
  187. icon: 'none'
  188. });
  189. }
  190. }).catch(err => {
  191. this.setData({
  192. loading: false
  193. });
  194. console.error("保存订单失败", err);
  195. wx.showToast({
  196. title: '网络错误',
  197. icon: 'none'
  198. });
  199. });
  200. },
  201. // 使用Promise.all绑定商品
  202. bindProductsWithPromise(orderId) {
  203. const productList = this.data.productList;
  204. // 创建商品绑定的Promise数组
  205. const bindPromises = productList.map(product => {
  206. return new Promise((resolve, reject) => {
  207. _Http.basic({
  208. "id": "2026031415462301", // 绑定商品接口ID
  209. content: {
  210. sa_custorderid: orderId,
  211. sa_custorderitemsid: 0,
  212. sys_enterprise_itemid: product.sys_enterprise_itemid || product.itemid,
  213. qty: product.qty,
  214. oldprice: product.originalPrice || product.price,
  215. discountrate: product.discount,
  216. price: product.price,
  217. amount: product.amount,
  218. remarks: product.remarks || ""
  219. }
  220. }).then(res => {
  221. if (res.code != 1) {
  222. console.error("绑定商品失败", res);
  223. }
  224. resolve(res);
  225. }).catch(err => {
  226. console.error("绑定商品失败", err);
  227. resolve(null); // 即使失败也继续处理
  228. });
  229. });
  230. });
  231. // 处理附件绑定(预留接口)
  232. const attachmentPromise = this.bindAttachmentsWithPromise(orderId);
  233. // 合并所有Promise
  234. const allPromises = [...bindPromises, attachmentPromise];
  235. // 使用Promise.all处理所有请求
  236. return Promise.all(allPromises);
  237. },
  238. // 绑定附件
  239. bindAttachmentsWithPromise(orderId, attachmentids) {
  240. return new Promise((resolve) => {
  241. if (!attachmentids || attachmentids.length === 0) {
  242. resolve(null);
  243. return;
  244. }
  245. _Http.basic({
  246. "classname": "system.attachment.Attachment",
  247. "method": "createFileLink",
  248. id: 10020501,
  249. "content": {
  250. ownertable: "sa_custorder",
  251. ownerid: orderId,
  252. usetype: 'default',
  253. attachmentids
  254. }
  255. }).then(res => {
  256. console.log('绑定附件', res);
  257. if (res.code == 1) {
  258. resolve(res.data);
  259. } else {
  260. console.error('绑定附件失败', res);
  261. resolve(); // 绑定失败也继续执行
  262. }
  263. }).catch(err => {
  264. console.error('绑定附件错误', err);
  265. resolve(); // 错误也继续执行
  266. });
  267. });
  268. },
  269. // 跳转到订单详情
  270. navigateToOrderDetail(orderId) {
  271. // 隐藏加载提示
  272. wx.hideLoading();
  273. this.setData({
  274. loading: false
  275. });
  276. wx.redirectTo({
  277. url: `/CRM/order/detail?id=${orderId}`,
  278. success: () => {
  279. // 跳转到订单详情页面
  280. wx.showToast({
  281. title: "开单成功",
  282. icon: "none",
  283. duration: 1500,
  284. });
  285. }
  286. });
  287. getCurrentPages().find(v => v.__route__ == 'CRM/customer/detail').partialRenewal(true);
  288. },
  289. interrupt({
  290. detail
  291. }) {
  292. // 处理中断逻辑,如果需要的话
  293. },
  294. /* 表单必填项是否完成 */
  295. onConfirm({
  296. detail
  297. }) {
  298. this.setData({
  299. disabled: detail
  300. });
  301. },
  302. onChange(e) {
  303. this.setData({
  304. showAll: e.detail
  305. });
  306. },
  307. closePage() {
  308. wx.navigateBack({
  309. delta: 1
  310. });
  311. },
  312. // tab切换
  313. onTabChange(e) {
  314. this.setData({
  315. activeTab: e.detail
  316. });
  317. },
  318. // 打开添加商品面板
  319. addProduct() {
  320. // 直接跳转到产品选择页面
  321. wx.navigateTo({
  322. url: `/CRM/customer/modules/orderCreate/productSelect/index?params=${JSON.stringify({
  323. "id": "2026031312441901",
  324. "content": {
  325. "pageNumber": 1,
  326. "pageSize": 20,
  327. "where": {
  328. "tablefilter": {
  329. "itemname": null,
  330. "itemno": null,
  331. "model": null,
  332. "guid_price": null,
  333. "guid_price_cus": null,
  334. "packageqty": null
  335. }
  336. }
  337. }
  338. })}&butText=添加商品`
  339. });
  340. // 设置全局回调函数
  341. getApp().globalData.handleSelect = this.handleSelect.bind(this);
  342. },
  343. // 处理选择商品回调
  344. handleSelect(detail) {
  345. if (detail && detail.list) {
  346. const existingItemIds = this.data.productList.map(item => item.sys_enterprise_itemid);
  347. // 过滤掉已经存在的商品
  348. const newProducts = detail.list.filter(item => !existingItemIds.includes(item.sys_enterprise_itemid)).map(item => {
  349. // 处理价格字段,去除格式化符号
  350. const priceStr = item.guid_price_cus || item.price || "0";
  351. const price = parseFloat(priceStr.toString().replace(/[¥,]/g, '')) || 0;
  352. const qty = parseInt(item.qty) || 1;
  353. return {
  354. itemid: item.itemid,
  355. sys_enterprise_itemid: item.sys_enterprise_itemid,
  356. itemname: item.itemname,
  357. itemno: item.itemno,
  358. model: item.model,
  359. originalPrice: price,
  360. price: price,
  361. discount: 1,
  362. qty: qty,
  363. amount: parseFloat((price * qty * 1).toFixed(2)),
  364. remarks: item.remarks || ''
  365. };
  366. });
  367. if (newProducts.length > 0) {
  368. this.setData({
  369. productList: [...this.data.productList, ...newProducts]
  370. });
  371. // 更新订单金额
  372. this.updateOrderAmount();
  373. } else {
  374. wx.showToast({
  375. title: '所选商品已在列表中',
  376. icon: 'none'
  377. });
  378. }
  379. // 返回页面
  380. wx.navigateBack();
  381. }
  382. },
  383. // 处理字段编辑
  384. onFieldBlur(e) {
  385. const index = e.currentTarget.dataset.index;
  386. const field = e.currentTarget.dataset.field;
  387. const value = e.detail.value;
  388. const productList = [...this.data.productList];
  389. const product = productList[index];
  390. // 处理不同字段的编辑
  391. switch (field) {
  392. case 'price':
  393. product.price = parseFloat(value) || 0;
  394. // 重新计算金额,保留两位小数,折扣1=100%
  395. product.amount = parseFloat((product.price * product.qty * product.discount).toFixed(2));
  396. break;
  397. case 'discount':
  398. product.discount = parseFloat(value) || 1;
  399. // 重新计算金额,保留两位小数,折扣1=100%
  400. product.amount = parseFloat((product.price * product.qty * product.discount).toFixed(2));
  401. break;
  402. case 'qty':
  403. let qty = parseInt(value) || 1;
  404. // 数量不能为0
  405. qty = Math.max(1, qty);
  406. product.qty = qty;
  407. // 重新计算金额,保留两位小数,折扣1=100%
  408. product.amount = parseFloat((product.price * product.qty * product.discount).toFixed(2));
  409. break;
  410. case 'amount':
  411. product.amount = parseFloat(value) || 0;
  412. // 重新计算单价(假设数量和折扣不变),保留两位小数,折扣1=100%
  413. if (product.qty > 0 && product.discount > 0) {
  414. product.price = parseFloat((product.amount / (product.qty * product.discount)).toFixed(2));
  415. }
  416. break;
  417. case 'remarks':
  418. product.remarks = value;
  419. break;
  420. }
  421. this.setData({
  422. productList
  423. });
  424. // 更新订单金额
  425. this.updateOrderAmount();
  426. },
  427. // 删除商品
  428. deleteProduct(e) {
  429. wx.showModal({
  430. title: '提示',
  431. content: '确定要删除这个商品吗?',
  432. confirmText: '确定',
  433. cancelText: '取消',
  434. success: (res) => {
  435. if (res.confirm) {
  436. const index = e.currentTarget.dataset.index;
  437. const productList = [...this.data.productList];
  438. productList.splice(index, 1);
  439. this.setData({
  440. productList
  441. });
  442. // 更新订单金额
  443. this.updateOrderAmount();
  444. }
  445. }
  446. });
  447. },
  448. // 更新订单金额
  449. updateOrderAmount() {
  450. // 计算总金额,确保不是NaN,默认0
  451. const totalAmount = parseFloat(this.data.productList.reduce((sum, item) => {
  452. const amount = parseFloat(item.amount) || 0;
  453. return sum + amount;
  454. }, 0)) || 0;
  455. // 计算总数量,所有商品数量的总计
  456. const totalQty = this.data.productList.reduce((sum, item) => {
  457. const qty = parseInt(item.qty) || 0;
  458. return sum + qty;
  459. }, 0);
  460. const form = [...this.data.form];
  461. form.forEach(item => {
  462. if (item.valueName === 'amount') {
  463. item.value = totalAmount.toString();
  464. this.setData({
  465. "content.amount": totalAmount
  466. });
  467. }
  468. if (item.valueName === 'orderCount') {
  469. item.value = totalQty.toString();
  470. this.setData({
  471. "content.orderCount": totalQty
  472. });
  473. }
  474. });
  475. this.setData({
  476. form
  477. });
  478. },
  479. // 处理上传状态
  480. changeState({
  481. detail
  482. }) {
  483. this.setData({
  484. loading: detail
  485. });
  486. },
  487. // 处理文件上传回调
  488. handleFileUpload({
  489. detail
  490. }) {
  491. console.log("detail",detail)
  492. Promise.all(detail.map(attachmentid => {
  493. return _Http.basic({
  494. "id": 2024061710590401,
  495. "content": {
  496. "pageSize": 1,
  497. "pageNumber": 1,
  498. "attachmentid": attachmentid
  499. }
  500. })
  501. })).then(res => {
  502. console.log('获取文件详情', res);
  503. this.selectComponent("#Yl_files").handleFiles(res.map(v=>v.data[0]))
  504. })
  505. },
  506. });