productdetails.js 5.0 KB
import {
  request
} from "../../request/index.js"
const a = getApp()
Page({
  data: {
    imagesUrl: a.globalData.baseUrl,
    detailsContent: [],
    id: "",
    _num: 1,
    txtContent: '暂无数据',
    shopCart: '',
    content: '',
    show: 1,
    msg: ''

  },
  onLoad: function (options) {
    // //  console.log(options);
    let that = this
    that.setData({
      id: options.id,
    })
  },
  onShow: function () {
    this.getDetailsContent()
  },
  text(details) {
    var texts = ''; //待拼接的内容
    while (details.indexOf('<img') != -1) { //寻找img 循环
      texts += details.substring('0', details.indexOf('<img') + 4); //截取到<img前面的内容
      details = details.substring(details.indexOf('<img') + 4); //<img 后面的内容
      if (details.indexOf('style=') != -1 && details.indexOf('style=') < details.indexOf('>')) {
        texts += details.substring(0, details.indexOf('style="') + 7) + "width:750rpx;height:auto;margin:0 auto; display:block"; //从 <img 后面的内容 截取到style= 加上自己要加的内容
        details = details.substring(details.indexOf('style="') + 7); //style后面的内容拼接
      } else {
        texts += ' style="width:100%;height:auto;margin:0 auto;  display:block" ';
      }
    }
    while (details.indexOf('<td') != -1) { //寻找img 循环
      texts += details.substring('0', details.indexOf('<td') + 4); //截取到<img前面的内容
      details = details.substring(details.indexOf('<td') + 4); //<img 后面的内容
      if (details.indexOf('style=') != -1 && details.indexOf('style=') < details.indexOf('>')) {
        texts += details.substring(0, details.indexOf('style="') + 7) + "width:750rpx!important;height:auto;margin:0 auto;"; //从 <img 后面的内容 截取到style= 加上自己要加的内容
        details = details.substring(details.indexOf('style="') + 7); //style后面的内容拼接
      } else {
        //  //  console.log(34449898)
        texts += ' style="width:750rpx;height:auto;margin:0 auto;" ';
      }
    }
    texts += details; //最后拼接的内容
    return texts;
  },
  async getDetailsContent() {
    let that = this;
    try {
      const {
        data: {
          data
        }
      } = await
      request({
        url: 'api/shop/goodDetail',
        data: {
          id: that.data.id,
        }
      })
      that.setData({
        detailsContent: data,
        content: that.text(data.content)
      })
      // //  console.log(that.data.detailsContent, "详情");

    } catch (err) {
      //  console.log(err);
      that.setData({
        msg: err.msg
      })
      that.popMaskTest()
    }

  },
  // 详情 / 评价
  changeTitle(e) {
    this.setData({
      _num: e.currentTarget.dataset.num
    })
  },
  clickme() {
    this.showModal();
  },
  popSuccessTest() { // 消息提示
    wx.showToast({
      title: this.data.msg,
      icon: '', //默认值是success,就算没有icon这个值,就算有其他值最终也显示success
      duration: 2000, //停留时间
    })
  },
  //显示对话框
  showModal() {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal() {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  popMaskTest() {
    wx.showToast({
      title: this.data.msg,
      duration: 2000,
      icon: 'none',
      mask: true //是否有透明蒙层,默认为false 
      //如果有透明蒙层,弹窗的期间不能点击文档内容 
    })
  },




















  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */


  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {


  //   // let url = encodeURIComponent('/packageNews/pages/news_detail/news_detail?news_id=' + this.data.news_id);

  //   return {
  //     title: "点赞商品详情",
  //     path: `api/shop/clickGoods?url=${url}`
  //   }

  // },




})