main.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. if (fontFamily === 'Poppins') {
  110. // 开发者工具没有该字体
  111. wx.getSystemInfo({
  112. success(res) {
  113. if (res.platform === 'devtools') {
  114. loadCloudFontFace(fontUrls[fontFamily], fontFamily + '.ttf', fontFamily)
  115. }
  116. }
  117. })
  118. } else {
  119. loadCloudFontFace(fontUrls[fontFamily], fontFamily + '.ttf', fontFamily)
  120. }
  121. }
  122. App.mpType = 'app'
  123. const app = new Vue({
  124. App,
  125. store
  126. })
  127. app.$mount()
  128. const token = uni.getStorageSync('token')
  129. const user = uni.getStorageSync('user')
  130. if (user) {
  131. store.commit('SET_USER', user)
  132. }
  133. if (token) {
  134. store.commit('SET_TOKEN', token)
  135. store.dispatch('getInfo')
  136. }
  137. initLocale()
  138. // #endif
  139. // #ifdef VUE3
  140. import {
  141. createSSRApp
  142. } from 'vue'
  143. export function createApp() {
  144. const app = createSSRApp(App)
  145. return {
  146. app
  147. }
  148. }
  149. // #endif