s-select-sku.js 14 KB

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