App.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <script>
  2. /**
  3. * vuex管理登录状态,具体可以参考官方登录模板示例
  4. */
  5. import {
  6. mapMutations
  7. } from "vuex";
  8. import config from "@/config/config";
  9. import storage from "@/utils/storage";
  10. import { mpLogin, alipayLogin } from "@/api/connect.js";
  11. import Foundation from "@/utils/Foundation.js";
  12. /**
  13. * 路由监听并删除路由
  14. * https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateTo.html
  15. * */
  16. // #ifdef MP-WEIXIN
  17. wx.onAppRoute((res) => {
  18. const pages = getCurrentPages();
  19. if (pages.length > 3) {
  20. delete getCurrentPages()[2]
  21. }
  22. })
  23. // #endif
  24. export default {
  25. data() {
  26. return {
  27. config,
  28. };
  29. },
  30. /**
  31. * 监听返回
  32. */
  33. onBackPress(e) {
  34. if (e.from == "backbutton") {
  35. let routes = getCurrentPages();
  36. routes.forEach((item) => {
  37. if (
  38. item.route.indexOf("pages/station/index") != -1
  39. ) {
  40. uni.redirectTo({
  41. url: item.route,
  42. });
  43. }
  44. });
  45. uni.navigateBack();
  46. return true; //阻止默认返回行为
  47. }
  48. },
  49. methods: {
  50. ...mapMutations(["login"]),
  51. },
  52. onLaunch: function (options) {
  53. // #ifdef APP-PLUS
  54. this.checkArguments(); // 检测启动参数
  55. // 重点是以下: 一定要监听后台恢复 !一定要
  56. plus.globalEvent.addEventListener("newintent", (e) => {
  57. this.checkArguments(); // 检测启动参数
  58. });
  59. // #endif
  60. // #ifdef MP-WEIXIN
  61. this.applyUpdateWeChat();
  62. this.autoMpLogin()
  63. // #endif
  64. // #ifdef MP-ALIPAY
  65. // this.autoMpLogin()
  66. // #endif
  67. // #ifdef MP-ALIPAY
  68. //存储支付宝扫码参数
  69. if(options.query && options.query.qrCode){
  70. const qrCode = options.query.qrCode
  71. let connectId = Foundation.urlParse(decodeURIComponent(qrCode)).n
  72. this.globalData.connectId = connectId
  73. }
  74. // #endif
  75. },
  76. onShow() {
  77. },
  78. methods: {
  79. /**
  80. * 微信小程序版本提交更新版本 解决缓存问题
  81. */
  82. applyUpdateWeChat() {
  83. const updateManager = uni.getUpdateManager();
  84. updateManager.onCheckForUpdate(function (res) {
  85. // 请求完新版本信息的回调
  86. });
  87. updateManager.onUpdateReady(function (res) {
  88. uni.showModal({
  89. title: "更新提示",
  90. content: "发现新版本,是否重启应用?",
  91. success(res) {
  92. if (res.confirm) {
  93. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  94. updateManager.applyUpdate();
  95. }
  96. },
  97. });
  98. });
  99. updateManager.onUpdateFailed(function (res) {
  100. // 新的版本下载失败
  101. });
  102. },
  103. // TODO 开屏广告 后续优化添加
  104. launch() {
  105. try {
  106. // 获取本地存储中launchFlag标识 开屏广告
  107. const value = uni.getStorageSync("launchFlag");
  108. if (!value) {
  109. // this.$u.route("/pages/index/agreement");
  110. } else {
  111. //app启动时打开启动广告页
  112. var w = plus.webview.open(
  113. "/hybrid/html/advertise/advertise.html",
  114. "本地地址", {
  115. top: 0,
  116. bottom: 0,
  117. zindex: 999,
  118. },
  119. "fade-in",
  120. 500
  121. );
  122. //设置定时器,4s后关闭启动广告页
  123. setTimeout(function () {
  124. plus.webview.close(w);
  125. }, 3000);
  126. }
  127. } catch (e) {
  128. // error
  129. uni.setStorage({
  130. key: "launchFlag",
  131. data: true,
  132. success: function () {
  133. console.log("error时存储launchFlag");
  134. },
  135. });
  136. }
  137. },
  138. /**
  139. * h5中打开app获取跳转app的链接并跳转
  140. */
  141. checkArguments() {
  142. // #ifdef APP-PLUS
  143. setTimeout(() => {
  144. const args = plus.runtime.arguments;
  145. if (args) {
  146. const argsStr = decodeURIComponent(args);
  147. const path = argsStr.split("//")[1];
  148. if (path.indexOf("tabbar") != -1) {
  149. uni.switchTab({
  150. url: `/${path}`,
  151. });
  152. } else {
  153. uni.navigateTo({
  154. url: `/${path}`,
  155. });
  156. }
  157. }
  158. });
  159. // #endif
  160. },
  161. /**
  162. * 自动登录
  163. */
  164. autoMpLogin() {
  165. // !this.$options.filters.isLogin("auth") &&
  166. uni.login({
  167. success: (res) => {
  168. let loginMethod = mpLogin
  169. // #ifdef MP-ALIPAY
  170. loginMethod = alipayLogin
  171. // #endif
  172. loginMethod({
  173. code: res.code,
  174. }, config.appid).then((res) => {
  175. if (res.data.code == 200) {
  176. storage.setAccessToken(res.data.data.token);
  177. storage.setUserInfo(res.data.data.userInfo);
  178. } else {
  179. storage.setAccessToken("");
  180. storage.setRefreshToken("");
  181. storage.setUserInfo({});
  182. uni.showToast({
  183. title: "账户异常",
  184. duration: 2000,
  185. icon: "error",
  186. });
  187. }
  188. });
  189. },
  190. });
  191. }
  192. },
  193. };
  194. </script>
  195. <style lang="scss">
  196. @import "uview-ui/index.scss";
  197. // -------适配底部安全区 苹果x系列刘海屏
  198. .mp-iphonex-bottom {
  199. padding-bottom: constant(safe-area-inset-bottom);
  200. padding-bottom: env(safe-area-inset-bottom);
  201. box-sizing: content-box;
  202. // height: auto !important;
  203. padding-top: 10rpx;
  204. }
  205. page {
  206. height: 100%;
  207. background-color: $bg-color;
  208. }
  209. /* DarkMode 下的样式 start */
  210. @media (prefers-color-scheme: dark) {
  211. page {
  212. background-color: $bg-color-dark;
  213. color: #fff;
  214. }
  215. }
  216. /************************ */
  217. .w200 {
  218. width: 200rpx !important;
  219. }
  220. .flex1 {
  221. flex: 1; //必须父级设置flex
  222. }
  223. </style>