registerBaseStyle.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import Vue from 'vue'
  2. // 动态渲染通用样式
  3. function wrapStyle(target) {
  4. if (!target) {
  5. return {}
  6. }
  7. let targetObj = {
  8. cmpRadius: target.cmpRadius,
  9. imgMargin: target.imgMargin,
  10. imgRadius: target.imgRadius,
  11. pagePadding: target.pagePadding,
  12. }
  13. let result = {}
  14. // 需px单位基础样式
  15. const needUnit = ['fontSize', 'width', 'height']
  16. for (const [key, value] of Object.entries(targetObj)) {
  17. // 需px单位基础样式
  18. if (needUnit.includes(key)) {
  19. result[key] = unit(value)
  20. continue
  21. }
  22. // 页面间距
  23. if (key == 'pagePadding') {
  24. result['paddingLeft'] = unit(value)
  25. result['paddingRight'] = unit(value)
  26. continue
  27. }
  28. }
  29. return result
  30. }
  31. function cmpStyle(target) {
  32. if (!target) {
  33. return {}
  34. }
  35. let result = {}
  36. for (const [key, value] of Object.entries(target)) {
  37. // 容器负边距
  38. if (key == 'negativeMarginBottom') {
  39. result['paddingBottom'] = unit(value)
  40. continue
  41. }
  42. // 上部内间距
  43. if (key == 'cmpUpperPadding') {
  44. result['paddingTop'] = unit(value)
  45. continue
  46. }
  47. // 下部内间距
  48. if (key == 'cmpLowerPadding') {
  49. result['paddingBottom'] = unit(value)
  50. continue
  51. }
  52. // 所有圆角
  53. if (key == 'cmpRadius') {
  54. result['borderRadius'] = unit(value)
  55. continue
  56. }
  57. // 上部圆角
  58. if (key == 'cmpUpperRadius') {
  59. result['borderTopLeftRadius'] = unit(value)
  60. result['borderTopRightRadius'] = unit(value)
  61. continue
  62. }
  63. // 下部圆角
  64. if (key == 'cmpLowerRadius') {
  65. result['borderBottomLeftRadius'] = unit(value)
  66. result['borderBottomRightRadius'] = unit(value)
  67. continue
  68. }
  69. // 组件背景色
  70. if (key == 'cmpBackground') {
  71. result['background'] = value
  72. continue
  73. }
  74. }
  75. return result
  76. }
  77. // 样式单位rpx
  78. function unit(target) {
  79. return `${target * 2}rpx`
  80. }
  81. Vue.prototype.$wrapStyle = wrapStyle
  82. Vue.prototype.$cmpStyle = cmpStyle
  83. Vue.prototype.$unit = unit