main.js 3.3 KB

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