s-select-groupon-sku.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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-groupon-sku",
  49. props: {
  50. show: {
  51. type: Boolean,
  52. default: false
  53. },
  54. goodsInfo: {
  55. type: Object,
  56. default() {
  57. }
  58. },
  59. grouponAction: {
  60. type: String,
  61. default: "create"
  62. },
  63. grouponNum: {
  64. type: [Number, String],
  65. default: 0
  66. }
  67. },
  68. emits: ["change", "addCart", "buy", "close", "ladder"],
  69. setup(__props, { emit: emits }) {
  70. const props = __props;
  71. common_vendor.useCssVars((_ctx) => ({
  72. "478364b0": common_vendor.unref(headerBg)
  73. }));
  74. const headerBg = sheep_index.sheep.$url.css("/static/img/shop/goods/groupon-btn-long.png");
  75. const state = common_vendor.reactive({
  76. selectedSku: {},
  77. // 选中的 SKU
  78. currentPropertyArray: [],
  79. // 当前选中的属性,实际是个 Map。key 是 property 编号,value 是 value 编号
  80. grouponNum: props.grouponNum
  81. });
  82. const propertyList = sheep_hooks_useGoods.convertProductPropertyList(props.goodsInfo.skus);
  83. const skuList = common_vendor.computed(() => {
  84. let skuPrices = props.goodsInfo.skus;
  85. for (let price of skuPrices) {
  86. price.value_id_array = price.properties.map((item) => item.valueId);
  87. }
  88. return skuPrices;
  89. });
  90. common_vendor.watch(
  91. () => state.selectedSku,
  92. (newVal) => {
  93. emits("change", newVal);
  94. },
  95. {
  96. immediate: true,
  97. // 立即执行
  98. deep: true
  99. // 深度监听
  100. }
  101. );
  102. function onNumberChange(e) {
  103. if (e === 0)
  104. return;
  105. if (state.selectedSku.count === e)
  106. return;
  107. state.selectedSku.count = e;
  108. }
  109. function onBuy() {
  110. if (!state.selectedSku.id || state.selectedSku.id <= 0) {
  111. sheep_index.sheep.$helper.toast("请选择规格");
  112. return;
  113. }
  114. if (state.selectedSku.stock <= 0) {
  115. sheep_index.sheep.$helper.toast("库存不足");
  116. return;
  117. }
  118. emits("buy", state.selectedSku);
  119. }
  120. function changeDisabled(isChecked = false, propertyId = 0, valueId = 0) {
  121. let newSkus = [];
  122. if (isChecked) {
  123. for (let price of skuList.value) {
  124. if (price.stock <= 0) {
  125. continue;
  126. }
  127. if (price.value_id_array.indexOf(valueId) >= 0) {
  128. newSkus.push(price);
  129. }
  130. }
  131. } else {
  132. newSkus = getCanUseSkuList();
  133. }
  134. let noChooseValueIds = [];
  135. for (let price of newSkus) {
  136. noChooseValueIds = noChooseValueIds.concat(price.value_id_array);
  137. }
  138. noChooseValueIds = Array.from(new Set(noChooseValueIds));
  139. if (isChecked) {
  140. let index = noChooseValueIds.indexOf(valueId);
  141. noChooseValueIds.splice(index, 1);
  142. } else {
  143. state.currentPropertyArray.forEach((currentPropertyId) => {
  144. if (currentPropertyId.toString() !== "") {
  145. return;
  146. }
  147. let index = noChooseValueIds.indexOf(currentPropertyId);
  148. if (index >= 0) {
  149. noChooseValueIds.splice(index, 1);
  150. }
  151. });
  152. }
  153. let choosePropertyIds = [];
  154. if (!isChecked) {
  155. state.currentPropertyArray.forEach((currentPropertyId, currentValueId) => {
  156. if (currentPropertyId !== "") {
  157. choosePropertyIds.push(currentValueId);
  158. }
  159. });
  160. } else {
  161. choosePropertyIds = [propertyId];
  162. }
  163. for (let propertyIndex in propertyList) {
  164. if (choosePropertyIds.indexOf(propertyList[propertyIndex]["id"]) >= 0) {
  165. continue;
  166. }
  167. for (let valueIndex in propertyList[propertyIndex]["values"]) {
  168. propertyList[propertyIndex]["values"][valueIndex]["disabled"] = noChooseValueIds.indexOf(propertyList[propertyIndex]["values"][valueIndex]["id"]) < 0;
  169. }
  170. }
  171. }
  172. function getCanUseSkuList() {
  173. let newSkus = [];
  174. for (let sku of skuList.value) {
  175. if (sku.stock <= 0) {
  176. continue;
  177. }
  178. let isOk = true;
  179. state.currentPropertyArray.forEach((propertyId) => {
  180. if (propertyId.toString() !== "" && sku.value_id_array.indexOf(propertyId) < 0) {
  181. isOk = false;
  182. }
  183. });
  184. if (isOk) {
  185. newSkus.push(sku);
  186. }
  187. }
  188. return newSkus;
  189. }
  190. function onSelectSku(propertyId, valueId) {
  191. let isChecked = true;
  192. if (state.currentPropertyArray[propertyId] !== void 0 && state.currentPropertyArray[propertyId] === valueId) {
  193. isChecked = false;
  194. state.currentPropertyArray.splice(propertyId, 1, "");
  195. } else {
  196. state.currentPropertyArray[propertyId] = valueId;
  197. }
  198. let choosePropertyId = [];
  199. state.currentPropertyArray.forEach((currentPropertyId) => {
  200. if (currentPropertyId !== "") {
  201. choosePropertyId.push(currentPropertyId);
  202. }
  203. });
  204. let newSkuList = getCanUseSkuList();
  205. if (choosePropertyId.length === propertyList.length && newSkuList.length) {
  206. newSkuList[0].count = state.selectedSku.count || 1;
  207. state.selectedSku = newSkuList[0];
  208. } else {
  209. state.selectedSku = {};
  210. }
  211. changeDisabled(isChecked, propertyId, valueId);
  212. }
  213. changeDisabled(false);
  214. return (_ctx, _cache) => {
  215. return common_vendor.e({
  216. a: common_vendor.unref(sheep_index.sheep).$url.cdn(state.selectedSku.picUrl || __props.goodsInfo.picUrl),
  217. b: common_vendor.unref(sheep_index.sheep).$url.static("/static/img/shop/goods/groupon-tag-white.png"),
  218. c: common_vendor.t(__props.goodsInfo.name),
  219. d: common_vendor.t(common_vendor.unref(sheep_hooks_useGoods.fen2yuan)(__props.goodsInfo.price)),
  220. e: common_vendor.t(state.selectedSku.stock || __props.goodsInfo.stock),
  221. f: common_vendor.f(common_vendor.unref(propertyList), (property, k0, i0) => {
  222. return {
  223. a: common_vendor.t(property.name),
  224. b: common_vendor.f(property.values, (value, k1, i1) => {
  225. return {
  226. a: common_vendor.t(value.name),
  227. b: common_vendor.n({
  228. "checked-btn": state.currentPropertyArray[property.id] === value.id
  229. }),
  230. c: common_vendor.n({
  231. "disabled-btn": value.disabled === true
  232. }),
  233. d: value.id,
  234. e: value.disabled === true,
  235. f: common_vendor.o(($event) => onSelectSku(property.id, value.id), value.id)
  236. };
  237. }),
  238. c: property.id
  239. };
  240. }),
  241. g: common_vendor.o(($event) => onNumberChange($event)),
  242. h: common_vendor.o(($event) => state.selectedSku.count = $event),
  243. i: common_vendor.p({
  244. min: 1,
  245. max: state.selectedSku.stock,
  246. step: 1,
  247. activity: "groupon",
  248. modelValue: state.selectedSku.count
  249. }),
  250. j: common_vendor.t(__props.grouponNum + "人团"),
  251. k: common_vendor.t(common_vendor.unref(sheep_hooks_useGoods.fen2yuan)(__props.goodsInfo.price)),
  252. l: __props.grouponAction === "create"
  253. }, __props.grouponAction === "create" ? {} : __props.grouponAction === "join" ? {} : {}, {
  254. m: __props.grouponAction === "join",
  255. n: common_vendor.o(onBuy),
  256. o: common_vendor.o(($event) => emits("close")),
  257. p: common_vendor.s(_ctx.__cssVars()),
  258. q: common_vendor.p({
  259. show: __props.show,
  260. round: "10"
  261. })
  262. });
  263. };
  264. }
  265. };
  266. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-f77ace9d"], ["__file", "/Users/RuHu.Xu/Desktop/mall-newfeifan-zx-app/sheep/components/s-select-groupon-sku/s-select-groupon-sku.vue"]]);
  267. wx.createComponent(Component);