// pages/index/scanResult/index.js const app = getApp() Page({ /** * 页面的初始数据 */ data: { textValue: "领取", rules: null, result: null, productInfo: [], couponList: [], isOther: false, activeCodeAndStoreChannelLinks: [], activeInfo: null, description: "", itemType: '', goodsData: '' }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { try { const result = JSON.parse(options.result) if (result && result.type) { this.setData({ result, textValue: result.type == "PromoteCode" ? '领取' : '激活' }) wx.setNavigationBarTitle({ title: result.type == "PromoteCode" ? '领取优惠卷' : '激活商品', }) // this.getRules(); this.getProductByCode(); } else { this.setData({ isOther: true }) } } catch (error) { console.log(error); this.setData({ isOther: true }) } }, // 激活码规则 getRules() { let query = { path: app.config.refCodes.learnPlatform.exchangeRules, storeInfo: app.config.learnPlatformGoodsStore, repositoryInfo: app.config.learnPlatformGoodsStore, paging: { start: 0, size: 9999 }, fields: { tourism_content: [] }, }; app.MG.resource.getItem(query).then((res) => { try { // const ruleData = res.datas.find(e => e.refCode == 'tourism_exchangeRules') this.setData({ rules: this.data.result.type == "ActivationCode" ? res.datas.find(e => e.refCode == 'tourism_activationCodeCollectionRules') : res.datas.find(e => e.refCode == 'tourism_couponCollectionRules') }) } catch (error) { console.log(error) } }) }, // 激活码激活 useActiveCode() { var that = this; if (that.data.result && that.data.result.type == "ActivationCode") { const activeInfo = that.data.activeInfo const activeCodeAndStoreChannelLinks = that.data.activeCodeAndStoreChannelLinks if ((!activeInfo || activeInfo.productIds.length == 0) && activeCodeAndStoreChannelLinks.length > 0) { wx.showModal({ title: '温馨提示', content: '请选择足够数量的可选商品!', showCancel: false }) return false } app.MG.store .userActiveCode({ code: that.data.result.code, cardNum: that.data.result.cardNum, apiActiveCodeSelectProductRequests: this.data.activeInfo ? [this.data.activeInfo] : [] }) .then(res => { wx.showModal({ title: '温馨提示', content: res, showCancel: false }) setTimeout(() => { wx.navigateTo({ url: '/pages/codeRecord/index', }) }, 2500); }); } else { let that = this; const data = { refCodeOrGuids: [that.data.result.guid], }; app.MG.store.getPromoteCode(data).then(res => { let message = ""; let icon = "successe" if (res == "-5") { message = "领取失败,优惠券已过期!"; icon = "fail" } else if (res == "-7") { message = "抱歉,优惠券已经领取完毕!"; icon = "fail" } else if (res == "-1") { message = "领取失败,系统错误!"; icon = "fail" } else if (res == "-2") { message = "抱歉,优惠券已经领取完毕!"; icon = "fail" } else if (res == "-3") { message = "领取失败,优惠券无效!"; icon = "fail" } else if (res == "-4") { message = "领取失败,优惠券无效!"; icon = "fail" } else if (res == "-6") { message = "领取失败,领取状态异常!"; icon = "fail" } else if (res == "1") { message = "领取成功!"; } wx.showModal({ title: '温馨提示', content: message, showCancel: false, success(res) { if (res.confirm) { wx.navigateBack() } } }) }); } }, // 根据code获取商品信息 getProductByCode() { var that = this; if (that.data.result && that.data.result.type == "ActivationCode") { app.MG.store .getActiveCode({ code: that.data.result.code, cardNum: that.data.result.cardNum, }) .then(res => { try { const list = JSON.parse(res.config); console.log('code', res, list); if (this.data.result) { const type = res.saleMethodList[0].type this.setData({ goodsData: res, }) this.getProductListById(list); } if (res?.activeCodeAndStoreChannelLinks) { this.setData({ description: res.description, activeCodeAndStoreChannelLinks: res.activeCodeAndStoreChannelLinks }) } } catch (error) { this.setData({ isOther: true }) } }); } else { if (this.data.result != null) { const data = { refCodeOrGuids: [that.data.result.guid], }; app.MG.store.getPromoteCodeDetail(data).then(res => { if (res && that.data.result) { res.map(item => { if (item.start) { item.start ? item.start = item.start.split("T")[0] : new Date() } if (item.end) { item.end ? item.end = item.end.split("T")[0] : new Date() } }) this.setData({ couponList: res }) } }); } } }, //根据id进行夸库查询商品 getProductListById(ids) { let that = this; app.MG.store .getProductList({ queryType: "*", // subAccess: [{ // StoreRefCode: app.config.learnPlatformGoodsStore, // 默认商品库 // Path: "", // Type: "*", // }, ], sort: { CreateDate: "Desc", }, fields: { "Id=": ids.map(item => item.id + ""), }, paging: { start: 0, size: 9999, }, coverSize: { width: 215, }, }) .then(res => { res.datas.map(item => { item.name = item.name + '-' + this.data.goodsData.saleMethodList[0].name item.price ? item.price = (item.price).toFixed(2) : ""; item.oldPrice ? item.oldPrice = (item.oldPrice).toFixed(2) : ""; item.icon ? item.icon = item.icon : item.icon = "/static/images/default-book-img.png", item.itemType = this.data.goodsData.saleMethodList[0].type == 'createProductFolderSaleMethod' ? '目录' : this.data.goodsData.saleMethodList[0].type == 'createProductItemSaleMethod' ? '资源' : '电子书' }) console.log(1, res.datas[0]); that.setData({ productInfo: res.datas }) }); }, // 跳转到商品选择页面 selectPage(e) { const { info } = e.currentTarget.dataset wx.navigateTo({ url: '/pages/scanResult/selectPage/index?info=' + JSON.stringify(info) + '&result=' + JSON.stringify(this.data.result), }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, })