index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. const _Http = getApp().globalData.http,
  2. currency = require("../../../../utils/currency"),
  3. CNY = value => currency(value, {
  4. symbol: "¥",
  5. precision: 2
  6. }).format();
  7. Component({
  8. properties: {
  9. orderId: {
  10. type: String,
  11. value: ''
  12. },
  13. orderStatus: {
  14. type: String,
  15. value: ''
  16. }
  17. },
  18. data: {
  19. list: [],
  20. loading: false,
  21. content: {
  22. nocache: true,
  23. pageNumber: 1,
  24. pageSize: 20,
  25. pageTotal: 1,
  26. total: null,
  27. where: {
  28. tablefilter: {
  29. itemname: null,
  30. itemno: null,
  31. model: null,
  32. amount: ""
  33. }
  34. }
  35. }
  36. },
  37. methods: {
  38. /* 获取订单明细列表 */
  39. getList(id, init) {
  40. let content = this.data.content;
  41. content.sa_custorderid = id || this.data.orderId || this.data.sa_custorderid;
  42. if (init) {
  43. content.pageNumber = 1;
  44. this.setData({
  45. loading: true
  46. });
  47. }
  48. _Http.basic({
  49. "id": "2026031414243401",
  50. content
  51. }).then(res => {
  52. this.setData({
  53. loading: false
  54. });
  55. console.log("订单明细列表", res)
  56. if (res.code != 1) return wx.showToast({
  57. title: res.msg,
  58. icon: "none"
  59. })
  60. // 格式化金额数据
  61. const formattedData = res.data.map(item => {
  62. const amount = (item.price || 0) * (item.discountrate || 1) * (item.qty || 0);
  63. return {
  64. ...item,
  65. showPrice: CNY(item.price || 0),
  66. showAmount: CNY(amount)
  67. };
  68. })
  69. this.setData({
  70. list: res.pageNumber == 1 ? formattedData : this.data.list.concat(formattedData),
  71. "content.pageNumber": res.pageNumber + 1,
  72. "content.pageSize": res.pageSize,
  73. "content.pageTotal": res.pageTotal,
  74. "content.total": res.total,
  75. "sa_custorderid": content.sa_custorderid
  76. })
  77. })
  78. },
  79. // 打开添加商品面板
  80. addProduct() {
  81. // 已退单状态不能添加商品
  82. if (this.data.orderStatus === '已退单') {
  83. wx.showToast({
  84. title: '已退单订单不能添加商品',
  85. icon: 'none'
  86. });
  87. return;
  88. }
  89. // 直接跳转到产品选择页面
  90. wx.navigateTo({
  91. url: `/CRM/customer/modules/orderCreate/productSelect/index?params=${JSON.stringify({
  92. "id": "2026031312441901",
  93. "content": {
  94. "pageNumber": 1,
  95. "pageSize": 20,
  96. "where": {
  97. "tablefilter": {
  98. "itemname": null,
  99. "itemno": null,
  100. "model": null,
  101. "guid_price": null,
  102. "guid_price_cus": null,
  103. "packageqty": null
  104. }
  105. }
  106. }
  107. })}&butText=添加商品`
  108. });
  109. // 设置全局回调函数
  110. getApp().globalData.handleSelect = this.handleSelect.bind(this);
  111. },
  112. // 处理选择商品回调
  113. handleSelect(detail) {
  114. // 已退单状态不能添加商品
  115. if (this.data.orderStatus === '已退单') {
  116. wx.showToast({
  117. title: '已退单订单不能添加商品',
  118. icon: 'none'
  119. });
  120. return;
  121. }
  122. if (detail && detail.list) {
  123. const orderId = this.data.sa_custorderid;
  124. if (!orderId) {
  125. wx.showToast({
  126. title: '订单ID不存在',
  127. icon: 'none'
  128. });
  129. return;
  130. }
  131. // 显示加载提示
  132. wx.showLoading({
  133. title: '正在添加商品...',
  134. mask: true
  135. });
  136. // 准备绑定操作
  137. const promises = detail.list.map(item => {
  138. return new Promise((resolve, reject) => {
  139. // 处理价格字段,去除格式化符号
  140. const priceStr = item.guid_price_cus || item.price || "0";
  141. const price = parseFloat(priceStr.toString().replace(/[¥,]/g, '')) || 0;
  142. const qty = parseInt(item.qty) || 1;
  143. const amount = parseFloat((price * qty * 1).toFixed(2));
  144. // 直接调用绑定商品接口
  145. _Http.basic({
  146. "id": "2026031415462301", // 绑定商品接口ID
  147. content: {
  148. sa_custorderid: orderId,
  149. sa_custorderitemsid: 0,
  150. sys_enterprise_itemid: item.sys_enterprise_itemid || item.itemid,
  151. qty: qty,
  152. oldprice: price,
  153. discountrate: 1,
  154. price: price,
  155. amount: amount,
  156. remarks: item.remarks || ""
  157. }
  158. }).then(res => {
  159. if (res.code != 1) {
  160. console.error("绑定商品失败", res);
  161. }
  162. resolve(res);
  163. }).catch(err => {
  164. console.error("绑定商品失败", err);
  165. resolve(null); // 即使失败也继续处理
  166. });
  167. });
  168. });
  169. // 使用Promise.all处理所有绑定操作
  170. Promise.all(promises).then(() => {
  171. // 隐藏加载提示
  172. wx.hideLoading();
  173. // 刷新商品列表
  174. this.getList(orderId, true);
  175. // 显示成功提示
  176. setTimeout(() => {
  177. wx.showToast({
  178. title: '商品添加成功',
  179. icon: 'none'
  180. });
  181. });
  182. // 返回页面
  183. wx.navigateBack();
  184. }).catch(() => {
  185. // 隐藏加载提示
  186. wx.hideLoading();
  187. // 刷新商品列表
  188. this.getList(orderId, true);
  189. // 显示成功提示
  190. setTimeout(() => {
  191. wx.showToast({
  192. title: '商品添加成功',
  193. icon: 'none'
  194. });
  195. });
  196. // 返回页面
  197. wx.navigateBack();
  198. });
  199. }
  200. },
  201. // 处理字段编辑
  202. onFieldBlur(e) {
  203. // 已退单状态不能编辑商品
  204. if (this.data.orderStatus === '已退单') {
  205. wx.showToast({
  206. title: '已退单订单不能编辑商品',
  207. icon: 'none'
  208. });
  209. return;
  210. }
  211. const index = e.currentTarget.dataset.index;
  212. const field = e.currentTarget.dataset.field;
  213. const value = e.detail.value;
  214. const list = [...this.data.list];
  215. const item = list[index];
  216. // 处理不同字段的编辑
  217. switch (field) {
  218. case 'price':
  219. item.price = parseFloat(value) || 0;
  220. // 重新计算金额
  221. item.amount = parseFloat((item.price * (item.discountrate || 1) * item.qty).toFixed(2));
  222. break;
  223. case 'discountrate':
  224. // 确保折扣值有效,默认为1(100%)
  225. let discount = parseFloat(value) || 1;
  226. // 折扣不能小于0
  227. discount = Math.max(0, discount);
  228. item.discountrate = discount;
  229. // 重新计算金额
  230. item.amount = parseFloat((item.price * discount * item.qty).toFixed(2));
  231. break;
  232. case 'qty':
  233. let qty = parseInt(value) || 1;
  234. // 数量不能为0
  235. qty = Math.max(1, qty);
  236. item.qty = qty;
  237. // 重新计算金额
  238. item.amount = parseFloat((item.price * (item.discountrate || 1) * qty).toFixed(2));
  239. break;
  240. case 'amount':
  241. // 根据新金额计算单价
  242. const newAmount = parseFloat(value) || 0;
  243. item.amount = newAmount;
  244. if (item.qty > 0 && (item.discountrate || 1) > 0) {
  245. item.price = parseFloat((newAmount / item.qty / (item.discountrate || 1)).toFixed(2));
  246. }
  247. break;
  248. case 'remarks':
  249. item.remarks = value;
  250. break;
  251. }
  252. // 确保 discountrate 存在
  253. if (item.discountrate === undefined || item.discountrate === null) {
  254. item.discountrate = 1;
  255. }
  256. // 立即更新页面显示
  257. this.setData({
  258. list
  259. });
  260. // 调用接口更新商品信息
  261. this.updateProduct(item);
  262. },
  263. // 更新商品信息
  264. updateProduct(item) {
  265. const orderId = this.data.sa_custorderid;
  266. if (!orderId || !item.sa_custorderitemsid) return;
  267. // 显示加载提示
  268. wx.showLoading({
  269. title: '正在更新商品...',
  270. mask: true
  271. });
  272. // 调用绑定商品接口(用于更新)
  273. _Http.basic({
  274. "id": "2026031415462301", // 绑定商品接口ID
  275. content: {
  276. sa_custorderid: orderId,
  277. sa_custorderitemsid: item.sa_custorderitemsid,
  278. sys_enterprise_itemid: item.sys_enterprise_itemid || item.itemid,
  279. qty: item.qty,
  280. oldprice: item.price, // 使用当前价格作为原价
  281. discountrate: item.discountrate || 1,
  282. price: item.price,
  283. amount: item.amount,
  284. remarks: item.remarks || ""
  285. }
  286. }).then(res => {
  287. // 隐藏加载提示
  288. wx.hideLoading();
  289. if (res.code === 1) {
  290. // 显示成功提示
  291. wx.showToast({
  292. title: '更新商品成功',
  293. icon: 'none'
  294. });
  295. // 刷新商品列表
  296. this.getList(orderId, true);
  297. } else {
  298. console.error("更新商品失败", res);
  299. // 显示失败提示
  300. wx.showToast({
  301. title: '更新商品失败',
  302. icon: 'none'
  303. });
  304. }
  305. }).catch(err => {
  306. // 隐藏加载提示
  307. wx.hideLoading();
  308. console.error("更新商品失败", err);
  309. // 显示失败提示
  310. wx.showToast({
  311. title: '网络错误',
  312. icon: 'none'
  313. });
  314. });
  315. },
  316. // 删除商品
  317. deleteProduct(e) {
  318. // 已退单状态不能删除商品
  319. if (this.data.orderStatus === '已退单') {
  320. wx.showToast({
  321. title: '已退单订单不能删除商品',
  322. icon: 'none'
  323. });
  324. return;
  325. }
  326. const index = e.currentTarget.dataset.index;
  327. const item = this.data.list[index];
  328. if (!item.sa_custorderitemsid) {
  329. wx.showToast({
  330. title: '商品ID不存在',
  331. icon: 'none'
  332. });
  333. return;
  334. }
  335. wx.showModal({
  336. title: '提示',
  337. content: '确定要删除这个商品吗?',
  338. confirmText: '确定',
  339. cancelText: '取消',
  340. success: (res) => {
  341. if (res.confirm) {
  342. // 调用删除接口
  343. _Http.basic({
  344. "id": "2026031416083401", // 删除产品明细接口ID
  345. content: {
  346. sa_custorderid: item.sa_custorderid,
  347. sa_custorderitemsid: item.sa_custorderitemsid
  348. }
  349. }).then(res => {
  350. console.log('删除商品结果:', res);
  351. if (res.code === 1) {
  352. // 显示成功提示
  353. wx.showToast({
  354. title: '删除成功',
  355. icon: 'none'
  356. });
  357. // 刷新商品列表
  358. this.getList(this.data.sa_custorderid, true);
  359. } else {
  360. wx.showToast({
  361. title: res.msg || '删除失败',
  362. icon: 'none'
  363. });
  364. }
  365. }).catch(err => {
  366. console.error('删除商品失败:', err);
  367. wx.showToast({
  368. title: '网络错误',
  369. icon: 'none'
  370. });
  371. });
  372. }
  373. }
  374. });
  375. }
  376. }
  377. })