DimensionWeightPicker.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <div class="dimension-weight-picker">
  3. <div class="dim-rows">
  4. <div v-for="dim in dimensions" :key="dim.code" class="dim-row">
  5. <span class="dim-label" :style="{ color: dim.color }">{{ dim.name }}</span>
  6. <el-slider
  7. v-model="dim.currentValue"
  8. :min="0"
  9. :max="getMaxForDim(dim.code)"
  10. :disabled="dim.currentValue === 0 && getRemaining() === 0"
  11. class="dim-slider"
  12. @change="onWeightChange"
  13. />
  14. <el-input-number
  15. v-model="dim.currentValue"
  16. :min="0"
  17. :max="getMaxForDim(dim.code)"
  18. :disabled="dim.currentValue === 0 && getRemaining() === 0"
  19. :controls="false"
  20. size="mini"
  21. class="dim-input"
  22. @change="onWeightChange"
  23. />
  24. <span class="dim-unit">%</span>
  25. </div>
  26. </div>
  27. <div class="sum-row" :class="{ error: sum !== 100 && sum > 0 }">
  28. <span>合计:{{ sum }}%</span>
  29. <span v-if="sum !== 100 && sum > 0" class="sum-tip">剩余 {{ 100 - sum }}%</span>
  30. <span v-if="sum === 100" class="sum-ok">已分配完成</span>
  31. </div>
  32. <div v-if="showPresets" class="preset-row">
  33. <el-button v-for="p in presets" :key="p.label" size="mini" @click="applyPreset(p)">{{ p.label }}</el-button>
  34. </div>
  35. </div>
  36. </template>
  37. <script>
  38. export default {
  39. name: 'DimensionWeightPicker',
  40. props: {
  41. value: {
  42. type: Array,
  43. default() {
  44. return []
  45. }
  46. },
  47. showPresets: {
  48. type: Boolean,
  49. default: false
  50. }
  51. },
  52. data() {
  53. return {
  54. dimensions: [
  55. { code: 'body', name: '身·土', color: '#FF8C42', currentValue: 0 },
  56. { code: 'mind', name: '智·金', color: '#6366F1', currentValue: 0 },
  57. { code: 'action', name: '行·木', color: '#10B981', currentValue: 0 },
  58. { code: 'wealth', name: '富·水', color: '#F59E0B', currentValue: 0 },
  59. { code: 'heart', name: '心·火', color: '#FF6B9D', currentValue: 0 }
  60. ],
  61. presets: [
  62. { label: '均衡', weights: { body: 20, mind: 20, action: 20, wealth: 20, heart: 20 } },
  63. { label: '偏身', weights: { body: 40, mind: 15, action: 15, wealth: 15, heart: 15 } },
  64. { label: '偏智', weights: { body: 15, mind: 40, action: 15, wealth: 15, heart: 15 } },
  65. { label: '偏行', weights: { body: 15, mind: 15, action: 40, wealth: 15, heart: 15 } },
  66. { label: '偏富', weights: { body: 15, mind: 15, action: 15, wealth: 40, heart: 15 } },
  67. { label: '偏心', weights: { body: 15, mind: 15, action: 15, wealth: 15, heart: 40 } }
  68. ]
  69. }
  70. },
  71. computed: {
  72. sum() {
  73. return this.dimensions.reduce((s, d) => s + (d.currentValue || 0), 0)
  74. }
  75. },
  76. watch: {
  77. value: {
  78. immediate: true,
  79. handler(val) {
  80. if (Array.isArray(val) && val.length > 0) {
  81. val.forEach(w => {
  82. const dim = this.dimensions.find(d => d.code === w.dimension)
  83. if (dim) {
  84. dim.currentValue = w.weight || 0
  85. }
  86. })
  87. }
  88. }
  89. }
  90. },
  91. methods: {
  92. getMaxForDim(code) {
  93. const otherTotal = this.dimensions
  94. .filter(d => d.code !== code && (d.currentValue || 0) > 0)
  95. .reduce((s, d) => s + (d.currentValue || 0), 0)
  96. return Math.max(0, 100 - otherTotal)
  97. },
  98. getRemaining() {
  99. return 100 - this.sum
  100. },
  101. onWeightChange() {
  102. this.dimensions.forEach(d => {
  103. const max = this.getMaxForDim(d.code)
  104. if (d.currentValue > max) {
  105. d.currentValue = max
  106. }
  107. if (d.currentValue < 0) {
  108. d.currentValue = 0
  109. }
  110. })
  111. this.emitValue()
  112. },
  113. emitValue() {
  114. const weights = this.dimensions.map(d => ({
  115. dimension: d.code,
  116. weight: d.currentValue || 0,
  117. enabled: (d.currentValue || 0) > 0
  118. }))
  119. this.$emit('input', weights)
  120. },
  121. applyPreset(preset) {
  122. this.dimensions.forEach(d => {
  123. d.currentValue = preset.weights[d.code] || 0
  124. })
  125. this.onWeightChange()
  126. }
  127. }
  128. }
  129. </script>
  130. <style scoped>
  131. .dimension-weight-picker {
  132. max-width: 520px;
  133. }
  134. .dim-rows {
  135. margin-bottom: 8px;
  136. }
  137. .dim-row {
  138. display: flex;
  139. align-items: center;
  140. margin-bottom: 10px;
  141. gap: 8px;
  142. }
  143. .dim-label {
  144. width: 64px;
  145. font-size: 14px;
  146. font-weight: 600;
  147. flex-shrink: 0;
  148. }
  149. .dim-slider {
  150. flex: 1;
  151. }
  152. .dim-input {
  153. width: 72px;
  154. flex-shrink: 0;
  155. }
  156. .dim-input /deep/ .el-input-number__decrease,
  157. .dim-input /deep/ .el-input-number__increase {
  158. display: none;
  159. }
  160. .dim-input /deep/ .el-input__inner {
  161. text-align: center;
  162. padding-left: 8px;
  163. padding-right: 8px;
  164. }
  165. .dim-unit {
  166. width: 16px;
  167. font-size: 13px;
  168. color: #666;
  169. flex-shrink: 0;
  170. }
  171. .sum-row {
  172. margin-top: 12px;
  173. padding-top: 10px;
  174. border-top: 1px solid #eee;
  175. font-size: 13px;
  176. color: #67c23a;
  177. display: flex;
  178. align-items: center;
  179. gap: 6px;
  180. }
  181. .sum-row.error {
  182. color: #f56c6c;
  183. }
  184. .sum-tip {
  185. font-size: 12px;
  186. }
  187. .sum-ok {
  188. font-size: 12px;
  189. }
  190. .preset-row {
  191. margin-top: 12px;
  192. display: flex;
  193. gap: 6px;
  194. flex-wrap: wrap;
  195. }
  196. </style>