loading改造方案

2024-01-26 16:16:02 阅读:1 编辑

app.js

 onLaunch() {
    wx.removeStorageSync('locallatlng');
    wx.removeStorageSync('tim_user_login');
    wx.removeStorageSync('system_order_type_list');
    this.initLoadingPath();
    if (!Object.entries);
    Object.entries = function (obj) {
      var ownProps = Object.keys(obj),
        i = ownProps.length,
        resArray = new Array(i); // preallocate the Array
      while (i--)
        resArray[i] = [ownProps[i], obj[ownProps[i]]];
      return resArray;
    };
    if (this.siteInfo.front_version) {
      this.globalData.front_version = this.siteInfo.front_version;
    }
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      //console.log(res.hasUpdate)
    })
    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好,是否重启应用?',
        success(res) {
          if (res.confirm) {
            // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
    updateManager.onUpdateFailed(function () {
      // 新版本下载失败
    })

    let haveunionid = wx.getStorageSync('haveunionid');
    if (haveunionid != 1) {
      wx.removeStorageSync('zhyshopInfo');
      wx.removeStorageSync('zhycenterInfo');
      wx.removeStorageSync('openid');
    }

    this.getLoadingPath();
    //add new app code

  },
  initLoadingPath(){
    let that = this;
    let system_loading_info = wx.getStorageSync('system_loading_info');
    if(system_loading_info){
      that.globalData.new_loading_url = system_loading_info;
      if(!this.app_type){
        let filePath = system_loading_info.filePath;
        const fs = wx.getFileSystemManager();
        fs.getFileInfo({
          filePath: filePath,
          success: function (res) {
            console.log("success", res);
            let new_loading_url = system_loading_info;
            new_loading_url.url = filePath;
            that.globalData.new_loading_url = new_loading_url;
          },
          fail: function (res) {
            console.log("fail", res);
          }
        });
      }

    }
  },
  getLoadingPath() {

    let that =this;
    wx.request({
      url: this.siteInfo.siteroot + '?i=' + this.siteInfo.uniacid + '&t=0&v=1.0&from=wxapp&c=entry&a=wxapp&do=Api_index|getLoadingPath&m=zhyshop_sun',
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: {},
      success: function (res) {
        let new_loading_url =res.data.data;
        that.globalData.new_loading_url =new_loading_url;
        if(!that.app_type){
          that.handleLoadPath(new_loading_url);
        }

      }
    });
  },
  handleLoadPath(new_loading_url){
    let that = this;
    this.isHaveLoadingCacheFile(new_loading_url.url, (filePath) => {
      new_loading_url.url = filePath;
      this.globalData.new_loading_url = new_loading_url;
    }, (loading_url) => {
      wx.downloadFile({
        url: loading_url,
        success(res) {
          console.log('download loading url success', res);
          let filePath = res.tempFilePath;
          let system_loading_info = new_loading_url;

          system_loading_info.filePath = filePath;

          new_loading_url.url = filePath;
          that.globalData.new_loading_url = new_loading_url;
          wx.setStorageSync('system_loading_info', system_loading_info);
        },
        fail(res) {
          console.log('download url fail', res)
        }
      });
    });
  },
  isHaveLoadingCacheFile(loading_url, no_download_callback, download_callback) {

    let system_loading_info = wx.getStorageSync('system_loading_info');
    console.log("system_loading_info", system_loading_info);
    console.log("loading_url", loading_url);
    if (system_loading_info && system_loading_info.loading_url == loading_url) {
      console.log("loading_url2", loading_url);
      let filePath = system_loading_info.filePath;
      const fs = wx.getFileSystemManager();
      fs.getFileInfo({
        filePath: filePath,
        success: function (res) {
          console.log("success", res);
          no_download_callback( system_loading_info.filePath);
        },
        fail: function (res) {
          console.log("fail", res);
          download_callback( loading_url);
        }
      });
    } else {
      download_callback(loading_url);
    }

  },

mixins.js(onLoad函数)

 this.setData({
      new_loading_url: app.globalData.new_loading_url
    });

zhy\template\temloading\temloading.wxml

<template name="temloading">
  <view class='loading-wrap' style="background-color: {{background_color}};">
    <image src="{{url}}" class="gif-loading" style="width:{{width}}rpx;height:{{height}}rpx"></image>
  </view>
</template>

zhy\template\temloading\temloading.wxss

.loading-wrap {
  position: fixed;
  top: 0;
  z-index: 11;
  width: 750rpx;
  height: 100%;
  background: #eeeeee;
}
修改wxml
 <template is="temloading" wx:if="{{!show}}" ></template>

改成

<template is="temloading" wx:if="{{!show}}" data="{{...new_loading_url}}" />
修改小程序退出的清除缓存(pages/mine/mine)
//1059行
 let remain_list = ["newapp_share_info","newapp_config","new_app_token","system_loading_info"];
      let remain_list_value = [];
      for(var i in remain_list){
        remain_list_value.push(wx.getStorageSync(remain_list[i]));
      }
      wx.clearStorageSync();
      wx.setStorageSync('has_login_flag', 1);
      if(true){
        for(var i in remain_list_value){
          if(remain_list_value[i]){
            wx.setStorageSync(remain_list[i], remain_list_value[i]);
          }
        }
      }