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

278 lines
6.2 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. flex-wrap: wrap;
  193. &-val {
  194. font-size: 24rpx;
  195. font-weight: 500;
  196. color: #FF4800;
  197. white-space: nowrap;
  198. .highlight {
  199. font-size: 32rpx;
  200. }
  201. }
  202. &-bef {
  203. text-decoration: line-through;
  204. font-size: 24rpx;
  205. color: #8B8B8B;
  206. }
  207. }
  208. &-registered {
  209. font-size: 24rpx;
  210. color: #8B8B8B;
  211. }
  212. .btn {
  213. padding: 11rpx 32rpx;
  214. font-size: 26rpx;
  215. font-weight: 500;
  216. color: #FFFFFF;
  217. background: #00A9FF;
  218. border-radius: 24rpx;
  219. white-space: nowrap;
  220. }
  221. &.small {
  222. .btn {
  223. padding: 11rpx 16rpx;
  224. }
  225. }
  226. }
  227. .btn.btn-collect {
  228. position: absolute;
  229. top: 0;
  230. right: 0;
  231. row-gap: 8rpx;
  232. // width: 112rpx;
  233. height: 100%;
  234. font-size: 24rpx;
  235. line-height: 1;
  236. color: #FFFFFF;
  237. // background: #FF9035;
  238. border-radius: 0;
  239. }
  240. </style>