s-select-sku.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. "use strict";
  2. const common_vendor = require("../../../common/vendor.js");
  3. const common_assets = require("../../../common/assets.js");
  4. const sheep_index = require("../../index.js");
  5. const sheep_hooks_useGoods = require("../../hooks/useGoods.js");
  6. const sheep_api_pay_wallet = require("../../api/pay/wallet.js");
  7. require("../../api/index.js");
  8. require("../../api/distri/score.js");
  9. require("../../request/index.js");
  10. require("../../config/index.js");
  11. require("../../store/index.js");
  12. require("../../store/app.js");
  13. require("../../api/promotion/diy.js");
  14. require("../../platform/index.js");
  15. require("../../platform/provider/wechat/index.js");
  16. require("../../platform/provider/wechat/miniProgram.js");
  17. require("../../api/member/auth.js");
  18. require("../../api/member/social.js");
  19. require("../../api/member/user.js");
  20. require("../../platform/provider/apple/index.js");
  21. require("../../platform/share.js");
  22. require("../../router/index.js");
  23. require("../../hooks/useModal.js");
  24. require("../../helper/index.js");
  25. require("../../helper/test.js");
  26. require("../../helper/digit.js");
  27. require("../../api/member/signin.js");
  28. require("../../helper/throttle.js");
  29. require("../../url/index.js");
  30. require("../../platform/pay.js");
  31. require("../../api/pay/order.js");
  32. require("../../store/user.js");
  33. require("../../store/cart.js");
  34. require("../../api/trade/cart.js");
  35. require("../../api/trade/order.js");
  36. require("../../api/promotion/coupon.js");
  37. require("../../store/sys.js");
  38. require("../../store/modal.js");
  39. require("../../api/distri/share.js");
  40. require("../../api/distri/team.js");
  41. require("../../api/infra/file.js");
  42. require("../../api/member/address.js");
  43. require("../../api/member/point.js");
  44. require("../../api/migration/app.js");
  45. require("../../api/migration/chat.js");
  46. require("../../api/migration/index.js");
  47. require("../../api/migration/third.js");
  48. require("../../api/pay/channel.js");
  49. require("../../api/product/category.js");
  50. require("../../api/product/comment.js");
  51. require("../../api/product/favorite.js");
  52. require("../../api/product/history.js");
  53. require("../../api/product/spu.js");
  54. require("../../api/promotion/activity.js");
  55. require("../../api/promotion/article.js");
  56. require("../../api/promotion/bargain.js");
  57. require("../../api/promotion/combination.js");
  58. require("../../api/promotion/rewardActivity.js");
  59. require("../../api/promotion/seckill.js");
  60. require("../../api/system/area.js");
  61. require("../../api/system/voice.js");
  62. require("../../api/trade/afterSale.js");
  63. require("../../api/trade/brokerage.js");
  64. require("../../api/trade/config.js");
  65. require("../../api/trade/delivery.js");
  66. require("../../config/zIndex.js");
  67. require("../../util/index.js");
  68. if (!Array) {
  69. const _easycom_su_number_box2 = common_vendor.resolveComponent("su-number-box");
  70. const _easycom_su_popup2 = common_vendor.resolveComponent("su-popup");
  71. (_easycom_su_number_box2 + _easycom_su_popup2)();
  72. }
  73. const _easycom_su_number_box = () => "../../ui/su-number-box/su-number-box.js";
  74. const _easycom_su_popup = () => "../../ui/su-popup/su-popup.js";
  75. if (!Math) {
  76. (_easycom_su_number_box + _easycom_su_popup)();
  77. }
  78. const _sfc_main = {
  79. __name: "s-select-sku",
  80. props: {
  81. goodsInfo: {
  82. type: Object,
  83. default() {
  84. }
  85. },
  86. show: {
  87. type: Boolean,
  88. default: false
  89. }
  90. },
  91. emits: ["change", "addCart", "buy", "close"],
  92. setup(__props, { emit: __emit }) {
  93. const emits = __emit;
  94. const props = __props;
  95. const userWallet = common_vendor.computed(() => sheep_index.sheep.$store("user").userWallet);
  96. const state = common_vendor.reactive({
  97. selectedSku: {},
  98. // 选中的 SKU
  99. currentPropertyArray: []
  100. // 当前选中的属性,实际是个 Map。key 是 property 编号,value 是 value 编号
  101. });
  102. const propertyList = sheep_hooks_useGoods.convertProductPropertyList(props.goodsInfo.skus);
  103. const skuList = common_vendor.computed(() => {
  104. let skuPrices = props.goodsInfo.skus;
  105. for (let price of skuPrices) {
  106. price.value_id_array = price.properties.map((item) => item.valueId);
  107. }
  108. return skuPrices;
  109. });
  110. function calcNums() {
  111. if (props.goodsInfo.highPrecision) {
  112. state.selectedSku.goods_num = parseInt(state.selectedSku.use_points / (state.selectedSku.highPrecisionPrice / 100));
  113. } else {
  114. state.selectedSku.goods_num = parseInt(state.selectedSku.use_points / sheep_hooks_useGoods.fen2yuan(state.selectedSku.price));
  115. }
  116. }
  117. async function useAllPonints() {
  118. const { code, data } = await sheep_api_pay_wallet.PayWalletApi.getDuserInfo();
  119. const userCanUsePoints = parseFloat(sheep_hooks_useGoods.points2point(data.integralDO.currentQuota));
  120. console.log(userCanUsePoints);
  121. state.selectedSku.use_points = parseInt(userCanUsePoints);
  122. calcNums();
  123. }
  124. function inputPoints(e) {
  125. const points = e.detail.value;
  126. console.log(typeof points);
  127. if (points == "") {
  128. return false;
  129. }
  130. const userCanUsePoints = parseFloat(sheep_hooks_useGoods.points2point(userWallet.value.integralDO.currentQuota));
  131. calcNums();
  132. common_vendor.nextTick$1(() => {
  133. state.selectedSku.use_points = parseInt(points);
  134. calcNums();
  135. });
  136. if (points <= 0) {
  137. sheep_index.sheep.$helper.toast("最低使用1积分");
  138. if (userCanUsePoints >= 1) {
  139. common_vendor.nextTick$1(() => {
  140. state.selectedSku.use_points = 1;
  141. calcNums();
  142. });
  143. } else {
  144. common_vendor.nextTick$1(() => {
  145. state.selectedSku.use_points = 0;
  146. calcNums();
  147. });
  148. }
  149. }
  150. if (points > userCanUsePoints) {
  151. sheep_index.sheep.$helper.toast("可用积分不足");
  152. common_vendor.nextTick$1(() => {
  153. state.selectedSku.use_points = parseInt(userCanUsePoints);
  154. calcNums();
  155. });
  156. }
  157. }
  158. common_vendor.watch(
  159. () => state.selectedSku,
  160. (newVal) => {
  161. emits("change", newVal);
  162. },
  163. {
  164. immediate: true,
  165. // 立即执行
  166. deep: true
  167. // 深度监听
  168. }
  169. );
  170. function onNumberChange(e) {
  171. if (e === 0)
  172. return;
  173. if (state.selectedSku.goods_num === e)
  174. return;
  175. state.selectedSku.goods_num = e;
  176. }
  177. function onAddCart() {
  178. if (state.selectedSku.id <= 0) {
  179. sheep_index.sheep.$helper.toast("请选择规格");
  180. return;
  181. }
  182. if (state.selectedSku.stock <= 0) {
  183. sheep_index.sheep.$helper.toast("库存不足");
  184. return;
  185. }
  186. emits("addCart", state.selectedSku);
  187. }
  188. async function onBuy() {
  189. if (props.goodsInfo.spuPayType == 2) {
  190. const { code, data } = await sheep_api_pay_wallet.PayWalletApi.getDuserInfo();
  191. const userCanUsePoints = parseFloat(sheep_hooks_useGoods.points2point(data.integralDO.currentQuota));
  192. if (userCanUsePoints < state.selectedSku.use_points) {
  193. sheep_index.sheep.$helper.toast("可用积分不足");
  194. return false;
  195. }
  196. if (state.selectedSku.goods_num < 1 || !state.selectedSku.use_points) {
  197. sheep_index.sheep.$helper.toast("输入金额少于可购买数量");
  198. return;
  199. }
  200. if (state.selectedSku.goods_num > state.selectedSku.stock) {
  201. state.selectedSku.use_points = (state.selectedSku.stock * sheep_hooks_useGoods.fen2yuan(state.selectedSku.price)).toFixed(2);
  202. state.selectedSku.goods_num = state.selectedSku.stock;
  203. }
  204. }
  205. if (state.selectedSku.id <= 0) {
  206. sheep_index.sheep.$helper.toast("请选择规格");
  207. return;
  208. }
  209. if (state.selectedSku.stock <= 0) {
  210. sheep_index.sheep.$helper.toast("库存不足");
  211. return;
  212. }
  213. emits("buy", state.selectedSku);
  214. state.selectedSku.use_points = 0;
  215. }
  216. function changeDisabled(isChecked = false, propertyId = 0, valueId = 0) {
  217. let newSkus = [];
  218. if (isChecked) {
  219. for (let price of skuList.value) {
  220. if (price.stock <= 0) {
  221. continue;
  222. }
  223. if (price.value_id_array.indexOf(valueId) >= 0) {
  224. newSkus.push(price);
  225. }
  226. }
  227. } else {
  228. newSkus = getCanUseSkuList();
  229. }
  230. let noChooseValueIds = [];
  231. for (let price of newSkus) {
  232. noChooseValueIds = noChooseValueIds.concat(price.value_id_array);
  233. }
  234. noChooseValueIds = Array.from(new Set(noChooseValueIds));
  235. if (isChecked) {
  236. let index = noChooseValueIds.indexOf(valueId);
  237. noChooseValueIds.splice(index, 1);
  238. } else {
  239. state.currentPropertyArray.forEach((currentPropertyId) => {
  240. if (currentPropertyId.toString() !== "") {
  241. return;
  242. }
  243. let index = noChooseValueIds.indexOf(currentPropertyId);
  244. if (index >= 0) {
  245. noChooseValueIds.splice(index, 1);
  246. }
  247. });
  248. }
  249. let choosePropertyIds = [];
  250. if (!isChecked) {
  251. state.currentPropertyArray.forEach((currentPropertyId, currentValueId) => {
  252. if (currentPropertyId !== "") {
  253. choosePropertyIds.push(currentValueId);
  254. }
  255. });
  256. } else {
  257. choosePropertyIds = [propertyId];
  258. }
  259. for (let propertyIndex in propertyList) {
  260. if (choosePropertyIds.indexOf(propertyList[propertyIndex]["id"]) >= 0) {
  261. continue;
  262. }
  263. for (let valueIndex in propertyList[propertyIndex]["values"]) {
  264. propertyList[propertyIndex]["values"][valueIndex]["disabled"] = noChooseValueIds.indexOf(
  265. propertyList[propertyIndex]["values"][valueIndex]["id"]
  266. ) < 0;
  267. }
  268. }
  269. }
  270. function getCanUseSkuList() {
  271. let newSkus = [];
  272. for (let sku of skuList.value) {
  273. if (sku.stock <= 0) {
  274. continue;
  275. }
  276. let isOk = true;
  277. state.currentPropertyArray.forEach((propertyId) => {
  278. if (propertyId.toString() !== "" && sku.value_id_array.indexOf(propertyId) < 0) {
  279. isOk = false;
  280. }
  281. });
  282. if (isOk) {
  283. newSkus.push(sku);
  284. }
  285. }
  286. return newSkus;
  287. }
  288. function onSelectSku(propertyId, valueId) {
  289. state.selectedSku.use_points = 0;
  290. let isChecked = true;
  291. if (state.currentPropertyArray[propertyId] !== void 0 && state.currentPropertyArray[propertyId] === valueId) {
  292. isChecked = false;
  293. state.currentPropertyArray.splice(propertyId, 1, "");
  294. } else {
  295. state.currentPropertyArray[propertyId] = valueId;
  296. }
  297. let choosePropertyId = [];
  298. state.currentPropertyArray.forEach((currentPropertyId) => {
  299. if (currentPropertyId !== "") {
  300. choosePropertyId.push(currentPropertyId);
  301. }
  302. });
  303. let newSkuList = getCanUseSkuList();
  304. if (choosePropertyId.length === propertyList.length && newSkuList.length) {
  305. newSkuList[0].goods_num = state.selectedSku.goods_num || 1;
  306. state.selectedSku = newSkuList[0];
  307. } else {
  308. state.selectedSku = {};
  309. }
  310. changeDisabled(isChecked, propertyId, valueId);
  311. }
  312. changeDisabled(false);
  313. common_vendor.onMounted(() => {
  314. if (propertyList.length == 1 && propertyList[0].values.length == 1) {
  315. onSelectSku(0, 0);
  316. }
  317. });
  318. return (_ctx, _cache) => {
  319. return common_vendor.e({
  320. a: state.selectedSku.picUrl || __props.goodsInfo.picUrl,
  321. b: common_vendor.t(__props.goodsInfo.name),
  322. c: __props.goodsInfo.spuPayType == 2
  323. }, __props.goodsInfo.spuPayType == 2 ? {
  324. d: common_assets._imports_0
  325. } : {}, {
  326. e: __props.goodsInfo.highPrecision
  327. }, __props.goodsInfo.highPrecision ? {
  328. f: common_vendor.t(common_vendor.unref(sheep_hooks_useGoods.fen2yuan6)(state.selectedSku.highPrecisionPrice || __props.goodsInfo.highPrecisionPrice))
  329. } : {
  330. g: common_vendor.t(common_vendor.unref(sheep_hooks_useGoods.fen2yuan)(state.selectedSku.price || __props.goodsInfo.price))
  331. }, {
  332. h: state.selectedSku.promotionFee >= 0 || __props.goodsInfo.promotionFee >= 0
  333. }, state.selectedSku.promotionFee >= 0 || __props.goodsInfo.promotionFee >= 0 ? {
  334. i: common_vendor.t(common_vendor.unref(sheep_hooks_useGoods.fen2yuan)(state.selectedSku.promotionFee || __props.goodsInfo.promotionFee))
  335. } : {}, {
  336. j: common_vendor.t(common_vendor.unref(sheep_hooks_useGoods.formatStock)("exact", state.selectedSku.stock || __props.goodsInfo.stock)),
  337. k: common_vendor.f(common_vendor.unref(propertyList), (property, k0, i0) => {
  338. return {
  339. a: common_vendor.t(property.name),
  340. b: common_vendor.f(property.values, (value, k1, i1) => {
  341. return {
  342. a: common_vendor.t(value.name),
  343. b: common_vendor.n({
  344. "ui-BG-Main-Gradient": state.currentPropertyArray[property.id] === value.id
  345. }),
  346. c: common_vendor.n({
  347. "disabled-btn": value.disabled === true
  348. }),
  349. d: value.id,
  350. e: value.disabled === true,
  351. f: common_vendor.o(($event) => onSelectSku(property.id, value.id), value.id)
  352. };
  353. }),
  354. c: property.id
  355. };
  356. }),
  357. l: __props.goodsInfo.spuPayType == 2
  358. }, __props.goodsInfo.spuPayType == 2 ? common_vendor.e({
  359. m: common_vendor.t(common_vendor.unref(sheep_hooks_useGoods.points2point)(userWallet.value.integralDO.currentQuota)),
  360. n: common_vendor.o(useAllPonints),
  361. o: __props.goodsInfo.spuPayType == 2
  362. }, __props.goodsInfo.spuPayType == 2 ? {
  363. p: common_assets._imports_0
  364. } : {}, {
  365. q: common_vendor.o([($event) => state.selectedSku.use_points = $event.detail.value, inputPoints]),
  366. r: !state.selectedSku.id,
  367. s: state.selectedSku.use_points
  368. }) : {
  369. t: common_vendor.o(($event) => onNumberChange($event)),
  370. v: common_vendor.o(($event) => state.selectedSku.goods_num = $event),
  371. w: common_vendor.p({
  372. min: 1,
  373. max: state.selectedSku.stock,
  374. step: 1,
  375. modelValue: state.selectedSku.goods_num
  376. })
  377. }, {
  378. x: common_vendor.o(() => {
  379. }),
  380. y: __props.goodsInfo.spuType && __props.goodsInfo.spuPayType == 1
  381. }, __props.goodsInfo.spuType && __props.goodsInfo.spuPayType == 1 ? {
  382. z: common_vendor.o(onAddCart),
  383. A: common_vendor.o(onBuy)
  384. } : common_vendor.e({
  385. B: __props.goodsInfo.spuPayType != 1 && !__props.goodsInfo.spuType
  386. }, __props.goodsInfo.spuPayType != 1 && !__props.goodsInfo.spuType ? {} : !__props.goodsInfo.spuType ? {} : {}, {
  387. C: !__props.goodsInfo.spuType,
  388. D: common_vendor.o(onBuy)
  389. }), {
  390. E: common_vendor.o(($event) => emits("close")),
  391. F: common_vendor.p({
  392. show: __props.show,
  393. round: "10"
  394. })
  395. });
  396. };
  397. }
  398. };
  399. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-a43d1e2a"], ["__file", "D:/zx/mall-front-app/sheep/components/s-select-sku/s-select-sku.vue"]]);
  400. wx.createComponent(Component);