jhzh
2025-04-27 c6c703c80be3ee1f1f2d28f820c4d3b730e17040
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<template>
  <view class="upgrade-mask d-flex align-center justify-center" v-if="show">
    <view class="upgrade-box w-8/12 bg-panel-4 p-t-md rounded">
      <!-- 进度条 -->
      <view class="progress d-flex justify-between p-x-md" :class="{isDwonload:isDwonload}">
        <view
          class="item w-5 h-20"
          v-for="item in 20"
          :class="{active:progress/20>=item}"
          :key="item"
          :style="{'animation-delay':item*.1+'s'}"
        ></view>
      </view>
      <view class="title fn-center p-y-xs color-warning-dark">V{{version}}</view>
      <view class="overflow-scroll bg-panel-1 m-x-md content" v-html="serverData.update_log">
      </view>
      <view class="d-flex fn-center btn-group" v-if="!isDwonload">
        <view class="btn flex-fill p-md" @click="show=false">取消</view>
        <view class="btn flex-fill p-md color-warning-dark" @click="downloadApp">升级</view>
      </view>
      <view class="d-flex justify-center p-y-xs" v-else>
        <van-loading />
      </view>
    </view>
  </view>
</template>
<script>
import Member from "@/api/member";
export default {
  name: "v-upgrade",
  data() {
    return {
      currentVersion: "1.0.0",
      version: "1.0.0",
      progress: 0,
      isDwonload: false,
      show: false,
      serverData: {},
    };
  },
  methods: {
    // 获取app版本
    getAppVersion() {
      plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
        this.currentVersion = widgetInfo.version;
        this.getServeVersion();
      });
    },
    // 获取服务器app版本
    getServeVersion() {
      Member.getNewestVersion().then((res) => {
        let key = "android";
        if (plus.os.name == "Android") {
          key = "android";
        } else if (plus.os.name == "iOS") {
          key = "ios";
        }
        this.serverData = res.data[key];
        this.version = this.serverData.version;
        this.upgradeType();
      });
    },
    // 判断是否下载和更新方式
    upgradeType() {
      if (this.version != this.currentVersion) {
        this.show = true;
      }
    },
    // 下载app或wgt
    downloadApp() {
      this.isDwonload = true;
      const downloadTask = uni.downloadFile({
        url: this.serverData.url, //仅为示例,并非真实的资源
        success: (downloadResult) => {
          if (downloadResult.statusCode === 200) {
            this.progress = 100;
            this.appInstall(downloadResult);
          }
        },
      });
 
      downloadTask.onProgressUpdate((res) => {
        if (this.progress >= 100) return;
        this.progress = res.progress;
      });
    },
    // 热更新
    wgtInstall() {},
    // 全包更新
    appInstall(downloadResult) {
      plus.runtime.install(
        downloadResult.tempFilePath,
        {
          force: false,
        },
        () => {
          console.log("install success...");
          plus.runtime.restart();
        },
        (e) => {
          console.error("install fail...");
        }
      );
    },
  },
  created() {
    // #ifdef APP-PLUS
    this.getAppVersion();
    // #endif
  },
};
</script>
<style lang="scss" scoped>
.upgrade-mask {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  display: flex;
  z-index: 999;
  background: rgba(0, 0, 0, 0.5);
  animation: pageShow 0.5s forwards;
  .progress {
    transform: skewX(-15deg);
    .item {
      background-color: transparent;
      border: 1px solid $orange-dark;
      &.active {
        background-color: $orange-dark;
      }
    }
    &.isDwonload {
      .item {
        animation: myPing 1s ease-in-out 1s infinite alternate;
      }
    }
  }
  .content {
    height: 150px;
  }
  .btn-group {
    .btn:active {
      background: rgba($gray-4, 0.1);
    }
  }
}
@keyframes myPing {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1);
  }
  70% {
    transform: scale(1);
  }
  100% {
    transform: scale(1.1);
  }
}
@keyframes pageShow {
  0% {
    transform: translateY(100%);
    opacity: 0;
  }
 
  100% {
    transform: translateY(0);
    opacity: 1;
  }
}
</style>