code.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. import search from './search/index.vue'
  2. /*
  3. * 已兼容h5和小程序端,其它端没测试过,估计问题不大,只需要改一下传值的方式
  4. *
  5. * 如有问题可以加qq:122720267
  6. *
  7. * 使用该插件的朋友请给个好评,或者到git start一下
  8. * git地址:https://github.com/LSZ579/xiaolu-tree-plugin.git
  9. * 插件市场地址: https://ext.dcloud.net.cn/plugin?id=2423
  10. *
  11. */
  12. import api from '@/api/api.js'
  13. export default {
  14. name: "treeComponent",
  15. data() {
  16. return {
  17. isre: false,
  18. tree: Object.freeze(this.treeNone),
  19. newNum: 0,
  20. oldNum: 0,
  21. orgText:null,
  22. catchTreeNone: [...this.treeNone],
  23. tree_stack: [1],
  24. searchResult: [],
  25. newCheckList: this.checkList,
  26. scrollLeft: 999,
  27. nodePathArray: []
  28. }
  29. },
  30. components: {
  31. search
  32. },
  33. computed: {
  34. isSelect() {
  35. return (item) => {
  36. const checkList = this.newCheckList
  37. if (checkList.length == 0) {
  38. this.props.checkStrictly ? (item.bx = 0, item.qx = 0) : ''
  39. return false
  40. }
  41. const i = checkList.findIndex(e => {
  42. return item[this.keyCode] == e[this.keyCode]
  43. }) > -1
  44. return i && !item.qx
  45. }
  46. },
  47. radioSelect() {
  48. const list = this.newCheckList
  49. return (item) => {
  50. return list.length > 0 && item[this.keyCode] == list[0][this.keyCode]
  51. }
  52. },
  53. keyCode() {
  54. return this.keyValue
  55. }
  56. },
  57. created() {
  58. this.Init()
  59. this.querydepartName()
  60. },
  61. methods: {
  62. getImg(a){
  63. var img='../../static/org.png';
  64. if(a.user){
  65. img='/static/avatar_boy.png';
  66. }
  67. var avat=a.avatar!=null? api.getFileAccessHttpUrl(a.avatar):img
  68. return avat;
  69. },
  70. querydepartName(){
  71. this.$http.get('/sys/user/departList',{params:{}}).then(res=>{
  72. console.log("res",res)
  73. this.orgText=res.data.orgName.departName;
  74. }).catch(err => {
  75. console.log(err);
  76. });
  77. },
  78. querydepart(e){
  79. this.$http.get('/sys/user/departList',{params:{departName:e}}).then(res=>{
  80. console.log("res",res)
  81. this.orgText=res.data.orgName.departName;
  82. this.tree=res.data.departName;
  83. }).catch(err => {
  84. console.log(err);
  85. });
  86. },
  87. // 初始化
  88. Init() {
  89. if (this.newCheckList.length !== 0) {
  90. let {
  91. tree_stack,
  92. props,
  93. catchTreeNone,
  94. newCheckList
  95. } = this
  96. if (props.multiple) {
  97. if (props.checkStrictly) {
  98. this.checkAllChoose();
  99. }
  100. } else {
  101. this.getNodeRoute(catchTreeNone, newCheckList[0][this.keyCode])
  102. let arr = this.nodePathArray.reverse()
  103. console.log(arr)
  104. if (arr.length == 0) return
  105. this.tree_stack = tree_stack.concat(arr);
  106. this.tree = this.tree_stack[this.tree_stack.length - 1].children;
  107. }
  108. }
  109. },
  110. // 返回 类似 2016-01-02 格式的字符串
  111. formatDate: function(year, month, day) {
  112. var y = year
  113. var m = month
  114. if (m < 10) m = '0' + m
  115. var d = day
  116. if(day!=null &&day !=""){
  117. if (d < 10) d = '0' + d
  118. return y + '-' + m + '-' + d
  119. }else{
  120. return y + '-' + m
  121. }
  122. },
  123. handles(item){
  124. uni.setStorageSync("id",item.id);
  125. var orgType=uni.getStorageSync('orgType');
  126. /* if(!item.user&&orgType==1){
  127. this.$store.commit('SET_DEPARTID',item.id)
  128. this.$Router.push({name:'empsign'})
  129. }else */if (item.user&&orgType==1){
  130. var date=new Date();
  131. var currentYear = date.getFullYear()
  132. var currentMonth = date.getMonth() + 1
  133. var dates=this.formatDate(currentYear,currentMonth,1);
  134. uni.setStorageSync("status",2);
  135. uni.setStorageSync("date",dates);
  136. this.$Router.push({name:'usersign'})
  137. }else if(orgType==2&&item.user){
  138. this.$Router.push({name:'leaves'})
  139. }else if(orgType==3&&item.user){
  140. this.$Router.push({name:'wagesdetails'})
  141. }else if(orgType==4&&item.user){
  142. this.$Router.push({name:'userwork'})
  143. }
  144. },
  145. // 点击项目处理
  146. handleClick(item, index) {
  147. let children = item[this.props.children]
  148. if (index > -1 && children && children.length > 0) {
  149. this.toChildren(item)
  150. } else if (this.props.multiple) {
  151. this.checkboxChange(item, index, item.bx, item.qx)
  152. } else {
  153. this.checkbox(item, index)
  154. }
  155. this.handles(item);
  156. },
  157. // 获取路径
  158. getPath() {
  159. const {
  160. keyCode,
  161. tree_stack,
  162. props
  163. } = this
  164. const path = [...tree_stack].map(e => {
  165. const item = Object.assign({}, e)
  166. delete item[props.children]
  167. return item
  168. })
  169. return path.slice(1, path.length) || []
  170. },
  171. //多选
  172. async checkboxChange(item, index, bx, qx) {
  173. let that = this;
  174. const {
  175. props
  176. } = that
  177. if (!props.multiple) return;
  178. let findIdex = that.newCheckList.findIndex(e => item[this.keyCode] == e[this.keyCode]);
  179. const path = this.getPath()
  180. if (findIdex > -1) { //反选
  181. if (props.checkStrictly) { //关联子级
  182. if (item.user) { //用户
  183. that.newCheckList.splice(findIdex, 1)
  184. } else { //非用户,取消所有下一级
  185. that.getIdBydelete(item.children)
  186. }
  187. } else {
  188. that.newCheckList.splice(findIdex, 1)
  189. }
  190. } else { //选中
  191. if (!item.user && props.checkStrictly) { //选中下一级
  192. if (qx || bx) { //取消下级
  193. await that.getIdBydelete(item.children);
  194. item.qx = 0;
  195. item.bx = 0;
  196. } else {
  197. item.qx = 1;
  198. item.bx = 0;
  199. const {
  200. id,
  201. name,
  202. user
  203. } = item
  204. const newObj = {
  205. id,
  206. name,
  207. user
  208. }
  209. const pathList = this.tree_stack.length === 1 ? [newObj, ...path] : [...path, newObj]
  210. await that.chooseChild(item.children, pathList);
  211. }
  212. this.$forceUpdate()
  213. return
  214. }
  215. that.newCheckList.push({ ...item,
  216. path
  217. });
  218. }
  219. },
  220. // 取消下一级的选中
  221. getIdBydelete(arr) {
  222. arr.forEach(e => {
  223. if (e.user) {
  224. for (var i = 0; i < this.newCheckList.length; i++) {
  225. if (e[this.keyCode] == this.newCheckList[i][this.keyCode]) {
  226. this.newCheckList.splice(i, 1)
  227. break;
  228. }
  229. }
  230. } else {
  231. this.getIdBydelete(e.children)
  232. }
  233. })
  234. },
  235. // 关联下一级,选中
  236. chooseChild(arr, path) {
  237. let that = this;
  238. const oldPath = [...path]
  239. for (var i = 0, len = arr.length; i < len; i++) {
  240. let item = arr[i];
  241. if (item.user) {
  242. that.newCheckList.push({ ...item,
  243. path: oldPath
  244. })
  245. } else {
  246. const newItem = { ...item
  247. }
  248. delete newItem[that.props.children]
  249. const newPath = [...oldPath, newItem]
  250. that.chooseChild(item.children, newPath)
  251. }
  252. }
  253. },
  254. // (tree为目标树,targetId为目标节点id)
  255. getNodeRoute(tree, targetId) {
  256. for (let index = 0; index < tree.length; index++) {
  257. if (tree[index].children) {
  258. let endRecursiveLoop = this.getNodeRoute(tree[index].children, targetId)
  259. if (endRecursiveLoop) {
  260. this.nodePathArray.push(tree[index])
  261. return true
  262. }
  263. }
  264. if (tree[index][this.keyCode] === targetId) {
  265. return true
  266. }
  267. }
  268. },
  269. //单选
  270. checkbox(item, index) {
  271. const path = this.getPath()
  272. this.$set(this, 'newCheckList', [{ ...item,
  273. path
  274. }])
  275. },
  276. //到下一级
  277. toChildren(item) {
  278. if (item.user) return
  279. var that = this;
  280. uni.showLoading({
  281. title: '加载中'
  282. })
  283. let children = that.props.children;
  284. if (item[children].length > 0 && !(that.tree_stack[0][this.keyCode] == item[this.keyCode])) {
  285. that.tree = item[children];
  286. that.tree_stack.push(item);
  287. }
  288. this.$nextTick(() => {
  289. uni.hideLoading()
  290. this.scrollLeft += 200;
  291. })
  292. if (this.props.checkStrictly) this.checkAllChoose();
  293. },
  294. //搜索
  295. confirmSearch(val) {
  296. console.log(val);
  297. this.searchResult = []
  298. //this.search(this.catchTreeNone, val)
  299. this.isre = true
  300. this.tree_stack.splice(1, 1000)
  301. uni.showLoading({
  302. title: '正在查找'
  303. })
  304. setTimeout(() => {
  305. this.tree =[];
  306. this.querydepart(val);
  307. //this.tree = this.searchResult
  308. uni.hideLoading()
  309. }, 300)
  310. },
  311. search(data, keyword) {
  312. let that = this
  313. let {label,children} = that.props
  314. for (var i = 0, len = data.length; i < len; i++) {
  315. if (data[i][label].indexOf(keyword) >= 0) {
  316. that.searchResult.push(data[i])
  317. }
  318. if (!data[i].user && data[i][children].length > 0) {
  319. that.search(data[i][children], keyword)
  320. }
  321. }
  322. },
  323. checkAllChoose() {
  324. let o = false,
  325. t = true;
  326. this.tree.forEach((e, i) => {
  327. if (!e.user) {
  328. e.qx = o;
  329. e.bx = o;
  330. this.computAllNumber(e.children);
  331. // console.log(this.newNum,this.oldNum)
  332. if (this.newNum != 0 && this.oldNum != 0) {
  333. if (this.newNum == this.oldNum) {
  334. e.qx = t;
  335. e.bx = o;
  336. } else {
  337. e.qx = o;
  338. e.bx = t;
  339. }
  340. }
  341. if (this.newNum != 0 && this.oldNum == 0) {
  342. this.$set(this.tree[i], 'bx', o);
  343. this.$set(this.tree[i], 'qx', o);
  344. }
  345. this.$forceUpdate()
  346. this.newNum = 0
  347. this.oldNum = 0
  348. }
  349. })
  350. },
  351. computAllNumber(arr) {
  352. for (let j = 0; j < arr.length; j++) {
  353. var e = arr[j];
  354. this.checkSum(e[this.keyCode])
  355. if (e.user) {
  356. this.newNum++;
  357. } else {
  358. this.computAllNumber(e.children)
  359. }
  360. }
  361. },
  362. checkSum(id) {
  363. for (let i = 0; i < this.newCheckList.length; i++) {
  364. if (id == this.newCheckList[i][this.keyCode]) {
  365. this.oldNum++;
  366. break
  367. }
  368. }
  369. },
  370. //返回其它层
  371. backTree(item, index) {
  372. let that = this,
  373. tree_stack = that.tree_stack,
  374. max = 300;
  375. if (index === -1) {
  376. that.tree = that.catchTreeNone
  377. that.tree_stack.splice(1, max)
  378. that.isre = false
  379. that.$refs.sea.clears()
  380. } else if (index === -2) {//搜索
  381. that.tree = that.searchResult
  382. that.tree_stack.splice(1, max)
  383. } else {
  384. if (tree_stack.length - index > 2) {
  385. that.tree_stack.splice(index+1, max)
  386. } else if (index !== tree_stack.length - 1) {
  387. that.tree_stack.splice(tree_stack.length - 1, 1)
  388. }
  389. that.tree = item[that.props.children]
  390. }
  391. if (this.props.checkStrictly) this.checkAllChoose();
  392. this.$forceUpdate()
  393. },
  394. backConfirm() {
  395. this.$emit('sendValue', this.newCheckList, 'back')
  396. }
  397. },
  398. props: {
  399. treeNone: {
  400. type: Array,
  401. default: () => {
  402. return []
  403. }
  404. },
  405. //是否开启选中
  406. isCheck: {
  407. type: Boolean,
  408. default: () => {
  409. return false
  410. }
  411. },
  412. checkList: {
  413. type: Array,
  414. default: () => []
  415. },
  416. parentList: {
  417. type: Array,
  418. default: () => []
  419. },
  420. searchIf: {
  421. type: Boolean,
  422. default: () => true
  423. },
  424. keyValue: {
  425. type: String,
  426. default: 'id',
  427. },
  428. props: {
  429. type: Object,
  430. default: () => {
  431. return {
  432. label: 'name',
  433. children: 'children',
  434. multiple: false,
  435. checkStrictly: false, //不关联
  436. }
  437. }
  438. }
  439. },
  440. }