CCL_QMS检验
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
5.5 KiB

4 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 months ago
​
​
​
​
​
​
​
3 months ago
​
​
​
​
​
​
​
4 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 months ago
​
​
​
​
​
​
4 months ago
4 months ago
​
​
​
​
​
​
​
4 months ago
​
​
​
​
​
​
​
​
4 months ago
​
​
​
​
​
4 months ago
​
​
​
​
​
​
​
​
  1. <template>
  2. <div>
  3. <el-select v-bind="$attrs" v-on="$listeners" :value="value" style="width: 100%" @change="changeDictData">
  4. <el-option v-for="(item, index) in options"
  5. :key="index" :label="item.dictLabel"
  6. :value="item.dictValue"
  7. :disabled="item.status === 'N' || disabledOptionsValue.includes(item.dictValue)">
  8. <slot :label="item.dictLabel" :value="item.dictValue"></slot>
  9. </el-option>
  10. </el-select>
  11. </div>
  12. </template>
  13. <script>
  14. import { selectDictDataList, selectDictTypeList } from '../../../api/dict'
  15. export default {
  16. name: 'dictDataSelect',
  17. model: {
  18. prop: 'value',
  19. event: 'change'
  20. },
  21. props: {
  22. dictType: {
  23. type: String
  24. },
  25. dictTypeList: {
  26. type: Array,
  27. default: () => []
  28. },
  29. value: {
  30. required: true
  31. },
  32. useDefaultValue: {
  33. type: Boolean,
  34. default: true
  35. },
  36. useTypeMultiDefaultValue: {
  37. type: Boolean,
  38. default: false
  39. },
  40. useSite: {
  41. type: Boolean,
  42. default: true
  43. },
  44. disabledOptionsValue: {
  45. type: Array,
  46. default: () => []
  47. },
  48. site: {
  49. type: String,
  50. default: undefined
  51. },
  52. siteRequired: {
  53. type: Boolean,
  54. default: false
  55. }
  56. },
  57. data () {
  58. return {
  59. options: [],
  60. dictTypeMultiDefaultValue: ''
  61. }
  62. },
  63. watch: {
  64. dictType () {
  65. this.initOption()
  66. },
  67. site () {
  68. this.initOption()
  69. }
  70. },
  71. created () {
  72. this.initOption()
  73. },
  74. methods: {
  75. hasCurrentValue () {
  76. if (Array.isArray(this.value)) {
  77. return this.value.length > 0
  78. }
  79. return this.value !== null && this.value !== undefined && String(this.value).trim() !== ''
  80. },
  81. isMultipleMode () {
  82. if (Array.isArray(this.value)) {
  83. return true
  84. }
  85. const multiple = this.$attrs ? this.$attrs.multiple : false
  86. if (multiple === '' || multiple === true || multiple === 'multiple') {
  87. return true
  88. }
  89. if (typeof multiple === 'string') {
  90. return multiple !== 'false'
  91. }
  92. return !!multiple
  93. },
  94. parseMultiDefaultValue (value) {
  95. if (value === null || value === undefined) {
  96. return []
  97. }
  98. const cache = new Set()
  99. return String(value)
  100. .split(/[;,\n]/)
  101. .map(item => String(item || '').trim())
  102. .filter(item => {
  103. if (!item || cache.has(item)) {
  104. return false
  105. }
  106. cache.add(item)
  107. return true
  108. })
  109. },
  110. async loadDictTypeMultiDefaultValue () {
  111. this.dictTypeMultiDefaultValue = ''
  112. const currentDictType = String(this.dictType || '').trim()
  113. if (!this.useTypeMultiDefaultValue || !currentDictType) {
  114. return
  115. }
  116. const site = this.resolveQuerySite()
  117. if (!site) {
  118. return
  119. }
  120. try {
  121. let { data } = await selectDictTypeList({ site, dictType: currentDictType })
  122. if (data && data.code === 0 && Array.isArray(data.rows)) {
  123. const matched = data.rows.find(item => String(item.dictType || '').trim() === currentDictType)
  124. this.dictTypeMultiDefaultValue = matched ? String(matched.multiDefaultValue || '').trim() : ''
  125. }
  126. } catch (e) {}
  127. },
  128. dictDefaultValue () {
  129. if (this.hasCurrentValue() || this.options.length === 0) {
  130. return
  131. }
  132. if (this.isMultipleMode()) {
  133. const enabledValueSet = new Set(
  134. this.options.filter(item => item.status === 'Y').map(item => item.dictValue)
  135. )
  136. let defaultValues = this.parseMultiDefaultValue(this.dictTypeMultiDefaultValue)
  137. .filter(item => enabledValueSet.has(item))
  138. if (defaultValues.length === 0) {
  139. defaultValues = this.options
  140. .filter(item => item.isDefault === 'Y' && item.status === 'Y')
  141. .map(item => item.dictValue)
  142. }
  143. if (defaultValues.length > 0) {
  144. this.$emit('change', defaultValues)
  145. }
  146. return
  147. }
  148. let find = this.options.find(item => item.isDefault === 'Y')
  149. if (find && find.status === 'Y') {
  150. this.$emit('change', find.dictValue)
  151. }
  152. },
  153. resolveQuerySite () {
  154. const explicitSite = String(this.site || '').trim()
  155. if (this.siteRequired) {
  156. return explicitSite
  157. }
  158. if (explicitSite) {
  159. return explicitSite
  160. }
  161. return this.useSite ? this.$store.state.user.site : '*'
  162. },
  163. async initOption () {
  164. const requestId = (this.dictRequestId || 0) + 1
  165. this.dictRequestId = requestId
  166. const site = this.resolveQuerySite()
  167. if (this.siteRequired && !String(site || '').trim()) {
  168. this.options = []
  169. return
  170. }
  171. let params = {
  172. site,
  173. dictType: this.dictType,
  174. dictTypeList: this.dictTypeList
  175. }
  176. try {
  177. let { data } = await selectDictDataList(params)
  178. if (requestId !== this.dictRequestId) {
  179. return
  180. }
  181. if (data && data.code === 0) {
  182. this.options = data.rows
  183. if (this.options.length === 0) {
  184. this.$emit('change', this.isMultipleMode() ? [] : '')
  185. }
  186. }
  187. await this.loadDictTypeMultiDefaultValue()
  188. if (requestId !== this.dictRequestId) {
  189. return
  190. }
  191. if (this.useDefaultValue) {
  192. this.dictDefaultValue()
  193. }
  194. } catch (e) {
  195. if (requestId !== this.dictRequestId) {
  196. return
  197. }
  198. return Promise.reject(e)
  199. }
  200. },
  201. changeDictData (val) {
  202. this.$emit('change', val)
  203. }
  204. }
  205. }
  206. </script>