main.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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('https') === -1) {
  37. uni.navigateTo({
  38. url: pagePath
  39. })
  40. } else {
  41. this.webviewTo(pagePath)
  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. let fontUrl = ''
  107. if (fontFamily === 'Poppins') {
  108. fontUrl = 'https://oss.starify.cn/test/Poppins-SemiBold.ttf'
  109. }
  110. loadCloudFontFace(fontUrl, fontFamily + '.ttf', fontFamily)
  111. }
  112. App.mpType = 'app'
  113. const app = new Vue({
  114. App,
  115. store
  116. })
  117. app.$mount()
  118. const token = uni.getStorageSync('token')
  119. const user = uni.getStorageSync('user')
  120. if (user) {
  121. store.commit('SET_USER', user)
  122. }
  123. if (token) {
  124. store.commit('SET_TOKEN', token)
  125. store.dispatch('getInfo')
  126. }
  127. initLocale()
  128. // #endif
  129. // #ifdef VUE3
  130. import {
  131. createSSRApp
  132. } from 'vue'
  133. export function createApp() {
  134. const app = createSSRApp(App)
  135. return {
  136. app
  137. }
  138. }
  139. // #endif