DimensionWeightPicker.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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: 'mind', name: '智·金', color: '#6366F1', currentValue: 20 },
  55. { code: 'action', name: '行·木', color: '#10B981', currentValue: 20 },
  56. { code: 'wealth', name: '富·水', color: '#F59E0B', currentValue: 20 },
  57. { code: 'heart', name: '心·火', color: '#FF6B9D', currentValue: 20 }
  58. ],
  59. presets: [
  60. { label: '均衡', weights: { body: 20, mind: 20, action: 20, wealth: 20, heart: 20 } },
  61. { label: '偏身', weights: { body: 40, mind: 15, action: 15, wealth: 15, heart: 15 } },
  62. { label: '偏智', weights: { body: 15, mind: 40, action: 15, wealth: 15, heart: 15 } },
  63. { label: '偏行', weights: { body: 15, mind: 15, action: 40, wealth: 15, heart: 15 } },
  64. { label: '偏富', weights: { body: 15, mind: 15, action: 15, wealth: 40, heart: 15 } },
  65. { label: '偏心', weights: { body: 15, mind: 15, action: 15, wealth: 15, heart: 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: {
  76. immediate: true,
  77. handler(val) {
  78. if (Array.isArray(val) && val.length > 0) {
  79. val.forEach(w => {
  80. const dim = this.dimensions.find(d => d.code === w.dimension)
  81. if (dim) {
  82. dim.currentValue = w.weight || 0
  83. }
  84. })
  85. }
  86. }
  87. }
  88. },
  89. methods: {
  90. onWeightChange(changedCode) {
  91. const changed = this.dimensions.find(function(d) { return d.code === changedCode })
  92. if (changed) {
  93. changed.currentValue = Math.max(0, Math.min(100, changed.currentValue))
  94. }
  95. var remaining = 100 - this.sum
  96. if (Math.abs(remaining) < 0.01) {
  97. this.emitValue()
  98. return
  99. }
  100. var others = this.dimensions.filter(function(d) { return d.code !== changedCode })
  101. if (remaining > 0) {
  102. var othersTotal = others.reduce(function(s, d) { return s + (d.currentValue || 0) }, 0)
  103. var exactVals = []
  104. for (var i = 0; i < others.length; i++) {
  105. exactVals.push(othersTotal > 0 ? ((others[i].currentValue || 0) / othersTotal) * remaining : remaining / others.length)
  106. }
  107. var distributed = 0
  108. for (var j = 0; j < others.length - 1; j++) {
  109. var rounded = Math.round(exactVals[j])
  110. others[j].currentValue = Math.max(0, rounded)
  111. distributed += rounded
  112. }
  113. others[others.length - 1].currentValue = Math.max(0, Math.round(remaining - distributed))
  114. } else {
  115. var excess = -remaining
  116. var othersTotal2 = others.reduce(function(s, d) { return s + (d.currentValue || 0) }, 0)
  117. var exactCuts = []
  118. for (var k = 0; k < others.length; k++) {
  119. exactCuts.push(othersTotal2 > 0 ? ((others[k].currentValue || 0) / othersTotal2) * excess : excess / others.length)
  120. }
  121. var cutSum = 0
  122. for (var m = 0; m < others.length - 1; m++) {
  123. var cutRounded = Math.round(exactCuts[m])
  124. others[m].currentValue = Math.max(0, (others[m].currentValue || 0) - cutRounded)
  125. cutSum += cutRounded
  126. }
  127. others[others.length - 1].currentValue = Math.max(0, (others[others.length - 1].currentValue || 0) - Math.round(excess - cutSum))
  128. }
  129. this.emitValue()
  130. },
  131. emitValue() {
  132. const weights = this.dimensions.map(d => ({
  133. dimension: d.code,
  134. weight: d.currentValue || 0,
  135. enabled: (d.currentValue || 0) > 0
  136. }))
  137. this.$emit('input', weights)
  138. },
  139. applyPreset(preset) {
  140. this.dimensions.forEach(function(d) {
  141. d.currentValue = preset.weights[d.code] || 0
  142. })
  143. this.emitValue()
  144. }
  145. }
  146. }
  147. </script>
  148. <style scoped>
  149. .dimension-weight-picker {
  150. max-width: 520px;
  151. }
  152. .dim-rows {
  153. margin-bottom: 8px;
  154. }
  155. .dim-row {
  156. display: flex;
  157. align-items: center;
  158. margin-bottom: 10px;
  159. gap: 8px;
  160. }
  161. .dim-label {
  162. width: 64px;
  163. font-size: 14px;
  164. font-weight: 600;
  165. flex-shrink: 0;
  166. }
  167. .dim-slider {
  168. flex: 1;
  169. height: 38px;
  170. }
  171. .dim-slider /deep/ .el-slider__runway {
  172. height: 6px;
  173. }
  174. .dim-input {
  175. width: 72px;
  176. flex-shrink: 0;
  177. }
  178. .dim-input /deep/ .el-input-number__decrease,
  179. .dim-input /deep/ .el-input-number__increase {
  180. display: none;
  181. }
  182. .dim-input /deep/ .el-input__inner {
  183. text-align: center;
  184. padding-left: 8px;
  185. padding-right: 8px;
  186. }
  187. .dim-unit {
  188. width: 16px;
  189. font-size: 13px;
  190. color: #666;
  191. flex-shrink: 0;
  192. }
  193. .sum-row {
  194. margin-top: 12px;
  195. padding-top: 10px;
  196. border-top: 1px solid #eee;
  197. font-size: 13px;
  198. color: #67c23a;
  199. display: flex;
  200. align-items: center;
  201. gap: 6px;
  202. }
  203. .sum-row.error {
  204. color: #f56c6c;
  205. }
  206. .sum-tip {
  207. font-size: 12px;
  208. }
  209. .sum-ok {
  210. font-size: 12px;
  211. }
  212. .preset-row {
  213. margin-top: 12px;
  214. display: flex;
  215. gap: 6px;
  216. flex-wrap: wrap;
  217. }
  218. </style>