s-select-sku.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. "use strict";
  2. const common_vendor = require("../../../common/vendor.js");
  3. const sheep_index = require("../../index.js");
  4. const sheep_hooks_useGoods = require("../../hooks/useGoods.js");
  5. require("../../url/index.js");
  6. require("../../store/index.js");
  7. require("../../store/app.js");
  8. require("../../api/promotion/diy.js");
  9. require("../../request/index.js");
  10. require("../../config/index.js");
  11. require("../../platform/index.js");
  12. require("../../platform/provider/wechat/index.js");
  13. require("../../platform/provider/wechat/miniProgram.js");
  14. require("../../api/member/auth.js");
  15. require("../../api/member/social.js");
  16. require("../../api/member/user.js");
  17. require("../../platform/provider/apple/index.js");
  18. require("../../platform/share.js");
  19. require("../../router/index.js");
  20. require("../../hooks/useModal.js");
  21. require("../../helper/index.js");
  22. require("../../helper/test.js");
  23. require("../../helper/digit.js");
  24. require("../../helper/throttle.js");
  25. require("../../platform/pay.js");
  26. require("../../api/pay/order.js");
  27. require("../../store/user.js");
  28. require("../../store/cart.js");
  29. require("../../api/trade/cart.js");
  30. require("../../api/pay/wallet.js");
  31. require("../../api/trade/order.js");
  32. require("../../api/promotion/coupon.js");
  33. require("../../store/sys.js");
  34. require("../../store/modal.js");
  35. require("../../config/zIndex.js");
  36. require("../../util/index.js");
  37. if (!Array) {
  38. const _easycom_su_number_box2 = common_vendor.resolveComponent("su-number-box");
  39. const _easycom_su_popup2 = common_vendor.resolveComponent("su-popup");
  40. (_easycom_su_number_box2 + _easycom_su_popup2)();
  41. }
  42. const _easycom_su_number_box = () => "../../ui/su-number-box/su-number-box.js";
  43. const _easycom_su_popup = () => "../../ui/su-popup/su-popup.js";
  44. if (!Math) {
  45. (_easycom_su_number_box + _easycom_su_popup)();
  46. }
  47. const _sfc_main = {
  48. __name: "s-select-sku",
  49. props: {
  50. goodsInfo: {
  51. type: Object,
  52. default() {
  53. }
  54. },
  55. show: {
  56. type: Boolean,
  57. default: false
  58. }
  59. },
  60. emits: ["change", "addCart", "buy", "close"],
  61. setup(__props, { emit: emits }) {
  62. const props = __props;
  63. const state = common_vendor.reactive({
  64. selectedSku: {},
  65. // 选中的 SKU
  66. currentPropertyArray: []
  67. // 当前选中的属性,实际是个 Map。key 是 property 编号,value 是 value 编号
  68. });
  69. const propertyList = sheep_hooks_useGoods.convertProductPropertyList(props.goodsInfo.skus);
  70. const skuList = common_vendor.computed(() => {
  71. let skuPrices = props.goodsInfo.skus;
  72. for (let price of skuPrices) {
  73. price.value_id_array = price.properties.map((item) => item.valueId);
  74. }
  75. return skuPrices;
  76. });
  77. common_vendor.watch(
  78. () => state.selectedSku,
  79. (newVal) => {
  80. emits("change", newVal);
  81. },
  82. {
  83. immediate: true,
  84. // 立即执行
  85. deep: true
  86. // 深度监听
  87. }
  88. );
  89. function onNumberChange(e) {
  90. if (e === 0)
  91. return;
  92. if (state.selectedSku.goods_num === e)
  93. return;
  94. state.selectedSku.goods_num = e;
  95. }
  96. function onAddCart() {
  97. if (state.selectedSku.id <= 0) {
  98. sheep_index.sheep.$helper.toast("请选择规格");
  99. return;
  100. }
  101. if (state.selectedSku.stock <= 0) {
  102. sheep_index.sheep.$helper.toast("库存不足");
  103. return;
  104. }
  105. emits("addCart", state.selectedSku);
  106. }
  107. function onBuy() {
  108. if (state.selectedSku.id <= 0) {
  109. sheep_index.sheep.$helper.toast("请选择规格");
  110. return;
  111. }
  112. if (state.selectedSku.stock <= 0) {
  113. sheep_index.sheep.$helper.toast("库存不足");
  114. return;
  115. }
  116. emits("buy", state.selectedSku);
  117. }
  118. function changeDisabled(isChecked = false, propertyId = 0, valueId = 0) {
  119. let newSkus = [];
  120. if (isChecked) {
  121. for (let price of skuList.value) {
  122. if (price.stock <= 0) {
  123. continue;
  124. }
  125. if (price.value_id_array.indexOf(valueId) >= 0) {
  126. newSkus.push(price);
  127. }
  128. }
  129. } else {
  130. newSkus = getCanUseSkuList();
  131. }
  132. let noChooseValueIds = [];
  133. for (let price of newSkus) {
  134. noChooseValueIds = noChooseValueIds.concat(price.value_id_array);
  135. }
  136. noChooseValueIds = Array.from(new Set(noChooseValueIds));
  137. if (isChecked) {
  138. let index = noChooseValueIds.indexOf(valueId);
  139. noChooseValueIds.splice(index, 1);
  140. } else {
  141. state.currentPropertyArray.forEach((currentPropertyId) => {
  142. if (currentPropertyId.toString() !== "") {
  143. return;
  144. }
  145. let index = noChooseValueIds.indexOf(currentPropertyId);
  146. if (index >= 0) {
  147. noChooseValueIds.splice(index, 1);
  148. }
  149. });
  150. }
  151. let choosePropertyIds = [];
  152. if (!isChecked) {
  153. state.currentPropertyArray.forEach((currentPropertyId, currentValueId) => {
  154. if (currentPropertyId !== "") {
  155. choosePropertyIds.push(currentValueId);
  156. }
  157. });
  158. } else {
  159. choosePropertyIds = [propertyId];
  160. }
  161. for (let propertyIndex in propertyList) {
  162. if (choosePropertyIds.indexOf(propertyList[propertyIndex]["id"]) >= 0) {
  163. continue;
  164. }
  165. for (let valueIndex in propertyList[propertyIndex]["values"]) {
  166. propertyList[propertyIndex]["values"][valueIndex]["disabled"] = noChooseValueIds.indexOf(propertyList[propertyIndex]["values"][valueIndex]["id"]) < 0;
  167. }
  168. }
  169. }
  170. function getCanUseSkuList() {
  171. let newSkus = [];
  172. for (let sku of skuList.value) {
  173. if (sku.stock <= 0) {
  174. continue;
  175. }
  176. let isOk = true;
  177. state.currentPropertyArray.forEach((propertyId) => {
  178. if (propertyId.toString() !== "" && sku.value_id_array.indexOf(propertyId) < 0) {
  179. isOk = false;
  180. }
  181. });
  182. if (isOk) {
  183. newSkus.push(sku);
  184. }
  185. }
  186. return newSkus;
  187. }
  188. function onSelectSku(propertyId, valueId) {
  189. let isChecked = true;
  190. if (state.currentPropertyArray[propertyId] !== void 0 && state.currentPropertyArray[propertyId] === valueId) {
  191. isChecked = false;
  192. state.currentPropertyArray.splice(propertyId, 1, "");
  193. } else {
  194. state.currentPropertyArray[propertyId] = valueId;
  195. }
  196. let choosePropertyId = [];
  197. state.currentPropertyArray.forEach((currentPropertyId) => {
  198. if (currentPropertyId !== "") {
  199. choosePropertyId.push(currentPropertyId);
  200. }
  201. });
  202. let newSkuList = getCanUseSkuList();
  203. if (choosePropertyId.length === propertyList.length && newSkuList.length) {
  204. newSkuList[0].goods_num = state.selectedSku.goods_num || 1;
  205. state.selectedSku = newSkuList[0];
  206. } else {
  207. state.selectedSku = {};
  208. }
  209. changeDisabled(isChecked, propertyId, valueId);
  210. }
  211. changeDisabled(false);
  212. return (_ctx, _cache) => {
  213. return {
  214. a: state.selectedSku.picUrl || __props.goodsInfo.picUrl,
  215. b: common_vendor.t(__props.goodsInfo.name),
  216. c: common_vendor.t(common_vendor.unref(sheep_hooks_useGoods.fen2yuan)(state.selectedSku.price || __props.goodsInfo.price)),
  217. d: common_vendor.t(common_vendor.unref(sheep_hooks_useGoods.formatStock)("exact", state.selectedSku.stock || __props.goodsInfo.stock)),
  218. e: common_vendor.f(common_vendor.unref(propertyList), (property, k0, i0) => {
  219. return {
  220. a: common_vendor.t(property.name),
  221. b: common_vendor.f(property.values, (value, k1, i1) => {
  222. return {
  223. a: common_vendor.t(value.name),
  224. b: common_vendor.n({
  225. "ui-BG-Main-Gradient": state.currentPropertyArray[property.id] === value.id
  226. }),
  227. c: common_vendor.n({
  228. "disabled-btn": value.disabled === true
  229. }),
  230. d: value.id,
  231. e: value.disabled === true,
  232. f: common_vendor.o(($event) => onSelectSku(property.id, value.id), value.id)
  233. };
  234. }),
  235. c: property.id
  236. };
  237. }),
  238. f: common_vendor.o(($event) => onNumberChange($event)),
  239. g: common_vendor.o(($event) => state.selectedSku.goods_num = $event),
  240. h: common_vendor.p({
  241. min: 1,
  242. max: state.selectedSku.stock,
  243. step: 1,
  244. modelValue: state.selectedSku.goods_num
  245. }),
  246. i: common_vendor.o(() => {
  247. }),
  248. j: common_vendor.o(onAddCart),
  249. k: common_vendor.o(onBuy),
  250. l: common_vendor.o(($event) => emits("close")),
  251. m: common_vendor.p({
  252. show: __props.show,
  253. round: "10"
  254. })
  255. };
  256. };
  257. }
  258. };
  259. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-a43d1e2a"], ["__file", "/Users/RuHu.Xu/Desktop/mall-newfeifan-zx-app/sheep/components/s-select-sku/s-select-sku.vue"]]);
  260. wx.createComponent(Component);