鸿宇研学生前端代码
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.

275 lines
6.1 KiB

  1. <template>
  2. <view :class="['product', size]"
  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.title }}</view>
  11. <view class="product-desc text-ellipsis" v-if="tagDesc">{{ tagDesc }}</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.priceOrigin">
  22. {{ `¥${data.priceOrigin}` }}
  23. </view>
  24. </view>
  25. <view class="product-registered">
  26. {{ `${data.applyNum}人已报名` }}
  27. </view>
  28. </view>
  29. <button class="btn" @click="onRegistrate">报名</button>
  30. </view>
  31. </view>
  32. <button class="flex btn btn-collect"
  33. :style="collectBtnStyle"
  34. @click.stop="onCollect"
  35. @touchstart.stop="onCollect"
  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. // todo: fetch
  51. isCollected: {
  52. type: Boolean,
  53. default: false,
  54. },
  55. size: {
  56. type: String,
  57. default: 'normal' // normal | small
  58. }
  59. },
  60. data() {
  61. return {
  62. isMove: false,
  63. startClientX: null,
  64. displayX: 0,
  65. }
  66. },
  67. computed: {
  68. tagDesc() {
  69. // todo: check key
  70. const { tagList } = this.data
  71. return tagList?.length ? tagList.split('、').join('·') : ''
  72. return
  73. const { categoryId_dictText, timeId_dictText, ageId_dictText } = this.data
  74. return [categoryId_dictText, timeId_dictText, ageId_dictText].filter(val => val).join('·')
  75. },
  76. priceInt() {
  77. return Math.floor(this.data.priceDiscount)
  78. },
  79. priceFrac() {
  80. let frac = this.data.priceDiscount % this.priceInt
  81. return frac > 0 ? frac.toFixed(2).slice(1) : ''
  82. },
  83. collectBtnWidth() {
  84. return this.isCollected ? 80 : 56
  85. },
  86. collectBtnStyle() {
  87. const width = this.collectBtnWidth
  88. const background = this.isCollected ? '#26334E' : '#FF9035'
  89. let display = Math.ceil(this.displayX / width * 100)
  90. display > 100 && (display = 100)
  91. const translateX = 100 - display
  92. return `width: ${width}px; transform: translateX(${translateX}%); background: ${background};`
  93. }
  94. },
  95. methods: {
  96. onTouchstart(e) {
  97. const clientX = e.changedTouches[0].clientX
  98. this.isMove = false
  99. this.startClientX = clientX
  100. this.displayX = 0
  101. },
  102. onTouchmove(e) {
  103. const clientX = e.changedTouches[0].clientX
  104. if (clientX < this.startClientX) {
  105. this.displayX = this.startClientX - clientX
  106. } else {
  107. this.displayX = 0
  108. }
  109. this.isMove = true
  110. },
  111. onTouchend() {
  112. console.log('displayX', this.displayX, this.collectBtnWidth, this.displayX < this.collectBtnWidth)
  113. if (this.displayX < this.collectBtnWidth) {
  114. this.displayX = 0
  115. }
  116. this.isMove = false
  117. },
  118. showCollectBtn() {
  119. this.displayX = 100
  120. },
  121. hiddenCollectBtn() {
  122. this.displayX = 0
  123. },
  124. async onCollect() {
  125. console.log('onCollect')
  126. try {
  127. let succ
  128. if (this.isCollected) {
  129. // todo: fetch cancel collect
  130. succ = true
  131. uni.showToast({
  132. icon: 'success',
  133. title: '已移除收藏',
  134. });
  135. } else {
  136. succ = await this.$store.dispatch('collect', this.data.id)
  137. }
  138. succ && this.hiddenCollectBtn()
  139. this.$emit('collect', !this.isCollected)
  140. } catch (err) {
  141. console.log('collect err', err)
  142. }
  143. },
  144. onRegistrate() {
  145. this.$utils.navigateTo(`/pages_order/product/productDetail?id=${this.data.id}`)
  146. },
  147. },
  148. }
  149. </script>
  150. <style scoped lang="scss">
  151. .product {
  152. position: relative;
  153. height: 464rpx;
  154. background: #FFFFFF;
  155. border: 2rpx solid #FFFFFF;
  156. border-radius: 32rpx;
  157. overflow: hidden;
  158. font-size: 0;
  159. &-img {
  160. width: 100%;
  161. height: 220rpx;
  162. }
  163. &-info {
  164. height: 244rpx;
  165. padding: 16rpx 16rpx 24rpx 16rpx;
  166. box-sizing: border-box;
  167. justify-content: space-between;
  168. &-top {
  169. width: 100%;
  170. }
  171. &-bottom {
  172. width: 100%;
  173. justify-content: space-between;
  174. }
  175. }
  176. &-name {
  177. font-size: 28rpx;
  178. font-weight: 500;
  179. color: #000000;
  180. }
  181. &-desc {
  182. margin-top: 8rpx;
  183. font-size: 24rpx;
  184. color: #8B8B8B;
  185. }
  186. &-detail {
  187. }
  188. &-price {
  189. justify-content: flex-start;
  190. align-items: baseline;
  191. column-gap: 12rpx;
  192. &-val {
  193. font-size: 24rpx;
  194. font-weight: 500;
  195. color: #FF4800;
  196. .highlight {
  197. font-size: 32rpx;
  198. }
  199. }
  200. &-bef {
  201. text-decoration: line-through;
  202. font-size: 24rpx;
  203. color: #8B8B8B;
  204. }
  205. }
  206. &-registered {
  207. font-size: 24rpx;
  208. color: #8B8B8B;
  209. }
  210. .btn {
  211. padding: 11rpx 32rpx;
  212. font-size: 26rpx;
  213. font-weight: 500;
  214. color: #FFFFFF;
  215. background: #00A9FF;
  216. border-radius: 24rpx;
  217. }
  218. &.small {
  219. .btn {
  220. padding: 11rpx 16rpx;
  221. }
  222. }
  223. }
  224. .btn.btn-collect {
  225. position: absolute;
  226. top: 0;
  227. right: 0;
  228. row-gap: 8rpx;
  229. // width: 112rpx;
  230. height: 100%;
  231. font-size: 24rpx;
  232. line-height: 1;
  233. color: #FFFFFF;
  234. // background: #FF9035;
  235. border-radius: 0;
  236. }
  237. </style>