index.vue 4.3 KB

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