鸿宇研学生前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
5.1 KiB

  1. <template>
  2. <view class="product"
  3. @touchstart="onTouchstart"
  4. @touchmove="onTouchmove"
  5. @touchend="onTouchend"
  6. >
  7. <image class="product-img" :src="data.image" mode="aspectFill"></image>
  8. <view class="flex flex-column product-info">
  9. <view class="product-info-top">
  10. <view class="product-name text-ellipsis-2">{{ data.name }}</view>
  11. <view class="product-desc text-ellipsis">{{ data.desc }}</view>
  12. </view>
  13. <view class="flex product-info-bottom">
  14. <view class="product-detail">
  15. <view class="flex product-price">
  16. <view class="product-price-val">
  17. <text>¥</text>
  18. <text class="highlight">{{ priceInt }}</text>
  19. <text>{{ `${priceFrac}` }}</text>
  20. </view>
  21. <view class="product-price-bef" v-if="data.originalPrice">
  22. {{ `¥${data.originalPrice}` }}
  23. </view>
  24. </view>
  25. <view class="product-registered">
  26. {{ `${data.registered}人已报名` }}
  27. </view>
  28. </view>
  29. <button class="btn" @click="onRegistrate">报名</button>
  30. </view>
  31. </view>
  32. <button class="flex btn-collect"
  33. :style="collectBtnStyle"
  34. @click.stop="onCollect"
  35. @touchstart.stop="() => {}"
  36. >
  37. <view>{{ isCollected ? '移除收藏' : '收藏' }}</view>
  38. </button>
  39. </view>
  40. </template>
  41. <script>
  42. export default {
  43. props: {
  44. data: {
  45. type: Object,
  46. default() {
  47. return {}
  48. }
  49. },
  50. isCollected: {
  51. type: Boolean,
  52. default: false,
  53. }
  54. },
  55. data() {
  56. return {
  57. isMove: false,
  58. startClientX: null,
  59. displayX: 0,
  60. }
  61. },
  62. computed: {
  63. priceInt() {
  64. return parseInt(this.data.currentPrice)
  65. },
  66. priceFrac() {
  67. return (this.data.currentPrice % this.priceInt).toFixed(2).slice(1)
  68. },
  69. collectBtnStyle() {
  70. const width = this.isCollected ? 80 : 56
  71. const background = this.isCollected ? '#26334E' : '#FF9035'
  72. let display = Math.ceil(this.displayX / this.collectBtnWidth * 100)
  73. display > 100 && (display = 100)
  74. const translateX = 100 - display
  75. return `width: ${width}px; transform: translateX(${translateX}%); background: ${background};`
  76. }
  77. },
  78. methods: {
  79. onTouchstart(e) {
  80. const clientX = e.changedTouches[0].clientX
  81. this.isMove = false
  82. this.startClientX = clientX
  83. this.displayX = 0
  84. },
  85. onTouchmove(e) {
  86. const clientX = e.changedTouches[0].clientX
  87. if (clientX < this.startClientX) {
  88. this.displayX = this.startClientX - clientX
  89. } else {
  90. this.displayX = 0
  91. }
  92. this.isMove = true
  93. },
  94. onTouchend() {
  95. if (this.displayX < 100) {
  96. this.displayX = 0
  97. }
  98. this.isMove = false
  99. },
  100. showCollectBtn() {
  101. this.displayX = 100
  102. },
  103. hiddenCollectBtn() {
  104. this.displayX = 0
  105. },
  106. onCollect() {
  107. console.log('onCollect')
  108. if (this.isCollected) {
  109. // todo: fetch cancel collect
  110. uni.showToast({
  111. icon: 'success',
  112. title: '已移除收藏',
  113. });
  114. } else {
  115. // todo: fetch collect
  116. uni.showToast({
  117. icon: 'success',
  118. title: '已收藏',
  119. });
  120. }
  121. this.hiddenCollectBtn()
  122. },
  123. onRegistrate() {
  124. this.$utils.navigateTo(`/pages_order/product/productDetail?id=${this.data.id}`)
  125. },
  126. },
  127. }
  128. </script>
  129. <style scoped lang="scss">
  130. .product {
  131. position: relative;
  132. height: 464rpx;
  133. background: #FFFFFF;
  134. border: 2rpx solid #FFFFFF;
  135. border-radius: 32rpx;
  136. overflow: hidden;
  137. font-size: 0;
  138. &-img {
  139. width: 100%;
  140. height: 220rpx;
  141. }
  142. &-info {
  143. height: 244rpx;
  144. padding: 16rpx 16rpx 24rpx 16rpx;
  145. box-sizing: border-box;
  146. justify-content: space-between;
  147. &-top {
  148. }
  149. &-bottom {
  150. width: 100%;
  151. justify-content: space-between;
  152. }
  153. }
  154. &-name {
  155. font-size: 28rpx;
  156. font-weight: 500;
  157. color: #000000;
  158. }
  159. &-desc {
  160. margin-top: 8rpx;
  161. font-size: 24rpx;
  162. color: #8B8B8B;
  163. }
  164. &-detail {
  165. }
  166. &-price {
  167. justify-content: flex-start;
  168. align-items: baseline;
  169. column-gap: 12rpx;
  170. &-val {
  171. font-size: 24rpx;
  172. font-weight: 500;
  173. color: #FF4800;
  174. .highlight {
  175. font-size: 32rpx;
  176. }
  177. }
  178. &-bef {
  179. text-decoration: line-through;
  180. font-size: 24rpx;
  181. color: #8B8B8B;
  182. }
  183. }
  184. &-registered {
  185. font-size: 24rpx;
  186. color: #8B8B8B;
  187. }
  188. .btn {
  189. padding: 11rpx 16rpx;
  190. font-size: 26rpx;
  191. font-weight: 500;
  192. color: #FFFFFF;
  193. background: #00A9FF;
  194. border-radius: 24rpx;
  195. }
  196. }
  197. .btn-collect {
  198. position: absolute;
  199. top: 0;
  200. right: 0;
  201. row-gap: 8rpx;
  202. // width: 112rpx;
  203. height: 100%;
  204. font-size: 24rpx;
  205. line-height: 1;
  206. color: #FFFFFF;
  207. // background: #FF9035;
  208. }
  209. </style>