You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. const update ={
  2. namespaced:true,
  3. state: {
  4. downloadTask: '',
  5. device: 'android', //设备环境
  6. progress: {}, //下载进度
  7. },
  8. mutations: {
  9. setDevice(state,device)
  10. {
  11. state.device = device
  12. },
  13. downloadAPP(state, data) {
  14. // const param = {
  15. // android_link:'http://192.168.1.190:8068/api/version/download?id=89'
  16. // }
  17. var wgtUrl = 'http://192.168.1.50:8068/api/version/download?id='+ data.id;
  18. //判断安卓ios,判断是否第一次更包,整包更新还是资源包更新,只做ios?安卓也可以,统一一个方法,如果要做安卓,在这里做判断条件即可
  19. // var wgtUrl = 'http://godee.cdn.dtbctech.com/_UNI_6FC15A3.wgt'
  20. // if (state.device == 'ios') {
  21. // console.log('ios')
  22. // if (!wgtUrl) {
  23. // wgtUrl = data.ios_link;
  24. // }
  25. // }
  26. plus.nativeUI.showWaiting("下载更新文件...");
  27. state.downloadTask = uni.downloadFile({
  28. url: wgtUrl,
  29. success: (res) => {
  30. console.log('文件下载信息',res)
  31. if (res.statusCode === 200) {
  32. plus.nativeUI.showWaiting("安装更新文件...");
  33. plus.runtime.install(res.tempFilePath, {force:true}, function() {
  34. plus.nativeUI.closeWaiting();
  35. plus.nativeUI.alert("应用资源更新完成!", function() {
  36. plus.runtime.restart();
  37. });
  38. }, function(e) {
  39. plus.nativeUI.closeWaiting();
  40. plus.nativeUI.alert("安装更新文件失败[" + e.code + "]:" + e.message);
  41. if (e.code == 10) {
  42. alert('请清除临时目录');
  43. }
  44. });
  45. // commit('setDevice','android')
  46. // commit('installWgt', res.tempFilePath); //安装wgt包
  47. }
  48. },
  49. fail: (res) => {
  50. plus.nativeUI.alert("下载失败!");
  51. }
  52. });
  53. state.downloadTask.onProgressUpdate((res) => {
  54. if (state.progress.progress < res.progress) {
  55. state.progress = res
  56. }
  57. });
  58. },
  59. installWgt(state, path) {
  60. plus.nativeUI.showWaiting("安装更新文件...");
  61. plus.runtime.install(path, {force:true}, function() {
  62. plus.nativeUI.closeWaiting();
  63. plus.nativeUI.alert("应用资源更新完成!", function() {
  64. plus.runtime.restart();
  65. });
  66. }, function(e) {
  67. plus.nativeUI.closeWaiting();
  68. plus.nativeUI.alert("安装更新文件失败[" + e.code + "]:" + e.message);
  69. if (e.code == 10) {
  70. alert('请清除临时目录');
  71. }
  72. });
  73. },
  74. },
  75. actions: {
  76. setDevice({commit},device)
  77. {
  78. commit('setDevice',device)
  79. },
  80. updateApp({commit}, is_force) {
  81. // is_force在点击事件的时候传,0或1,分辨是否弹出提示信息
  82. // let the_date = new Date().toDateString();//利用时间戳更新缓存,看实际项目需要
  83. // console.log('时间',the_date)
  84. let device = uni.getSystemInfoSync().platform; //判断设备
  85. let type = 2;//资源包更新
  86. let json_path = '';
  87. if (device.toLowerCase() == 'android') {
  88. console.log('安卓?')
  89. json_path = 'android'
  90. } else {
  91. console.log('ios?')
  92. json_path = 'ios'
  93. // return;//如果不需要上ios
  94. }
  95. commit('setDevice',json_path)
  96. console.log('appid',plus.runtime.appid)
  97. console.log('version',plus.runtime.version)
  98. console.log('device',plus.device.imei)
  99. // state.device = json_path;
  100. plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
  101. uni.request({
  102. // url: 'http://139.159.237.94:8068/api/version/getnewversion', //检查更新的服务器地址
  103. url: 'http://192.168.1.50:8068/api/version/getnewversion', //检查更新的服务器地址
  104. data: {
  105. type:type,
  106. appid: plus.runtime.appid,
  107. //version_id: plus.runtime.version,
  108. //imei: plus.device.imei
  109. //这里只要是针对大后台,多个app管理配置传参
  110. },
  111. success: (result) => {
  112. console.log(widgetInfo)
  113. var data = result.data.update_list;
  114. console.log(data)
  115. // commit('downloadAPP', param)
  116. let cur_v = '1';
  117. let pass_v = '0';
  118. try {
  119. cur_v = parseInt(data.fileversionid.split('.').join('')); //转换为number类型进行比较,后台返回的最新版本号
  120. if (isNaN(cur_v)) {
  121. cur_v = 1;
  122. }
  123. pass_v = parseInt(widgetInfo.version.split('.').join('')); //app本地版本号
  124. if (isNaN(pass_v)) {
  125. pass_v = 0;
  126. }
  127. console.log(cur_v)
  128. console.log(pass_v)
  129. } catch (e) {}
  130. if (cur_v > pass_v && data.filename) {
  131. uni.showModal({
  132. title: '更新提示',
  133. content: data.msg || '检测到有数据包更新,是否进行更新?', //可后台配置更新文字返回显示:data.msg
  134. confirmText: '立即进行',
  135. success: function(res) {
  136. if (res.confirm) {
  137. commit('downloadAPP', data)
  138. } else if (res.cancel) {}
  139. }
  140. });
  141. } else {
  142. if (is_force) {
  143. uni.showToast({
  144. title: '当前已是最新版本',
  145. icon: 'none'
  146. })
  147. }
  148. }
  149. },
  150. fail(e) {
  151. console.error(e);
  152. if (e) {
  153. uni.showToast({
  154. title: '更新失败,请联系客服!',
  155. icon: 'none',
  156. })
  157. }
  158. }
  159. });
  160. });
  161. },
  162. }
  163. }
  164. export default update