selectProduct.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const _Http = getApp().globalData.http;
  2. Page({
  3. data: {
  4. list: [],
  5. selectedCount: 0,
  6. selectedTotalPoints: 0,
  7. total: 0,
  8. sc_orderid: '',
  9. content: {
  10. nocache: true,
  11. pageNumber: 1,
  12. pageSize: 20,
  13. where: {
  14. condition: ""
  15. }
  16. }
  17. },
  18. onLoad(options) {
  19. this.setData({
  20. sc_orderid: options.sc_orderid || ''
  21. });
  22. getApp().globalData.Language.getLanguagePackage(this, '选择商品');
  23. this.getList();
  24. },
  25. // 搜索
  26. onSearch({ detail }) {
  27. this.data.content.where.condition = detail;
  28. this.getList(true);
  29. },
  30. // 获取商品列表
  31. getList(init = false) {
  32. if (init) this.selectComponent('#ListBox').goTop();
  33. _Http.init(this.data.content, init).then(content => {
  34. _Http.basic({
  35. id: 2026052510110006,
  36. content
  37. }).then(res => {
  38. this.selectComponent('#ListBox').RefreshToComplete();
  39. if (res.code != '1') return wx.showToast({ title: res.msg, icon: "none" });
  40. const list = (res.data || []).map(item => {
  41. // 处理图片URL
  42. try {
  43. if (item.attinfos && item.attinfos.length) {
  44. let att = item.attinfos[0];
  45. item.imgUrl = att.subfiles ? _Http.getSpecifiedImage(att) : (att.url || '');
  46. } else {
  47. item.imgUrl = '';
  48. }
  49. } catch (e) {
  50. item.imgUrl = '';
  51. }
  52. item.selected = false;
  53. item.qty = item.minimumorderqty || 1; // 初始化数量为起订量
  54. return item;
  55. });
  56. this.setData({
  57. content: _Http.paging(content, res),
  58. total: res.total || 0,
  59. list: res.pageNumber == 1 ? list : this.data.list.concat(list)
  60. });
  61. });
  62. });
  63. },
  64. // 切换选中状态
  65. onToggleSelect(e) {
  66. let index = e.currentTarget.dataset.index;
  67. let key = `list[${index}].selected`;
  68. this.setData({
  69. [key]: !this.data.list[index].selected
  70. });
  71. this.updateSelectedCount();
  72. },
  73. // 数量变化
  74. onQtyChange(e) {
  75. let qty = e.detail;
  76. let index = e.currentTarget.dataset.index;
  77. let item = this.data.list[index];
  78. if (!item) return;
  79. let minQty = item.minimumorderqty || 1;
  80. let increment = item.increment || 1;
  81. // 校验起订量
  82. if (qty < minQty) {
  83. qty = minQty;
  84. }
  85. // 校验增量
  86. if (increment > 1) {
  87. let remainder = (qty - minQty) % increment;
  88. if (remainder !== 0) {
  89. qty = qty + (increment - remainder);
  90. }
  91. }
  92. let key = `list[${index}].qty`;
  93. this.setData({ [key]: qty });
  94. this.updateSelectedCount();
  95. },
  96. // 阻止事件冒泡
  97. stopPropagation() {},
  98. // 更新选中数量和合计积分
  99. updateSelectedCount() {
  100. let selected = this.data.list.filter(v => v.selected);
  101. let count = selected.length;
  102. let totalPoints = selected.reduce((sum, v) => sum + (v.points || 0) * (v.qty || 0), 0);
  103. this.setData({ selectedCount: count, selectedTotalPoints: totalPoints });
  104. },
  105. // 确认添加
  106. onConfirm() {
  107. let selected = this.data.list.filter(v => v.selected);
  108. if (selected.length === 0) {
  109. return wx.showToast({ title: '请选择商品', icon: 'none' });
  110. }
  111. // 调用回调(回调中会处理API调用和页面跳转)
  112. if (getApp().globalData.handleSelect) {
  113. getApp().globalData.handleSelect(selected);
  114. }
  115. }
  116. });