skuSelect.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <template>
  2. <uv-popup ref="popupRef" mode="bottom" round="18rpx" z-index="9999">
  3. <view class="popupBox">
  4. <view class="popupBox_t">
  5. <view class="popupBox_t_l">
  6. <image
  7. class=""
  8. :src="skuValue.cover || details.coverImage"
  9. mode="aspectFill"
  10. ></image>
  11. <view class="popupBox_t_l_info">
  12. <view class="popupBox_t_l_info_price">
  13. <view
  14. class="popupBox_t_l_info_price_l"
  15. v-if="details.productPaymentMode == 1"
  16. >
  17. <text class="iconfont">&#xe669;</text>
  18. <text>{{ skuValue.exchangePoint }}</text>
  19. </view>
  20. <view class="popupBox_t_l_info_price_l" v-else>
  21. <rich-text :nodes="$mUtil.priceBigSmall(skuValue.salePrice)">
  22. </rich-text>
  23. </view>
  24. <!-- <text class="popupBox_t_l_info_price_r">¥80 </text> -->
  25. <!-- <text class="u-ml10">7.5折</text> -->
  26. </view>
  27. <view class="popupBox_t_l_info_stock"
  28. >库存 {{ skuValue.stock }} 件</view
  29. >
  30. <view class="popupBox_t_l_info_sku">{{ skuValue.skuSetName }}</view>
  31. </view>
  32. </view>
  33. <!-- <view class="popupBox_t_r iconfont">&#xe612;</view> -->
  34. <view>
  35. <uv-icon
  36. name="close-circle"
  37. color="#999999"
  38. size="28"
  39. @click="closePopup"
  40. ></uv-icon>
  41. </view>
  42. </view>
  43. <scroll-view scroll-y class="popupBox_sku_box">
  44. <view
  45. class="popupBox_sku"
  46. v-for="(item, index) in skuTable"
  47. :key="index"
  48. >
  49. <view class="popupBox_sku_item">
  50. <view class="popupBox_sku_item_lab">{{ item.head.name }}</view>
  51. <view class="popupBox_sku_item_list">
  52. <view
  53. :class="selectedObj[index] == v.id ? 'active' : ''"
  54. v-for="(v, i) in item.values || []"
  55. :key="i"
  56. @click="chonseSku(index, v.id)"
  57. >{{ v.name }}</view
  58. >
  59. </view>
  60. </view>
  61. </view>
  62. </scroll-view>
  63. <view class="popupBox_num">
  64. <view class="popupBox_num_lab">购买数量</view>
  65. <view class="popupBox_num_inp">
  66. <!-- <view class="iconfont minus" @click="resNum">&#xe60b;</view>
  67. <view class="num">{{ add_num }}</view>
  68. <view class="iconfont plus-sign" @click="addNum">&#xe686;</view> -->
  69. <uv-number-box v-model="addNum"></uv-number-box>
  70. </view>
  71. </view>
  72. <view class="popupBox_btn">
  73. <button @click="addCart(1)">{{ btnName }}</button>
  74. </view>
  75. </view>
  76. </uv-popup>
  77. </template>
  78. <script setup>
  79. import { ref, watch, nextTick } from "vue";
  80. import { userShoppingCartAdd_Api } from "@/api/shop.js";
  81. const emit = defineEmits(["updateShoppingCart"]);
  82. const props = defineProps({
  83. skuTable: {
  84. type: Array,
  85. default: () => [],
  86. },
  87. details: {
  88. type: Object,
  89. default: () => ({}),
  90. },
  91. });
  92. const popupRef = ref(null);
  93. const addNum = ref("1"); // 购买数量
  94. const btnName = ref("加入购物车");
  95. const skuTable = ref(props.details.skuTable || []); // 商品sku属性
  96. const skuValue = ref({}); // 商品sku属性值
  97. const selectedObj = ref({}); // 选中的sku属性值
  98. const details = ref(props.details || {}); // 商品详情
  99. const productSkuSetList = ref([]); // sku属性列表
  100. watch(
  101. () => props.details,
  102. (newVal) => {
  103. details.value = newVal || {};
  104. if (newVal.productSkuSetList)
  105. productSkuSetList.value = newVal.productSkuSetList || [];
  106. skuTable.value = newVal.skuTable || [];
  107. }
  108. );
  109. watch(
  110. () => skuTable.value,
  111. (newVal) => {
  112. addNum.value = "1";
  113. let objs = [];
  114. newVal.forEach((v, i) => {
  115. objs.push(v.values[0].id || "");
  116. });
  117. selectedObj.value = [...objs];
  118. skuValueChange();
  119. }
  120. );
  121. // 商品规格选择
  122. const chonseSku = (index, id) => {
  123. selectedObj.value[index] = id;
  124. skuValueChange();
  125. };
  126. const skuValueChange = () => {
  127. // skuValue.value[index] = id;
  128. let val = [...selectedObj.value];
  129. val = val.sort((a, b) => a - b);
  130. let ids = "_" + val.join("_") + "_";
  131. const objs = productSkuSetList.value.find((v) => v.skuHashCode == ids) || {};
  132. nextTick(() => {
  133. skuValue.value = objs || {};
  134. });
  135. };
  136. // 加入购物车/立即购买
  137. const addCart = (type = null) => {
  138. // console.log("加入购物车");
  139. if (btnName.value == "加入购物车") {
  140. let obj = {
  141. productId: details.value.productId,
  142. skuHashCode: skuValue.value.skuHashCode,
  143. skuSetName: skuValue.value.skuSetName,
  144. num: addNum.value || 1,
  145. effectiveStatus: 1,
  146. businessId: skuValue.value.businessId,
  147. };
  148. uni.showLoading({
  149. title: "加入购物车中...",
  150. mask: true,
  151. });
  152. userShoppingCartAdd_Api(obj)
  153. .then((res) => {
  154. uni.hideLoading();
  155. if (res.code == 200) {
  156. uni.showToast({
  157. title: "加入成功!",
  158. duration: 1500,
  159. });
  160. if (type == 1) {
  161. closePopup();
  162. }
  163. emit("updateShoppingCart");
  164. } else {
  165. uni.showToast({
  166. title: "加入失败!",
  167. duration: 1500,
  168. });
  169. }
  170. })
  171. .catch((err) => {
  172. // uni.hideLoading();
  173. });
  174. } else {
  175. // 立即购买
  176. // console.log("立即购买", skuValue.value);
  177. let dataJson = {
  178. shippingMethod: 0, //0物流,10自提
  179. createOrderDetailBos: [
  180. {
  181. skuHashCode: skuValue.value.skuHashCode,
  182. productNum: addNum.value || 1,
  183. productId: skuValue.value.productId,
  184. },
  185. ],
  186. marketingType: 0, //0无活动,1秒杀,2拼团
  187. orderType: 0,
  188. channelType: 5,
  189. exchange: details.value.productPaymentMode ? true : false,
  190. userUsePoint: true,
  191. businessId: skuValue.value.businessId,
  192. };
  193. if (type == 1) {
  194. closePopup();
  195. }
  196. uni.setStorageSync("dataJson", dataJson);
  197. uni.navigateTo({
  198. url: "/pages/surePay/surePay",
  199. });
  200. }
  201. };
  202. const closePopup = () => {
  203. addNum.value = "1";
  204. popupRef.value.close();
  205. };
  206. const open = (name, info = null) => {
  207. btnName.value = name;
  208. let detailsInfo = info || details.value || {};
  209. nextTick(() => {
  210. if (detailsInfo.singleSku) {
  211. skuValue.value = detailsInfo.productSkuSetList[0] || {};
  212. }
  213. });
  214. popupRef.value.open();
  215. };
  216. defineExpose({
  217. open,
  218. });
  219. </script>
  220. <style lang="scss" scoped>
  221. .popupBox {
  222. border-radius: 18rpx 18rpx 0rpx 0rpx;
  223. padding-top: 30rpx;
  224. overflow-y: auto;
  225. padding-bottom: 30rpx;
  226. .popupBox_t {
  227. padding: 0 30rpx 30rpx;
  228. display: flex;
  229. justify-content: space-between;
  230. .popupBox_t_l {
  231. display: flex;
  232. align-items: center;
  233. image {
  234. width: 200rpx;
  235. height: 200rpx;
  236. border-radius: 8rpx;
  237. overflow: hidden;
  238. margin-right: 20rpx;
  239. flex-shrink: 0;
  240. }
  241. .popupBox_t_l_info {
  242. .popupBox_t_l_info_price {
  243. display: flex;
  244. color: #999;
  245. font-size: 24rpx;
  246. align-items: baseline;
  247. .popupBox_t_l_info_price_l {
  248. font-size: 36rpx;
  249. color: #da4f4f;
  250. }
  251. .popupBox_t_l_info_price_r {
  252. text-decoration: line-through;
  253. margin-left: 15px;
  254. }
  255. }
  256. .popupBox_t_l_info_stock {
  257. color: #999;
  258. font-size: 24rpx;
  259. margin-top: 5rpx;
  260. }
  261. .popupBox_t_l_info_sku {
  262. font-size: 28rpx;
  263. color: #1a1a1a;
  264. margin-top: 25rpx;
  265. }
  266. }
  267. }
  268. .popupBox_t_r {
  269. color: #999;
  270. font-size: 50rpx;
  271. }
  272. }
  273. .popupBox_sku_box {
  274. max-height: 35vh;
  275. }
  276. .popupBox_sku {
  277. margin-top: 10rpx;
  278. .popupBox_sku_item {
  279. .popupBox_sku_item_lab {
  280. padding: 0 30rpx;
  281. font-size: 28rpx;
  282. }
  283. .popupBox_sku_item_list {
  284. padding: 0 30rpx;
  285. display: flex;
  286. flex-direction: row;
  287. align-items: center;
  288. margin-top: 20rpx;
  289. flex-wrap: wrap;
  290. > view {
  291. background-color: #f6f6f6;
  292. border: 1px solid #f6f6f6;
  293. padding: 12rpx 30rpx;
  294. margin-bottom: 24rpx;
  295. font-size: 24rpx;
  296. color: #1a1a1a;
  297. border-radius: 4px;
  298. margin-right: 10rpx;
  299. }
  300. > .active {
  301. background-color: #f2f5f4;
  302. border: 1px solid #eb5153;
  303. color: #eb5153;
  304. border-radius: 4px;
  305. }
  306. }
  307. }
  308. }
  309. .popupBox_num {
  310. display: flex;
  311. justify-content: space-between;
  312. padding: 0 30rpx;
  313. margin-top: 30rpx;
  314. .popupBox_num_lab {
  315. font-size: 28rpx;
  316. }
  317. }
  318. .popupBox_btn {
  319. padding: 0 30rpx;
  320. margin-top: 68rpx;
  321. button {
  322. width: 100%;
  323. height: 84rpx;
  324. background: #eb5153;
  325. border-radius: 43rpx;
  326. font-size: 30rpx;
  327. color: #ffffff;
  328. text-align: center;
  329. line-height: 84rpx;
  330. }
  331. }
  332. }
  333. </style>