main.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <el-select
  3. v-model="value"
  4. multiple
  5. filterable
  6. remote
  7. clearable
  8. :multiple-limit="1"
  9. reserve-keyword
  10. :size="size || 'medium'"
  11. style="width: 100%"
  12. :placeholder="placeholder || ''"
  13. :disabled="disabled"
  14. :remote-method="remoteMethod"
  15. :loading="selectLoading"
  16. @change="selectChange"
  17. >
  18. <el-option
  19. v-for="(item, index) in options"
  20. :key="item.code + '' + index"
  21. :label="item.search_name + ''"
  22. :value="item.code + ''"
  23. />
  24. </el-select>
  25. </template>
  26. <script>
  27. import asyncRequest from "@/apis/components/search-area";
  28. import resToken from "@/mixins/resToken";
  29. export default {
  30. name: "SearchArea",
  31. mixins: [resToken],
  32. props: ["size", "value", "placeholder", "isDetail", "disabled", "names", "level",'detailSelect'],
  33. /**
  34. * 属性集合
  35. * @param {String} size : 组件大小 非必填
  36. * @param {Array} value : 选中值 必填
  37. * @param {String} placeholder : 提示信息 非必填
  38. * @param {Boolean} isDetail : 是否是详情逻辑 必填
  39. * @param {Boolean} disabled : 是否禁用 必填
  40. * @param {String} names : 选中值label 展示详情必填
  41. */
  42. /**
  43. * 事件集合
  44. * @searchChange : 选中值变化调用 抛出选中数据
  45. */
  46. data() {
  47. return {
  48. options: [],
  49. selectLoading: false,
  50. searchName: "",
  51. formValue: {
  52. // page: 1,
  53. // size: 100,
  54. level: 3,
  55. name: "",
  56. },
  57. };
  58. },
  59. watch: {
  60. names: {
  61. handler:function (val, old) {
  62. // console.log(val, old, this.isDetail, "1");
  63. this.searchName = val;
  64. if (this.isDetail && (Array.isArray(this.searchName) && this.searchName[0])) {
  65. this.remoteMethod(Array.isArray(this.searchName) ? this.searchName[0] : this.searchName,true);
  66. }
  67. },
  68. immediate:true
  69. },
  70. },
  71. mounted() {
  72. this.options = [];
  73. this.selectLoading = false;
  74. this.formValue.level = this.level ? this.level : 3;
  75. },
  76. methods: {
  77. async selectChange(e) {
  78. console.log(e)
  79. if (e && e.length > 0) {
  80. const index = this.options.findIndex((v) => v.code + "" === e[0] + "");
  81. if (index !== -1) {
  82. const { code, search_name, info } = this.options[index];
  83. const { province, city, area } = info;
  84. let arr_code = "";
  85. switch (this.formValue.level + "") {
  86. case "1":
  87. arr_code = province.code;
  88. break;
  89. case "2":
  90. arr_code = `${province.code},${city.code}`;
  91. break;
  92. default:
  93. arr_code = `${province.code},${city.code},${area.code}`;
  94. break;
  95. }
  96. const model = {
  97. id: code ?? "",
  98. code: arr_code ?? "",
  99. province_code: province.code ?? "",
  100. city_code: city.code ?? "",
  101. area_code: area.code ?? "",
  102. label: search_name ?? "",
  103. };
  104. this.$emit("searchChange", model);
  105. } else {
  106. this.$emit("searchChange", {});
  107. }
  108. } else {
  109. this.$emit("searchChange", {});
  110. }
  111. },
  112. async remoteMethod(query,formDetail = false) {
  113. this.selectLoading = true;
  114. if (query !== "") {
  115. this.options = [];
  116. this.formValue.name = query;
  117. const { code, data, message } = await asyncRequest.list({
  118. ...this.formValue,
  119. level:this.level
  120. });
  121. if (code === 0) {
  122. this.options = data;
  123. if(this.detailSelect && formDetail && data.length > 0){ this.selectChange([data[0].code])}
  124. } else if (code >= 100 && code <= 104) {
  125. await this.logout();
  126. } else {
  127. this.$message.warning(message);
  128. }
  129. } else {
  130. this.options = [];
  131. }
  132. this.selectLoading = false;
  133. },
  134. },
  135. };
  136. </script>
  137. <style></style>