productDetail.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. <route lang="json5" type="page">
  2. {
  3. layout: 'default',
  4. style: {
  5. navigationStyle: 'custom',
  6. },
  7. }
  8. </route>
  9. <script lang="ts" setup>
  10. // 必须导入需要用到的页面生命周期(即使在当前页面上没有直接使用到)
  11. // eslint-disable-next-line unused-imports/no-unused-imports
  12. import { onPageScroll, onReachBottom } from '@dcloudio/uni-app'
  13. import useZPaging from 'z-paging/components/z-paging/js/hooks/useZPaging.js'
  14. import { myFavoriteAdd, myFavoriteDel } from '@/api/mine'
  15. import { preOrder as _preOrder } from '@/api/order'
  16. import { getDetail, pinkList } from '@/api/product'
  17. import { getUserInfoHook, requireLogin } from '@/hooks/usePageAuth'
  18. import { t } from '@/locale'
  19. import { formatNumber } from '@/utils/index'
  20. import { getPageParams, goBack, toPage } from '@/utils/page'
  21. import { toast } from '@/utils/toast'
  22. import CustomTooltip from './components/CustomTooltip.vue'
  23. import NotificationCarousel from './components/NotificationCarousel.vue'
  24. defineOptions({
  25. name: 'ProductDetail', // 商品详情
  26. })
  27. const userInfo = computed(() => {
  28. return getUserInfoHook()
  29. })
  30. // 获取屏幕边界到安全区域距离
  31. const systemInfo = uni.getSystemInfoSync()
  32. const safeAreaInsets = systemInfo.safeAreaInsets
  33. // z-paging
  34. const paging = ref(null)
  35. // 类似mixins,如果是页面滚动务必要写这一行,并传入当前ref绑定的paging,注意此处是paging,而非paging.value
  36. useZPaging(paging)
  37. function goHome() {
  38. uni.switchTab({
  39. url: '/pages/index/index',
  40. })
  41. }
  42. // 添加导航栏背景色变量
  43. const navBgColor = ref('transparent')
  44. const changeNavbarThreshold = 300 // 滚动到这个高度时改变导航栏颜色
  45. const showTip = ref(false)
  46. onPageScroll((e) => {
  47. // 根据滚动高度改变导航栏背景色
  48. if (e.scrollTop > changeNavbarThreshold) {
  49. navBgColor.value = '#ffffff'
  50. }
  51. else {
  52. navBgColor.value = 'transparent'
  53. }
  54. })
  55. const productId = ref('') // 商品id
  56. const id = ref('') // 数据id
  57. const detail = ref<any>({
  58. sliderImage: '',
  59. flatPattern: '',
  60. })
  61. const pinkInfo = ref<any>([])
  62. // 添加通知轮播数据
  63. const notifications = ref([
  64. { id: 1, name: 'Aamir Khan', time: '10s' },
  65. { id: 2, name: 'John Smith', time: '30s' },
  66. { id: 3, name: 'Maria Garcia', time: '1m' },
  67. ])
  68. // 点击页面任意地方隐藏提示
  69. function handlePageClick() {
  70. if (showTip.value) {
  71. showTip.value = false
  72. }
  73. }
  74. // 生命周期钩子
  75. onMounted(() => {
  76. showTip.value = true // 显示提示
  77. })
  78. // 搜索结果
  79. // 轮播图
  80. const current = ref<number>(0)
  81. function handleClick(e) {
  82. // console.log(e)
  83. }
  84. function onChange(e) {
  85. // console.log(e)
  86. }
  87. // 拼团类型 join-加团 open-开团
  88. const groupType = ref('open')
  89. const pinkId = ref('') // 拼团id
  90. const joinOrderId = ref('') // 加团的拼团-订单id
  91. // sku 逻辑
  92. const showSku = ref(false)
  93. function openSku(type: string, id?: string, pinkOrderId?: string) {
  94. showSku.value = true
  95. groupType.value = type
  96. if (id)
  97. pinkId.value = id
  98. if (pinkOrderId)
  99. joinOrderId.value = pinkOrderId
  100. }
  101. const formData = ref({
  102. productNum: 1,
  103. selectedSpecs: {}, // 存储选中的规格 { 颜色: '红色', 尺寸: 'M' }
  104. })
  105. const matchedAttrValue = ref<any>({})
  106. // 计算选中规格的文案
  107. const selectedSpecsText = computed(() => {
  108. if (!formData.value.selectedSpecs || Object.keys(formData.value.selectedSpecs).length === 0) {
  109. return ''
  110. }
  111. // 按照attr的顺序生成文案
  112. const specTexts = []
  113. if (detail.value.attr) {
  114. detail.value.attr.forEach((attr) => {
  115. const selectedValue = formData.value.selectedSpecs[attr.attrName]
  116. if (selectedValue) {
  117. specTexts.push(`${attr.attrName}:${selectedValue}`)
  118. }
  119. })
  120. }
  121. return specTexts.length > 0 ? ` ${specTexts.join(',')}` : ''
  122. })
  123. // 选择规格方法
  124. function selectSpec(attrName, specValue) {
  125. formData.value.selectedSpecs[attrName] = specValue
  126. // 按照attr的顺序重新构建对象
  127. const orderedSpecs = {}
  128. if (detail.value.attr) {
  129. detail.value.attr.forEach((attr) => {
  130. if (formData.value.selectedSpecs[attr.attrName]) {
  131. orderedSpecs[attr.attrName] = formData.value.selectedSpecs[attr.attrName]
  132. }
  133. })
  134. }
  135. // 打印当前选中的规格
  136. const selectedSpecsJson = JSON.stringify(orderedSpecs)
  137. console.log('当前选中规格:', selectedSpecsJson)
  138. // 检查是否所有规格都已选中
  139. const totalAttrs = detail.value.attr ? detail.value.attr.length : 0
  140. const selectedAttrs = Object.keys(orderedSpecs).length
  141. if (selectedAttrs === totalAttrs && totalAttrs > 0) {
  142. // 所有规格都选中了,匹配 attrValue
  143. matchedAttrValue.value = findMatchingAttrValue(orderedSpecs)
  144. if (matchedAttrValue.value) {
  145. console.log('匹配到的规格组合:', matchedAttrValue.value)
  146. console.log('对应的图片:', matchedAttrValue.value.image)
  147. }
  148. else {
  149. console.log('未找到匹配的规格组合')
  150. }
  151. }
  152. }
  153. // 匹配 attrValue 中对应的规格组合
  154. function findMatchingAttrValue(selectedSpecs) {
  155. if (!detail.value.attrValue || !Array.isArray(detail.value.attrValue)) {
  156. return null
  157. }
  158. return detail.value.attrValue.find((attrValue) => {
  159. // 将 attrValue 的规格转换为对象进行比较
  160. const attrValueSpecs = {}
  161. if (attrValue.suk) {
  162. // 假设 suk 格式类似 "红色,M" 或者其他分隔符
  163. const sukParts = attrValue.suk.split(',')
  164. if (detail.value.attr && sukParts.length === detail.value.attr.length) {
  165. detail.value.attr.forEach((attr, index) => {
  166. attrValueSpecs[attr.attrName] = sukParts[index].trim()
  167. })
  168. }
  169. }
  170. // 比较选中的规格和当前 attrValue 的规格是否完全匹配
  171. const selectedKeys = Object.keys(selectedSpecs)
  172. const attrValueKeys = Object.keys(attrValueSpecs)
  173. if (selectedKeys.length !== attrValueKeys.length) {
  174. return false
  175. }
  176. return selectedKeys.every(key => selectedSpecs[key] === attrValueSpecs[key])
  177. })
  178. }
  179. // 查询商品详情
  180. async function queryDetail() {
  181. const res = await getDetail({ id: productId.value })
  182. console.log(res)
  183. detail.value = res.data
  184. // 默认选择第一个规格
  185. setDefaultSpecs()
  186. paging.value.complete()
  187. }
  188. // 查询商品拼团信息
  189. async function queryPinkInfo() {
  190. const res = await pinkList({ cid: id.value })
  191. console.log(res)
  192. pinkInfo.value = res.data.list
  193. }
  194. // 设置默认规格选择
  195. function setDefaultSpecs() {
  196. if (detail.value.attr && detail.value.attr.length > 0) {
  197. const defaultSpecs = {}
  198. // 为每个规格属性选择第一个值
  199. detail.value.attr.forEach((attr) => {
  200. if (attr.attrImgValues && attr.attrImgValues.length > 0) {
  201. defaultSpecs[attr.attrName] = attr.attrImgValues[0].name
  202. }
  203. })
  204. // 更新选中的规格
  205. formData.value.selectedSpecs = defaultSpecs
  206. // 按照attr的顺序重新构建对象
  207. const orderedSpecs = {}
  208. detail.value.attr.forEach((attr) => {
  209. if (formData.value.selectedSpecs[attr.attrName]) {
  210. orderedSpecs[attr.attrName] = formData.value.selectedSpecs[attr.attrName]
  211. }
  212. })
  213. // 匹配对应的规格组合
  214. matchedAttrValue.value = findMatchingAttrValue(orderedSpecs)
  215. if (matchedAttrValue.value) {
  216. console.log('默认选中规格:', JSON.stringify(orderedSpecs))
  217. console.log('默认匹配到的规格组合:', matchedAttrValue.value)
  218. console.log('默认对应的图片:', matchedAttrValue.value.image)
  219. }
  220. }
  221. }
  222. // 收藏/取消收藏
  223. async function toggleFavorite() {
  224. if (!requireLogin()) {
  225. return
  226. }
  227. try {
  228. if (detail.value.isFavorite) {
  229. // 取消收藏
  230. const res = await myFavoriteDel({ id: productId.value })
  231. if (res.code === '200') {
  232. detail.value.isFavorite = false
  233. toast.success(t('productDetail.unfavoriteSuccess'))
  234. }
  235. }
  236. else {
  237. // 添加收藏
  238. const res = await myFavoriteAdd({ productIdList: [productId.value] })
  239. if (res.code === '200') {
  240. detail.value.isFavorite = true
  241. toast.success(t('productDetail.favoriteSuccess'))
  242. }
  243. }
  244. }
  245. catch (error) {
  246. console.error('收藏操作失败:', error)
  247. }
  248. }
  249. // 预下单
  250. async function preOrder() {
  251. if (!requireLogin()) {
  252. return
  253. }
  254. const data = {
  255. orderDetails: {
  256. attrValueId: matchedAttrValue.value.id,
  257. productId: productId.value,
  258. cid: id.value,
  259. productNum: formData.value.productNum,
  260. },
  261. preOrderType: 'buyNow',
  262. }
  263. const res = await _preOrder(data)
  264. if (res.code === '200') {
  265. showSku.value = false
  266. toPage(
  267. '/pages/productDetail/checkOut',
  268. {
  269. preOrderId: res.data,
  270. joinOrderId: joinOrderId.value,
  271. pinkId: pinkId.value,
  272. cid: id.value,
  273. groupType: groupType.value,
  274. },
  275. )
  276. }
  277. }
  278. // 商品详情初始化
  279. onLoad((options) => {
  280. console.log(options)
  281. const params = getPageParams(options)
  282. id.value = params.id
  283. productId.value = params.productId
  284. })
  285. onShow(() => {
  286. queryDetail()
  287. queryPinkInfo()
  288. })
  289. </script>
  290. <template>
  291. <z-paging ref="paging" use-page-scroll refresher-only @on-refresh="queryDetail" @click="handlePageClick">
  292. <wd-navbar :bordered="false" safe-area-inset-top fixed :left-arrow="false" :custom-style="`background: ${navBgColor}; transition: background 0.3s;`" custom-class="h-auto!">
  293. <template #title>
  294. <view class="box-border h-full flex items-center justify-between p-24rpx">
  295. <image :src="`/static/icons/left-icon${navBgColor === '#ffffff' ? '-tr' : ''}.png`" class="h-56rpx w-56rpx" @click="() => goBack()" />
  296. <image :src="`/static/icons/menu-icon${navBgColor === '#ffffff' ? '-tr' : ''}.png`" class="h-56rpx w-56rpx" @click="() => goBack()" />
  297. </view>
  298. </template>
  299. </wd-navbar>
  300. <view class="relative">
  301. <wd-swiper
  302. v-model:current="current" :list="detail.sliderImage.split(',')" autoplay height="750rpx"
  303. custom-indicator-class="bottom-40rpx!" :indicator="{ type: 'fraction' }" indicator-position="bottom-right"
  304. image-mode="aspectFit" @click="handleClick" @change="onChange"
  305. />
  306. <NotificationCarousel
  307. :notifications="notifications"
  308. :top="`${safeAreaInsets?.top + 52}px`"
  309. />
  310. </view>
  311. <view class="relative -top-24rpx">
  312. <view
  313. class="flex items-center justify-between rounded-t-24rpx from-[#FF3779] to-[#FF334A] bg-gradient-to-br px-24rpx pb-24rpx pt-18rpx text-white"
  314. >
  315. <view>
  316. <view class="mb-12rpx flex items-baseline">
  317. <text class="text-28rpx">
  318. {{ $t('productDetail.price') }}
  319. </text>
  320. <view class="ml-8rpx rounded-t-18rpx rounded-br-18rpx bg-#202221 px-12rpx text-24rpx">
  321. 20GB
  322. </view>
  323. </view>
  324. <view>
  325. <text class="text-48rpx">
  326. <text class="text-28rpx">
  327. </text>{{ formatNumber(detail.price) }}
  328. </text>
  329. <text class="ml-22rpx text-28rpx line-through">
  330. ৳{{ formatNumber(detail.otPrice) }}
  331. </text>
  332. </view>
  333. </view>
  334. <text class="text-28rpx">
  335. {{ $t('productDetail.sold', [detail.sales]) }}
  336. </text>
  337. </view>
  338. <view class="bg-white px-24rpx pb-24rpx pt-20rpx text-32rpx">
  339. <view class="line-clamp-2font-bold mb-16rpx">
  340. {{ detail.storeName }}
  341. </view>
  342. <view class="flex items-center justify-between" @click="openSku('open')">
  343. <view>
  344. <text class="mr-20rpx">
  345. {{ $t('productDetail.selected') }}
  346. </text>
  347. <text class="text-#757575">
  348. {{ selectedSpecsText }}
  349. </text>
  350. </view>
  351. <wd-icon name="arrow-right" color="#7D7D7D" size="36rpx" />
  352. </view>
  353. </view>
  354. </view>
  355. <view class="mb-20rpx bg-white p-24rpx">
  356. <view class="mb-20rpx flex items-center justify-between">
  357. <view
  358. class="flex items-center before:h-45rpx before:w-8rpx before:rounded-4rpx before:bg-#FF3778 before:content-empty"
  359. >
  360. <text class="ml-10rpx text-32rpx">
  361. {{ $t('productDetail.groupRules') }}
  362. </text>
  363. </view>
  364. <view class="flex items-center">
  365. <text class="mr-8rpx text-24rpx text-#3A444C">
  366. {{ $t('productDetail.viewRules') }}
  367. </text>
  368. <wd-icon name="arrow-right" color="#7D7D7D" size="24rpx" />
  369. </view>
  370. </view>
  371. <image src="/static/images/buy-flow.png" class="w-full" mode="widthFix" />
  372. </view>
  373. <view v-if="pinkInfo && pinkInfo.length" class="bg-white p-24rpx">
  374. <view
  375. class="mb-20rpx flex items-center before:h-45rpx before:w-8rpx before:rounded-4rpx before:bg-#FF3778 before:content-empty"
  376. >
  377. <text class="ml-10rpx text-32rpx">
  378. {{ $t('productDetail.ongoingGroup') }}
  379. </text>
  380. </view>
  381. <view class="flex flex-col gap-24rpx">
  382. <view v-for="(item, index) in pinkInfo" :key="index" class="flex items-center justify-between">
  383. <view class="flex items-center">
  384. <view>
  385. <!-- 头像组 最多五个 -->
  386. <view class="mr-16rpx min-w-220rpx flex items-center">
  387. <view
  388. v-for="(e, i) in item.successAvatar.slice(0, 5)"
  389. :key="i"
  390. :style="{ marginLeft: i !== 0 ? '-20rpx' : '0', zIndex: 10 - i }"
  391. class="h-56rpx w-56rpx overflow-hidden border-2rpx border-white rounded-full border-solid"
  392. >
  393. <image :src="e ? e : '/static/images/default-avatar.png'" class="h-full w-full" mode="aspectFill" />
  394. </view>
  395. </view>
  396. </view>
  397. <view>
  398. <view class="text-28rpx">
  399. {{ $t('productDetail.need') }}
  400. <text class="text-[var(--wot-color-theme)]">
  401. {{ item.remainNum }}
  402. </text>
  403. {{ $t('productDetail.more') }}
  404. </view>
  405. </view>
  406. </view>
  407. <wd-button size="small" @click="openSku('join', item.id, item.orderId)">
  408. {{ $t('productDetail.joinGroup') }}
  409. </wd-button>
  410. </view>
  411. </view>
  412. </view>
  413. <view class="bg-white p-24rpx">
  414. <view
  415. class="mb-20rpx flex items-center before:h-45rpx before:w-8rpx before:rounded-4rpx before:bg-#FF3778 before:content-empty"
  416. >
  417. <text class="ml-10rpx text-32rpx">
  418. {{ $t('productDetail.details') }}
  419. </text>
  420. </view>
  421. <view v-for="i in detail.flatPattern.split(',')" :key="i">
  422. <image
  423. :src="i"
  424. mode="widthFix"
  425. class="w-full"
  426. />
  427. </view>
  428. </view>
  429. <template #bottom>
  430. <view class="flex gap-32rpx bg-white/60 px-28rpx py-30rpx backdrop-blur-20">
  431. <view class="flex items-center justify-between gap-20rpx">
  432. <view class="flex flex-col items-center justify-center">
  433. <image
  434. src="/static/icons/go-home.png"
  435. class="h-40rpx w-40rpx"
  436. @click="goHome"
  437. />
  438. <text class="text-18rpx text-#757575">
  439. {{ $t('productDetail.home') }}
  440. </text>
  441. </view>
  442. <view class="flex flex-col items-center justify-center" @click="toggleFavorite">
  443. <image
  444. v-if="detail.isFavorite"
  445. src="/static/icons/favorite-active.png"
  446. class="h-40rpx w-40rpx"
  447. />
  448. <image
  449. v-else
  450. src="/static/icons/favorite.png"
  451. class="h-40rpx w-40rpx"
  452. />
  453. <text class="text-18rpx text-#757575">
  454. {{ $t('productDetail.favorite') }}
  455. </text>
  456. </view>
  457. </view>
  458. <view class="flex flex-1 items-center justify-end text-32rpx">
  459. <view class="relative">
  460. <view class="rounded-l-full bg-#2F2D31 px-34rpx py-18rpx text-white" @click="openSku('open')">
  461. {{ $t('productDetail.openGroup') }}
  462. </view>
  463. <CustomTooltip
  464. v-model:visible="showTip"
  465. :highlight-text1="`৳${formatNumber(detail.price)}`"
  466. />
  467. </view>
  468. <view class="rounded-r-full bg-[var(--wot-color-theme)] px-34rpx py-18rpx text-white" @click="openSku('join')">
  469. {{ $t('productDetail.joinGroup') }}
  470. </view>
  471. </view>
  472. </view>
  473. </template>
  474. </z-paging>
  475. <wd-action-sheet v-model="showSku" :z-index="9999">
  476. <view class="px-24rpx">
  477. <view class="mb-16rpx flex items-center gap-24rpx border-b-1 border-b-color-#E1E1E1 border-b-solid py-24rpx">
  478. <image
  479. :src="matchedAttrValue.image || detail?.image"
  480. class="h-160rpx w-160rpx shrink-0"
  481. />
  482. <view class="flex-1">
  483. <view class="line-clamp-2 mb-32rpx text-28rpx">
  484. {{ detail.storeName }}
  485. </view>
  486. <view class="flex items-baseline">
  487. <view class="text-#FF0010">
  488. <text class="text-28rpx">
  489. </text>
  490. <text class="text-48rpx">
  491. {{ matchedAttrValue.price || 0 }}
  492. </text>
  493. </view>
  494. <view class="ml-20rpx text-28rpx text-#787878 line-through">
  495. ৳{{ matchedAttrValue.otPrice || 0 }}
  496. </view>
  497. </view>
  498. </view>
  499. </view>
  500. <view v-for="i in detail.attr" :key="i.id" class="mb-24rpx border-b-1 border-b-color-#E1E1E1 border-b-solid pb-40rpx">
  501. <view class="mb-12rpx text-32rpx">
  502. {{ i.attrName }}
  503. </view>
  504. <view class="grid grid-cols-4 gap-20rpx">
  505. <view v-for="(e, j) in i.attrImgValues" :key="j" class="flex flex-col justify-end">
  506. <view
  507. class="box-border flex flex-col border-1 border-transparent border-dashed bg-#F5F5F7 text-center"
  508. :style="{ borderColor: formData.selectedSpecs[i.attrName] === e.name ? 'var(--wot-color-theme)' : '' }"
  509. @click="selectSpec(i.attrName, e.name)"
  510. >
  511. <view>
  512. <view v-if="e.img" class="h-160rpx w-full">
  513. <image
  514. :src="e.img"
  515. class="h-full w-full"
  516. mode="aspectFit"
  517. />
  518. </view>
  519. <view class="text-22rpx text-#757575">
  520. {{ e.name }}
  521. </view>
  522. </view>
  523. </view>
  524. </view>
  525. </view>
  526. </view>
  527. <view class="mb-100rpx flex items-center justify-between text-32rpx">
  528. <view>{{ $t('productDetail.quantity') }}</view>
  529. <wd-input-number v-model="formData.productNum" />
  530. </view>
  531. <view class="py-24rpx">
  532. <wd-button block :style="{ backgroundColor: groupType === 'open' ? '#2F2D31' : 'var(--wot-color-theme)' }" @click="preOrder">
  533. {{ groupType === 'open' ? $t('productDetail.openGroup') : $t('productDetail.joinGroup') }}
  534. </wd-button>
  535. </view>
  536. </view>
  537. </wd-action-sheet>
  538. </template>
  539. <style lang="scss" scoped>
  540. :deep() {
  541. .wd-navbar__title {
  542. margin: 0;
  543. max-width: 100%;
  544. }
  545. }
  546. </style>