// pages/digitalTextbooks/digitalTextbooksDetails/index.js import Toast from "tdesign-miniprogram/toast"; const app = getApp() Page({ /** * 页面的初始数据 */ data: { digitalTextId: '', digitalsData: {}, isFavourite: true, richStyle: 'font-size: 28rpx;color: #333; line-height: 48rpx;text-align: justify;margin: 20rpx; ', briefIconClick: { name: "/static/images/bookService/detail/tushujianjie-click.png", }, relatedBookData: [], publicationDate: '', isBuy: false, loading: true, }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { console.log(options.id); this.setData({ digitalTextId: options.id }) this.digitalTextbooksDetailsGet(this.data.digitalTextId) }, // 格式化日期 // 格式化日期 formatDate(dateString) { if (!dateString) { return ""; } 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]; }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { this.setData({ digitalsData: {} }) }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { }, digitalTextbooksDetailsGet(digitalId) { let query = { path: '*', queryType: '*', productId: digitalId, storeInfo: 'jsek_digitalTextbooks', coverSize: { height: 300 }, fields: { seriesName: [], author: [], isbn: [], publicationDate: [], bookClassification: [], paperPrice: [], JDLink: [], tmallLink: [], dangdangLink: [], weidianLink: [], content: [], authorIntroduction: [], isApplyBook: [], isSell: [], pdf: [], protectedEpub: [], probationPage: [], //pdf试读页数 freeEpubPage: [], catalogue: [] //epub试读百分比 } } app.MG.store.getProductDetail(query).then(res => { // console.log(res); if (res.datas.purchasedSaleMethodIdList.includes(res.datas.defaultSaleMethodId)) { this.setData({ isBuy: true }) } else { this.setData({ isBuy: false }) } // res.datas.publicationDate = if (res.datas.icon == '') { res.datas.icon = '/static/images/default-book-img.png' } this.setData({ publicationDate: this.formatDate(res.datas.publicationDate) }) res.datas.price = res.datas.price.toFixed(2) res.datas.isFavourite = false this.setData({ digitalsData: res.datas }) wx.setNavigationBarTitle({ title: res.datas.name, }) this.getAboutBook(res.datas.productLinkInfo[res.datas.productLinkInfo.length - 1].LinkPath) console.log(this.data.isBuy); this.setData({ loading: false, }); }) }, setCoolect() { console.log(this.data.digitalsData.id); console.log(this.data.digitalsData.isFavourite); this.setData({ isFavourite: !this.data.isFavourite }) if (this.data.digitalsData.isFavourite) { app.MG.store .delProductLink({ productIds: [this.data.digitalsData.id], linkType: 'FavoriteBookCity' }) .then(() => { this.data.digitalsData.isFavourite = false }) } else { let params = { productIds: [this.data.digitalsData.id], linkType: 'FavoriteBookCity' } app.MG.store.addProductLink(params).then((res) => { this.data.digitalsData.isFavourite = true }) } }, onTabsChange(event) { console.log(`Change tab, tab-panel value is ${event.detail.value}.`); }, onTabsClick(event) { console.log(`Click tab, tab-panel value is ${event.detail.value}.`); }, // 获取相关教材 getAboutBook(path) { let query = { path, queryType: '*', coverSize: { height: 243 }, paging: { start: 0, size: 99 }, fields: { author: [], publicationDate: [] } } app.MG.store.getProductList(query).then(res => { const Arr = res.datas.filter( (item) => item.id != this.data.digitalsData.id ); let bookArr = [] if (Arr.length) { if (Arr.length > 9) { for (var i = 0; i < 9; i++) { var _num = Math.floor(Math.random() * Arr.length) var mm = Arr[_num] Arr.splice(_num, 1) bookArr.push(mm) } } else { bookArr = Arr } bookArr.forEach(item => { if (item.icon == '') { item.icon = '/static/images/default-book-img.png' } }) this.setData({ relatedBookData: bookArr }) console.log(this.data.relatedBookData); } else { this.setData({ relatedBookData: [] }) } }) }, onCorrelationBook(e) { const item = e.currentTarget.dataset.item console.log(item); this.digitalTextbooksDetailsGet(item.id) }, // 图书添加购物车 async addBookShopcCar() { if (!this.data.expire) { const shoppingCartGetId = []; let query = { start: 0, size: 9999, filterList: [], searchList: [], }; const res = await app.MG.store.getShoppingCartProductList(query); res.datas.forEach((item) => { shoppingCartGetId.push(item.saleMethod.id); }); const determine = shoppingCartGetId.some( (item) => item == this.data.digitalsData.defaultSaleMethodId ); if (!determine) { let query = { requests: [{ saleMethodId: this.data.digitalsData.defaultSaleMethodId, storeEventId: null, agentCode: "电子书", }, ], }; const addRes = app.MG.store.addShoppingCart(query); this.showSuccessToast(); } else { Toast({ context: this, selector: "#t-toast", message: "该书已在购物车,请勿重复添加", theme: "warning", direction: "column", }); } } else { wx.showToast({ title: "商品不在有效期", icon: "none", duration: 1000, }); } }, showSuccessToast() { Toast({ context: this, selector: "#t-toast", message: "添加成功", theme: "success", direction: "column", }); }, //购买按钮 async buyBtn() { if (!this.data.expire) { let bookOrdersId = ""; let query = { remarks: "电子书", requests: [{ saleMethodId: this.data.digitalsData.defaultSaleMethodId, count: 1, }, ], }; // 发起订单初始化请求并等待结果 const res = await app.MG.store.initOrder(query); // 获取订单号并赋值给 orderNumber.value console.log(res, 'res456'); bookOrdersId = res.orderNumber; // 检查订单号是否存在 if (bookOrdersId) { if (this.data.digitalsData.price == "0.00") { app.MG.store .confirmOrder({ orderNum: bookOrdersId, }) .then((res) => { if (res) { wx.showToast({ title: "领取成功", icon: "none", duration: 1000, }); this.getBookInfo(this.data.bookDetail.id); } }); } else { const url = "/pages/cart/paymentPage/index?orderNumber=" + bookOrdersId + '&onNorderSaleMethod=' + res.saleMethodLinks[0].orderSaleMethod.id; wx.navigateTo({ url, }); } } else { console.log(222); } } else { wx.showToast({ title: "商品不在有效期", icon: "none", duration: 1000, }); } } })