index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <view class="jnpf-relation-form">
  3. <selectBox v-model="innerValue" :disabled='disabled' :placeholder="placeholder" @openSelect="openSelect">
  4. </selectBox>
  5. <DisplayList :extraObj="extraObj" :extraOptions="extraOptions"
  6. v-if="Object.keys(extraObj).length && innerValue">
  7. </DisplayList>
  8. </view>
  9. </template>
  10. <script>
  11. import {
  12. getRelationFormDetail
  13. } from '@/api/common.js'
  14. import DisplayList from '@/components/displayList'
  15. import selectBox from '@/components/selectBox'
  16. import {
  17. useBaseStore
  18. } from '@/store/modules/base'
  19. const baseStore = useBaseStore()
  20. export default {
  21. name: 'jnpf-relation-form',
  22. components: {
  23. DisplayList,
  24. selectBox
  25. },
  26. props: {
  27. modelValue: {
  28. default: ''
  29. },
  30. placeholder: {
  31. type: String,
  32. default: '请选择'
  33. },
  34. formType: {
  35. type: String,
  36. default: 'select'
  37. },
  38. disabled: {
  39. type: Boolean,
  40. default: false
  41. },
  42. columnOptions: {
  43. type: Array,
  44. default: []
  45. },
  46. extraOptions: {
  47. type: Array,
  48. default: () => []
  49. },
  50. relationField: {
  51. type: String,
  52. default: ''
  53. },
  54. propsValue: {
  55. type: String,
  56. default: ''
  57. },
  58. modelId: {
  59. type: String,
  60. default: ''
  61. },
  62. hasPage: {
  63. type: Boolean,
  64. default: false
  65. },
  66. pageSize: {
  67. type: Number,
  68. default: 10000
  69. },
  70. vModel: {
  71. type: String,
  72. default: ''
  73. },
  74. popupTitle: {
  75. type: String,
  76. default: ''
  77. },
  78. queryType: {
  79. type: Number,
  80. default: 1
  81. }
  82. },
  83. data() {
  84. return {
  85. selectShow: false,
  86. innerValue: '',
  87. defaultValue: '',
  88. current: null,
  89. defaultOptions: [],
  90. firstVal: '',
  91. firstId: 0,
  92. extraObj: {}
  93. }
  94. },
  95. watch: {
  96. modelValue(val) {
  97. this.setDefault()
  98. },
  99. },
  100. created() {
  101. uni.$on('relationConfirm', (subVal, innerValue, list, selectRow) => {
  102. this.confirm(subVal, innerValue, list, selectRow)
  103. })
  104. this.setDefault()
  105. },
  106. methods: {
  107. setDefault() {
  108. if (!this.modelId || !this.vModel) return
  109. if (!this.modelValue) {
  110. this.innerValue = ''
  111. let relationData = baseStore.relationData
  112. relationData[this.vModel] = {}
  113. baseStore.updateRelationData(relationData)
  114. return
  115. }
  116. let query = {
  117. id: this.modelValue,
  118. };
  119. if (this.propsValue) query = {
  120. ...query,
  121. propsValue: this.propsValue
  122. };
  123. getRelationFormDetail(this.modelId, query).then(res => {
  124. if (!res.data || !res.data.data) return
  125. let data = JSON.parse(res.data.data)
  126. this.extraObj = data
  127. this.innerValue = data[this.relationField] ? data[this.relationField] : this
  128. .modelValue
  129. let relationData = baseStore.relationData
  130. relationData[this.vModel] = data
  131. baseStore.updateRelationData(relationData)
  132. })
  133. },
  134. openSelect() {
  135. if (this.disabled) {
  136. if (!this.modelValue) return
  137. let config = {
  138. modelId: this.modelId,
  139. id: this.modelValue,
  140. formTitle: '详情',
  141. noShowBtn: 1,
  142. noDataLog: 1
  143. }
  144. this.$nextTick(() => {
  145. const url =
  146. '/pages/apply/dynamicModel/detail?config=' +
  147. this.jnpf.base64.encode(JSON.stringify(config))
  148. uni.navigateTo({
  149. url: url
  150. })
  151. })
  152. return
  153. }
  154. let data = {
  155. columnOptions: this.columnOptions,
  156. relationField: this.relationField,
  157. type: 'relation',
  158. propsValue: this.propsValue,
  159. modelId: this.modelId,
  160. hasPage: this.hasPage,
  161. pageSize: this.pageSize,
  162. id: this.modelValue,
  163. vModel: this.vModel,
  164. popupTitle: this.popupTitle || '选择数据',
  165. innerValue: this.innerValue,
  166. queryType: this.queryType
  167. }
  168. uni.navigateTo({
  169. url: '/pages/apply/popSelect/index?data=' + encodeURIComponent(JSON.stringify(
  170. data))
  171. })
  172. },
  173. confirm(subVal, innerValue, vModel, selectRow) {
  174. if (vModel !== this.vModel) return; // 早期返回,如果条件不满足,则直接退出
  175. this.firstVal = innerValue;
  176. this.firstId = subVal;
  177. // 确定 innerValue 的值
  178. let innerValueStr = !this.propsValue ? innerValue + '' : selectRow[this.propsValue];
  179. this.innerValue = selectRow[this.relationField] || "";
  180. // 触发 update:modelValue 事件
  181. this.$emit('update:modelValue', selectRow[this.propsValue]);
  182. // 触发 change 事件
  183. this.$emit('change', subVal, selectRow);
  184. }
  185. }
  186. }
  187. </script>
  188. <style lang="scss" scoped>
  189. .jnpf-relation-form {
  190. width: 100%;
  191. height: 100%;
  192. }
  193. </style>