select-goods.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import DictionariesBehavior from "../../../../core/behavior/dictionaries.behavior";
  2. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  3. import tickleBehavior, {
  4. getTickleContext,
  5. } from "../../../../core/behavior/tickle.behavior";
  6. import I18nBehavior from "../../../../i18n/behavior";
  7. import { getOfflineTreatmentListMethod } from "../../request";
  8. // module/order/pages/select-goods/select-goods.ts
  9. Component({
  10. behaviors: [PageContainerBehavior, DictionariesBehavior, tickleBehavior,I18nBehavior],
  11. lifetimes: {
  12. attached() {
  13. const healthAnalysisReportId = wx.getStorageSync('healthAnalysisReportId');
  14. if(healthAnalysisReportId){
  15. this.getOfflineTreatmentList(healthAnalysisReportId);
  16. }
  17. },
  18. },
  19. data: {
  20. goodsList: [] as Array<{
  21. category: string;
  22. goods: Array<{
  23. id: string;
  24. name: string;
  25. description?: string;
  26. image: string;
  27. price: number;
  28. quantity: number;
  29. checked: boolean;
  30. badge?: string;
  31. }>;
  32. }>,
  33. selectAll: true,
  34. selectedCount: 0,
  35. totalPrice: 0,
  36. i18n: {
  37. orderText: {
  38. selectGoods: '选择',
  39. payMeant:'确认'
  40. }
  41. }
  42. },
  43. observers: {
  44. 'goodsList': function () {
  45. this.calculateSummary();
  46. }
  47. },
  48. methods: {
  49. async getOfflineTreatmentList(healthAnalysisReportId: string) {
  50. const res = await getOfflineTreatmentListMethod(healthAnalysisReportId);
  51. const goodsList: any[] = [];
  52. Object.keys(res).forEach((categoryName: string) => {
  53. const categoryItems = res[categoryName] || [];
  54. if (categoryItems.length > 0) {
  55. const goods = categoryItems.map((item: any) => {
  56. return {
  57. id: item.id || '',
  58. name: item.name || '', //商品名称
  59. description: (() => {
  60. const dose = item?.cpFixedPricingRule?.convertDose ?? '';
  61. const unit = item?.cpFixedPricingRule?.convertUnit ?? '';
  62. return `${dose} ${unit}`;
  63. })(),
  64. image: item.itemImgSecond || '', //商品图片
  65. price: item.cpFixedPricingRule?.unitPrice || 0, //商品单价
  66. quantity: 1, //商品数量
  67. checked: true, //是否选中
  68. };
  69. });
  70. goodsList.push({
  71. category: categoryName,
  72. goods: goods,
  73. });
  74. }
  75. });
  76. this.setData({ goodsList });
  77. this.calculateSummary();
  78. },
  79. // 商品复选框变化
  80. onGoodsCheckChange(e: any) {
  81. const { categoryIndex, goodsIndex } = e.currentTarget.dataset;
  82. const checked = e.detail.checked;
  83. const goodsList = this.data.goodsList;
  84. const goods = goodsList[categoryIndex].goods[goodsIndex];
  85. goods.checked = checked;
  86. // 如果勾选且数量为0,设置为1
  87. if (checked && goods.quantity === 0) {
  88. goods.quantity = 1;
  89. }
  90. // 单价为0的商品,如果勾选,数量固定为1
  91. if (goods.price === 0) {
  92. if (checked) {
  93. goods.quantity = 1;
  94. } else {
  95. goods.quantity = 0;
  96. }
  97. }
  98. this.setData({ goodsList });
  99. this.calculateSummary();
  100. },
  101. // 全选变化
  102. onSelectAllChange(e: any) {
  103. const checked = e.detail.checked;
  104. const goodsList = this.data.goodsList.map(category => ({
  105. ...category,
  106. goods: category.goods.map(goods => ({
  107. ...goods,
  108. checked: checked,
  109. // 0元商品:选中时数量为1,未选中时数量为0
  110. // 非0元商品:选中时如果数量为0则设为1,否则保持原数量
  111. quantity: goods.price === 0
  112. ? (checked ? 1 : 0)
  113. : (checked && goods.quantity === 0 ? 1 : goods.quantity)
  114. }))
  115. }));
  116. this.setData({
  117. goodsList,
  118. selectAll: checked
  119. });
  120. this.calculateSummary();
  121. },
  122. // 数量变化(加减按钮)
  123. onQuantityChange(e: any) {
  124. const { categoryIndex, goodsIndex, type } = e.currentTarget.dataset;
  125. const goodsList = this.data.goodsList;
  126. const goods = goodsList[categoryIndex].goods[goodsIndex];
  127. // 单价为0的商品数量固定为1(如果被选中)
  128. if (goods.price === 0) {
  129. // 0元商品数量固定为1,不允许通过按钮修改
  130. return;
  131. } else {
  132. // 非0价格商品的正常逻辑
  133. let newQuantity = goods.quantity;
  134. if (type === "plus") {
  135. newQuantity = goods.quantity + 1;
  136. } else if (type === "minus") {
  137. newQuantity = Math.max(0, goods.quantity - 1);
  138. }
  139. goods.quantity = newQuantity;
  140. // 如果数量为0,自动取消勾选
  141. if (newQuantity === 0) {
  142. goods.checked = false;
  143. } else if (!goods.checked) {
  144. // 如果数量大于0且未勾选,自动勾选
  145. goods.checked = true;
  146. }
  147. }
  148. this.setData({ goodsList });
  149. this.calculateSummary();
  150. },
  151. // 数量输入
  152. onQuantityInput(e: any) {
  153. const { categoryIndex, goodsIndex } = e.currentTarget.dataset;
  154. const goodsList = this.data.goodsList;
  155. const goods = goodsList[categoryIndex].goods[goodsIndex];
  156. // 单价为0的商品数量固定为1,不允许修改
  157. if (goods.price === 0) {
  158. // 保持数量为1
  159. goods.quantity = 1;
  160. this.setData({ goodsList });
  161. return;
  162. }
  163. const value = parseInt(e.detail.value) || 0;
  164. goods.quantity = Math.max(0, value);
  165. this.setData({ goodsList });
  166. },
  167. // 数量输入失焦
  168. onQuantityBlur(e: any) {
  169. const { categoryIndex, goodsIndex } = e.currentTarget.dataset;
  170. const goodsList = this.data.goodsList;
  171. const goods = goodsList[categoryIndex].goods[goodsIndex];
  172. // 单价为0的商品数量固定为1(如果被选中)
  173. if (goods.price === 0) {
  174. // 如果被选中,数量固定为1
  175. if (goods.checked) {
  176. goods.quantity = 1;
  177. } else {
  178. // 如果未选中,数量为0
  179. goods.quantity = 0;
  180. }
  181. } else {
  182. // 非0价格商品的逻辑
  183. if (goods.quantity === 0) {
  184. goods.checked = false;
  185. } else if (!goods.checked) {
  186. // 如果数量大于0且未勾选,自动勾选
  187. goods.checked = true;
  188. }
  189. }
  190. this.setData({ goodsList });
  191. this.calculateSummary();
  192. },
  193. // 计算汇总信息
  194. calculateSummary() {
  195. const goodsList = this.data.goodsList;
  196. let selectedCount = 0;
  197. let totalPrice = 0;
  198. let allChecked = true;
  199. goodsList.forEach((category: any) => {
  200. category.goods.forEach((goods: any) => {
  201. if (goods.checked && goods.quantity > 0) {
  202. selectedCount++; // 已选件数 = 选中的商品种类数
  203. totalPrice += goods.price * goods.quantity;
  204. }
  205. if (!goods.checked || goods.quantity === 0) {
  206. allChecked = false;
  207. }
  208. });
  209. });
  210. this.setData({
  211. selectedCount,
  212. totalPrice: totalPrice.toFixed(2),
  213. selectAll: allChecked
  214. });
  215. },
  216. // 结算
  217. onCheckout() {
  218. const goodsList = this.data.goodsList;
  219. // 过滤出选中的商品
  220. const selectedGoods = goodsList.map((category: any) => ({
  221. category: category.category,
  222. goods: category.goods.filter((goods: any) => goods.checked && goods.quantity > 0)
  223. })).filter((category: any) => category.goods.length > 0);
  224. if (selectedGoods.length === 0) {
  225. getTickleContext.call(this).showWarnMessage("请至少选择一件商品");
  226. return;
  227. }
  228. // 跳转到确认订单页面
  229. wx.setStorageSync('selectedGoods', selectedGoods);
  230. wx.setStorageSync('totalPrice', this.data.totalPrice);
  231. wx.navigateTo({
  232. url: `/module/order/pages/confirme-order/confirme-order`,
  233. fail: (err) => {
  234. getTickleContext.call(this).showWarnMessage(err.errMsg || "跳转失败");
  235. },
  236. });
  237. },
  238. }
  239. });