You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

313 lines
8.1 KiB

4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
  1. <template>
  2. <el-row>
  3. <div class="field-box">
  4. <el-avatar class="avater" :icon="getMyIcon(myVal)" :style="{backgroundColor:getMyColor(myVal)}">{{getMyAvaterInfo(myVal)}}</el-avatar>
  5. <div class="field-info">
  6. <slot name="field-info" :value="myVal">
  7. <span class="field-value">{{showMyValue( myVal )}} </span>
  8. <slot name="label">
  9. <span class="field-label">{{label}}</span>
  10. </slot>
  11. </slot>
  12. </div>
  13. <div v-if="disabled!==true" class="my-select" name="select" :value="myVal">
  14. <el-select v-model="myVal" @change="onSelectChange" :clearable="clearable">
  15. <el-option disabled value="" style="margin-bottom:5px;">
  16. <el-row><el-button :type="deptUserVisible?'':'primary'" @click.stop="deptUserVisible=false">常用用户</el-button> <el-button :type="deptUserVisible?'primary':''" @click.stop="deptUserVisible=true"></el-button><el-button v-if="projectId" :type="projectVisible?'primary':''" @click.stop="projectVisible=true"></el-button> </el-row>
  17. </el-option>
  18. <el-option class="avatar-container" v-for="(item,index) in users" :key="index" :value="item" :label="item.username">
  19. <div class="avatar-wrapper">
  20. <el-avatar class="user-avatar" :style="{backgroundColor:getMyColor(item)}">{{item.username}}</el-avatar>
  21. <span class="username">{{item.username}}</span>
  22. <i v-if="myVal.userid==item.userid" class="el-icon-check"></i>
  23. <i v-else>&nbsp;&nbsp;</i>
  24. </div>
  25. </el-option>
  26. </el-select>
  27. </div>
  28. </div>
  29. <el-dialog :visible.sync="deptUserVisible" append-to-body top="20px" width="60%">
  30. <users-select :visible="deptUserVisible" :isSingleUser="true" :isSelectByDept="true" @confirm="onConfirmUsers"></users-select>
  31. </el-dialog>
  32. </el-row>
  33. </template>
  34. <script>
  35. import util from '@/common/js/util';//全局公共库
  36. import UsersSelect from '@/views/mdp/sys/user/UsersSelectOnly.vue'
  37. export default {
  38. name: 'user-field',
  39. components: { UsersSelect, },
  40. computed: {
  41. },
  42. data(){
  43. return {
  44. myVal:null,
  45. users:[],
  46. deptUserVisible:false,
  47. projectVisible:false,
  48. }
  49. },
  50. watch:{
  51. value:{
  52. handler(){
  53. this.initData();
  54. },
  55. deep:true,
  56. },
  57. myVal(){
  58. if(this.value instanceof String){
  59. this.$emit('input',this.myVal)
  60. }else if(this.value instanceof Object){
  61. if(!this.myVal||!this.myVal.userid){
  62. if(this.value[this.useridKey]){
  63. this.value[this.useridKey]=""
  64. this.value[this.usernameKey]=""
  65. this.$emit('input',this.value)
  66. }
  67. }else{
  68. if(this.value[this.useridKey]!=this.myVal.userid){
  69. this.value[this.useridKey]=this.myVal.userid
  70. this.value[this.usernameKey]=this.myVal.username
  71. this.$emit('input',this.value)
  72. }
  73. }
  74. }
  75. }
  76. },
  77. props: {
  78. projectId:{
  79. type:String,
  80. default:''
  81. },
  82. disabled:{
  83. type:Boolean,
  84. default:false,
  85. },
  86. label:{
  87. type:String,
  88. default:''
  89. },
  90. clearable:{
  91. type:Boolean,
  92. default:false,
  93. },
  94. value: {
  95. },
  96. useridKey: {
  97. type: String,
  98. default: 'userid'
  99. },
  100. usernameKey: {
  101. type: String,
  102. default: 'username'
  103. },
  104. },
  105. methods: {
  106. showMyValue(myVal){
  107. if(!myVal){
  108. return ""
  109. }else{
  110. if(this.value instanceof String){
  111. return myVal
  112. }else if(this.value instanceof Object){
  113. if(!myVal||!myVal.userid){
  114. return ""
  115. }
  116. if(myVal.username){
  117. return myVal.username
  118. }else if(myVal.userid){
  119. return myVal.userid
  120. }else{
  121. return ""
  122. }
  123. }
  124. }
  125. },
  126. getMyAvaterInfo(item){
  127. return this.showMyValue(item)
  128. },
  129. getMyColor(item){
  130. if(this.value instanceof String){
  131. if(item){
  132. if(this.getColor){
  133. return this.getColor(item)
  134. }
  135. return util.getColor(item)
  136. }else{
  137. if(this.getColor){
  138. return this.getColor("0")
  139. }else{
  140. return util.getColor(0)
  141. }
  142. }
  143. }else if(this.value instanceof Object){
  144. if(item&&item.userid){
  145. if(this.getColor){
  146. return this.getColor(item.userid)
  147. }
  148. return util.getColor(item.userid)
  149. }else{
  150. if(this.getColor){
  151. return this.getColor("0")
  152. }else{
  153. return util.getColor(0)
  154. }
  155. }
  156. }
  157. },
  158. getMyIcon(item){
  159. if(item){
  160. if(this.getIcon){
  161. return this.getIcon(item)
  162. }
  163. return "el-icon-user";
  164. }else{
  165. if(this.getIcon){
  166. return this.getIcon(this.myVal)
  167. }else{
  168. return "el-icon-user"
  169. }
  170. }
  171. },
  172. initData(){
  173. if(this.value instanceof String){
  174. this.myVal=this.value
  175. }else if(this.value instanceof Object){
  176. this.myVal={}
  177. this.myVal.userid=this.value[this.useridKey]
  178. this.myVal.username=this.value[this.usernameKey]
  179. }
  180. },
  181. onSelectChange(item){
  182. this.onChange([item])
  183. },
  184. onChange(data){
  185. this.$emit('change',data)
  186. },
  187. onConfirmUsers(users){
  188. this.onChange(users)
  189. this.myVal=users[0]
  190. this.deptUserVisible=false;
  191. var notHad=false;
  192. users.forEach(u=>{
  193. if(!this.users.some(k=>k.userid==u.userid)){
  194. notHad=true;
  195. this.users.unshift(u)
  196. }
  197. })
  198. if(notHad){
  199. var us=JSON.stringify(this.users)
  200. localStorage.setItem("mdp-his-users",us)
  201. }
  202. }
  203. },
  204. mounted(){
  205. var us=localStorage.getItem("mdp-his-users")
  206. this.users=us?JSON.parse(us):[]
  207. this.initData();
  208. }
  209. }
  210. </script>
  211. <style lang="scss" scoped>
  212. .field-box {
  213. display: flex;
  214. margin-right:5px;
  215. align-items: center;
  216. cursor: pointer;
  217. .avater {
  218. background-color:#FF9F73;
  219. }
  220. .field-info {
  221. margin-left: 10px;
  222. display: flex;
  223. flex-direction: column;
  224. .field-value {
  225. font-size: 16px;
  226. }
  227. .field-label{
  228. font-size: 14px;
  229. color: #C0C4CC;
  230. }
  231. }
  232. .my-select{
  233. margin-left: 5px;
  234. margin-right:5px;
  235. max-width: 120px;
  236. display: none;
  237. }
  238. .btn{
  239. margin-top: 0px;
  240. visibility:hidden;
  241. }
  242. }
  243. .field-box:hover .btn{
  244. visibility: visible !important;
  245. }
  246. .field-box:hover .my-select{
  247. margin-left: 5px;
  248. display: inline;
  249. }
  250. .avatar-container {
  251. height: 50px;
  252. display: flex;
  253. align-items: center;
  254. .avatar-wrapper {
  255. cursor: pointer;
  256. display: flex;
  257. flex-direction: row;
  258. align-items: center;
  259. .user-avatar {
  260. height: 34px;
  261. width: 34px;
  262. border-radius: 50%;
  263. margin-right: 12px;
  264. }
  265. .username{
  266. color: #7D7D7D;
  267. font-size: 14px;
  268. }
  269. .el-icon-caret-bottom {
  270. font-size: 22px;
  271. }
  272. }
  273. }
  274. </style>