main.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import App from './App'
  2. // #ifndef VUE3
  3. import Vue from 'vue'
  4. import store from './store'
  5. import { initLocale } from '@/locales/i18n'
  6. import './uni.promisify.adaptor'
  7. Vue.config.productionTip = false
  8. Vue.prototype.$config = {
  9. pageHeight: 0
  10. }
  11. Vue.prototype.onInitNavbar = function(e) {
  12. this.$config.pageHeight = e.detail.pageHeight
  13. }
  14. Vue.prototype.checkAuth = function(pagePath) {
  15. if (!store.getters.user) {
  16. this.redirectTo('/pages/user/login?redirect=' + encodeURIComponent(pagePath))
  17. return false
  18. } else {
  19. return true
  20. }
  21. }
  22. Vue.prototype.redirectTo = function(pagePath) {
  23. if (pagePath.indexOf('https') === -1) {
  24. uni.redirectTo({
  25. url: pagePath
  26. })
  27. } else {
  28. uni.redirectTo({
  29. url: '/pages/index/webview?url=' + encodeURIComponent(pagePath)
  30. })
  31. }
  32. }
  33. Vue.prototype.navigateTo = function(pagePath) {
  34. if (pagePath.indexOf('https') === -1) {
  35. uni.navigateTo({
  36. url: pagePath
  37. })
  38. } else {
  39. this.webviewTo(pagePath)
  40. }
  41. }
  42. Vue.prototype.webviewTo = function(url) {
  43. uni.navigateTo({
  44. url: '/pages/index/webview?url=' + encodeURIComponent(url)
  45. })
  46. }
  47. Vue.prototype.showToast = function(title, complete) {
  48. uni.showToast({
  49. icon: 'none',
  50. title: title
  51. })
  52. if (complete)
  53. setTimeout(complete, 2000)
  54. }
  55. Vue.prototype.showSuccessToast = function(title, complete) {
  56. uni.showToast({
  57. icon: 'success',
  58. title: title,
  59. duration: 2000
  60. })
  61. if (complete)
  62. setTimeout(complete, 2000)
  63. }
  64. Vue.prototype.showFailToast = function(title, complete) {
  65. uni.showToast({
  66. icon: 'fail',
  67. title: title,
  68. duration: 2000
  69. })
  70. if (complete)
  71. setTimeout(complete, 2000)
  72. }
  73. Vue.prototype.showLoading = function(options) {
  74. options = options || {
  75. forbidClick: true,
  76. loadingType: 'spinner',
  77. }
  78. if (!options.title) {
  79. options.title = '加载中'
  80. }
  81. uni.showLoading(options)
  82. }
  83. Vue.prototype.hideLoading = function() {
  84. uni.hideLoading()
  85. }
  86. App.mpType = 'app'
  87. const app = new Vue({
  88. App,
  89. store
  90. })
  91. app.$mount()
  92. const token = uni.getStorageSync('token')
  93. const user = uni.getStorageSync('user')
  94. if (user) {
  95. store.commit('SET_USER', user)
  96. }
  97. if (token) {
  98. store.commit('SET_TOKEN', token)
  99. store.dispatch('getInfo')
  100. }
  101. initLocale()
  102. // #endif
  103. // #ifdef VUE3
  104. import {
  105. createSSRApp
  106. } from 'vue'
  107. export function createApp() {
  108. const app = createSSRApp(App)
  109. return {
  110. app
  111. }
  112. }
  113. // #endif