echone-sku.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <template>
  2. <popup-bottom :show="show" @close="closeSkuBox">
  3. <view class="sku-box" :style="{paddingBtoom:isIphoneX ? '188rpx' : '120px'}">
  4. <view class="sku-header container">
  5. <image class="goods-img" :src="selectSkuInfo[cbImage]"></image>
  6. <view class="sku-goods-info">
  7. <view class="money" :style="{color:themeColor}">
  8. <text class="symbol fs-26">¥</text>
  9. <text class="amount fs-38">{{selectSkuInfo[cbPrice] | toFixed2}}</text>
  10. </view>
  11. <view class="stock fs-24">
  12. 库存{{selectSkuInfo[cbStock]}}件
  13. </view>
  14. <view class="fs-24">
  15. 已选:"{{selectSkuInfo[cbValue]}}"
  16. </view>
  17. </view>
  18. </view>
  19. <view class="sku-item container" v-for="(sku,sIndex) in mySpecifications" :key="sku[speId]">
  20. <view class="sku-name">{{sku[speName]}}</view>
  21. <view class="sku-content">
  22. <text
  23. class="sku-content-item"
  24. v-for="(item,index) in sku[speList]"
  25. :key="index"
  26. :style="{
  27. border: index===sku.sidx?`${borderWidth}px solid ${themeColor}`:`${borderWidth}px solid transparent`,
  28. color:index===sku.sidx?themeColor:'#333',
  29. backgroundColor: index===sku.sidx?'#FFFFFF':'#f5f5f5' }"
  30. @click="selectSku(sIndex,index)"
  31. >{{item}}</text>
  32. </view>
  33. </view>
  34. <view class="sku-item container count">
  35. <view class="sku-name">数量</view>
  36. <view class="count-box">
  37. <text class="minus" :class="{disabled:buyCount<=1}" @click="handleBuyCount('minus')">-</text>
  38. <input class="count-content" v-model="buyCount" :disabled="selectSkuInfo[cbStock]<=0"/>
  39. <text class="add" :class="{disabled:buyCount>=selectSkuInfo[cbStock]}" @click="handleBuyCount('add')">+</text>
  40. </view>
  41. </view>
  42. <view class="tui-right-flex tui-popup-btn" :style="{bottom:isIphoneX ? '20rpx' : '0'}">
  43. <view class="tui-flex-1"><tui-button height="72rpx" :size="28" type="danger" shape="circle" @click="handleConfirm('add')">加入购物车</tui-button></view>
  44. <view class="tui-flex-1"><tui-button height="72rpx" :size="28" type="warning" shape="circle" @click="handleConfirm('buy')">立即购买</tui-button></view>
  45. </view>
  46. </view>
  47. </popup-bottom>
  48. </template>
  49. <script>
  50. import PopupBottom from './popup-bottom'
  51. export default {
  52. components:{
  53. PopupBottom
  54. },
  55. filters: {
  56. toFixed2: function (value) {
  57. if(!value) return '0.00'
  58. return Number(value).toFixed(2)
  59. }
  60. },
  61. props: {
  62. show: {
  63. type: Boolean,
  64. default: false
  65. },
  66. themeColor: {
  67. type: String,
  68. default: '#1ac792'
  69. },
  70. combinations: {
  71. type: Array,
  72. default(){
  73. return []
  74. }
  75. },
  76. specifications: {
  77. type: Array,
  78. default(){
  79. return []
  80. }
  81. },
  82. defaultSelectIndex: {
  83. type: Number,
  84. default: 0
  85. },
  86. combinationsProps: {
  87. type: Object,
  88. default(){
  89. return {
  90. id: 'id',
  91. value: 'value',
  92. image: 'image',
  93. price: 'price',
  94. stock: 'stock'
  95. }
  96. }
  97. },
  98. specificationsProps: {
  99. type: Object,
  100. default(){
  101. return {
  102. id: 'id',
  103. list: 'list',
  104. name: 'name'
  105. }
  106. }
  107. },
  108. },
  109. data() {
  110. return {
  111. isIphoneX:this.$store.state.isIphoneX,
  112. buyCount: 1,
  113. selectedIndex: 0,
  114. borderWidth: uni.upx2px(2),
  115. mySpecifications: [],
  116. selectSkuInfo: {},
  117. cbStockNone:false
  118. }
  119. },
  120. watch:{
  121. specifications(val){
  122. this.initSkuData()
  123. }
  124. },
  125. computed: {
  126. speId() {
  127. return this.specificationsProps.id
  128. },
  129. speList() {
  130. return this.specificationsProps.list
  131. },
  132. speName() {
  133. return this.specificationsProps.name
  134. },
  135. cbValue() {
  136. return this.combinationsProps.value
  137. },
  138. cbImage() {
  139. return this.combinationsProps.image
  140. },
  141. cbPrice() {
  142. return this.combinationsProps.price
  143. },
  144. cbStock() {
  145. return this.combinationsProps.stock
  146. }
  147. },
  148. created() {
  149. console.log(this.combinationsProps)
  150. // if(this.specifications.length) {
  151. this.initSkuData()
  152. // }
  153. },
  154. methods: {
  155. initSkuData() {
  156. this.selectedIndex = this.defaultSelectIndex
  157. console.log(this.combinations)
  158. this.selectSkuInfo = this.combinations[this.selectedIndex]
  159. this.mySpecifications = JSON.parse(JSON.stringify(this.specifications))
  160. console.log(this.mySpecifications)
  161. this.mySpecifications.forEach((item,idx) => {
  162. //当前规格组合值
  163. const selects = this.combinations[this.selectedIndex][this.cbValue].split(',')
  164. //每类规格对应其列表的下标 并记录在属性sidx在mySpecifications的子对象中
  165. const sIndex = item[this.speList].indexOf(selects[idx])
  166. this.$set(item,'sidx',sIndex>-1?sIndex:0)
  167. })
  168. },
  169. selectSku(sIndex,index) {
  170. this.mySpecifications[sIndex].sidx = index
  171. const selectInfo = this.mySpecifications.reduce((prev,cur) => {
  172. if(prev) {
  173. return prev+','+cur[this.speList][cur.sidx]
  174. }else {
  175. return cur[this.speList][cur.sidx]
  176. }
  177. },'')
  178. this.selectedIndex = this.combinations.findIndex(item => item[this.cbValue] === selectInfo)
  179. this.selectSkuInfo = this.combinations[this.selectedIndex]
  180. // if(this.selectSkuInfo[this.cbStock] === 0) {
  181. // this.buyCount = 0
  182. // }
  183. if(this.selectSkuInfo[this.cbStock] !== 0 && this.buyCount*1 === 0) {
  184. this.buyCount = 1
  185. }
  186. },
  187. handleBuyCount(type) {
  188. if(type === 'minus') {
  189. if(this.buyCount <= 1) return
  190. this.buyCount = this.buyCount*1 - 1
  191. }
  192. if(type === 'add') {
  193. if(this.buyCount >= this.selectSkuInfo[this.cbStock]) return
  194. this.buyCount = this.buyCount*1 + 1
  195. }
  196. },
  197. closeSkuBox() {
  198. this.$emit('close')
  199. },
  200. handleConfirm(type) {
  201. const result = this.selectSkuInfo
  202. result.count = this.buyCount*1
  203. let data = { type:type ,result:result}
  204. this.$emit('confirm', data)
  205. }
  206. }
  207. }
  208. </script>
  209. <style>
  210. page {
  211. font-size: 28upx;
  212. color: #333333;
  213. }
  214. </style>
  215. <style lang="scss" scoped>
  216. @mixin flex-center {
  217. display: flex;
  218. align-items: center;
  219. }
  220. @mixin flex-center-center {
  221. display: flex;
  222. justify-content: center;
  223. align-items: center;
  224. }
  225. $font-color-light: #999999;
  226. $page-bg-color-grey: #f5f5f5;
  227. .fs-24 {
  228. font-size: 24upx;
  229. }
  230. .fs-26 {
  231. font-size: 26upx;
  232. }
  233. .fs-38 {
  234. font-size: 38upx;
  235. }
  236. .container {
  237. width: 690upx;
  238. margin: 0 auto;
  239. }
  240. .sku-box {
  241. min-height: 500upx;
  242. background-color: #FFFFFF;
  243. padding-bottom: 120upx;
  244. .sku-header {
  245. display: flex;
  246. padding: 40upx 0 20upx;
  247. .goods-img {
  248. width: 200upx;
  249. height: 200upx;
  250. border-radius: 6upx;
  251. border: 4upx solid #FFFFFF;
  252. flex: none;
  253. margin-top: -80upx;
  254. }
  255. }
  256. .sku-goods-info {
  257. margin-left: 20upx;
  258. .money {
  259. border: none;
  260. padding-bottom: 0;
  261. }
  262. .stock {
  263. color: $font-color-light;
  264. }
  265. }
  266. .sku-item {
  267. padding: 30upx 0;
  268. .sku-name {
  269. font-size: 30upx;
  270. }
  271. .sku-content {
  272. border-bottom: 2upx solid $page-bg-color-grey;
  273. padding: 20upx 0;
  274. @include flex-center;
  275. flex-wrap: wrap;
  276. .sku-content-item {
  277. padding: 16upx 20upx;
  278. border-radius: 6upx;
  279. margin: 0 30upx 20upx 0;
  280. }
  281. }
  282. &.count {
  283. @include flex-center;
  284. justify-content: space-between;
  285. .count-box {
  286. @include flex-center;
  287. text,.count-content {
  288. @include flex-center-center;
  289. width: 70upx;
  290. height: 70upx;
  291. font-size: 32upx;
  292. border: 2upx solid $page-bg-color-grey;
  293. }
  294. .add,.minus {
  295. font-size: 44upx;
  296. }
  297. .count-content {
  298. border-width: 2upx 0;
  299. text-align: center;
  300. }
  301. }
  302. }
  303. }
  304. .confirm-btn {
  305. @include flex-center-center;
  306. height: 80upx;
  307. border-radius: 80upx;
  308. color: #FFFFFF;
  309. font-size: 32upx;
  310. margin-top: 10upx;
  311. }
  312. }
  313. [class*=disabled] {
  314. color: $font-color-light;
  315. opacity: .7;
  316. }
  317. .tui-right-flex {
  318. display: flex;
  319. align-items: center;
  320. justify-content: center;
  321. }
  322. .tui-popup-btn {
  323. width: 100%;
  324. position: absolute;
  325. left: 0;
  326. bottom: 0;
  327. z-index: 9999;
  328. }
  329. .tui-flex-1 {
  330. flex: 1;
  331. padding: 16rpx;
  332. }
  333. </style>