main.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.handleImageError = function(callback) {
  121. callback('https://onlinecatelogue.productronicachina.com.cn/assets/image/normal-logo.jpg')
  122. }
  123. Vue.prototype.hideLoading = function() {
  124. uni.hideLoading()
  125. }
  126. // 所在页面需放置元素
  127. // <van-dialog id="van-dialog" />
  128. Vue.prototype.startLoading = function (status) {
  129. return Toast.loading({
  130. message: '加载中...',
  131. forbidClick: true,
  132. });
  133. if (status) {
  134. } else {
  135. }
  136. }
  137. Vue.prototype.showAlertDialog = function(message, title) {
  138. return Dialog.alert({
  139. title: title || '系统提示',
  140. message: message
  141. })
  142. }
  143. // 所在页面需放置元素
  144. // <van-dialog id="van-dialog" />
  145. Vue.prototype.showConfirmDialog = function(message, title) {
  146. const options = {
  147. title: title || '系统提示',
  148. message: message
  149. }
  150. return Dialog.confirm(options)
  151. }
  152. Vue.prototype.loadFontFace = function(fontFamily) {
  153. /* const fontUrls = {
  154. 'Poppins': 'https://oss.starify.cn/test/Poppins-Regular.ttf'
  155. }
  156. loadCloudFontFace(fontUrls[fontFamily], fontFamily + '.ttf', fontFamily)*/
  157. }
  158. App.mpType = 'app'
  159. const app = new Vue({
  160. App,
  161. store
  162. })
  163. app.$mount()
  164. const token = uni.getStorageSync('token')
  165. const user = uni.getStorageSync('user')
  166. if (user) {
  167. store.commit('SET_USER', user)
  168. }
  169. if (token) {
  170. store.commit('SET_TOKEN', token)
  171. store.dispatch('getInfo')
  172. }
  173. initLocale()
  174. // #endif
  175. // #ifdef VUE3
  176. import {
  177. createSSRApp
  178. } from 'vue'
  179. export function createApp() {
  180. const app = createSSRApp(App)
  181. return {
  182. app
  183. }
  184. }
  185. // #endif