App.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <script>
  2. /**
  3. * vuex管理登录状态,具体可以参考官方登录模板示例
  4. */
  5. import { mapMutations } from "vuex";
  6. import APPUpdate from "@/plugins/APPUpdate";
  7. import { getClipboardData } from "@/js_sdk/h5-copy/h5-copy.js";
  8. import config from "@/config/config";
  9. // 悬浮球
  10. export default {
  11. data() {
  12. return {
  13. config,
  14. };
  15. },
  16. /**
  17. * 监听返回
  18. */
  19. onBackPress(e) {
  20. if (e.from == "backbutton") {
  21. let routes = getCurrentPages();
  22. let curRoute = routes[routes.length - 1].options;
  23. routes.forEach((item) => {
  24. if (
  25. item.route == "pages/tabbar/cart/cartList" ||
  26. item.route.indexOf("pages/product/goods") != -1
  27. ) {
  28. uni.redirectTo({
  29. url: item.route,
  30. });
  31. }
  32. });
  33. if (curRoute.addId) {
  34. uni.reLaunch({
  35. url: "/pages/tabbar/cart/cartList",
  36. });
  37. } else {
  38. uni.navigateBack();
  39. }
  40. return true; //阻止默认返回行为
  41. }
  42. },
  43. methods: {
  44. ...mapMutations(["login"]),
  45. },
  46. onLaunch: function () {
  47. // #ifdef APP-PLUS
  48. this.checkArguments(); // 检测启动参数
  49. APPUpdate();
  50. // 重点是以下: 一定要监听后台恢复 !一定要
  51. plus.globalEvent.addEventListener("newintent", (e) => {
  52. this.checkArguments(); // 检测启动参数
  53. });
  54. // #endif
  55. // #ifdef MP-WEIXIN
  56. this.applyUpdateWeChat();
  57. // #endif
  58. },
  59. onShow() {
  60. // #ifndef H5
  61. // this.getClipboard();
  62. // #endif
  63. },
  64. methods: {
  65. /**
  66. * 微信小程序版本提交更新版本 解决缓存问题
  67. */
  68. applyUpdateWeChat() {
  69. const updateManager = uni.getUpdateManager();
  70. updateManager.onCheckForUpdate(function (res) {
  71. // 请求完新版本信息的回调
  72. });
  73. updateManager.onUpdateReady(function (res) {
  74. uni.showModal({
  75. title: "更新提示",
  76. content: "发现新版本,是否重启应用?",
  77. success(res) {
  78. if (res.confirm) {
  79. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  80. updateManager.applyUpdate();
  81. }
  82. },
  83. });
  84. });
  85. updateManager.onUpdateFailed(function (res) {
  86. // 新的版本下载失败
  87. });
  88. },
  89. // TODO 开屏广告 后续优化添加
  90. launch() {
  91. try {
  92. // 获取本地存储中launchFlag标识 开屏广告
  93. const value = uni.getStorageSync("launchFlag");
  94. if (!value) {
  95. // this.$u.route("/pages/index/agreement");
  96. } else {
  97. //app启动时打开启动广告页
  98. var w = plus.webview.open(
  99. "/hybrid/html/advertise/advertise.html",
  100. "本地地址",
  101. {
  102. top: 0,
  103. bottom: 0,
  104. zindex: 999,
  105. },
  106. "fade-in",
  107. 500
  108. );
  109. //设置定时器,4s后关闭启动广告页
  110. setTimeout(function () {
  111. plus.webview.close(w);
  112. APPUpdate();
  113. }, 3000);
  114. }
  115. } catch (e) {
  116. // error
  117. uni.setStorage({
  118. key: "launchFlag",
  119. data: true,
  120. success: function () {
  121. console.log("error时存储launchFlag");
  122. },
  123. });
  124. }
  125. },
  126. /**
  127. * 获取粘贴板数据
  128. */
  129. async getClipboard() {
  130. let res = await getClipboardData();
  131. /**
  132. * 解析粘贴板数据
  133. */
  134. if (res.indexOf(config.shareLink) != -1) {
  135. uni.showModal({
  136. title: "提示",
  137. content: "检测到一个分享链接是否跳转?",
  138. confirmText: "跳转",
  139. success: function (callback) {
  140. if (callback.confirm) {
  141. const path = res.split(config.shareLink)[1];
  142. if (path.indexOf("tabbar") != -1) {
  143. uni.switchTab({
  144. url: path,
  145. });
  146. } else {
  147. uni.navigateTo({
  148. url: path,
  149. });
  150. }
  151. }
  152. },
  153. });
  154. }
  155. },
  156. /**
  157. * h5中打开app获取跳转app的链接并跳转
  158. */
  159. checkArguments() {
  160. // #ifdef APP-PLUS
  161. setTimeout(() => {
  162. const args = plus.runtime.arguments;
  163. if (args) {
  164. const argsStr = decodeURIComponent(args);
  165. const path = argsStr.split("//")[1];
  166. if (path.indexOf("tabbar") != -1) {
  167. uni.switchTab({
  168. url: `/${path}`,
  169. });
  170. } else {
  171. uni.navigateTo({
  172. url: `/${path}`,
  173. });
  174. }
  175. }
  176. });
  177. // #endif
  178. },
  179. },
  180. };
  181. </script>
  182. <style lang="scss">
  183. @import "uview-ui/index.scss";
  184. @import "./static/font/iconfont/iconfont.css";
  185. // -------适配底部安全区 苹果x系列刘海屏
  186. // #ifdef MP-WEIXIN
  187. .mp-iphonex-bottom {
  188. padding-bottom: constant(safe-area-inset-bottom);
  189. padding-bottom: env(safe-area-inset-bottom);
  190. box-sizing: content-box;
  191. height: auto !important;
  192. padding-top: 10rpx;
  193. }
  194. // #endif
  195. body {
  196. background-color: $bg-color;
  197. }
  198. /************************ */
  199. .w200 {
  200. width: 200rpx !important;
  201. }
  202. .flex1 {
  203. flex: 1; //必须父级设置flex
  204. }
  205. </style>