main.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 loadCloudFontFace from '@/utils/font'
  7. import Dialog from '@/wxcomponents/vant/dialog/dialog'
  8. import './uni.promisify.adaptor'
  9. Vue.config.productionTip = false
  10. Vue.prototype.$config = {
  11. pageHeight: 0
  12. }
  13. Vue.prototype.onInitNavbar = function(e) {
  14. this.$config.pageHeight = e.detail.pageHeight
  15. }
  16. Vue.prototype.checkAuth = function(pagePath) {
  17. if (!store.getters.user) {
  18. this.redirectTo('/pages/user/login?redirect=' + encodeURIComponent(pagePath))
  19. return false
  20. } else {
  21. return true
  22. }
  23. }
  24. Vue.prototype.redirectTo = function(pagePath) {
  25. if (pagePath.indexOf('https') === -1) {
  26. uni.redirectTo({
  27. url: pagePath
  28. })
  29. } else {
  30. uni.redirectTo({
  31. url: '/pages/index/webview?url=' + encodeURIComponent(pagePath)
  32. })
  33. }
  34. }
  35. Vue.prototype.navigateTo = function(pagePath) {
  36. if (pagePath.indexOf('http') === 0) {
  37. this.webviewTo(pagePath)
  38. } else {
  39. uni.navigateTo({
  40. url: pagePath
  41. })
  42. }
  43. }
  44. Vue.prototype.webviewTo = function(url) {
  45. uni.navigateTo({
  46. url: '/pages/index/webview?url=' + encodeURIComponent(url)
  47. })
  48. }
  49. Vue.prototype.showToast = function(title, complete) {
  50. uni.showToast({
  51. icon: 'none',
  52. title: title
  53. })
  54. if (complete)
  55. setTimeout(complete, 2000)
  56. }
  57. Vue.prototype.showSuccessToast = function(title, complete) {
  58. uni.showToast({
  59. icon: 'success',
  60. title: title,
  61. duration: 2000
  62. })
  63. if (complete)
  64. setTimeout(complete, 2000)
  65. }
  66. Vue.prototype.showFailToast = function(title, complete) {
  67. uni.showToast({
  68. icon: 'fail',
  69. title: title,
  70. duration: 2000
  71. })
  72. if (complete)
  73. setTimeout(complete, 2000)
  74. }
  75. Vue.prototype.showLoading = function(options) {
  76. options = options || {
  77. forbidClick: true,
  78. loadingType: 'spinner',
  79. }
  80. if (!options.title) {
  81. options.title = '加载中'
  82. }
  83. uni.showLoading(options)
  84. }
  85. Vue.prototype.hideLoading = function() {
  86. uni.hideLoading()
  87. }
  88. // 所在页面需放置元素
  89. // <van-dialog id="van-dialog" />
  90. Vue.prototype.showAlertDialog = function(message, title) {
  91. return Dialog.alert({
  92. title: title || '系统提示',
  93. message: message
  94. })
  95. }
  96. // 所在页面需放置元素
  97. // <van-dialog id="van-dialog" />
  98. Vue.prototype.showConfirmDialog = function(message, title) {
  99. const options = {
  100. title: title || '系统提示',
  101. message: message
  102. }
  103. return Dialog.confirm(options)
  104. }
  105. Vue.prototype.loadFontFace = function(fontFamily) {
  106. const fontUrls = {
  107. 'Poppins': 'https://oss.starify.cn/test/Poppins-Regular.ttf'
  108. }
  109. loadCloudFontFace(fontUrls[fontFamily], fontFamily + '.ttf', fontFamily)
  110. }
  111. App.mpType = 'app'
  112. const app = new Vue({
  113. App,
  114. store
  115. })
  116. app.$mount()
  117. const token = uni.getStorageSync('token')
  118. const user = uni.getStorageSync('user')
  119. if (user) {
  120. store.commit('SET_USER', user)
  121. }
  122. if (token) {
  123. store.commit('SET_TOKEN', token)
  124. store.dispatch('getInfo')
  125. }
  126. initLocale()
  127. // #endif
  128. // #ifdef VUE3
  129. import {
  130. createSSRApp
  131. } from 'vue'
  132. export function createApp() {
  133. const app = createSSRApp(App)
  134. return {
  135. app
  136. }
  137. }
  138. // #endif