setting.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <view class="adminSetting">
  3. <navigation-bar-view ref="nav"></navigation-bar-view>
  4. <view class="group">
  5. <view class="label">
  6. <text>当前版本</text>
  7. <view class="more" style="color: #007AFF;" @click="uploadVersion" v-if="version">
  8. <text>有新版本</text>
  9. </view>
  10. <view class="more" v-else>
  11. <text>v{{$config.version}}</text>
  12. </view>
  13. </view>
  14. <view class="label" @click="clearCache">
  15. <text>清除缓存</text>
  16. <view class="more">
  17. <text>{{fileSizeString}}</text>
  18. <image class="arrow" src="../../static/images/icon-arrow-right-grey.png"></image>
  19. </view>
  20. </view>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. import navigationBarView from '../../components/navigationBarView.vue';
  26. export default {
  27. components: {
  28. navigationBarView
  29. },
  30. data() {
  31. return {
  32. body: {},
  33. data: {},
  34. version: '',
  35. fileSizeString: '',
  36. }
  37. },
  38. onShow() {
  39. this.getVersion();
  40. },
  41. methods: {
  42. getVersion() {
  43. let sn = uni.getStorageSync('sn');
  44. this.$api.getCurrentVersion({
  45. deviceSn: sn
  46. }).then(res => {
  47. if (this.$config.version != res.version) {
  48. this.version = res.url;
  49. } else {
  50. this.version = ''
  51. }
  52. })
  53. },
  54. formatSize() {
  55. let that = this;
  56. plus.cache.calculate(function(size) {
  57. let sizeCache = parseInt(size);
  58. if (sizeCache == 0) {
  59. that.fileSizeString = "0B";
  60. } else if (sizeCache < 1024) {
  61. that.fileSizeString = sizeCache + "B";
  62. } else if (sizeCache < 1048576) {
  63. that.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
  64. } else if (sizeCache < 1073741824) {
  65. that.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
  66. } else {
  67. that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
  68. }
  69. });
  70. },
  71. clearCache() {
  72. let that = this;
  73. let os = plus.os.name;
  74. if (os == 'Android') {
  75. let main = plus.android.runtimeMainActivity();
  76. let sdRoot = main.getCacheDir();
  77. let files = plus.android.invoke(sdRoot, "listFiles");
  78. let len = files.length;
  79. for (let i = 0; i < len; i++) {
  80. let filePath = '' + files[i]; // 没有找到合适的方法获取路径,这样写可以转成文件路径
  81. plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
  82. if (entry.isDirectory) {
  83. entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录
  84. uni.showToast({
  85. title: '缓存清理完成',
  86. duration: 2000
  87. });
  88. that.formatSize(); // 重新计算缓存
  89. }, function(e) {
  90. console.log(e.message)
  91. });
  92. } else {
  93. entry.remove();
  94. }
  95. }, function(e) {
  96. console.log('文件路径读取失败')
  97. });
  98. }
  99. }
  100. },
  101. uploadVersion() {
  102. uni.showLoading({
  103. title: '更新中……'
  104. })
  105. console.log('ssaaa', this.version);
  106. const downloadTask = uni.downloadFile({ //执行下载
  107. url: this.version, //下载地址
  108. success: downloadResult => { //下载成功
  109. uni.hideLoading();
  110. if (downloadResult.statusCode == 200) {
  111. uni.showModal({
  112. title: '',
  113. content: '更新成功,确定现在重启吗?',
  114. confirmText: '重启',
  115. confirmColor: '#EE8F57',
  116. success: function(res) {
  117. if (res.confirm == true) {
  118. plus.runtime.install( //安装
  119. downloadResult.tempFilePath, {
  120. force: true
  121. },
  122. function(res) {
  123. utils.showToast('更新成功,重启中');
  124. plus.runtime.restart();
  125. }
  126. );
  127. }
  128. }
  129. });
  130. }
  131. },
  132. fail: err => {
  133. console.log('下载错误', err);
  134. }
  135. });
  136. downloadTask.onProgressUpdate((res) => {
  137. console.log('下载进度' + res.progress);
  138. console.log('已经下载的数据长度' + res.totalBytesWritten);
  139. console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
  140. // 测试条件,取消下载任务。
  141. // if (res.progress == 100) {
  142. // downloadTask.abort();
  143. // }
  144. });
  145. },
  146. }
  147. }
  148. </script>
  149. <style lang="scss">
  150. .adminSetting {
  151. min-height: 100vh;
  152. background-color: #f8f8f8;
  153. .group {
  154. border-radius: 2.7vw;
  155. margin: 2.7vw 0 0;
  156. background: #FFFFFF;
  157. padding: 0 4.1vw;
  158. .label {
  159. display: flex;
  160. align-items: center;
  161. height: 13.2vw;
  162. font-size: 4vw;
  163. border-bottom: 2rpx solid #F8F8F8;
  164. .line {
  165. width: 1.1vw;
  166. height: 4vw;
  167. border-radius: 1.1vw;
  168. background-color: #FDD302;
  169. margin-right: 2.1vw;
  170. }
  171. text {
  172. flex: 1;
  173. }
  174. .more {
  175. display: flex;
  176. align-items: center;
  177. font-weight: 300;
  178. font-size: 3.7vw;
  179. color: #333333;
  180. .arrow {
  181. width: 2vw;
  182. height: 3.6vw;
  183. margin-left: 2vw;
  184. }
  185. }
  186. }
  187. }
  188. }
  189. </style>