details.js 8.3 KB
import {
  request
} from "../../request/index.js";
// import {
//   WxParse
// } from "../../wxParse/wxParse.js";
const a = getApp()
var WxParse = require('../../wxParse/wxParse.js');
Page({
  data: {
    imagesUrl: a.globalData.baseUrl,
    time: 30 * 60 * 60 * 1000,
    timeData: {},
    content: '',
    showDialog: false,
    messageAlert: '',
    names: '',
    phone: '',
    msg: '',
    klist: [],
    id: '',
    type: '',
    time_data: '',
    obj: '',
    texts: 0,
    code: '',
  },
  onShow: function () {},

  onLoad: function (options) {
    let that = this
    let token = wx.getStorageSync('token')
    that.setData({
      id: options.id,
      type: options.type,
    })
    if (!token) {
      wx.showModal({
        title: '提示',
        content: '您尚未登录,请登录后操作',
        success: function (res) {
          if (res.confirm) {
            that.showModal()
          } else {
            wx.switchTab({
              url: '/pages/home/home'
            })
          }
        }
      })
    } else {
      that.gethuodong()
    }
  },
  async gethuodong() {
    let that = this
    try {
      const {
        data: {
          data
        }
      } = await request({
        url: 'api/active/detail4s',
        data: {
          id: that.data.id,
          type: that.data.type
        },
      })
      console.log(data, "活动兴趣");
      that.setData({
        klist: data,
        time_data: data.time_num,
        content: data.content,
        time: data.time_num * 1000,
        content2: that.text(data.say)
      })
      WxParse.wxParse('content', 'html', that.data.content, that, 5);
    } catch (err) {
      console.log(err);
      that.setData({
        msg: err.msg
      })
      that.popMaskTest()
    }
  },
  onChange(e) {
    this.setData({
      timeData: e.detail,
    });
  },
  toggleDialog(e) {
    let id = this.data.id
    wx.navigateTo({
      url: '/pages/distributionChanged/distributionChanged?id=' + id
    })
    // this.setData({
    //   showDialog: !this.data.showDialog
    // })
  },
  formSubmit(e) {
    // console.log('form发生了submit事件,携带数据为:', e.detail.value)
    let phone = e.detail.value.phone
    let that = this
    if (!(/^1[34578]\d{9}$/.test(phone))) {
      if (phone.length >= 11) {
        wx.showToast({
          title: '手机号有误',
          icon: 'none',
          duration: 1300
        })
      }
    } else {
      that.setData({
        obj: e.detail.value
      })
      // console.log(that.data.obj);
      that.getsubmit()
    }
  },
  async getsubmit() {
    let that = this
    let obj = that.data.obj
    try {
      const {
        data
      } = await request({
        url: 'api/active/baoMing',
        data: {
          name: obj.names,
          phone: obj.phone,
          id: that.data.id
        }
      })
      that.setData({
        msg: data.msg,
        texts: 1
      })
      that.popSuccessTest()
      console.log(data);
      setTimeout(() => {
        that.toggleDialog()
      }, 1000);
    } catch (err) {
      console.log(err);
      that.setData({
        msg: err.msg
      })
      that.popMaskTest()
    }
  },
  // 正则验证图标图片  
  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) + "max-width:100%;height:auto;margin:0 auto;"; //从 <img 后面的内容 截取到style= 加上自己要加的内容

        details = details.substring(details.indexOf('style="') + 7); //style后面的内容拼接

      } else {

        texts += ' style="max-width:100%;height:auto;margin:0 auto;" ';

      }
    }

    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) + "max-width:74!important;height:auto;margin:0 auto;"; //从 <img 后面的内容 截取到style= 加上自己要加的内容

        details = details.substring(details.indexOf('style="') + 7); //style后面的内容拼接

      } else {

        texts += ' style="max-width:100%;height:auto;margin:0 auto;" ';

      }



    }


    texts += details; //最后拼接的内容

    return texts

  },
  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), 100)
  },
  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), 100)
  },
  bindGetUserInfo(e) { // 立即授权
    let that = this;
    wx.login({
      success(res) {
        console.log(res);
        that.setData({
          code: res.code
        })
        request({
            url: 'api/common/login',
            method: "POST",
            data: {
              rawData: e.detail.rawData,
              code: that.data.code,
            }
          })
          .then(res => {
            console.log(res);
            if (res.data.code == 1) {
              wx.setStorageSync('token', res.data.data.userInfo.token);
              wx.setStorageSync('avatar', res.data.data.userInfo.avatar);
              wx.setStorageSync('name', res.data.data.userInfo.nickname);
              wx.setStorageSync('user_id', res.data.data.userInfo.user_id);

            }
            console.log(res.data.data.userInfo.token);
            that.hideModal()
            that.setData({
              msg: res.data.msg
            })
            that.popSuccessTest();
            that.gethuodong()
            //  setTimeout(function () {
            //  wx.navigateBack()
            //  }, 2000)
          }).catch(err => {
            console.log(err);
          })
      }
    })
  },
  onShareAppMessage: function (options) {
    var that = this;
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var shareObj = {
      title: "分享", // 默认是小程序的名称(可以写slogan等)
      path: '/pages/details/details?id=' + that.data.id + '&type=' + that.data.type, // 默认是当前页面,必须是以‘/’开头的完整路径
      imageUrl: '', //自定义图片路径,可以是本地文件路径、代码包文件路径或者网络图片路径,支持PNG及JPG,不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
      success: function (res) {
        if (res.errMsg == 'shareAppMessage:ok') {}
      },
      fail: function () {
        // 转发失败之后的回调
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          // 用户取消转发
        } else if (res.errMsg == 'shareAppMessage:fail') {
          // 转发失败,其中 detail message 为详细失败信息
        }
      },
    };
    return shareObj;
  },
  popMaskTest() {
    wx.showToast({
      title: this.data.msg,
      icon: 'none',
      duration: 1300,
      mask: true //是否有透明蒙层,默认为false 
      //如果有透明蒙层,弹窗的期间不能点击文档内容 
    })
  },
  popSuccessTest() {
    wx.showToast({
      title: this.data.msg,
      icon: '', //默认值是success,就算没有icon这个值,就算有其他值最终也显示success
      duration: 1300, //停留时间
    })
  },





})