deptselector.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <div>
  3. <el-select id="deptSelector" v-model="selectValue" placeholder="请选择" @change="mychange">
  4. <el-option :key="index" :value="dept.deptId" :label="dept.deptName" v-for="(dept,index) in selectList">
  5. </el-option>
  6. </el-select>
  7. </div>
  8. </template>
  9. <script>
  10. import {getDepts} from '../api/components/deptselector'
  11. import {setToken} from '../plugins/auth'
  12. import VueCookies from 'vue-cookies'
  13. export default {
  14. name: "deptSelector",
  15. props:{
  16. setValue : Function
  17. },
  18. created() {
  19. setToken("eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxMDAsInVzZXJfa2V5IjoiNDAwOGIwMmUtZTFhNS00OTM4LTkxOGUtMGUxMjA4NDExYzMxIiwidXNlcm5hbWUiOiJzdW55aXNoaSJ9.uUKds26n0WUVB0TjpZyZG4GhOWpBLEyzQKin2i_VIt_26omwZPVHoRo1LdVFt0HicOiKao5OGCq-CSYRUTq_FA")
  20. this.showDepts();
  21. },
  22. data(){
  23. return {
  24. selectDeptCookieKey: "Global-Dept",
  25. selectValue: '',
  26. selectList: [],
  27. }
  28. },
  29. methods :{
  30. mychange : function(){
  31. this.setValue(this.selectValue);
  32. VueCookies.set(this.selectDeptCookieKey, this.selectValue)
  33. },
  34. showDepts(){
  35. let _this = this;
  36. getDepts({userName:"sunyishi"}).then(resp =>{
  37. let deptList = new Array();
  38. //console.log(resp);
  39. let depts = resp.data.depts;
  40. let deptParties = resp.data.deptParties;
  41. for (let i = 0; i < depts.length; i++) {
  42. let dept = new Object();
  43. dept.deptId = depts[i].deptId;
  44. dept.deptName = depts[i].deptName;
  45. deptList.push(dept);
  46. }
  47. for (let i = 0; i < deptParties.length; i++) {
  48. let dept = new Object();
  49. dept.deptId = deptParties[i].deptId;
  50. dept.deptName = deptParties[i].deptName;
  51. deptList.push(dept);
  52. }
  53. _this.selectList = deptList;
  54. if(deptList.length == 0){//当前用户没有部门
  55. //alert("当前用户没有部门")
  56. return;
  57. }
  58. let cookie = VueCookies.get(this.selectDeptCookieKey);
  59. if(cookie != null){//cookie 获取到了 证明曾经触发过 onchange
  60. console.log(cookie+'=cookie 获取到了 证明曾经触发过 onchange');
  61. let isCookieExists = false;
  62. deptList.forEach((dept) => {
  63. if(cookie == dept.deptId){
  64. isCookieExists = true;
  65. _this.selectValue = dept.deptId;
  66. _this.setValue(_this.selectValue);
  67. //break;
  68. }
  69. });
  70. if(isCookieExists == false) {//cookie存在但当前不在那个部门了
  71. console.log("cookie存在但当前不在那个部门了");
  72. _this.selectValue = deptList[0].deptId;
  73. _this.setValue(_this.selectValue);
  74. }
  75. }else {
  76. _this.selectValue = deptList[0].deptId;
  77. _this.setValue(_this.selectValue);
  78. console.log(cookie+'=cookie false');
  79. }
  80. // this.$emit("wangzhe",xxxxx)
  81. })
  82. }
  83. }
  84. }
  85. </script>
  86. <style scoped>
  87. </style>