upkeepInstruction.js 3.2 KB
// pages/upkeepInstruction/upkeepInstruction.js
import {
  request
} from "../../request/index.js"
const a = getApp()
Page({
  data: {
    imagesUrl: a.globalData.baseUrl,
    Upkeepimg: '',
    msg: '',
    content: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUpkeepimg()
  },
  async getUpkeepimg() {
    let that = this
    try {
      const {
        data
      } = await request({
        url: 'api/index/baoSay',
        method: 'GET'
      })
      //  console.log(data.data);
      that.setData({
        Upkeepimg: data.data,
        content: that.text(data.data.content)
      })
    } catch (err) {
      that.setData({
        msg: err.msg
      })
      that.popTest()
    }
  },
  popTest() {
    wx.showToast({
      title: this.data.msg,
      icon: 'none', //如果要纯文本,不要icon,将值设为'none'
      duration: 4000
    })
  },
  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 {
        texts += ' style="width:750rpx;height:auto;margin:0 auto;" ';
      }
    }
    texts += details; //最后拼接的内容
    // //  console.log(texts)
    return texts;

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

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

  }
})