userLabel.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <div class="label-wrapper">
  3. <div v-if="!labelList[0]" class="nonefont">暂无标签</div>
  4. <div v-else class="label-box" v-for="(item, index) in labelList" :key="index">
  5. <div class="title">{{ item.name }}</div>
  6. <div class="list">
  7. <div
  8. class="label-item"
  9. :class="{ on: label.disabled }"
  10. v-for="(label, j) in item.label"
  11. :key="j"
  12. @click="selectLabel(label)"
  13. >
  14. {{ label.label_name }}
  15. </div>
  16. </div>
  17. </div>
  18. <div class="footer">
  19. <Button type="primary" class="btns" @click="subBtn">确定</Button>
  20. <Button type="primary" class="btns" ghost @click="cancel">取消</Button>
  21. </div>
  22. </div>
  23. </template>
  24. <script>
  25. import { getUserLabel, putUserLabel } from '@/api/user';
  26. export default {
  27. name: 'userLabel',
  28. props: {
  29. uid: {
  30. type: String | Number,
  31. default: 0,
  32. },
  33. only_get: {
  34. default: false,
  35. },
  36. selectDataLabel: {
  37. type: Array,
  38. default: () => {
  39. [];
  40. },
  41. },
  42. },
  43. data() {
  44. return {
  45. labelList: [],
  46. activeIds: [],
  47. unLaberids: [],
  48. };
  49. },
  50. watch: {
  51. uid: {
  52. handler(nVal, oVal) {
  53. if (nVal != oVal) {
  54. this.getList();
  55. }
  56. },
  57. deep: true,
  58. },
  59. },
  60. mounted() {
  61. this.getList();
  62. },
  63. methods: {
  64. getList() {
  65. getUserLabel(this.uid || 0).then((res) => {
  66. if (this.selectDataLabel && this.selectDataLabel.length) {
  67. this.selectDataLabel.map((el) => {
  68. res.data.map((re) => {
  69. re.label.map((label) => {
  70. if (label.id === el.id) {
  71. label.disabled = true;
  72. }
  73. });
  74. });
  75. });
  76. }
  77. res.data.map((el) => {
  78. el.label.map((label) => {
  79. if (label.disabled) {
  80. this.activeIds.push(label.id);
  81. }
  82. });
  83. });
  84. this.labelList = res.data;
  85. });
  86. },
  87. selectLabel(label) {
  88. if (label.disabled) {
  89. let index = this.activeIds.indexOf(label.id);
  90. this.activeIds.splice(index, 1);
  91. label.disabled = false;
  92. } else {
  93. this.activeIds.push(label.id);
  94. label.disabled = true;
  95. }
  96. },
  97. // 确定
  98. subBtn() {
  99. let unLaberids = [];
  100. if (this.only_get) {
  101. this.labelList.map((item) => {
  102. item.label.map((i) => {
  103. if (i.disabled == true) {
  104. unLaberids.push({ id: i.id, label_name: i.label_name });
  105. }
  106. });
  107. });
  108. this.$emit('activeData', unLaberids);
  109. return;
  110. }
  111. this.labelList.map((item) => {
  112. item.label.map((i) => {
  113. if (i.disabled == false) {
  114. unLaberids.push(i.id);
  115. }
  116. });
  117. });
  118. this.unLaberids = unLaberids;
  119. putUserLabel(this.uid, {
  120. label_ids: this.activeIds,
  121. un_label_ids: this.unLaberids,
  122. })
  123. .then((res) => {
  124. this.$emit('onceGetList');
  125. this.activeIds = [];
  126. this.unLaberids = [];
  127. this.$Message.success(res.msg);
  128. this.$emit('close');
  129. })
  130. .catch((error) => {
  131. this.$Message.error(error.msg);
  132. });
  133. },
  134. cancel() {
  135. this.activeIds = [];
  136. this.unLaberids = [];
  137. this.$emit('close');
  138. },
  139. },
  140. };
  141. </script>
  142. <style lang="stylus" scoped>
  143. .label-wrapper {
  144. .list {
  145. display: flex;
  146. flex-wrap: wrap;
  147. .label-item {
  148. margin: 10px 8px 10px 0;
  149. padding: 3px 8px;
  150. background: #EEEEEE;
  151. color: #333333;
  152. border-radius: 2px;
  153. cursor: pointer;
  154. font-size: 12px;
  155. &.on {
  156. color: #fff;
  157. background: #1890FF;
  158. }
  159. }
  160. }
  161. .footer {
  162. display: flex;
  163. justify-content: flex-end;
  164. margin-top: 40px;
  165. button {
  166. margin-left: 10px;
  167. }
  168. }
  169. }
  170. .btn {
  171. width: 60px;
  172. height: 24px;
  173. }
  174. .title {
  175. font-size: 13px;
  176. }
  177. .nonefont {
  178. text-align: center;
  179. padding-top: 20px;
  180. }
  181. </style>