skuSelect.vue 8.8 KB

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