App.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. }
  6. },
  7. onLaunch: function() {
  8. setInterval(d => { //定时器,定时去调取聊天未读消息
  9. console.log(uni.getStorageSync('shopToken'),'初始化')
  10. let shopToken = uni.getStorageSync('shopToken')
  11. if (shopToken) {
  12. let shopId = uni.getStorageSync('shopId')
  13. let data={
  14. shopId:shopId
  15. }
  16. this.$Request.get('/shop/ordersChat/selectShopAllUnreadCount',data).then(res => {
  17. if (res.code === 0) {
  18. // let chatCount = res.data
  19. let messageCount = res.data
  20. uni.setStorageSync('messageCount', messageCount)
  21. let num = messageCount
  22. if (num == 0) {
  23. uni.removeTabBarBadge({
  24. index: 2
  25. })
  26. return;
  27. }
  28. uni.setTabBarBadge({
  29. index: 2,
  30. text: num + ""
  31. })
  32. }
  33. });
  34. }
  35. }, 3000);
  36. //#ifdef APP-PLUS
  37. // APP检测更新 具体打包流程可以参考:https://ask.dcloud.net.cn/article/35667
  38. plus.screen.lockOrientation('portrait-primary'); //竖屏正方向锁定
  39. //获取是否热更新过
  40. const updated = uni.getStorageSync('updated'); // 尝试读取storage
  41. let that = this;
  42. if (updated.completed === true) {
  43. // 如果上次刚更新过
  44. // 删除安装包及安装记录
  45. console.log('安装记录被删除,更新成功');
  46. uni.removeSavedFile({
  47. filePath: updated.packgePath,
  48. success: res => {
  49. uni.removeStorageSync('updated');
  50. }
  51. });
  52. } else if (updated.completed === false) {
  53. uni.removeStorageSync('updated');
  54. plus.runtime.install(updated.packgePath, {
  55. force: true
  56. });
  57. uni.setStorage({
  58. key: 'updated',
  59. data: {
  60. completed: true,
  61. packgePath: updated.packgePath
  62. },
  63. success: res => {
  64. console.log('成功安装上次的更新,应用需要重启才能继续完成');
  65. }
  66. });
  67. uni.showModal({
  68. title: '温馨提示',
  69. content: '应用将重启以完成更新',
  70. showCancel: false,
  71. complete: () => {
  72. plus.runtime.restart();
  73. }
  74. });
  75. } else {
  76. //获取当前系统版本信息
  77. plus.runtime.getProperty(plus.runtime.appid, widgetInfo => {
  78. //请求后台接口 解析数据 对比版本
  79. // let data = {
  80. // classify: 1
  81. // }
  82. that.$Request.getT('/app/user/selectNewApp').then(res => {
  83. res = res.data[2];
  84. if (res.wgtUrl && widgetInfo.version < res.version) {
  85. let downloadLink = '';
  86. let androidLink = res.androidWgtUrl;
  87. let iosLink = res.iosWgtUrl;
  88. let ready = false;
  89. //校验是是不是热更新
  90. if (res.wgtUrl.match(RegExp(/.wgt/))) {
  91. // 判断系统类型
  92. if (plus.os.name.toLowerCase() === 'android') {
  93. console.log('安卓系统');
  94. if (androidLink && androidLink !== '#') {
  95. // 我这里默认#也是没有地址,请根据业务自行修改
  96. console.log('发现下载地址');
  97. // 安卓:创建下载任务
  98. if (androidLink.match(RegExp(/.wgt/))) {
  99. console.log('确认wgt热更新包');
  100. downloadLink = androidLink;
  101. ready = true;
  102. } else {
  103. console.log('安卓推荐.wgt强制更新,.apk的强制更新请您自行修改程序');
  104. }
  105. } else {
  106. console.log('下载地址是空的,无法继续');
  107. }
  108. } else {
  109. console.log('苹果系统');
  110. if (iosLink && iosLink !== '#') {
  111. // 我这里默认#也是没有地址,请根据业务自行修改
  112. console.log('发现下载地址');
  113. // 苹果(A):进行热更新(如果iosLink是wgt更新包的下载地址)判断文件名中是否含有.wgt
  114. if (iosLink.match(RegExp(/.wgt/))) {
  115. console.log('确认wgt热更新包');
  116. downloadLink = iosLink;
  117. ready = true;
  118. } else {
  119. console.log('苹果只支持.wgt强制更新');
  120. }
  121. } else {
  122. console.log('下载地址是空的,无法继续');
  123. }
  124. }
  125. if (ready) {
  126. console.log('任务开始');
  127. let downloadTask = uni.downloadFile({
  128. url: downloadLink,
  129. success: res => {
  130. if (res.statusCode === 200) {
  131. // 保存下载的安装包
  132. console.log('保存安装包');
  133. uni.saveFile({
  134. tempFilePath: res.tempFilePath,
  135. success: res => {
  136. const packgePath = res
  137. .savedFilePath;
  138. // 保存更新记录到stroage,下次启动app时安装更新
  139. uni.setStorage({
  140. key: 'updated',
  141. data: {
  142. completed: false,
  143. packgePath: packgePath
  144. },
  145. success: () => {
  146. console
  147. .log(
  148. '成功保存记录'
  149. );
  150. }
  151. });
  152. // 任务完成,关闭下载任务
  153. console.log(
  154. '任务完成,关闭下载任务,下一次启动应用时将安装更新'
  155. );
  156. downloadTask.abort();
  157. downloadTask = null;
  158. }
  159. });
  160. }
  161. }
  162. });
  163. } else {
  164. console.log('下载地址未准备,无法开启下载任务');
  165. }
  166. } else {
  167. //不是热更新是在线更新 校验是否强制升级
  168. if (res.method == 'true') {
  169. uni.showModal({
  170. showCancel: false,
  171. confirmText: '立即更新',
  172. title: '发现新版本',
  173. content: res.des,
  174. success: res => {
  175. if (res.confirm) {
  176. that.$queue.showLoading('下载中...');
  177. if (uni.getSystemInfoSync().platform ==
  178. 'android') {
  179. uni.downloadFile({
  180. url: androidLink,
  181. success: downloadResult => {
  182. if (downloadResult
  183. .statusCode ===
  184. 200) {
  185. plus.runtime
  186. .install(
  187. downloadResult
  188. .tempFilePath, {
  189. force: false
  190. },
  191. d => {
  192. console
  193. .log(
  194. 'install success...'
  195. );
  196. plus.runtime
  197. .restart();
  198. },
  199. e => {
  200. console
  201. .error(
  202. 'install fail...'
  203. );
  204. }
  205. );
  206. }
  207. }
  208. });
  209. }
  210. if (uni.getSystemInfoSync().platform ==
  211. 'ios') {
  212. plus.runtime.openURL(iosLink, function(
  213. res) {});
  214. }
  215. } else if (res.cancel) {
  216. console.log('取消');
  217. }
  218. }
  219. });
  220. } else {
  221. uni.showModal({
  222. title: '发现新版本',
  223. confirmText: '立即更新',
  224. cancelText: '下次更新',
  225. content: res.des,
  226. success: res => {
  227. if (res.confirm) {
  228. that.$queue.showLoading('下载中...');
  229. if (uni.getSystemInfoSync().platform ==
  230. 'android') {
  231. uni.downloadFile({
  232. url: androidLink,
  233. success: downloadResult => {
  234. if (downloadResult
  235. .statusCode ===
  236. 200) {
  237. plus.runtime
  238. .install(
  239. downloadResult
  240. .tempFilePath, {
  241. force: false
  242. },
  243. d => {
  244. console
  245. .log(
  246. 'install success...'
  247. );
  248. plus.runtime
  249. .restart();
  250. },
  251. e => {
  252. console
  253. .error(
  254. 'install fail...'
  255. );
  256. }
  257. );
  258. }
  259. }
  260. });
  261. }
  262. if (uni.getSystemInfoSync().platform ==
  263. 'ios') {
  264. plus.runtime.openURL(iosLink, function(
  265. res) {});
  266. }
  267. } else if (res.cancel) {
  268. console.log('取消');
  269. }
  270. }
  271. });
  272. }
  273. }
  274. }
  275. });
  276. });
  277. }
  278. //#endif
  279. },
  280. onShow: function() {
  281. console.log('App Show')
  282. let updateManager = uni.getUpdateManager();
  283. if (!updateManager) {
  284. return;
  285. } else {
  286. //新版本更新
  287. if (uni.canIUse("getUpdateManager")) {
  288. //判断当前微信版本是否支持版本更新
  289. updateManager.onCheckForUpdate(function (res) {
  290. if (res.hasUpdate) {
  291. // 请求完新版本信息的回调
  292. updateManager.onUpdateReady(function () {
  293. //新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  294. updateManager.applyUpdate();
  295. // uni.showModal({
  296. // title: "更新提示",
  297. // content: "新版本已经准备好,是否重启应用?",
  298. // success: function (res) {
  299. // if (res.confirm) {
  300. // // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  301. // updateManager.applyUpdate();
  302. // }
  303. // },
  304. // });
  305. });
  306. // updateManager.onUpdateFailed(function () {
  307. // uni.showModal({
  308. // // 新的版本下载失败
  309. // title: "已经有新版本了哟~",
  310. // content:
  311. // "新版本已经上线啦~,请您删除当前小程序,到微信 “发现-小程序” 页,重新搜索打开哦~",
  312. // });
  313. // });
  314. } else {
  315. }
  316. });
  317. } else {
  318. uni.showModal({
  319. // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
  320. title: "溫馨提示",
  321. content:
  322. "当前微信版本过低,部分功能无法使用,请升级到最新微信版本后重试。",
  323. });
  324. }
  325. }
  326. this.$Request.get('/app/common/type/323').then(res => {
  327. if (res.code == 0) {
  328. // #ifdef MP-WEIXIN
  329. this.$queue.setData('XCXIsSelect', res.data.value);
  330. // #endif
  331. // #ifndef MP-WEIXIN
  332. this.$queue.setData('XCXIsSelect', '是');
  333. // #endif
  334. }
  335. });
  336. // #ifdef APP-PLUS
  337. if (uni.getSystemInfoSync().platform == 'android') {
  338. let clientid = plus.push.getClientInfo().clientid;
  339. let userId = this.$queue.getData('userId');
  340. if (userId) {
  341. this.$Request.postT('/user/updateShopClientId?clientId=' + clientid).then(
  342. res => {});
  343. }
  344. }
  345. //#endif
  346. },
  347. methods: {
  348. },
  349. onHide: function() {
  350. console.log('App Hide')
  351. },
  352. }
  353. </script>
  354. <style lang="scss">
  355. /*每个页面公共css */
  356. @import "uview-ui/index.scss";
  357. @import 'components/colorui/main.css';
  358. @import 'components/colorui/icon.css';
  359. </style>