index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // 看到此报错,是因为没有配置vite.config.js的【transpileDependencies】
  2. // const pleaseSetTranspileDependencies = {}, babelTest = pleaseSetTranspileDependencies?.test
  3. // 引入全局mixin
  4. import { mixin } from './libs/mixin/mixin.js'
  5. // 小程序特有的mixin
  6. import { mpMixin } from './libs/mixin/mpMixin.js'
  7. // 路由封装
  8. import route from './libs/util/route.js'
  9. // 颜色渐变相关,colorGradient-颜色渐变,hexToRgb-十六进制颜色转rgb颜色,rgbToHex-rgb转十六进制
  10. import colorGradient from './libs/function/colorGradient.js'
  11. // 规则检验
  12. import test from './libs/function/test.js'
  13. // 防抖方法
  14. import debounce from './libs/function/debounce.js'
  15. // 节流方法
  16. import throttle from './libs/function/throttle.js'
  17. // 浮点计算
  18. import calc from './libs/function/calc.js'
  19. // 浮点计算
  20. import digit from './libs/function/digit.js'
  21. // 公共文件写入的方法
  22. import index from './libs/function/index.js'
  23. // 配置信息
  24. import config from './libs/config/config.js'
  25. // props配置信息
  26. import props from './libs/config/props.js'
  27. // 各个需要fixed的地方的z-index配置文件
  28. import zIndex from './libs/config/zIndex.js'
  29. // 关于颜色的配置,特殊场景使用
  30. import color from './libs/config/color.js'
  31. // 平台
  32. import platform from './libs/function/platform'
  33. // http
  34. import http from './libs/function/http.js'
  35. // fontUtil
  36. import fontUtil from './components/u-icon/util.js';
  37. // i18n
  38. import i18n, { t } from './libs/i18n/index.js'
  39. // 导出
  40. let themeType = ['primary', 'success', 'error', 'warning', 'info'];
  41. export { route, http, debounce, throttle, calc, digit, platform, themeType, mixin, mpMixin, props, color, test, zIndex, fontUtil, i18n ,t}
  42. export * from './libs/function/index.js'
  43. export * from './libs/function/colorGradient.js'
  44. /**
  45. * @description 修改uView内置属性值
  46. * @param {object} props 修改内置props属性
  47. * @param {object} config 修改内置config属性
  48. * @param {object} color 修改内置color属性
  49. * @param {object} zIndex 修改内置zIndex属性
  50. */
  51. export function setConfig(configs) {
  52. index.shallowMerge(config, configs.config || {})
  53. index.shallowMerge(props, configs.props || {})
  54. index.shallowMerge(color, configs.color || {})
  55. index.shallowMerge(zIndex, configs.zIndex || {})
  56. }
  57. index.setConfig = setConfig
  58. const $u = {
  59. route,
  60. date: index.timeFormat, // 另名date
  61. colorGradient: colorGradient.colorGradient,
  62. hexToRgb: colorGradient.hexToRgb,
  63. rgbToHex: colorGradient.rgbToHex,
  64. colorToRgba: colorGradient.colorToRgba,
  65. test,
  66. type: themeType,
  67. http,
  68. config, // uview-plus配置信息相关,比如版本号
  69. zIndex,
  70. debounce,
  71. throttle,
  72. calc,
  73. mixin,
  74. mpMixin,
  75. // props,
  76. ...index,
  77. color,
  78. platform
  79. }
  80. export const mount$u = function() {
  81. uni.$u = $u
  82. }
  83. function toCamelCase(str) {
  84. return str.replace(/-([a-z])/g, function(match, group1) {
  85. return group1.toUpperCase();
  86. }).replace(/^[a-z]/, function(match) {
  87. return match.toUpperCase();
  88. });
  89. }
  90. // #ifdef APP || H5
  91. const importFn = import.meta.glob('./components/u-*/u-*.vue', { eager: true })
  92. let components = [];
  93. // 批量注册全局组件
  94. for (const key in importFn) {
  95. let component = importFn[key].default;
  96. if (component.name && component.name.indexOf('u--') !== 0) {
  97. component.install = function (Vue) {
  98. Vue.component(name, component);
  99. };
  100. // 导入组件
  101. components.push(component);
  102. }
  103. }
  104. // #endif
  105. const install = (Vue, upuiParams = '') => {
  106. // #ifdef APP || H5
  107. components.forEach(function(component) {
  108. const name = component.name.replace(/u-([a-zA-Z0-9-_]+)/g, 'up-$1');
  109. if (name != component.name) {
  110. Vue.component(component.name, component);
  111. }
  112. Vue.component(name, component);
  113. });
  114. // #endif
  115. // 初始化
  116. if (upuiParams) {
  117. uni.upuiParams = upuiParams
  118. let temp = upuiParams()
  119. if (temp.httpIns) {
  120. temp.httpIns(http)
  121. }
  122. if (temp.options) {
  123. setConfig(temp.options)
  124. }
  125. }
  126. // 同时挂载到uni和Vue.prototype中
  127. // $u挂载到uni对象上
  128. uni.$u = $u
  129. // #ifndef APP-NVUE
  130. // 只有vue,挂载到Vue.prototype才有意义,因为nvue中全局Vue.prototype和Vue.mixin是无效的
  131. Vue.config.globalProperties.$u = $u
  132. Vue.mixin(mixin)
  133. // #endif
  134. }
  135. export default {
  136. install
  137. }