shop-update.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <view>
  3. <view v-if="isShow" class="shop-update">
  4. <view class="upgrade">
  5. <view class="content">
  6. <view class="title">
  7. <text>{{upgrading ? "正在升级" : "发现新版本"}}</text>
  8. </view>
  9. <view class="container">
  10. <view class="descriptions">
  11. <text>{{upgrading ? "正在为您下载,请耐心等待" : "本次版本更新描述内容:"}}</text>
  12. </view>
  13. <view class="details" v-if="!upgrading">
  14. <view class="item">
  15. <rich-text v-html="intro"></rich-text>
  16. </view>
  17. </view>
  18. <view v-else class="prpgroess">
  19. <progress :percent="downloadProgress" active-mode="forwards" activeColor="red" active
  20. stroke-width="4" show-info />
  21. </view>
  22. </view>
  23. <view v-if="!upgrading" class="btn-group">
  24. <view class="cancel" @click="cancel">
  25. <text>取消</text>
  26. </view>
  27. <view class="confirm" @click="onConfirm">
  28. <text>更新</text>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. <popup v-model="isShow"></popup>
  35. </view>
  36. </template>
  37. <script>
  38. export default {
  39. data(){
  40. return {
  41. isShow: false,
  42. // 是否下载
  43. upgrading: false,
  44. // 下载时间
  45. downloadProgress: 0,
  46. // 下载任务
  47. downloadTask: null,
  48. platform: 'android',
  49. version: '1.0.0',
  50. intro: "",
  51. url: ""
  52. };
  53. },
  54. mounted() {
  55. this.upgrading = false;
  56. this.isShow = false;
  57. // #ifdef APP-PLUS
  58. this.platform = uni.getSystemInfoSync().platform;
  59. this.version = plus.runtime.version;
  60. plus.runtime.getProperty(plus.runtime.appid,wgtinfo=>{
  61. this.version = wgtinfo.version;
  62. this.$http.getUpdateAPK({
  63. ver: this.version,
  64. type: this.platform == 'android' ? 1 : 2
  65. }).then(res=>{
  66. if(res.status){
  67. this.url = res.data.url;
  68. this.intro = res.data.content;
  69. this.isShow = true;
  70. }
  71. }).catch(err=>{
  72. this.upgrading = false;
  73. this.isShow = false;
  74. });
  75. });
  76. // #endif
  77. },
  78. methods: {
  79. cancel(){
  80. this.isShow = false;
  81. },
  82. onConfirm(){
  83. if (!this.upgrading) {
  84. this.upgrading = true
  85. if (this.platform == 'android') {
  86. this.downloadApplications()
  87. }
  88. if (this.platform == 'ios') {
  89. plus.runtime.openURL(this.url)
  90. }
  91. }
  92. },
  93. downloadApplications(){
  94. let that = this
  95. // 建立下载任务
  96. that.downloadTask = uni.downloadFile({
  97. // 下载地址
  98. url: that.url,
  99. success: (res) => {
  100. if (res.statusCode === 200) {
  101. // 把当前app保存下载
  102. uni.saveFile({
  103. tempFilePath: res.tempFilePath,
  104. success: (resp) => {
  105. let savedFilePath = resp.savedFilePath;
  106. let installPath = plus.io.convertLocalFileSystemURL(savedFilePath);
  107. // 安装
  108. that.installApplications({
  109. filePath: installPath,
  110. success: (res) => {
  111. plus.runtime.restart();
  112. },
  113. error: (err) => {
  114. that.isShow = false;
  115. uni.showToast({
  116. icon: 'none',
  117. title: '更新失败,请稍后再试~~~'
  118. })
  119. }
  120. })
  121. },
  122. fail: (err) => {
  123. that.isShow = false;
  124. }
  125. });
  126. } else {
  127. that.isShow = false;
  128. uni.showToast({
  129. icon: 'none',
  130. title: '更新失败,请稍后再试~~~'
  131. })
  132. }
  133. },
  134. });
  135. this.downloadTask.onProgressUpdate((res) => {
  136. // 下载进度
  137. this.downloadProgress = res.progress
  138. });
  139. },
  140. //安装
  141. installApplications({filePath,success,error}) {
  142. plus.runtime.install(
  143. filePath, {
  144. force: true
  145. },
  146. success,
  147. error
  148. );
  149. }
  150. }
  151. }
  152. </script>
  153. <style lang="scss" scoped>
  154. .shop-update{
  155. z-index: 9999;
  156. position: fixed;
  157. left: 50%; top: 50%; transform: translate(-50%,-50%);
  158. background: #fff;
  159. width: 468rpx;
  160. min-height: 238rpx;
  161. border-radius: 20rpx;
  162. .upgrade {
  163. .content {
  164. .container {
  165. color: #666;
  166. }
  167. .title {
  168. text-align: center;
  169. font-size: 34rpx;
  170. font-weight: bold;
  171. height: 80rpx; line-height: 80rpx;
  172. }
  173. .descriptions {
  174. padding: 0rpx 30rpx;
  175. text-align: center;
  176. font-size: 30rpx;
  177. }
  178. .details,.prpgroess {
  179. padding: 16rpx 46rpx;
  180. box-sizing: border-box;
  181. font-size: 24rpx;
  182. }
  183. .btn-group {
  184. display: flex;
  185. justify-content: center;
  186. align-items: center;
  187. }
  188. .btn-group view {
  189. width: 200rpx;
  190. height: 68rpx;
  191. display: flex;
  192. justify-content: center;
  193. align-items: center;
  194. margin: 14rpx;
  195. font-size: 24rpx;
  196. border-radius: 16rpx;
  197. line-height: 1.5;
  198. }
  199. }
  200. }
  201. }
  202. </style>