DownloadHeader.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <template>
  2. <el-dropdown class="download-header float-left mr-3" ref="downloadmenu" trigger="click" :hide-on-click=false>
  3. <span class="el-dropdown-link">
  4. <span class="badge badge-orange">
  5. <i class="fas fa-download"></i><span v-show="downloadNum !== 0"> {{ downloadNum }}</span>
  6. </span>
  7. </span>
  8. <!--<el-button @click="testbtn">test</el-button>-->
  9. <el-dropdown-menu slot="dropdown" class="downloadlist">
  10. <div v-for="(item, index) in downloads" v-show="!item.delete" :key="item.id" class="downloaditem">
  11. <div class="pb-2"><span class="float-right">{{ item.size }}</span>{{ item.fulltitle }}</div>
  12. <div class="pb-2">
  13. <div class="progress-content" v-if="item.isfinish === false">
  14. <div class="float-right">
  15. <span v-if="item.show">
  16. <i v-show="item.pause" class="far fa-pause-circle text-secondary mr-2" @click="operationDownload('pause', item.id, index)"></i>
  17. <i v-show="item.resume" class="far fa-play-circle text-secondary mr-2" @click="operationDownload('resume', item.id, index)"></i>
  18. <i v-show="item.start" class="far fa-play-circle text-secondary mr-2" @click="operationDownload('start', item.id, index)"></i>
  19. </span>
  20. <i class="fas fa-times text-danger" @click="deleteDownload(item.id, index)"></i>
  21. </div>
  22. <div class="progress-width">
  23. <el-progress :text-inside="true" :stroke-width="18" :percentage="item.percent" status="success"></el-progress>
  24. </div>
  25. </div>
  26. <div class="progress-content" v-else>
  27. <div class="float-right"><i class="fas fa-times text-danger" @click="deleteDownload(item.id, index)"></i></div>
  28. <div class="progress-width">
  29. <button v-if="item.status === 2" @click="installbtn(item.id, index)" class="btn btn-blue btn-sm btn-lg btn-block">安装软件</button>
  30. <button v-else-if="item.status === 3" class="btn btn-secondary btn-sm btn-lg btn-block">正在安装本软件...</button>
  31. <button v-else-if="item.status === 4" class="btn btn-success btn-sm btn-lg btn-block">已安装</button>
  32. </div>
  33. </div>
  34. </div>
  35. </div>
  36. </el-dropdown-menu>
  37. </el-dropdown>
  38. </template>
  39. <script>
  40. import mixin from '../mixin'
  41. const fs = require('fs-extra')
  42. const path = require('path')
  43. const lodashId = require('lodash-id')
  44. const ffi = require('ffi')
  45. export default {
  46. mixins: [mixin],
  47. data () {
  48. return {
  49. downloadNum: 0,
  50. downloads: ''
  51. }
  52. },
  53. created () {
  54. this.$electron.ipcRenderer.on('downloadtips', (event, msg) => {
  55. let percent = msg.percent
  56. let info = this.$db.read().get('sc_download').getById(msg.id).value()
  57. info.percent = percent
  58. console.log(info)
  59. this.downloads.splice(msg.index, 1, info)
  60. if (!this.checkOnline() && info.pause === true) {
  61. this.operationDownload('pause', msg.id, msg.index)
  62. }
  63. })
  64. this.$db._.mixin(lodashId)
  65. this.downloads = this.$db.read().get('sc_download').value()
  66. this.downloadNum = this.$db.read().get('sc_download').filter({delete: false}).size().value()
  67. this.$electron.ipcRenderer.on('downloadsuccess', (event, msg) => {
  68. let info = this.$db.read().get('sc_download').getById(msg.id).value()
  69. info.isfinish = true
  70. info.show = false
  71. info.pause = false
  72. info.savepath = msg.savepath
  73. info.status = 2
  74. this.$db.read().get('sc_download').getById(msg.id).assign(info).write()
  75. // 通知下载完成
  76. let notification = {
  77. title: '启动器',
  78. body: info.fulltitle + '下载完成'
  79. }
  80. let myNotification = new window.Notification(notification.title, notification)
  81. this.downloads.splice(msg.index, 1, info)
  82. // 下载完成当前后 获取下一个info并启动它,没有则停止
  83. let info2 = this.$db.read().get('sc_download').find({ status: 1 }).value()
  84. if (info2 !== undefined) {
  85. info2.show = true
  86. info2.status = 5
  87. this.$db.read().get('sc_download').getById(info2.id).assign(info2).write()
  88. let index = this.downloads.findIndex(function (item) {
  89. return item.id === info2.id
  90. })
  91. this.downloads.splice(index, 1, info2)
  92. this.operationDownload('start', info2.id, index)
  93. }
  94. myNotification.onclick = () => {
  95. return true
  96. }
  97. })
  98. this.$electron.ipcRenderer.on('InstallResult', (event, item) => {
  99. let info = this.$db.read().get('sc_download').getById(item.id).value()
  100. if (item.status === 400) {
  101. this.$message.error(item.msg)
  102. this.$db.read().get('sc_download').updateById(item.id, { status: 2 }).write()
  103. info.status = 2
  104. this.downloads.splice(item.index, 1, info)
  105. } else {
  106. this.$db.read().get('sc_download').updateById(item.id, { status: 4 }).write()
  107. info.status = 4
  108. this.downloads.splice(item.index, 1, info)
  109. }
  110. })
  111. },
  112. beforeDestroy () {
  113. },
  114. methods: {
  115. checkOnline () {
  116. if (!navigator.onLine) {
  117. this.$message.error('当前网络不可用,无法下载')
  118. return false
  119. } else {
  120. return true
  121. }
  122. },
  123. operationDownload (status, id, index) {
  124. let info = this.$db.read().get('sc_download').getById(id).value()
  125. if (this.checkOnline()) {
  126. if (status === 'start') {
  127. this.$electron.ipcRenderer.send('download', {id: id, index: index})
  128. info.start = false
  129. info.resume = false
  130. info.pause = true
  131. } else if (status === 'resume') {
  132. this.$electron.ipcRenderer.send(status, id)
  133. info.pause = true
  134. info.resume = false
  135. info.start = false
  136. }
  137. }
  138. if (status === 'pause') {
  139. this.$electron.ipcRenderer.send(status, id)
  140. info.pause = false
  141. info.resume = true
  142. info.start = false
  143. }
  144. this.downloads.splice(index, 1, info)
  145. this.$db.read().get('sc_download').getById(id).assign(info).write()
  146. },
  147. deleteDownload (id, index) {
  148. try {
  149. let info = this.$db.read().get('sc_download').getById(id).value()
  150. if (info.status === 5) {
  151. if (info.start === false) {
  152. this.$electron.ipcRenderer.send('cancel', id)
  153. info.show = false
  154. info.start = true
  155. info.pause = false
  156. info.resume = false
  157. }
  158. // 改变下一个数据的下载状态,没有则结束
  159. let info2 = this.$db.read().get('sc_download').find({status: 1}).value()
  160. if (info2 !== undefined) {
  161. info2.show = true
  162. info2.status = 5
  163. this.$db.read().get('sc_download').getById(info2.id).assign(info2).write()
  164. let index2 = this.downloads.findIndex(function (item) {
  165. return item.id === info2.id
  166. })
  167. this.downloads.splice(index2, 1, info2)
  168. // this.operationDownload('start', info2.id, index)
  169. }
  170. }
  171. info.delete = true
  172. info.status = 6
  173. this.$db.read().get('sc_download').getById(id).assign(info).write()
  174. this.downloads.splice(index, 1, info)
  175. // this.$db.read().get('sc_download').removeById(id).write()
  176. // this.downloads.splice(index, 1)
  177. this.downloadNum = this.$db.read().get('sc_download').filter({delete: false}).size().value()
  178. // fs.removeSync(info.savepath)
  179. } catch (err) {
  180. console.log(err)
  181. }
  182. },
  183. adddownload (downID) {
  184. if (this.checkOnline()) {
  185. this.$refs.downloadmenu.show()
  186. let softwarejson = path.join('data/sc_software.json')
  187. let downlist = fs.readJsonSync(softwarejson).sc_down
  188. let downinfo = downlist.find(function (item) {
  189. return item.down_id === downID
  190. })
  191. delete downinfo.regedits
  192. delete downinfo.versionDesc
  193. downinfo.isfinish = false
  194. downinfo.percent = 0
  195. downinfo.savepath = downinfo.down_url.substring(downinfo.down_url.lastIndexOf('/') + 1)
  196. downinfo.addtime = Date.parse(new Date()) / 1000
  197. downinfo.show = false
  198. downinfo.pause = false
  199. downinfo.resume = false
  200. downinfo.start = true
  201. downinfo.delete = false
  202. downinfo.status = 1
  203. // 若产品不存在则插入到数据库sc_productData中,并隐藏
  204. let productinfo = this.$db.read().get('sc_productData').find({ product_id: downinfo.product_id }).value()
  205. if (productinfo === undefined) {
  206. let productlist = fs.readJsonSync(softwarejson).sc_product
  207. let scproductinfo = productlist.find(function (item) {
  208. return item.product_id === downinfo.product_id
  209. })
  210. scproductinfo.addtime = Date.parse(new Date()) / 1000
  211. scproductinfo.isshow = false
  212. productinfo = this.$db.read().get('sc_productData').insert(scproductinfo).write()
  213. }
  214. downinfo.pid = productinfo.id
  215. let info = this.$db.read().get('sc_download').insert(downinfo).write()
  216. this.downloads.push(downinfo)
  217. this.downloadNum = this.$db.read().get('sc_download').filter({delete: false}).size().value()
  218. // 如果downloads中没有未下载的info,就让新增的info进入下载过程
  219. let info2 = this.$db.read().get('sc_download').find({status: 5}).value()
  220. let info3 = this.$db.read().get('sc_download').find({status: 1}).value()
  221. if (info2 === undefined && info.id === info3.id) {
  222. info.show = true
  223. info.status = 5
  224. this.$db.read().get('sc_download').getById(info.id).assign(info).write()
  225. let index = this.downloads.findIndex(function (item) {
  226. return item.id === info.id
  227. })
  228. this.operationDownload('start', info.id, index)
  229. }
  230. }
  231. },
  232. testbtn () {
  233. // console.log('C:\\Program Files (x86)\\纵横软件\\纵横结算决算计量一体化软件(云版)')
  234. // this.$electron.shell.openItem('C:\\Program Files (x86)\\纵横软件\\纵横结算决算计量一体化软件(云版)')
  235. window.open('http://softmicroindex.smartcost.com.cn/')
  236. },
  237. installbtn (id, index) {
  238. // 安装exe监测,5s后开始监听安装状态
  239. let info = this.$db.read().get('sc_download').getById(id).value()
  240. if (info.status === 2) {
  241. // 判断启动器是否已安装本软件
  242. let exesqlinfo = this.$db.read().get('sc_exeData').find({ regeditName: info.regedit, product_version: info.version }).value()
  243. if (exesqlinfo !== undefined) {
  244. this.$message({
  245. message: `启动器已存在相同版本的 ${info.fulltitle} 软件`,
  246. iconClass: '',
  247. type: 'error'
  248. })
  249. } else {
  250. this.$electron.shell.openItem(info.savepath)
  251. let self = this
  252. setTimeout(function () {
  253. info.status = 3
  254. self.$db.read().get('sc_download').getById(id).assign(info).write()
  255. self.downloads.splice(index, 1, info)
  256. self.watchExeInstall(index, info)
  257. }, 5000)
  258. }
  259. } else {
  260. this.$message({
  261. message: info.fulltitle + ' 正在安装中,请稍后......',
  262. iconClass: '',
  263. type: 'warning'
  264. })
  265. }
  266. },
  267. watchExeInstall (index, info) {
  268. // 监听安装程序目录,不存在则结束监听,进入判断安装是否完成or取消
  269. let self = this
  270. let interval = setInterval(function () {
  271. let result = self.checkExeExist(info.savepath)
  272. if (result === 0) {
  273. clearInterval(interval)
  274. // 判断是否完成还是取消了
  275. self.$electron.ipcRenderer.send('checkExeInstall', { index: index, info: info })
  276. }
  277. }, 3000)
  278. },
  279. checkExeExist (exepath) {
  280. try {
  281. let ffifile = path.join('data/fileInfo.dll')
  282. let libm = ffi.Library(ffifile, {
  283. 'FindProcess': ['int', ['string']]
  284. })
  285. let exerun = libm.FindProcess(exepath)
  286. if (exerun === 0) {
  287. return 0
  288. }
  289. return 1
  290. } catch (err) {
  291. console.error('ffi.Library', err)
  292. }
  293. }
  294. }
  295. }
  296. </script>