DataRange.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <a-popover v-model="visible" trigger="click" placement="bottomRight">
  3. <template slot="content">
  4. <div class="d-flex align-items-center">
  5. <span class="label mr-3">{{ $t('scope.kpi.data_range') }}:</span>
  6. <a-radio-group v-model="form.scope">
  7. <a-radio-button v-for="(item, index) in scopeOpts" :key="`${item.value}$$${index}`" :value="item.value">{{ item.label }}</a-radio-button>
  8. </a-radio-group>
  9. </div>
  10. <base-select class="mt-2" v-if="form.scope === 'domain'" v-model="form.domain" remote resource="domains" isDefaultSelect :params="domainParams" @update:resList="updateDomainList" />
  11. <base-select class="mt-2" v-else-if="form.scope === 'project'" v-model="form.project" remote resource="projects" :params="projectParams" @update:resList="updateProjectList" />
  12. <div class="d-flex justify-content-end mt-2">
  13. <a-button type="primary" @click="handleConfirm">{{ $t('dialog.ok') }}</a-button>
  14. </div>
  15. </template>
  16. <div class="data-range-text" :style="textStyle">
  17. <span class="label mr-2">{{ $t('scope.kpi.data_range') }}:</span>
  18. <span class="value">{{ dataRangeText }}<icon type="caret-down" class="ml-1" /></span>
  19. </div>
  20. </a-popover>
  21. </template>
  22. <script>
  23. import * as R from 'ramda'
  24. import storage from '@/utils/storage'
  25. export default {
  26. name: 'DataRange',
  27. props: {
  28. edit: {
  29. type: Boolean,
  30. default: false,
  31. },
  32. dataRangeParams: {
  33. type: Object,
  34. },
  35. },
  36. data () {
  37. return {
  38. visible: false,
  39. form: {
  40. scope: this.dataRangeParams?.scope || this.$store.getters.scope,
  41. domain: this.dataRangeParams?.domain || '',
  42. project: this.dataRangeParams?.project || '',
  43. },
  44. domainParams: {
  45. scope: this.$store.getters.scope,
  46. limit: 20,
  47. },
  48. projectParams: {
  49. scope: this.$store.getters.scope,
  50. limit: 20,
  51. },
  52. domainList: [],
  53. projectList: [],
  54. }
  55. },
  56. computed: {
  57. scopeOpts () {
  58. if (this.$store.getters.scope === 'system') {
  59. return [
  60. { value: 'system', label: this.$t('dashboard.current_system') },
  61. { value: 'domain', label: this.$t('dashboard.specified_domain') },
  62. { value: 'project', label: this.$t('dashboard.specified_project') },
  63. ]
  64. } else if (this.$store.getters.scope === 'domain') {
  65. return [
  66. { value: 'domain', label: this.$t('dashboard.current_domain') },
  67. { value: 'project', label: this.$t('dashboard.specified_project') },
  68. ]
  69. }
  70. return [
  71. { value: 'project', label: this.$t('dashboard.specified_project') },
  72. ]
  73. },
  74. dataRangeText () {
  75. let text = ''
  76. if (this.dataRangeParams?.scope === 'system') {
  77. text = this.$t('dashboard.current_system')
  78. } else if (this.dataRangeParams?.scope === 'domain') {
  79. if (this.$store.getters.isDomainMode) {
  80. text = this.$t('dashboard.current_domain')
  81. }
  82. if (this.$store.getters.isAdminMode) {
  83. text = this.currentDomain?.name ? `${this.currentDomain.name} (${this.$t('dictionary.domain')})` : ''
  84. }
  85. }
  86. if (this.dataRangeParams?.scope === 'project') {
  87. text = this.currentProject?.name ? `${this.currentProject.name} (${this.$t('dictionary.project')})` : ''
  88. }
  89. if (!text) {
  90. text = this.$store.getters.isAdminMode ? this.$t('dashboard.current_system') : this.$t('dashboard.current_domain')
  91. }
  92. return text
  93. },
  94. currentDomain () {
  95. return this.domainList.find(domain => domain.id === this.dataRangeParams?.domain)
  96. },
  97. currentProject () {
  98. return this.projectList.find(project => project.id === this.dataRangeParams?.project)
  99. },
  100. textStyle () {
  101. if (this.edit) {
  102. return {
  103. border: '1px solid #d9d9d9',
  104. 'border-left': 'none',
  105. 'background-color': '#fff',
  106. padding: '0 15px',
  107. }
  108. }
  109. return {}
  110. },
  111. },
  112. watch: {
  113. 'dataRangeParams.scope': {
  114. handler (val) {
  115. this.checkSelectedRange()
  116. },
  117. immediate: true,
  118. },
  119. 'dataRangeParams.domain': {
  120. handler (val) {
  121. this.checkSelectedRange()
  122. },
  123. immediate: true,
  124. },
  125. 'dataRangeParams.project': {
  126. handler (val) {
  127. this.checkSelectedRange()
  128. },
  129. immediate: true,
  130. },
  131. },
  132. methods: {
  133. handleConfirm () {
  134. console.log('handleConfirm', this.form)
  135. if (this.form.scope === 'domain' && !this.form.domain) {
  136. this.$message.error(this.$t('common.tips.select', [this.$t('dictionary.domain')]))
  137. return
  138. }
  139. if (this.form.scope === 'project' && !this.form.project) {
  140. this.$message.error(this.$t('common.tips.select', [this.$t('dictionary.project')]))
  141. return
  142. }
  143. this.updateDataRange(R.clone(this.form))
  144. this.visible = false
  145. },
  146. updateDomainList (resList) {
  147. resList.forEach(item => {
  148. if (!this.domainList.some(domain => domain.id === item.id)) {
  149. this.domainList.push(item)
  150. }
  151. })
  152. },
  153. updateProjectList (resList) {
  154. resList.forEach(item => {
  155. if (!this.projectList.some(project => project.id === item.id)) {
  156. this.projectList.push(item)
  157. }
  158. })
  159. },
  160. async checkSelectedRange () {
  161. if (this.dataRangeParams?.scope === 'domain' && this.dataRangeParams?.domain && !this.currentDomain) {
  162. try {
  163. console.log('domain', this.dataRangeParams?.domain)
  164. const res = await new this.$Manager('domains', 'v1').list({ params: { scope: this.$store.getters.scope, limit: 1, filter: `id.in(${this.dataRangeParams?.domain})` } })
  165. const data = res.data?.data || []
  166. if (data.length > 0) {
  167. this.domainList.push(data[0])
  168. } else {
  169. this.updateDataRange({ scope: this.$store.getters.scope, domain: '', project: '' })
  170. }
  171. } catch (error) {
  172. // 获取失败,更新为当前视图
  173. throw error
  174. }
  175. } else if (this.dataRangeParams?.scope === 'project' && this.dataRangeParams?.project && !this.currentProject) {
  176. try {
  177. const res = await new this.$Manager('projects', 'v1').list({ params: { scope: this.$store.getters.scope, limit: 1, filter: `id.in(${this.dataRangeParams?.project})` } })
  178. const data = res.data?.data || []
  179. if (data.length > 0) {
  180. this.projectList.push(data[0])
  181. } else {
  182. this.updateDataRange({ scope: this.$store.getters.scope, domain: '', project: '' })
  183. }
  184. } catch (error) {
  185. throw error
  186. }
  187. }
  188. },
  189. updateDataRange (params) {
  190. this.$emit('updateDataRange', params)
  191. storage.set('__oc_dashboard_data_range__', params)
  192. },
  193. },
  194. }
  195. </script>
  196. <style lang="less" scoped>
  197. .data-range-text {
  198. display: inline-flex;
  199. align-items: center;
  200. line-height: 30px;
  201. padding: 0 10px;
  202. height: 32px;
  203. cursor: pointer;
  204. width: max-content;
  205. flex: 0 0 auto;
  206. white-space: nowrap;
  207. transition: color 0.2s ease;
  208. &:hover {
  209. color: var(--antd-wave-shadow-color);
  210. }
  211. }
  212. </style>