index.vue 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <app-remote-select v-model="selectedValue" :options="dataList"
  3. :option-props="{ label: getLocalizedKey(), value: 'goodsId' }" :remote-method="remoteMethod" @change="onChange"
  4. @reset="onReset">
  5. <template #default="{ option }">
  6. <div style="display: flex;justify-content: space-between;">
  7. <span>{{ option.goodsCode }}</span>
  8. <span>{{ option[getLocalizedKey()] || option.goodsId }}</span>
  9. </div>
  10. </template>
  11. </app-remote-select>
  12. </template>
  13. <script lang="ts" setup>
  14. import { computed, toRaw, PropType, nextTick } from 'vue'
  15. import { Language } from '@/constants/language'
  16. import { useRequest } from '@/hooks/request'
  17. import { queryGoodsSelectYYJG } from '@/services/api/common'
  18. import { i18n } from '@/stores'
  19. import AppRemoteSelect from '@pc/components/base/remote-select/index.vue'
  20. const props = defineProps({
  21. modelValue: [Number, String],
  22. params: {
  23. type: Object as PropType<Model.GoodsSelectYYJGReq>
  24. },
  25. })
  26. const emit = defineEmits(['update:modelValue', 'change'])
  27. const selectedValue = computed({
  28. get: () => props.modelValue,
  29. set: (val) => emit('update:modelValue', val)
  30. })
  31. // 获取本地化属性键
  32. const getLocalizedKey: () => keyof Model.GoodsSelectYYJGRsp = () => {
  33. switch (i18n.global.locale) {
  34. case Language.Simplified:
  35. return 'goodsName'
  36. case Language.Thai:
  37. return 'goodsnameth'
  38. case Language.Traditional:
  39. return 'goodsnametw'
  40. case Language.Vietnamese:
  41. return 'goodsnamevi'
  42. default:
  43. return 'goodsnameen'
  44. }
  45. }
  46. const { dataList, loading, run } = useRequest(queryGoodsSelectYYJG, {
  47. manual: true,
  48. params: props.params,
  49. onSuccess: (res) => {
  50. if (!res.data.some((e) => e.goodsId.toString() === selectedValue.value?.toString())) {
  51. selectedValue.value = undefined
  52. onChange()
  53. }
  54. }
  55. })
  56. const onReset = () => {
  57. nextTick(() => {
  58. if (!loading.value) {
  59. run(props.params)
  60. }
  61. })
  62. }
  63. const remoteMethod = (query: string) => {
  64. nextTick(() => {
  65. if (!loading.value) {
  66. if (query) {
  67. run({
  68. ...props.params,
  69. searchCode: query
  70. })
  71. } else if (!dataList.value.length) {
  72. onReset()
  73. }
  74. }
  75. })
  76. }
  77. const onChange = (value?: number) => {
  78. const item = dataList.value.find((e) => e.goodsId === value)
  79. emit('change', item && toRaw(item))
  80. }
  81. defineExpose({
  82. onReset
  83. })
  84. </script>