wk-field-user.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <view class="wk-field wk-field-user" @click.stop="handleChooseUser">
  3. <view v-if="_label" class="wk-field__label">
  4. <view v-if="field.isNull === 1" class="line" />
  5. {{ _label }}
  6. </view>
  7. <view class="wk-field__body">
  8. <view class="wk-field__body-core">
  9. <template v-if="isAvatar && formValue.length > 0">
  10. <wk-avatar
  11. v-for="(user, index) in formValue"
  12. :key="index"
  13. :avatar="user.img"
  14. :name="user.realname"
  15. :size="12"
  16. class="avatar" />
  17. </template>
  18. <template v-else>
  19. {{ valueStr }}
  20. </template>
  21. </view>
  22. <text class="wk wk-arrow-right icon-right" />
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. import mixins from './mixins'
  28. import { isArray } from '@/utils/types.js'
  29. export default {
  30. name: 'WkFieldUser',
  31. mixins: [mixins],
  32. props: {
  33. isAvatar: {
  34. type: Boolean,
  35. default: false
  36. }
  37. },
  38. data() {
  39. return {
  40. guid: null,
  41. formValue: []
  42. }
  43. },
  44. computed: {
  45. valueStr() {
  46. const arr = this.formValue || []
  47. return arr.map(o => o.realname).join(',')
  48. }
  49. },
  50. watch: {
  51. value: {
  52. handler(val) {
  53. if (
  54. !isArray(this.value) &&
  55. !this.$isEmpty(this.value)
  56. ) {
  57. this.formValue = [this.value]
  58. } else {
  59. this.formValue = this.value
  60. }
  61. },
  62. deep: true,
  63. immediate: true
  64. },
  65. },
  66. created() {
  67. this.guid = this.$guid()
  68. },
  69. methods: {
  70. handleChooseUser() {
  71. if (this.field.disabled) {
  72. if (this.config && this.config.disabledMsg) {
  73. this.$toast(this.config.disabledMsg)
  74. }
  75. return
  76. }
  77. const bridge = getApp().globalData.selectedValBridge
  78. bridge.user = {
  79. guid: this.guid,
  80. title: '选择' + this.field.name,
  81. defaultVal: this.formValue || []
  82. }
  83. if (this.field.formType === 'single_user') {
  84. bridge.user.maxlength = 1
  85. }
  86. uni.$on('selected-user', this.selectedUser)
  87. this.$Router.navigateTo('/pages_common/selectList/user')
  88. },
  89. /**
  90. * 选中员工
  91. * @param {Object} data
  92. */
  93. selectedUser(data) {
  94. if (this.guid === data.guid) {
  95. this.formValue = data.data
  96. this.$emit('input', this.formValue)
  97. this.$emit('change', {
  98. index: this.index,
  99. field: this.field,
  100. value: this.formValue
  101. })
  102. }
  103. uni.$off('selected-user')
  104. }
  105. }
  106. }
  107. </script>
  108. <style scoped lang="scss">
  109. @import './wkField.scss';
  110. .wk-field-user {
  111. .avatar {
  112. width: 58rpx;
  113. height: 58rpx;
  114. margin: 5rpx;
  115. }
  116. }
  117. </style>