goods.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <template>
  2. <div class="wrapper">
  3. <u-popup class="popup" v-model="buyMask" :height="setup.height" closeable :mode="setup.mode" :mask-close-able="isClose" :mask="isMask" :border-radius="setup.radius" @close="closeMask()">
  4. <!-- 商品 -->
  5. <view class="goods-box bottom">
  6. <view class="goods-header">
  7. <view class="goods-img">
  8. <u-image width="200rpx" border-radius="20" class="uimage" height="200rpx" :src="selectedSpecImg ? selectedSpecImg : goodsDetail.thumbnail"></u-image>
  9. </view>
  10. <view class="goods-skus">
  11. <!-- 有活动商品价格 -->
  12. <view class="goods-price " v-if="goodsDetail.promotionPrice">
  13. <span>
  14. <span class="goods-price-promotionShow goods-price-bigshow" v-if="goodsDetail.promotionPrice">{{ formatPrice(goodsDetail.promotionPrice)[0] }}</span>
  15. .{{ formatPrice(goodsDetail.promotionPrice)[1] }}
  16. <span></span>
  17. </span>
  18. <div class="promotion-box">
  19. <span class="goods-price-bigshow">{{
  20. formatPrice(goodsDetail.price)[0]
  21. }}</span>
  22. .{{ formatPrice(goodsDetail.price)[1] }}
  23. <span></span>
  24. </div>
  25. </view>
  26. <!-- 正常商品的价格 -->
  27. <view class="goods-price" v-else>
  28. <span class="goods-price-bigshow">{{
  29. formatPrice(goodsDetail.price)[0]
  30. }}</span>
  31. .{{ formatPrice(goodsDetail.price)[1] }}
  32. <span></span>
  33. </view>
  34. <view class="goods-check-skus">
  35. 已选
  36. <span class="goods-check-skus-name">
  37. {{ selectName }}
  38. <span>,{{ num }}个</span>
  39. </span>
  40. </view>
  41. </view>
  42. </view>
  43. <!-- 商品信息 -->
  44. <view class="goods-skus-box">
  45. <!-- 规格 -->
  46. <view class="goods-skus-view" :key="specIndex" v-for="(spec, specIndex) in formatList">
  47. <view class="skus-view-list">
  48. <view class="view-class-title">{{ spec.name }}</view>
  49. <view :class="{ active: spec_val.id == currentSelceted[specIndex] }" class="skus-view-item" v-for="(spec_val, spec_index) in spec.values" :key="spec_index"
  50. @click="handleClickSpec(spec, specIndex, spec_val)">{{ spec_val.value }}</view>
  51. </view>
  52. </view>
  53. <!-- 数量 -->
  54. <view class="goods-skus-number">
  55. <view class="view-class-title">数量</view>
  56. <u-number-box :bg-color="numberBox.bgColor" :color="numberBox.color" :input-width="numberBox.width" :input-height="numberBox.height" :size="numberBox.size" :min="1" v-model="num">
  57. </u-number-box>
  58. </view>
  59. </view>
  60. <!-- 按钮 -->
  61. <view class="btns">
  62. <view class="box-btn card" v-if="buyType !='PINTUAN'" @click="addToCartOrBuy('cart')">加入购物车</view>
  63. <view class="box-btn buy" @click="addToCartOrBuy('buy')">立即购买</view>
  64. </view>
  65. </view>
  66. </u-popup>
  67. </div>
  68. </template>
  69. <script>
  70. import * as API_trade from "@/api/trade.js";
  71. import setup from "./popup";
  72. export default {
  73. data() {
  74. return {
  75. setup,
  76. num: 1,
  77. // 步进器的大小尺寸单位是 rpx
  78. numberBox: {
  79. width: "50",
  80. height: "50",
  81. size: "22",
  82. color: "#333",
  83. bgColor: "#fff",
  84. },
  85. selectName: "", //选中商品的昵称
  86. selectSkuList: "", //选中商铺sku,
  87. selectedSpecImg: "", //选中的图片路径
  88. buyType: "", //用于存储促销,拼团等活动类型
  89. parentOrder: "", //父级拼团活动的数据 - 如果是团员则有数据
  90. formatList: [],
  91. currentSelceted: [],
  92. skuList: "",
  93. isMask:false, //是否显示遮罩层
  94. isClose:false, //是否可以点击遮罩关闭
  95. };
  96. },
  97. props: [
  98. "goodsDetail",
  99. "buyMask",
  100. "selectedSku",
  101. "goodsSpec",
  102. "addr",
  103. ],
  104. watch: {
  105. buyType: {
  106. handler(val) {
  107. this.buyType = val;
  108. },
  109. immediate: true,
  110. },
  111. selectSkuList: {
  112. handler(val, oldval) {
  113. this.$emit("changed", val);
  114. },
  115. deep: true,
  116. },
  117. },
  118. methods: {
  119. // 格式化金钱 1999 --> [1999,00]
  120. formatPrice(val) {
  121. if (typeof val == "undefined") {
  122. return val;
  123. }
  124. return val.toFixed(2).split(".");
  125. },
  126. closeMask() {
  127. this.$emit("closeBuy", false);
  128. },
  129. /**点击规格 */
  130. handleClickSpec(val, index, specValue) {
  131. this.$set(this.currentSelceted, index, specValue.id);
  132. let selectedSkuId = this.goodsSpec.find((i) => {
  133. let matched = true;
  134. let specValues = i.specValues.filter((j) => j.specName !== "images");
  135. for (let n = 0; n < specValues.length; n++) {
  136. if (specValues[n].specValueId !== this.currentSelceted[n]) {
  137. matched = false;
  138. return;
  139. }
  140. }
  141. if (matched) {
  142. return i;
  143. }
  144. });
  145. this.selectSkuList = {
  146. spec: {
  147. specName: val.name,
  148. specValue: specValue.value,
  149. },
  150. data: this.goodsDetail,
  151. };
  152. this.selectName = specValue.value;
  153. this.$emit("handleClickSku", selectedSkuId.skuId,this.goodsDetail.id);
  154. },
  155. /**
  156. * 添加到购物车或购买
  157. */
  158. addToCartOrBuy(val) {
  159. if (!this.selectSkuList) {
  160. uni.showToast({
  161. title: "请选择规格商品",
  162. icon: "none",
  163. });
  164. return;
  165. }
  166. let data = {
  167. skuId: this.goodsDetail.id,
  168. num: this.num,
  169. };
  170. if (val == "cart") {
  171. API_trade.addToCart(data).then((res) => {
  172. if (res.data.code == 200) {
  173. uni.showToast({
  174. title: "商品已添加到购物车",
  175. icon: "none",
  176. });
  177. this.$emit("queryCart");
  178. this.closeMask();
  179. } else {
  180. uni.showToast({
  181. title: res.data.message,
  182. duration: 2000,
  183. icon: "none",
  184. });
  185. return false;
  186. }
  187. });
  188. } else {
  189. // 判断是否拼团商品
  190. if (this.buyType) {
  191. data.cartType = "PINTUAN";
  192. } else {
  193. data.cartType = "BUY_NOW";
  194. }
  195. API_trade.addToCart(data).then((res) => {
  196. if (res.data.code == 200) {
  197. uni.navigateTo({
  198. url: `/pages/order/fillorder?way=${data.cartType}&addr=${
  199. this.addr.id || ''
  200. }&parentOrder=${encodeURIComponent(
  201. JSON.stringify(this.parentOrder)
  202. )}`,
  203. });
  204. }
  205. });
  206. }
  207. },
  208. formatSku(list) {
  209. // 格式化数据
  210. let arr = [{}];
  211. list.forEach((item, index) => {
  212. item.specValues.forEach((spec, specIndex) => {
  213. let id = spec.specNameId;
  214. let name = spec.specName;
  215. let values = {
  216. id: spec.specValueId,
  217. value: spec.specValue,
  218. quantity: item.quantity,
  219. };
  220. if (name === "images") {
  221. return;
  222. }
  223. arr.forEach((arrItem, arrIndex) => {
  224. if (
  225. arrItem.name == name &&
  226. arrItem.values &&
  227. !arrItem.values.find((i) => i.id === values.id)
  228. ) {
  229. arrItem.values.push(values);
  230. }
  231. let keys = arr.map((key) => {
  232. return key.name;
  233. });
  234. if (!keys.includes(name)) {
  235. arr.push({
  236. id: id,
  237. name: name,
  238. values: [values],
  239. });
  240. }
  241. });
  242. });
  243. });
  244. arr.shift();
  245. this.formatList = arr;
  246. list.forEach((item) => {
  247. if (item.skuId === this.goodsDetail.id) {
  248. item.specValues
  249. .filter((i) => i.specName !== "images")
  250. .forEach((value, _index) => {
  251. this.currentSelceted[_index] = value.specValueId;
  252. this.selectName = value.specValue;
  253. this.selectSkuList = {
  254. spec: value,
  255. data: this.goodsDetail,
  256. };
  257. });
  258. }
  259. });
  260. this.skuList = list;
  261. },
  262. },
  263. mounted() {
  264. this.formatSku(this.goodsSpec);
  265. },
  266. };
  267. </script>
  268. <style lang="scss" scoped>
  269. @import "./popup.scss";
  270. .buy {
  271. background-image: linear-gradient(135deg, #ffba0d, #ffc30d 69%, #ffcf0d);
  272. box-shadow: 0 2px 6px 0 rgba(255, 65, 66, 0.2);
  273. }
  274. .card {
  275. background-image: linear-gradient(135deg, #f2140c, #f2270c 70%, #f24d0c);
  276. box-shadow: 0 2px 6px 0 rgba(255, 65, 66, 0.2);
  277. }
  278. /deep/.u-icon-plus,
  279. .u-icon-minus,
  280. .u-icon-disabled {
  281. height: 30rpx !important;
  282. background: #fff !important;
  283. }
  284. .goods-skus-number {
  285. justify-content: space-between;
  286. display: flex;
  287. }
  288. /deep/ .uni-scroll-view {
  289. overflow: hidden !important;
  290. }
  291. .active {
  292. background: $jd-light-color !important;
  293. border: 2rpx solid $jd-color;
  294. font-weight: bold;
  295. color: $jd-color !important;
  296. box-sizing: border-box;
  297. }
  298. .goods-skus-box {
  299. overflow-y: auto;
  300. height: 610rpx;
  301. // #ifdef MP-WEIXIN
  302. height: 570rpx;
  303. // #endif
  304. margin-bottom: 10rpx;
  305. }
  306. .goods-skus-view {
  307. overflow: hidden;
  308. .skus-view-list {
  309. > .skus-view-item {
  310. flex: 1;
  311. padding: 0 36rpx;
  312. overflow: hidden;
  313. height: 60rpx;
  314. line-height: 60rpx;
  315. float: left;
  316. text-align: center;
  317. margin-left: 24rpx;
  318. margin-bottom: 20rpx;
  319. font-size: 22rpx;
  320. color: #262626;
  321. background: #f2f2f2;
  322. border-radius: 30rpx;
  323. }
  324. }
  325. }
  326. .goods-header {
  327. height: 200rpx;
  328. display: flex;
  329. align-items: center;
  330. margin-bottom: 36rpx;
  331. }
  332. .goods-box {
  333. padding: 50rpx 36rpx 0 36rpx;
  334. }
  335. .goods-skus {
  336. padding: 0 20rpx;
  337. }
  338. .goods-price {
  339. color: $jd-color;
  340. line-height: 80rpx;
  341. display: flex;
  342. }
  343. .promotion-box {
  344. line-height: 1;
  345. display: flex;
  346. align-items: center;
  347. text-decoration: line-through;
  348. color: #999;
  349. margin-left: 10rpx;
  350. /deep/ span {
  351. font-size: 30rpx;
  352. }
  353. }
  354. .promotion {
  355. font-size: 30rpx;
  356. }
  357. .goods-price-promotionShow {
  358. font-size: 48rpx;
  359. }
  360. .goods-check-skus {
  361. font-size: 24rpx;
  362. color: #999;
  363. > .goods-check-skus-name {
  364. margin-left: 4rpx;
  365. }
  366. > span {
  367. color: #333;
  368. }
  369. }
  370. </style>