index.js 3.9 KB

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