index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 index from './libs/function/index.js'
  21. // 配置信息
  22. import config from './libs/config/config.js'
  23. // props配置信息
  24. import props from './libs/config/props.js'
  25. // 各个需要fixed的地方的z-index配置文件
  26. import zIndex from './libs/config/zIndex.js'
  27. // 关于颜色的配置,特殊场景使用
  28. import color from './libs/config/color.js'
  29. // 平台
  30. import platform from './libs/function/platform'
  31. // http
  32. import http from './libs/function/http.js'
  33. // 导出
  34. let themeType = ['primary', 'success', 'error', 'warning', 'info'];
  35. export { route, http, debounce, throttle, calc, platform, themeType, mixin, mpMixin, props, color, test, zIndex }
  36. export * from './libs/function/index.js'
  37. export * from './libs/function/colorGradient.js'
  38. /**
  39. * @description 修改uView内置属性值
  40. * @param {object} props 修改内置props属性
  41. * @param {object} config 修改内置config属性
  42. * @param {object} color 修改内置color属性
  43. * @param {object} zIndex 修改内置zIndex属性
  44. */
  45. export function setConfig(configs) {
  46. index.shallowMerge(config, configs.config || {})
  47. index.shallowMerge(props, configs.props || {})
  48. index.shallowMerge(color, configs.color || {})
  49. index.shallowMerge(zIndex, configs.zIndex || {})
  50. }
  51. index.setConfig = setConfig
  52. const $u = {
  53. route,
  54. date: index.timeFormat, // 另名date
  55. colorGradient: colorGradient.colorGradient,
  56. hexToRgb: colorGradient.hexToRgb,
  57. rgbToHex: colorGradient.rgbToHex,
  58. colorToRgba: colorGradient.colorToRgba,
  59. test,
  60. type: themeType,
  61. http,
  62. config, // uview-plus配置信息相关,比如版本号
  63. zIndex,
  64. debounce,
  65. throttle,
  66. calc,
  67. mixin,
  68. mpMixin,
  69. props,
  70. ...index,
  71. color,
  72. platform
  73. }
  74. export const mount$u = function() {
  75. uni.$u = $u
  76. }
  77. // #ifdef H5
  78. const importFn = import.meta.glob('./components/u-*/u-*.vue', { eager: true })
  79. let components = [];
  80. // 批量注册全局组件
  81. for (const key in importFn) {
  82. let component = importFn[key].default;
  83. if (component.name && component.name.indexOf('u--') !== 0) {
  84. component.install = function (Vue) {
  85. Vue.component(name, component);
  86. };
  87. // 导入组件
  88. components.push(component);
  89. }
  90. }
  91. // #endif
  92. function toCamelCase(str) {
  93. return str.replace(/-([a-z])/g, function(match, group1) {
  94. return group1.toUpperCase();
  95. }).replace(/^[a-z]/, function(match) {
  96. return match.toUpperCase();
  97. });
  98. }
  99. const install = (Vue, upuiParams = '') => {
  100. // #ifdef H5
  101. components.forEach(function(component) {
  102. const name = component.name.replace(/u-([a-zA-Z0-9-_]+)/g, 'up-$1');
  103. Vue.component(name, component);
  104. });
  105. // #endif
  106. // 初始化
  107. if (upuiParams) {
  108. uni.upuiParams = upuiParams
  109. let temp = upuiParams()
  110. if (temp.httpIns) {
  111. temp.httpIns(http)
  112. }
  113. if (temp.options) {
  114. setConfig(temp.options)
  115. }
  116. }
  117. // 同时挂载到uni和Vue.prototype中
  118. // $u挂载到uni对象上
  119. uni.$u = $u
  120. // #ifndef APP-NVUE
  121. // 只有vue,挂载到Vue.prototype才有意义,因为nvue中全局Vue.prototype和Vue.mixin是无效的
  122. Vue.config.globalProperties.$u = $u
  123. Vue.mixin(mixin)
  124. // #endif
  125. }
  126. export default {
  127. install
  128. }