school.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <template>
  2. <div class="oSchool controller" :class="[preview ? '' : 'o-unit']" v-if="widget.type == 'school'">
  3. <van-field
  4. :name="widget.model"
  5. v-model="dataModel"
  6. :label="widget.name || '文本框'"
  7. :required="fileCheck ? false : options.required || false"
  8. :disabled="options.disabled || false"
  9. clearable
  10. readonly
  11. clickable
  12. :placeholder="options.placeholder || '请输入'"
  13. :rules="rule"
  14. clear-trigger="always"
  15. @click="onClick"
  16. v-if="!preview"
  17. >
  18. <template #right-icon>
  19. <i class="van-icon van-icon-arrow van-cell__right-icon"></i>
  20. </template>
  21. </van-field>
  22. <van-cell class="preview" :border="false" :title="widget.name || '文本框'" :value="dataModel" v-else></van-cell>
  23. <!-- 处理显示问题 -->
  24. <div class="valueShow" v-if="showDataModel && !preview">{{ showDataModel }}</div>
  25. <div class="valueShow valuePreview" v-if="showDataModel && preview">{{ showDataModel }}</div>
  26. <van-popup v-model="showSelect" position="bottom">
  27. <van-picker
  28. show-toolbar
  29. :columns="columns"
  30. @confirm="onConfirm"
  31. visible-item-count="4"
  32. @cancel="onCancel"
  33. />
  34. </van-popup>
  35. </div>
  36. </template>
  37. <script>
  38. import { getOrganCooperation } from '../api'
  39. export default {
  40. name: 'oSchool',
  41. props: ['widget', 'preview', 'value', 'fileCheck'],
  42. data() {
  43. return {
  44. dataModel: this.widget.options?.defaultValue || null,
  45. showDataModel: null,
  46. showSelect: false,
  47. columns: [],
  48. organId: null,
  49. }
  50. },
  51. async mounted() {
  52. // 初始化参数
  53. this.options?.options?.forEach(item => {
  54. this.columns.push(item.value)
  55. })
  56. // console.log(this.widget, this.value)
  57. if(this.value) {
  58. const widget = this.widget
  59. const model = widget.originModel || widget.model
  60. for(let v in this.value) {
  61. if(v == model) {
  62. this.dataModel = this.value[v]
  63. }
  64. }
  65. setTimeout(async () => {
  66. let subForm = widget.model.indexOf('subform')
  67. let name = widget.options.displayRelation
  68. let relation = null
  69. if(subForm > -1) {
  70. relation = widget.model.replace(widget.originModel, name)
  71. }
  72. // 判断,使用的是哪个分部为基本
  73. let organElement = document.querySelector(`input[name="${name}"]`) || document.querySelector(`input[name="${relation}"]`)
  74. const organId = organElement ? organElement.value : null
  75. if(!organId) {
  76. return
  77. }
  78. this.organId = organId
  79. await this.getList()
  80. // 反显分部
  81. this.columns.forEach(item => {
  82. if(this.dataModel == item.value) {
  83. this.showDataModel = item.text
  84. }
  85. })
  86. }, 200);
  87. }
  88. },
  89. methods: {
  90. async onClick() {
  91. const widget = this.widget
  92. let subForm = widget.model.indexOf('subform')
  93. let name = widget.options.displayRelation
  94. let relation = null
  95. if(subForm > -1) {
  96. relation = widget.model.replace(widget.originModel, name)
  97. }
  98. // 判断,使用的是哪个分部为基本
  99. let organElement = document.querySelector(`input[name="${name}"]`) || document.querySelector(`input[name="${relation}"]`)
  100. if(!name || !organElement) {
  101. this.$toast('模板配置有误,请联系总部管理员')
  102. return
  103. }
  104. const organId = organElement.value
  105. if(!organId) {
  106. this.$toast('请选择分部')
  107. return
  108. }
  109. // 判断分部编号是否有修改或更新
  110. if(organId != this.organId) {
  111. this.organId = organId
  112. await this.getList()
  113. }
  114. this.showSelect = true
  115. },
  116. async getList() {
  117. // 获取所有分部
  118. try {
  119. this.columns = []
  120. let res = await getOrganCooperation({ organId: this.organId })
  121. let tempRes = res.data || []
  122. tempRes.forEach(item => {
  123. this.columns.push({
  124. text: item.name,
  125. value: item.id
  126. })
  127. })
  128. } catch {
  129. //
  130. }
  131. },
  132. onConfirm(val) {
  133. this.dataModel = val.value
  134. this.showDataModel = val.text
  135. this.showSelect = false
  136. },
  137. onCancel() {
  138. this.showSelect = false
  139. },
  140. },
  141. computed: {
  142. options() {
  143. return this.widget.options || {}
  144. },
  145. rule() {
  146. let rules = this.widget.rules || []
  147. if(rules && rules.length > 0) {
  148. rules.forEach(item => {
  149. if(item.pattern) {
  150. item.pattern = eval(item.pattern)
  151. }
  152. // 判断是否上传文件
  153. if(this.fileCheck) {
  154. item.required = false
  155. }
  156. });
  157. }
  158. return rules
  159. }
  160. },
  161. watch: {
  162. // dataModel: {
  163. // deep: true,
  164. // handler(newValue) {
  165. // if (newValue !== undefined && newValue !== null) {
  166. // }
  167. // }
  168. // },
  169. }
  170. }
  171. </script>
  172. <style lang='less' scoped>
  173. @import url('./controlCommon.less');
  174. .oSchool {
  175. position: relative;
  176. .valueShow {
  177. position: absolute;
  178. bottom: 12px;
  179. left: 16px;
  180. line-height: 24px;
  181. padding: .03rem 0;
  182. background: #fff;
  183. }
  184. .valuePreview {
  185. bottom: 2px;
  186. }
  187. }
  188. </style>