App.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. progress: 0
  6. }
  7. },
  8. onLaunch: function() {
  9. console.log('App Launch')
  10. // #ifdef APP-PLUS
  11. this.checkUpdate();
  12. // #endif
  13. const value = uni.getStorageSync('launchFlag');
  14. if (!value) {
  15. // launchFlag=true直接跳转到首页
  16. uni.reLaunch({
  17. url: '/pages/index/index'
  18. });
  19. }
  20. uni.$on(["login_succeeded", "logout"], () => {
  21. if (this.$store.state.user.group_id === 2) {
  22. let iconIndex = 2
  23. // #ifdef H5
  24. iconIndex = 3
  25. // #endif
  26. uni.setTabBarItem({
  27. index: iconIndex,
  28. text: '商品库',
  29. pagePath: '/pages/product/my-product-tabbar',
  30. iconPath: "static/images/kan.png",
  31. selectedIconPath: "static/images/kan_selected.png",
  32. })
  33. uni.setTabBarItem({
  34. index: 1,
  35. text: '待发货'
  36. })
  37. } else {
  38. let iconIndex = 2
  39. // #ifdef H5
  40. iconIndex = 3
  41. // #endif
  42. uni.setTabBarItem({
  43. index: iconIndex,
  44. text: '购物车',
  45. pagePath: '/pages/shopping-cart/shopping-cart',
  46. iconPath: "static/images/kan.png",
  47. selectedIconPath: "static/images/kan_selected.png",
  48. })
  49. uni.setTabBarItem({
  50. index: 1,
  51. text: '待收货'
  52. })
  53. }
  54. });
  55. uni.onTabBarMidButtonTap(() => {
  56. if (this.$store.state.user.token) {
  57. uni.chooseImage({
  58. count: 1,
  59. sourceType: ['camera']
  60. })
  61. } else {
  62. uni.navigateTo({
  63. url: "pages/user/login"
  64. })
  65. }
  66. })
  67. uni.$on("refresh_userinfo", () => {
  68. this.$http.get({
  69. url: "/user/current",
  70. success: (res) => {
  71. this.$store.dispatch("user/save", res.data.data)
  72. }
  73. })
  74. })
  75. },
  76. onShow: function() {
  77. console.log('App Show')
  78. this.$store.commit("user/load")
  79. this.$store.dispatch("cart/load");
  80. uni.$emit("login_succeeded");
  81. },
  82. onHide: function() {
  83. console.log('App Hide')
  84. },
  85. methods: {
  86. checkUpdate() {
  87. this.$http.get({
  88. url: '/update',
  89. success: (res) => {
  90. let temp = [];
  91. let last_version = res.data.data.versionName.substring(1);
  92. temp = last_version.split(".");
  93. let last_version_code = parseInt(temp[0]) * 10000 + parseInt(temp[1]) * 100 + parseInt(
  94. temp[2]);
  95. let cur_version = plus.runtime.version;
  96. temp = cur_version.split(".");
  97. let cur_version_code = parseInt(temp[0]) * 10000 + parseInt(temp[1]) * 100 + parseInt(
  98. temp[2]);
  99. console.log(`最新版本:${last_version} 当前版本${cur_version}`)
  100. // uni.showModal({
  101. // content:`${最新版本:${last_version} ${last_version_code} 当前版本${cur_version} ${cur_version_code} 当前环境 ${process.env.NODE_ENV}`
  102. // })
  103. if (last_version_code > cur_version_code || process.env.NODE_ENV === 'development') {
  104. uni.showModal({
  105. title: "版本更新",
  106. content: `检测到新版本,是否更新?`,
  107. confirmText: "立即更新",
  108. success: (e) => {
  109. if (e.confirm) {
  110. console.log('用户点击确定');
  111. if (plus.os.name === 'iOS') {
  112. plus.runtime.openURL(
  113. "https://apps.apple.com/cn/app/id387682726")
  114. }
  115. if (plus.os.name === 'Android') {
  116. let downloadTask = uni.downloadFile({
  117. url: this.$http.baseUrl + res.data.data
  118. .apk_file,
  119. success: (downloadResult) => {
  120. if (downloadResult.statusCode ===
  121. 200) {
  122. plus.runtime.install(
  123. downloadResult
  124. .tempFilePath, {
  125. force: false
  126. },
  127. function() {
  128. console.log(
  129. 'install success...'
  130. );
  131. plus.runtime
  132. .restart();
  133. },
  134. function(e) {
  135. console.error(
  136. 'install fail...'
  137. );
  138. });
  139. }
  140. }
  141. });
  142. let loading = plus.nativeUI.showWaiting("已下载0%", {
  143. width: "30%",
  144. height: "15%"
  145. })
  146. downloadTask.onProgressUpdate((e) => {
  147. // console.log('下载进度' + e.progress);
  148. // console.log('已经下载的数据长度' + e.totalBytesWritten);
  149. // console.log('预期需要下载的数据总长度' + e.totalBytesExpectedToWrite);
  150. if (e.progress % 5 === 0) {
  151. loading.setTitle("已下载" + e.progress +
  152. "%")
  153. }
  154. if (e.progress === 100) {
  155. loading.close();
  156. }
  157. })
  158. }
  159. } else if (e.cancel) {
  160. console.log('用户点击取消');
  161. }
  162. }
  163. })
  164. }
  165. }
  166. });
  167. }
  168. }
  169. }
  170. </script>
  171. <style>
  172. page {
  173. background: #F8F8F8;
  174. }
  175. uni-button:after {
  176. border: none;
  177. }
  178. @font-face {
  179. font-family: "Roboto";
  180. src: url(static/Roboto-Regular.ttf);
  181. }
  182. @font-face {
  183. font-family: "Roboto-Bold";
  184. src: url(static/Roboto-Bold.ttf);
  185. }
  186. /*每个页面公共css */
  187. ::-webkit-scrollbar {
  188. display: none;
  189. width: 0 !important;
  190. height: 0 !important;
  191. -webkit-appearance: none;
  192. background: transparent;
  193. }
  194. </style>