DimensionWeightPicker.vue 5.7 KB

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