update.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <div>
  3. <div :style="{background: '#fafafa', padding: '5px 20px', lineHeight: '34px'}" class="footer-between">
  4. <div>
  5. <a href="https://www.xingyousoft.com"><span class="footer-menu"><i class="iconfont iconx-wangzhi"></i>官网</span></a>
  6. <el-popover placement="bottom" width="200" trigger="click" popper-class="wei-dialog">
  7. <a slot="reference"><span class="footer-menu"><i class="iconfont iconx-qiyeweixin"></i>客服</span></a>
  8. <div class="api">
  9. <img src="../assets/image/qrcode.png" style="width: 100%;"/>
  10. </div>
  11. </el-popover>
  12. </div>
  13. <div>
  14. <span v-if="linkList.length > 0" class="footer-menu" style="padding: 0;cursor:default;"><i class="iconfont iconx-lianjie"></i>应用推荐:</span>
  15. <a v-for="(item, key) in linkList" :key="key" @click="openUrl(item.url)"><span class="footer-menu">{{item.title}}</span></a>
  16. </div>
  17. <div style="cursor:pointer;position: relative;font-size: 13px;" @click="updateSoft()">
  18. <span>
  19. <el-badge is-dot v-if="isUpdate">
  20. <i class="el-icon-refresh-right" style="font-size: 20px;"></i>
  21. </el-badge>
  22. 版本:v{{softInfo.version}}
  23. </span>
  24. </div>
  25. </div>
  26. <!-- 下载进度 -->
  27. <el-dialog title="软件更新" :visible.sync="isShow" width="400px" :show-close="false">
  28. <div style="text-align: center;">
  29. <div class="demo-Circle-custom">
  30. 文件大小:<i>{{$utils.handleSize(Number(softInfo.totalSize))}}</i>
  31. <p>下载进度</p>
  32. </div>
  33. <el-progress type="circle" :stroke-width="10" :percentage="Number(softInfo.percent)" :width="220"></el-progress>
  34. <div>
  35. <el-button type="primary" style="margin-top: 30px;" @click="isShow = false">后台下载</el-button>
  36. </div>
  37. </div>
  38. </el-dialog>
  39. <!-- 下载完成 -->
  40. <el-dialog title="下载完成,是否退出并安装" :visible.sync="isFinished" width="400px" :close-on-click-modal="false">
  41. <p>文件下载位置{{softdownloadDir}}</p>
  42. <span slot="footer" class="dialog-footer">
  43. <el-button @click="isFinished = false">取 消</el-button>
  44. <el-button type="primary" @click="execSoft()">确 定</el-button>
  45. </span>
  46. </el-dialog>
  47. </div>
  48. </template>
  49. <script>
  50. import os from 'os'
  51. import fs from 'fs'
  52. import request from 'request'
  53. import { ipcRenderer } from 'electron'
  54. import pjson from '/package.json'
  55. export default {
  56. name: "updateRef",
  57. data(){
  58. return {
  59. softInfo: {
  60. version: pjson.version,
  61. name: pjson.softInfo.downloadName,
  62. percent: 0,
  63. downloadedSize: 0,
  64. totalSize: 0
  65. },
  66. softdownloadDir: os.userInfo().homedir+"\\Downloads", // 下载路径
  67. isShow: this.showDowload,
  68. isFinished: this.dowloadFinish,
  69. isUpdate: false,
  70. linkList: [], // 友情链接
  71. softdownloadPath: '',
  72. }
  73. },
  74. props: {
  75. showDowload: {
  76. type: Boolean,
  77. required: true,
  78. default: false
  79. },
  80. dowloadFinish: {
  81. type: Boolean,
  82. required: true,
  83. default: false
  84. },
  85. },
  86. mounted() {
  87. // 友情连接
  88. this.$http.post(this.$api.target + '/api/api/m_position_list', { group_id: pjson.softInfo.linkId }).then((response) => {
  89. if (!response.data.error) {
  90. this.linkList = response.data.result.list;
  91. }
  92. })
  93. },
  94. methods: {
  95. // 打开浏览器
  96. openUrl(url){
  97. const { shell } = require('electron');
  98. shell.openExternal(url);
  99. },
  100. // 检查更新
  101. updateSoft(flag){ // flag 软件打开时检测是否更新,true则静默不弹出最新版本提示
  102. if(Number(this.softInfo.percent) > 0){
  103. this.isShow = true;
  104. }else{
  105. this.$http.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
  106. this.$http.get(pjson.softInfo.downloadPrefix + '/' + pjson.name + "/version.json?"+Math.random()).then((response) => {
  107. let getResult = response.data || {};
  108. let currentVersion = this.softInfo.version.split('.');
  109. let updateVersion = getResult.v.split('.');
  110. currentVersion.map((item, index) => {
  111. currentVersion[index] = Number(item);
  112. })
  113. updateVersion.map((item, index) => {
  114. updateVersion[index] = Number(item);
  115. })
  116. let result1 = updateVersion[0] > currentVersion[0];
  117. let result2 = (updateVersion[0] == currentVersion[0]) && (updateVersion[1] > currentVersion[1]);
  118. let result3 = (updateVersion[0] == currentVersion[0]) && (updateVersion[1] == currentVersion[1]) && (updateVersion[2] > currentVersion[2]);
  119. let result4 = (updateVersion[0] == currentVersion[0]) && (updateVersion[1] == currentVersion[1]) && (updateVersion[2] == currentVersion[2]) && (updateVersion[3] > currentVersion[3]);
  120. if(result1 || result2 || result3 || result4){
  121. this.isUpdate = true;
  122. let updateContent = getResult.c || '';
  123. let html = '<div>检测到有新版本v'+getResult.v+',是否更新最新版本?</div><div>'+updateContent+'</div>';
  124. this.$confirm(html, '应用更新', {
  125. dangerouslyUseHTMLString: true,
  126. confirmButtonText: '立即更新',
  127. type: 'warning'
  128. }).then(() => {
  129. this.softdownloadPath = this.softdownloadDir+'/'+ pjson.name + '_XY.exe';
  130. this.downloadFile(pjson.softInfo.downloadPrefix + '/' + pjson.name + '/' + pjson.name + "_XY.exe", this.softdownloadPath);
  131. }).catch(() => {
  132. });
  133. }else{
  134. this.isUpdate = false;
  135. if(!flag){
  136. this.$message({message: '当前已经是最新版本',type: 'success', center: true});
  137. }
  138. }
  139. }).catch(function(error){
  140. console.log(error);
  141. });
  142. }
  143. },
  144. // 下载文件
  145. downloadFile(file_url, targetPath){
  146. let _this = this;
  147. let received_bytes = 0;
  148. let total_bytes = 0;
  149. let isError = false;
  150. let req = request({
  151. method: 'GET', uri: file_url, strictSSL: false
  152. });
  153. let out = fs.createWriteStream(targetPath);
  154. req.pipe(out);
  155. req.on('response', function ( data ) {
  156. total_bytes = parseInt(data.headers['content-length']);
  157. const status = data.statusCode
  158. if (status < 200 || status >= 300) {
  159. _this.$message({showClose: true,message: '网络异常,请稍后重试!',type: 'warning'});
  160. isError = true;
  161. }else if(isNaN(total_bytes)){
  162. _this.$message({showClose: true,message: '网络异常,请稍后重试!',type: 'warning'});
  163. isError = true;
  164. }else{
  165. _this.isShow = true;
  166. isError = false;
  167. }
  168. });
  169. req.on('data', function(chunk) {
  170. received_bytes += chunk.length;
  171. _this.showProgress(received_bytes, total_bytes);
  172. });
  173. req.on('end', function() {
  174. if(!isError){
  175. _this.softInfo.percent = 100;
  176. setTimeout(()=>{
  177. _this.isFinished = true;
  178. _this.isShow = false;
  179. _this.softInfo.percent = 0;
  180. }, 1000)
  181. }else{
  182. _this.isFinished = false;
  183. _this.softInfo.percent = 0;
  184. if(fs.existsSync(targetPath)){
  185. fs.unlinkSync(targetPath);
  186. }
  187. }
  188. });
  189. },
  190. // 显示进度
  191. showProgress(received, total){
  192. let percentage = (received * 100) / total;
  193. let temp = percentage - this.softInfo.percent;
  194. if(Number(temp) > 1.5){
  195. this.softInfo.percent = Number(percentage).toFixed(2);
  196. }
  197. this.softInfo.downloadedSize = Number(received);
  198. this.softInfo.totalSize = Number(total);
  199. // console.log(percentage + "% | " + received + " bytes out of " + total + " bytes.");
  200. },
  201. // 安装
  202. execSoft(){
  203. const exec = require('child_process').exec;
  204. exec(this.softdownloadPath, (err, data) => {
  205. if (err) {
  206. throw err;
  207. } else { // 完成
  208. this.isFinished = false;
  209. }
  210. });
  211. }
  212. },
  213. }
  214. </script>
  215. <style>
  216. .wei-dialog{
  217. padding: 0 !important;
  218. }
  219. </style>