// pages/bookServices/detail/index.js import Message from 'tdesign-miniprogram/message/index'; const app = getApp(); Page({ /** * 页面的初始数据 */ data: { barHeight: '', navBarHeight: '', options: { id: '', name: '', }, bookDetail: {}, link: { jd: '', tamll: '', dang: '', micro: '', }, }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { const systInfo = wx.getSystemInfoSync(); const menu = wx.getMenuButtonBoundingClientRect(); // 胶囊信息 const navBarHeight = (menu.top - systInfo.statusBarHeight) * 2 + menu.height; // 导航栏高度 this.setData({ barHeight: systInfo.statusBarHeight, navBarHeight: navBarHeight, }); this.setData({ options: { id: options.id, name: options.name, }, }); this.getBookInfo(options.id); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() {}, /** * 生命周期函数--监听页面显示 */ onShow() {}, /** * 生命周期函数--监听页面隐藏 */ onHide() {}, /** * 生命周期函数--监听页面卸载 */ onUnload() {}, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() {}, /** * 页面上拉触底事件的处理函数 */ onReachBottom() {}, /** * 用户点击右上角分享 */ onShareAppMessage() {}, // 格式化日期 formatDate(dateString) { if (!dateString) { return 'Invalid date'; } const match = dateString.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/); if (!match) { throw new Error('Invalid date format'); } const [, year, month, day, hours, minutes, seconds] = match; const date = new Date( parseInt(year, 10), parseInt(month, 10) - 1, parseInt(day, 10), parseInt(hours, 10), parseInt(minutes, 10), parseInt(seconds, 10), ); if (isNaN(date.getTime())) { throw new Error('Invalid date'); } // 由于小程序环境可能不支持 Intl.DateTimeFormat,我们使用简化的格式化方法 const formatted = `${year}年${this.formatMonth(month)}`; return formatted; }, // 简化的月份格式化函数,返回月份的中文表达 formatMonth(month) { const months = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']; return months[parseInt(month, 10) - 1]; }, // 金额显示.00格式 numFormat(value) { if (!value) return '0.00'; value = Number(value).toFixed(2); var intPart = Math.trunc(value); // 获取整数部分 var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'); // 将整数部分逢三一断 var floatPart = '.00'; // 预定义小数部分 var value2Array = value.split('.'); // =2表示数据有小数位 if (value2Array.length === 2) { floatPart = value2Array[1].toString(); // 拿到小数部分 if (floatPart.length === 1) { return `${intPartFormat}.${floatPart}0`; } return `${intPartFormat}.${floatPart}`; } return intPartFormat + floatPart; }, // 返回 goBack() { wx.navigateBack(); }, // 获取图书详情 getBookInfo(id) { const query = { path: '*', queryType: '*', productId: id, favoriteTypes: 'FavoriteBookCity', itemFields: { 'SysType=': 'CmsFolder', }, coverSize: { height: 300, }, fields: { seriesName: [], author: [], isbn: [], publicationDate: [], bookClassification: [], paperPrice: [], JDLink: [], tmallLink: [], dangdangLink: [], weidianLink: [], content: [], authorIntroduction: [], // isApplyPaperBook: [], // isApplyEBook: [], isApplyBook: [], isSell: [], pdf: [], protectedEpub: [], probationPage: [], //pdf试读页数 freeEpubPage: [], //epub试读百分比 }, }; app.MG.store.getProductDetail(query).then((res) => { this.setData({ bookDetail: res.datas, }); console.log('详情', res.datas); // 获取图书分类 const iconType = JSON.parse(res.datas.bookClassification)[0][0]; const classType = JSON.parse(res.datas.bookClassification)[0][1]; this.setData({ 'bookDetail.publicationDate': this.formatDate(this.data.bookDetail.publicationDate), 'bookDetail.class': this.getBookClass(iconType, classType), 'bookDetail.price': this.numFormat(this.data.bookDetail.price), 'bookDetail.oldPrice': this.numFormat(this.data.bookDetail.oldPrice), 'bookDetail.paperPrice': this.numFormat(this.data.bookDetail.paperPrice), }); }); }, // 获取图书分类 getBookClass(iconType, classType) { let name = ''; const query = { refCodes: ['bookClassification'], }; app.MG.store.getProductTypeField(query).then((res) => { JSON.parse(res[0].config).option.forEach((item) => { if (item.value == iconType) { item.child.forEach((item) => { if (item.value == classType) { name = item.name; } }); } }); }); return name; }, // 收藏 setCollect() { if (this.data.bookDetail.isFavourite) { app.MG.store .delProductLink({ productIds: [this.data.bookDetail.id], linkType: 'FavoriteBookCity', }) .then(() => { this.data.bookDetail.isFavourite = false; Message.success({ context: this, offset: ['20rpx', '32rpx'], duration: 2000, content: '取消收藏', }); }); } else { const params = { productIds: [this.data.bookDetail.id], linkType: 'FavoriteBookCity', }; app.MG.store.addProductLink(params).then((res) => { console.log(res); this.data.bookDetail.isFavourite = true; Message.success({ context: this, offset: ['20rpx', '32rpx'], duration: 2000, content: '收藏成功', }); }); } }, // 跳转网店 goShop(e) { const { link } = e.currentTarget.dataset; wx.navigateTo({ url: link, }); }, });