select-tag.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. var demo_str = `
  2. <van-popup v-model="popupVisible" duration="0.2" round position="bottom" :close-on-click-overlay="false" :style="{ width: '100%', height: '566px' }">
  3. <div class="clientTag_title">
  4. <div class="close_icon"></div>
  5. <div>按客户标签筛选</div>
  6. <img class="close_icon" :src="closeIcon" alt="" @click="popupVisible = false">
  7. </div>
  8. <div class="search_box">
  9. <van-search class="search_input" placeholder="搜索标签" v-model="keyword" search :clearable="false" left-icon="" @search="getTagList">
  10. <!-- 自定义右侧图标 -->
  11. <template v-slot:right-icon>
  12. <img class="search_icon" :src="searchIcon" alt="" @click="getTagList" />
  13. </template>
  14. </van-search>
  15. </div>
  16. <van-radio-group v-model="tagType" direction="horizontal" v-if="showLabelFilter">
  17. <van-radio :name="0">
  18. 标签满足其一
  19. <template #icon="props">
  20. <div :class="props.checked ? 'check_div_active' : 'check_div'"></div>
  21. </template>
  22. </van-radio>
  23. <van-radio :name="1">
  24. 标签同时满足
  25. <template #icon="props">
  26. <div :class="props.checked ? 'check_div_active' : 'check_div'"></div>
  27. </template>
  28. </van-radio>
  29. </van-radio-group>
  30. <div class="tag_list1">
  31. <div class="tag_item" v-for="(item, index) in tagGroupList" :key="index">
  32. <div class="tag_top">
  33. <van-checkbox v-model="item.single" @change="selectAllTag(item)">
  34. <template #icon="props">
  35. <div :class="props.checked ? 'check_div_active' : 'check_div'"></div>
  36. </template>
  37. </van-checkbox>
  38. <div class="tag_item_title">{{ item.groupName }}</div>
  39. </div>
  40. <div class="tag_item_data">
  41. <span :class="{tag_checked: tag.checked, tag_unchecked: !tag.checked}" v-for="(tag, tagIndex) in item.tagList" :key="tagIndex" @click="handleTag(tag, item)">{{ tag.name }}</span>
  42. </div>
  43. </div>
  44. </div>
  45. <div class="tag_footer">
  46. <div class="tag_re_btn" @click="handleClear">重选</div>
  47. <div class="tag_ok_btn" @click="handleSubmit">确定</div>
  48. </div>
  49. </van-popup>
  50. `
  51. var demoComponent = Vue.extend({
  52. template:demo_str ,
  53. data: function () {
  54. // 初始化时先获取路径信息
  55. const isQwPath = window.location.pathname.includes('/qw')
  56. return {
  57. // 根据路径设置不同的图片路径
  58. closeIcon: isQwPath ? '../img/qw/close_icon.png' : './img/qw/close_icon.png',
  59. searchIcon: isQwPath ? '../img/qw/search_icon.png' : './img/qw/search_icon.png',
  60. httpUrl: '',
  61. env: '',
  62. keyword: '',
  63. tagType: 0,
  64. popupVisible: this.showClientTag, // 本地状态,避免直接修改 prop
  65. tagGroupList: [],
  66. showTagList: [],
  67. }
  68. },
  69. props: {
  70. labelFilter: {
  71. type: Number,
  72. default: 0
  73. },
  74. showClientTag: {
  75. type: Boolean,
  76. default: () => false
  77. },
  78. selectTagList: {
  79. type: Array,
  80. default: () => []
  81. },
  82. showLabelFilter: {
  83. type: Boolean,
  84. default: true
  85. }
  86. },
  87. watch: {
  88. showClientTag(val) {
  89. this.popupVisible = val
  90. if (val) {
  91. this.showTagList = this.selectTagList || []
  92. this.getTagList()
  93. this.tagType = this.labelFilter
  94. console.log('tagType', this.tagType)
  95. }
  96. },
  97. popupVisible(val) {
  98. this.$emit('update:showClientTag', val)
  99. }
  100. },
  101. created() {
  102. this.env = this.getQueryParam('env')
  103. if (!this.env || this.env === 'prod') {
  104. this.httpUrl = 'https://wlapi.wefanbot.com'
  105. } else {
  106. this.httpUrl = 'http://test.wefanbot.com:18993'
  107. }
  108. },
  109. methods: {
  110. getTagList() {
  111. const headers = new Headers()
  112. headers.append('token', 'k1176728601917100001')
  113. headers.append('tenancyId', '103548289110001')
  114. headers.append('userId', '100884320310007')
  115. fetch(this.httpUrl + `/scrm/v1/wxcp-tag/m/findListWithGroup?keyword=${this.keyword}`, {
  116. method: 'GET',
  117. headers: headers
  118. }).then(res => {
  119. return res.json()
  120. }).then(result => {
  121. let { data, code, msg } = result
  122. if (code === 1) {
  123. if (data && data.length > 0) {
  124. const groupedData = {}
  125. data.forEach(item => {
  126. if (!groupedData[item.groupId]) {
  127. groupedData[item.groupId] = {
  128. ...item,
  129. single: false,
  130. tagList: []
  131. }
  132. }
  133. this.$set(item, 'checked', false)
  134. const tag = {
  135. id: item.tagId,
  136. name: item.tagName,
  137. checked: item.checked,
  138. }
  139. groupedData[item.groupId].tagList.push(tag)
  140. delete groupedData[item.groupId].tagId
  141. delete groupedData[item.groupId].tagName
  142. delete groupedData[item.groupId].tagSort
  143. })
  144. // 将分组结果转换为数组形式
  145. this.tagGroupList = Object.values(groupedData)
  146. this.tagGroupList.forEach(item => {
  147. item.tagList.forEach(t => {
  148. this.selectTagList.forEach(tl => {
  149. if (t.id === tl.id) {
  150. this.$set(t, 'checked', true)
  151. }
  152. })
  153. })
  154. if (item.tagList.every(obj => obj['checked'] === true)) {
  155. this.$set(item, 'single', true)
  156. } else {
  157. this.$set(item, 'single', false)
  158. }
  159. })
  160. }
  161. } else {
  162. vant.Toast.fail(msg)
  163. }
  164. })
  165. },
  166. handleTag(val, data) {
  167. this.tagGroupList.forEach(item => {
  168. if (data.groupId === item.groupId) {
  169. item.tagList.forEach(t => {
  170. if (t.id === val.id) {
  171. this.$set(t, 'checked', !t.checked)
  172. }
  173. })
  174. if (val.checked) {
  175. this.showTagList.push(val)
  176. if (item.tagList.every(obj => obj['checked'] === true)) {
  177. this.$set(item, 'single', true)
  178. } else {
  179. this.$set(item, 'single', false)
  180. }
  181. } else {
  182. this.showTagList = this.showTagList.filter(item => item.id !== val.id)
  183. this.$set(item, 'single', false)
  184. }
  185. }
  186. })
  187. },
  188. selectAllTag(data) {
  189. this.tagGroupList.forEach(item => {
  190. if (data.groupId === item.groupId) {
  191. item.tagList.forEach(t => {
  192. this.$set(t, 'checked', data.single)
  193. if (data.single) {
  194. this.showTagList.push(t)
  195. // 去重
  196. const map = new Map()
  197. this.showTagList = this.showTagList.filter(v => !map.has(v.id) && map.set(v.id, v))
  198. } else {
  199. this.showTagList = this.showTagList.filter(s => s.id !== t.id)
  200. }
  201. })
  202. }
  203. })
  204. },
  205. handleClear() {
  206. this.showTagList = []
  207. this.tagGroupList.forEach(item => {
  208. item.single = false
  209. item.tagList.forEach(t => {
  210. this.$set(t, 'checked', false)
  211. })
  212. })
  213. },
  214. handleSubmit() {
  215. this.$emit('add', this.tagType, this.showTagList)
  216. this.$emit('update:showClientTag', false)
  217. },
  218. // 截取url中的数据
  219. getQueryParam(paramName) {
  220. // 获取当前URL的查询字符串部分
  221. const queryString = window.location.search;
  222. // 创建一个URLSearchParams对象
  223. const urlParams = new URLSearchParams(queryString);
  224. // 返回指定参数的值,如果不存在则返回null
  225. return urlParams.get(paramName);
  226. },
  227. },
  228. })
  229. Vue.component('select-tag',demoComponent ) //注:此处my-demo就是html中组件的名字