Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

313 righe
8.6 KiB

  1. // 补齐部分平台缺失的 uni API(如支付宝小程序的 onWindowResize)
  2. import { applyUniApiShims } from './libs/function/uni-api-shims.js'
  3. applyUniApiShims()
  4. // 看到此报错,是因为没有配置vite.config.js的【transpileDependencies】
  5. // const pleaseSetTranspileDependencies = {}, babelTest = pleaseSetTranspileDependencies?.test
  6. // 引入全局mixin
  7. import { mixin } from './libs/mixin/mixin.js'
  8. // 小程序特有的mixin
  9. import { mpMixin } from './libs/mixin/mpMixin.js'
  10. // 路由封装
  11. import route from './libs/util/route.js'
  12. // 颜色渐变相关,colorGradient-颜色渐变,hexToRgb-十六进制颜色转rgb颜色,rgbToHex-rgb转十六进制
  13. import colorGradient from './libs/function/colorGradient.js'
  14. // 规则检验
  15. import test from './libs/function/test.js'
  16. // 防抖方法
  17. import debounce from './libs/function/debounce.js'
  18. // 节流方法
  19. import throttle from './libs/function/throttle.js'
  20. // 浮点计算
  21. import calc from './libs/function/calc.js'
  22. // 浮点计算
  23. import digit from './libs/function/digit.js'
  24. // 公共文件写入的方法
  25. import index, { rpx2px } from './libs/function/index.js'
  26. // 配置信息
  27. import config from './libs/config/config.js'
  28. // props配置信息
  29. import props, { setPropsConfig } from './libs/config/props.js'
  30. // 各个需要fixed的地方的z-index配置文件
  31. import zIndex from './libs/config/zIndex.js'
  32. // 关于颜色的配置,特殊场景使用
  33. import color from './libs/config/color.js'
  34. // 平台
  35. import platform from './libs/function/platform'
  36. // http
  37. import http from './libs/function/http.js'
  38. // fontUtil
  39. import fontUtil from './components/u-icon/util.js';
  40. // i18n
  41. import i18n, { t } from './libs/i18n/index.js'
  42. import {
  43. themeState,
  44. setTheme,
  45. setThemePreference,
  46. getThemePreference,
  47. getSystemTheme,
  48. getThemeVars,
  49. initThemeSystem,
  50. refreshThemeFromConfig,
  51. syncThemeColorOverrideState
  52. } from './libs/theme/theme.js'
  53. import {
  54. applyNativeThemeUI,
  55. getThemeCardStyle,
  56. getThemeIsDark,
  57. getThemePageStyle,
  58. getThemeTabBarStyle,
  59. getThemeVar,
  60. getThemeVarsForStyle
  61. } from './libs/theme/runtime.js'
  62. const rootToastState = {
  63. ref: null
  64. }
  65. const rootNotifyState = {
  66. ref: null
  67. }
  68. function normalizeRootToastOptions(options = {}) {
  69. const toastOptions = typeof options === 'string'
  70. ? { message: options }
  71. : (options && typeof options === 'object' ? { ...options } : {})
  72. if (!toastOptions.message && toastOptions.title) {
  73. toastOptions.message = toastOptions.title
  74. }
  75. return toastOptions
  76. }
  77. function setRootToastRef(ref = null) {
  78. rootToastState.ref = ref || null
  79. }
  80. function rootToast(options = {}) {
  81. const toastOptions = normalizeRootToastOptions(options)
  82. const toastRef = rootToastState.ref
  83. if (toastRef && typeof toastRef.show === 'function') {
  84. toastRef.show(toastOptions)
  85. return
  86. }
  87. if (!toastOptions.message) return
  88. if (typeof uni !== 'undefined' && typeof uni.showToast === 'function') {
  89. uni.showToast({
  90. title: toastOptions.message,
  91. icon: 'none',
  92. duration: Number(toastOptions.duration) || 2000,
  93. })
  94. }
  95. }
  96. function normalizeRootNotifyOptions(options = {}) {
  97. const notifyOptions = typeof options === 'string'
  98. ? { message: options }
  99. : (options && typeof options === 'object' ? { ...options } : {})
  100. if (!notifyOptions.message && notifyOptions.title) {
  101. notifyOptions.message = notifyOptions.title
  102. }
  103. return notifyOptions
  104. }
  105. function setRootNotifyRef(ref = null) {
  106. rootNotifyState.ref = ref || null
  107. }
  108. function rootNotify(options = {}) {
  109. const notifyOptions = normalizeRootNotifyOptions(options)
  110. const notifyRef = rootNotifyState.ref
  111. if (notifyRef && typeof notifyRef.show === 'function') {
  112. notifyRef.show(notifyOptions)
  113. return
  114. }
  115. if (!notifyOptions.message) return
  116. if (typeof uni !== 'undefined' && typeof uni.showToast === 'function') {
  117. uni.showToast({
  118. title: notifyOptions.message,
  119. icon: 'none',
  120. duration: Number(notifyOptions.duration) || 3000,
  121. })
  122. }
  123. }
  124. // 导出
  125. let themeType = ['primary', 'success', 'error', 'warning', 'info'];
  126. export { route, http, debounce, throttle, calc, digit, platform, themeType, mixin, mpMixin, props, color, test, zIndex, fontUtil, i18n , rpx2px, t}
  127. export * from './libs/function/index.js'
  128. export * from './libs/function/colorGradient.js'
  129. /**
  130. * @description 修改uView内置属性值
  131. * @param {object} props 修改内置props属性
  132. * @param {object} config 修改内置config属性
  133. * @param {object} color 修改内置color属性
  134. * @param {object} zIndex 修改内置zIndex属性
  135. */
  136. export function setConfig(configs) {
  137. const settings = configs || {}
  138. index.shallowMerge(config, settings.config || {})
  139. setPropsConfig(settings.props || {})
  140. index.shallowMerge(color, settings.color || {})
  141. index.shallowMerge(zIndex, settings.zIndex || {})
  142. syncThemeColorOverrideState({
  143. color: settings.color,
  144. configColor: settings?.config?.color
  145. })
  146. const shouldRefreshTheme = !!settings.color
  147. || !!settings?.config?.color
  148. || themeState.version > 0
  149. if (shouldRefreshTheme) {
  150. refreshThemeFromConfig()
  151. }
  152. }
  153. index.setConfig = setConfig
  154. const $u = {
  155. route,
  156. date: index.timeFormat, // 另名date
  157. colorGradient: colorGradient.colorGradient,
  158. hexToRgb: colorGradient.hexToRgb,
  159. rgbToHex: colorGradient.rgbToHex,
  160. colorToRgba: colorGradient.colorToRgba,
  161. test,
  162. type: themeType,
  163. http,
  164. config, // uview-plus配置信息相关,比如版本号
  165. zIndex,
  166. debounce,
  167. throttle,
  168. calc,
  169. mixin,
  170. mpMixin,
  171. props,
  172. ...index,
  173. color,
  174. platform,
  175. theme: themeState,
  176. setTheme,
  177. setThemePreference,
  178. getThemePreference,
  179. getSystemTheme,
  180. getThemeVars,
  181. getThemeTabBarStyle,
  182. applyNativeThemeUI,
  183. rootToast,
  184. setRootToastRef,
  185. rootNotify,
  186. setRootNotifyRef
  187. }
  188. export const mount$u = function() {
  189. uni.$u = $u
  190. initThemeSystem()
  191. }
  192. function defineGlobalThemeHelpers(Vue) {
  193. const globalProperties = Vue?.config?.globalProperties
  194. if (!globalProperties) return
  195. Object.defineProperty(globalProperties, 'upThemeIsDark', {
  196. configurable: true,
  197. get() {
  198. return getThemeIsDark()
  199. }
  200. })
  201. Object.defineProperty(globalProperties, 'upThemeVars', {
  202. configurable: true,
  203. get() {
  204. return getThemeVarsForStyle()
  205. }
  206. })
  207. Object.defineProperty(globalProperties, 'upThemePageStyle', {
  208. configurable: true,
  209. get() {
  210. return getThemePageStyle()
  211. }
  212. })
  213. Object.defineProperty(globalProperties, 'upThemeCardStyle', {
  214. configurable: true,
  215. get() {
  216. return getThemeCardStyle()
  217. }
  218. })
  219. globalProperties.upThemeVar = function(varName, fallbackColor) {
  220. return getThemeVar(varName, fallbackColor)
  221. }
  222. globalProperties.upApplyNativeThemeUI = function() {
  223. return applyNativeThemeUI()
  224. }
  225. }
  226. function toCamelCase(str) {
  227. return str.replace(/-([a-z])/g, function(match, group1) {
  228. return group1.toUpperCase();
  229. }).replace(/^[a-z]/, function(match) {
  230. return match.toUpperCase();
  231. });
  232. }
  233. let components = []
  234. function resolveComponents() {
  235. if (components.length) return components
  236. // #ifdef H5
  237. // import.meta.glob is a Vite compile-time macro; the runtime guard would
  238. // always fail in the browser and block component registration.
  239. const importFn = import.meta.glob('./components/u-*/u-*.vue', { eager: true })
  240. for (const key in importFn) {
  241. const component = importFn[key]?.default
  242. if (component?.name && component.name.indexOf('u--') !== 0) {
  243. components.push(component)
  244. }
  245. }
  246. // #endif
  247. return components
  248. }
  249. const install = (Vue, upuiParams = '') => {
  250. // #ifdef H5
  251. resolveComponents().forEach(function(component) {
  252. const name = component.name.replace(/u-([a-zA-Z0-9-_]+)/g, 'up-$1');
  253. if (name != component.name) {
  254. Vue.component(component.name, component);
  255. }
  256. Vue.component(name, component);
  257. });
  258. // #endif
  259. // 初始化
  260. if (upuiParams) {
  261. uni.upuiParams = upuiParams
  262. let temp = upuiParams()
  263. if (temp.httpIns) {
  264. temp.httpIns(http)
  265. }
  266. if (temp.options) {
  267. setConfig(temp.options)
  268. }
  269. }
  270. // 同时挂载到uni和Vue.prototype中
  271. // $u挂载到uni对象上
  272. uni.$u = $u
  273. initThemeSystem()
  274. if (Vue && Vue.config && Vue.config.globalProperties) {
  275. Vue.config.globalProperties.$u = $u
  276. defineGlobalThemeHelpers(Vue)
  277. }
  278. if (Vue && typeof Vue.mixin === 'function') {
  279. Vue.mixin(mixin)
  280. }
  281. }
  282. export default {
  283. install
  284. }