u-goods-sku.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <template>
  2. <view class="up-goods-sku">
  3. <view @click="open">
  4. <slot name="trigger"></slot>
  5. </view>
  6. <up-popup
  7. v-model:show="show"
  8. mode="bottom"
  9. :closeable="pageInline ? false : closeable"
  10. :pageInline="pageInline"
  11. :border-radius="20"
  12. @close="close"
  13. >
  14. <view class="up-goods-sku-container" :style="{padding: pageInline ? '0px' : ''}">
  15. <view class="up-goods-sku__header">
  16. <slot name="header">
  17. <view class="up-goods-sku__header__image">
  18. <image :src="goodsInfo.image || goodsInfo.picture" mode="aspectFill"></image>
  19. </view>
  20. <view class="up-goods-sku__header__info">
  21. <view class="up-goods-sku__header__info__price">
  22. <text class="up-goods-sku__header__info__price__symbol">¥</text>
  23. <text class="up-goods-sku__header__info__price__value">{{ price }}</text>
  24. </view>
  25. <view class="up-goods-sku__header__info__stock">{{ t('up.goodsSku.stock') }} {{ stock }} {{ t('up.goodsSku.amount') }}</view>
  26. <view class="up-goods-sku__header__info__selected">{{ t('up.goodsSku.choosed') }}: {{ selectedSkuText }}</view>
  27. </view>
  28. </slot>
  29. </view>
  30. <scroll-view class="up-goods-sku__content" scroll-y>
  31. <view v-for="(treeItem, index) in skuTree" :key="index" class="up-goods-sku__content__item">
  32. <view class="up-goods-sku__content__item__title">{{ treeItem.label }}</view>
  33. <view class="up-goods-sku__content__item__list">
  34. <view
  35. v-for="(leafItem, leafIndex) in treeItem.children"
  36. :key="leafIndex"
  37. class="up-goods-sku__content__item__list__item"
  38. :class="{
  39. 'up-goods-sku__content__item__list__item--active': isSelected(treeItem.name, leafItem.id),
  40. 'up-goods-sku__content__item__list__item--disabled': isDisabled(treeItem.name, leafItem.id)
  41. }"
  42. @click="onSkuClick(treeItem.name, leafItem)"
  43. >
  44. <text>{{ leafItem.name }}</text>
  45. </view>
  46. </view>
  47. </view>
  48. <view class="up-goods-sku__content__count">
  49. <view class="up-goods-sku__content__count__title">{{ t('"up.goodsSku.buyAmount"') }}</view>
  50. <view class="up-goods-sku__content__count__control">
  51. <up-number-box
  52. v-model="buyNum"
  53. :min="1"
  54. :max="maxBuyNum"
  55. :disabled="!canBuy"
  56. @change="onNumChange"
  57. ></up-number-box>
  58. </view>
  59. </view>
  60. </scroll-view>
  61. <view class="up-goods-sku__footer">
  62. <up-button
  63. type="primary"
  64. :disabled="!canBuy"
  65. @click="onConfirm"
  66. >
  67. {{ confirmText }}
  68. </up-button>
  69. </view>
  70. </view>
  71. </up-popup>
  72. </view>
  73. </template>
  74. <script>
  75. import { t } from '../../libs/i18n'
  76. export default {
  77. name: 'up-goods-sku',
  78. props: {
  79. // 商品信息
  80. goodsInfo: {
  81. type: Object,
  82. default: () => ({})
  83. },
  84. // SKU树形结构
  85. skuTree: {
  86. type: Array,
  87. default: () => []
  88. },
  89. // SKU列表
  90. skuList: {
  91. type: Array,
  92. default: () => []
  93. },
  94. // 最大购买数量
  95. maxBuy: {
  96. type: Number,
  97. default: 999
  98. },
  99. // 确认按钮文字
  100. confirmText: {
  101. type: String,
  102. default: '确定'
  103. },
  104. // 是否显示关闭弹窗按钮
  105. closeable: {
  106. type: Boolean,
  107. default: true
  108. },
  109. // 是否页面内联模式
  110. pageInline: {
  111. type: Boolean,
  112. default: false
  113. }
  114. },
  115. data() {
  116. return {
  117. show: false,
  118. // 已选择的SKU
  119. selectedSku: {},
  120. // 购买数量
  121. buyNum: 1
  122. }
  123. },
  124. computed: {
  125. // 当前价格
  126. price() {
  127. const selectedSkuComb = this.getSelectedSkuComb()
  128. if (selectedSkuComb) {
  129. return selectedSkuComb.price || selectedSkuComb.price_fee
  130. }
  131. return this.goodsInfo.price || this.goodsInfo.price_fee || 0
  132. },
  133. // 当前库存
  134. stock() {
  135. const selectedSkuComb = this.getSelectedSkuComb()
  136. if (selectedSkuComb) {
  137. return selectedSkuComb.stock || selectedSkuComb.quantity
  138. }
  139. return this.goodsInfo.stock || this.goodsInfo.quantity || 0
  140. },
  141. // 最大购买数量
  142. maxBuyNum() {
  143. const stock = this.stock
  144. return stock > this.maxBuy ? this.maxBuy : stock
  145. },
  146. // 是否可以购买
  147. canBuy() {
  148. const selectedSkuCount = Object.keys(this.selectedSku).length
  149. const skuTreeCount = this.skuTree.length
  150. return selectedSkuCount === skuTreeCount && this.buyNum > 0 && this.stock > 0
  151. },
  152. // 已选SKU文字描述
  153. selectedSkuText() {
  154. const selected = []
  155. Object.keys(this.selectedSku).forEach(key => {
  156. const value = this.selectedSku[key]
  157. if (value) {
  158. this.skuTree.forEach(treeItem => {
  159. if (treeItem.name === key) {
  160. treeItem.children.forEach(leafItem => {
  161. if (leafItem.id === value) {
  162. selected.push(leafItem.name)
  163. }
  164. })
  165. }
  166. })
  167. }
  168. })
  169. return selected.join(', ')
  170. }
  171. },
  172. watch: {
  173. },
  174. emits: ['open', 'confirm', 'close'],
  175. created() {
  176. if (this.pageInline) {
  177. this.show = true;
  178. }
  179. },
  180. methods: {
  181. t,
  182. // 判断SKU是否被选中
  183. isSelected(skuKey, skuValueId) {
  184. return this.selectedSku[skuKey] === skuValueId
  185. },
  186. // 判断SKU是否禁用
  187. isDisabled(skuKey, skuValueId) {
  188. // 构造一个临时的已选中SKU对象
  189. const tempSelected = { ...this.selectedSku, [skuKey]: skuValueId }
  190. // 检查是否还有未选择的SKU维度
  191. const selectedCount = Object.keys(tempSelected).filter(key => tempSelected[key]).length
  192. const totalSkuCount = this.skuTree.length
  193. // 如果所有SKU都已选择,则检查组合是否存在
  194. if (selectedCount === totalSkuCount) {
  195. return !this.getSkuComb(tempSelected)
  196. }
  197. // 检查当前选择的SKU是否会导致无法组成有效组合
  198. for (let i = 0; i < this.skuList.length; i++) {
  199. const sku = this.skuList[i]
  200. let match = true
  201. // 检查已选中的SKU是否匹配
  202. for (const key in tempSelected) {
  203. if (tempSelected[key] && sku[key] !== tempSelected[key]) {
  204. match = false
  205. break
  206. }
  207. }
  208. if (match) {
  209. return false
  210. }
  211. }
  212. return true
  213. },
  214. // SKU点击事件
  215. onSkuClick(skuKey, skuValue) {
  216. // 如果是禁用状态,直接返回
  217. if (this.isDisabled(skuKey, skuValue.id)) {
  218. return
  219. }
  220. // 如果已选中,则取消选中
  221. if (this.selectedSku[skuKey] === skuValue.id) {
  222. this.$set(this.selectedSku, skuKey, '')
  223. } else {
  224. this.$set(this.selectedSku, skuKey, skuValue.id)
  225. }
  226. },
  227. // 数量改变事件
  228. onNumChange(e) {
  229. this.buyNum = e.value
  230. },
  231. // 获取选中的SKU组合
  232. getSelectedSkuComb() {
  233. return this.getSkuComb(this.selectedSku)
  234. },
  235. // 根据已选SKU获取组合信息
  236. getSkuComb(selectedSku) {
  237. const selected = { ...selectedSku }
  238. // 过滤掉空值
  239. Object.keys(selected).forEach(key => {
  240. if (!selected[key]) {
  241. delete selected[key]
  242. }
  243. })
  244. // 检查是否所有SKU都已选择
  245. if (Object.keys(selected).length !== this.skuTree.length) {
  246. return null
  247. }
  248. // 查找匹配的SKU组合
  249. for (let i = 0; i < this.skuList.length; i++) {
  250. const sku = this.skuList[i]
  251. let match = true
  252. for (const key in selected) {
  253. if (sku[key] !== selected[key]) {
  254. match = false
  255. break
  256. }
  257. }
  258. if (match) {
  259. return sku
  260. }
  261. }
  262. return null
  263. },
  264. // 重置选择
  265. reset() {
  266. this.selectedSku = {}
  267. this.buyNum = 1
  268. },
  269. open() {
  270. this.show = true;
  271. this.$emit('open')
  272. },
  273. // 关闭弹窗
  274. close() {
  275. this.false = true;
  276. this.$emit('close')
  277. },
  278. // 确认选择
  279. onConfirm() {
  280. if (!this.canBuy) {
  281. return
  282. }
  283. const selectedSkuComb = this.getSelectedSkuComb()
  284. this.$emit('confirm', {
  285. sku: selectedSkuComb,
  286. goodsInfo: this.goodsInfo,
  287. num: this.buyNum,
  288. selectedText: this.selectedSkuText
  289. })
  290. }
  291. }
  292. }
  293. </script>
  294. <style lang="scss" scoped>
  295. .up-goods-sku {
  296. background-color: #fff;
  297. overflow: hidden;
  298. .up-goods-sku-container {
  299. padding: 4rpx 30rpx;
  300. }
  301. &__header {
  302. display: flex;
  303. flex-direction: row;
  304. padding: 30rpx 0;
  305. position: relative;
  306. &__image {
  307. width: 180rpx;
  308. height: 180rpx;
  309. border-radius: 10rpx;
  310. overflow: hidden;
  311. margin-right: 20rpx;
  312. image {
  313. width: 100%;
  314. height: 100%;
  315. }
  316. }
  317. &__info {
  318. flex: 1;
  319. &__price {
  320. display: flex;
  321. flex-direction: row;
  322. align-items: baseline;
  323. margin-bottom: 20rpx;
  324. &__symbol {
  325. font-size: 24rpx;
  326. color: #fa3534;
  327. margin-right: 4rpx;
  328. }
  329. &__value {
  330. font-size: 36rpx;
  331. color: #fa3534;
  332. font-weight: bold;
  333. }
  334. }
  335. &__stock {
  336. font-size: 26rpx;
  337. color: #999;
  338. margin-bottom: 20rpx;
  339. }
  340. &__selected {
  341. font-size: 26rpx;
  342. color: #333;
  343. }
  344. }
  345. }
  346. &__content {
  347. max-height: 600rpx;
  348. padding: 0 30rpx 30rpx 0;
  349. &__item {
  350. margin-bottom: 30rpx;
  351. &__title {
  352. font-size: 28rpx;
  353. color: #333;
  354. margin-bottom: 20rpx;
  355. }
  356. &__list {
  357. display: flex;
  358. flex-direction: row;
  359. flex-wrap: wrap;
  360. &__item {
  361. padding: 10rpx 20rpx;
  362. border: 2rpx solid #eee;
  363. border-radius: 10rpx;
  364. margin-right: 20rpx;
  365. margin-bottom: 20rpx;
  366. font-size: 26rpx;
  367. color: #333;
  368. &--active {
  369. border-color: #fa3534;
  370. color: #fa3534;
  371. }
  372. &--disabled {
  373. color: #ccc;
  374. border-color: #eee;
  375. }
  376. }
  377. }
  378. }
  379. &__count {
  380. display: flex;
  381. flex-direction: row;
  382. align-items: center;
  383. justify-content: space-between;
  384. margin-top: 20rpx;
  385. &__title {
  386. font-size: 28rpx;
  387. color: #333;
  388. }
  389. }
  390. }
  391. &__footer {
  392. padding: 20rpx 0rpx 40rpx 0;
  393. }
  394. }
  395. </style>