Files
JayuCloud/WeChatProgram/pages/shop-detail/index.js
T

99 lines
2.0 KiB
JavaScript

const shopApi = require('../../api/shop')
const navigation = require('../../utils/navigation')
const { resolveImageList } = require('../../utils/file')
const { DEFAULT_SHOP_IMAGE } = require('../../utils/config')
Page({
data: {
loading: true,
loadError: '',
shopId: '',
returnTo: '/pages/home/index',
shopInfo: null,
shopImages: []
},
onLoad(options) {
this.setData({
shopId: options && options.id ? options.id : '',
returnTo: options && options.returnTo ? decodeURIComponent(options.returnTo) : '/pages/home/index'
})
this.loadShopDetail()
},
async onPullDownRefresh() {
await this.loadShopDetail()
wx.stopPullDownRefresh()
},
async loadShopDetail() {
if (!this.data.shopId) {
this.setData({
loading: false,
loadError: '门店 ID 不存在'
})
return
}
this.setData({
loading: true,
loadError: ''
})
try {
const shopInfo = await shopApi.getShopById(this.data.shopId)
const shopImages = await resolveImageList(shopInfo.imageFileIds || shopInfo.coverImageFileId, DEFAULT_SHOP_IMAGE)
this.setData({
shopInfo,
shopImages
})
} catch (error) {
this.setData({
loadError: error.message || '门店详情加载失败'
})
} finally {
this.setData({
loading: false
})
}
},
handleBack() {
navigation.goBack(this.data.returnTo || '/pages/home/index')
},
handlePreviewImage(event) {
const url = event.currentTarget.dataset.url
if (!url) {
return
}
wx.previewImage({
current: url,
urls: this.data.shopImages
})
},
handleCopyName() {
const shopInfo = this.data.shopInfo
if (!shopInfo || !shopInfo.shopName) {
return
}
wx.setClipboardData({
data: shopInfo.shopName
})
},
handleCopyAddress() {
const shopInfo = this.data.shopInfo
if (!shopInfo || !shopInfo.address) {
return
}
wx.setClipboardData({
data: shopInfo.address
})
}
})