main.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 Toast from '@/wxcomponents/vant/toast/toast'
  9. import './uni.promisify.adaptor'
  10. Vue.config.productionTip = false
  11. Vue.prototype.$config = {
  12. pageHeight: 0
  13. }
  14. Vue.prototype.onInitNavbar = function(e) {
  15. this.$config.pageHeight = e.detail.pageHeight
  16. }
  17. Vue.prototype.refreshData = function() {
  18. const pages = getCurrentPages(); // 获取当前页面栈
  19. if (pages.length - 2 >= 0) {
  20. const currentPage = pages[pages.length - 2]; // 当前页面实例
  21. const route = currentPage.route; // 页面路径
  22. if (route === 'pages/index/index') {
  23. uni.$emit('refreshData', store.getters.page[store.getters.page.length - 1]);
  24. } else {
  25. uni.$emit('refreshData', route);
  26. }
  27. }
  28. }
  29. Vue.prototype.checkAuth = function(pagePath) {
  30. if (!store.getters.user) {
  31. this.navigateTo('/pages/user/login?redirect=' + encodeURIComponent(pagePath))
  32. return false
  33. } else {
  34. return true
  35. }
  36. }
  37. Vue.prototype.redirectTo = function(pagePath) {
  38. if (pagePath.indexOf('https') === -1) {
  39. uni.redirectTo({
  40. url: pagePath
  41. })
  42. } else {
  43. uni.redirectTo({
  44. url: '/pages/index/webview?url=' + encodeURIComponent(pagePath)
  45. })
  46. }
  47. }
  48. Vue.prototype.navigateTo = function(pagePath) {
  49. if (pagePath.indexOf('http') === 0) {
  50. this.webviewTo(pagePath)
  51. } else {
  52. uni.navigateTo({
  53. url: pagePath,
  54. animationType: 'pop-in',
  55. animationDuration: 200
  56. })
  57. }
  58. }
  59. Vue.prototype.webviewTo = function(url) {
  60. uni.navigateTo({
  61. url: '/pages/index/webview?url=' + encodeURIComponent(url),
  62. animationType: 'pop-in',
  63. animationDuration: 200
  64. })
  65. }
  66. Vue.prototype.showToast = function(title, complete) {
  67. uni.showToast({
  68. icon: 'none',
  69. title: title
  70. })
  71. if (complete)
  72. setTimeout(complete, 2000)
  73. }
  74. Vue.prototype.getSystemInfo = function(self,callback) {
  75. uni.getSystemInfo({
  76. success(res) {
  77. const os = res.system.toLowerCase()
  78. self.$config.os = os
  79. if (os.includes('windows') || os.includes('mac')) {
  80. if (callback) {
  81. callback('pc')
  82. }
  83. } else {
  84. if (callback) {
  85. callback('mobile')
  86. }
  87. }
  88. }
  89. })
  90. }
  91. Vue.prototype.showSuccessToast = function(title, complete) {
  92. uni.showToast({
  93. icon: 'success',
  94. title: title,
  95. duration: 2000
  96. })
  97. if (complete)
  98. setTimeout(complete, 2000)
  99. }
  100. Vue.prototype.showFailToast = function(title, complete) {
  101. uni.showToast({
  102. icon: 'fail',
  103. title: title,
  104. duration: 2000
  105. })
  106. if (complete)
  107. setTimeout(complete, 2000)
  108. }
  109. Vue.prototype.showLoading = function(options) {
  110. options = options || {
  111. forbidClick: true,
  112. loadingType: 'spinner',
  113. mask: true
  114. }
  115. if (!options.title) {
  116. options.title = '加载中'
  117. }
  118. uni.showLoading(options)
  119. }
  120. Vue.prototype.hideLoading = function() {
  121. uni.hideLoading()
  122. }
  123. // 所在页面需放置元素
  124. // <van-dialog id="van-dialog" />
  125. Vue.prototype.startLoading = function (status) {
  126. return Toast.loading({
  127. message: '加载中...',
  128. forbidClick: true,
  129. });
  130. if (status) {
  131. } else {
  132. }
  133. }
  134. Vue.prototype.showAlertDialog = function(message, title) {
  135. return Dialog.alert({
  136. title: title || '系统提示',
  137. message: message
  138. })
  139. }
  140. // 所在页面需放置元素
  141. // <van-dialog id="van-dialog" />
  142. Vue.prototype.showConfirmDialog = function(message, title) {
  143. const options = {
  144. title: title || '系统提示',
  145. message: message
  146. }
  147. return Dialog.confirm(options)
  148. }
  149. Vue.prototype.loadFontFace = function(fontFamily) {
  150. /* const fontUrls = {
  151. 'Poppins': 'https://oss.starify.cn/test/Poppins-Regular.ttf'
  152. }
  153. loadCloudFontFace(fontUrls[fontFamily], fontFamily + '.ttf', fontFamily)*/
  154. }
  155. App.mpType = 'app'
  156. const app = new Vue({
  157. App,
  158. store
  159. })
  160. app.$mount()
  161. const token = uni.getStorageSync('token')
  162. const user = uni.getStorageSync('user')
  163. if (user) {
  164. store.commit('SET_USER', user)
  165. }
  166. if (token) {
  167. store.commit('SET_TOKEN', token)
  168. store.dispatch('getInfo')
  169. }
  170. initLocale()
  171. // #endif
  172. // #ifdef VUE3
  173. import {
  174. createSSRApp
  175. } from 'vue'
  176. export function createApp() {
  177. const app = createSSRApp(App)
  178. return {
  179. app
  180. }
  181. }
  182. // #endif