Department.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace app\admin\controller\qingdong\dingding;
  3. use app\common\controller\Backend;
  4. use addons\qingdong\model\StaffDepartment;
  5. use fast\Tree;
  6. use app\admin\model\AuthGroup;
  7. use think\Exception;
  8. /**
  9. * 钉钉同步部门
  10. *
  11. */
  12. class Department extends Backend
  13. {
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->model = new StaffDepartment();
  18. }
  19. /**
  20. * 部门列表
  21. */
  22. public function index(){
  23. //设置过滤方法
  24. $this->request->filter(['strip_tags', 'trim']);
  25. if ($this->request->isAjax()) {
  26. // 必须将结果集转换为数组
  27. $ruleList = collection($this->model->where(['type'=>0])->order('id', 'desc')->select())->toArray();
  28. foreach ($ruleList as $k => &$v) {
  29. $v['name'] = __($v['name']);
  30. }
  31. unset($v);
  32. Tree::instance()->init($ruleList);
  33. $rulelists = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
  34. $ruledata = [0 => __('None')];
  35. foreach ($rulelists as $k => &$v) {
  36. $ruledata[$v['id']] = $v['name'];
  37. }
  38. $list = $rulelists;
  39. $total = count($rulelists);
  40. $result = array("total" => $total, "rows" => $list);
  41. return json($result);
  42. }
  43. return $this->view->fetch();
  44. }
  45. /**
  46. * 删除部门
  47. */
  48. public function del($ids = null) {
  49. if ($this->request->isAjax()) {
  50. //删除子部门
  51. $where=['pid' => array('in', $ids),'type'=>0];
  52. StaffDepartment::where($where)->delete();
  53. $map['id'] = array('in', $ids);
  54. $result = StaffDepartment::where($map)->delete();
  55. if (!$result) {
  56. $this->error('删除失败');
  57. }
  58. $this->success('删除成功');
  59. }
  60. return $this->view->fetch();
  61. }
  62. /**
  63. * 同步到CRM
  64. */
  65. public function batch(){
  66. $info = $this->model->where(array('status'=>0))->find();
  67. if(!$info){
  68. $this->error('无数据可同步');
  69. }
  70. try {
  71. $result=$this->setDepartment();
  72. }catch (Exception $e){
  73. $this->error($e->getMessage());
  74. }
  75. if($result){
  76. $this->success('同步成功');
  77. }
  78. $this->error('无数据可同步');
  79. }
  80. /**
  81. * 设置部门
  82. */
  83. private function setDepartment($pid=0,$idinfo=0){
  84. $list = $this->model->where(array('type'=>0,'pid'=>$pid))->select();
  85. if(empty($list)){
  86. return false;
  87. }
  88. foreach ($list as $k => $v) {
  89. if($v['status'] != 0){
  90. $this->setDepartment($v['id'], $v['role_id']);
  91. continue;
  92. }
  93. $res1 = array(
  94. 'pid' => $idinfo,
  95. 'name' => $v['name'],
  96. 'status' => 'normal',
  97. 'rules' => '*'
  98. );
  99. $result1 = AuthGroup::create($res1);
  100. if (!$result1) {
  101. throw new Exception('同步失败');
  102. }
  103. $idinfo = AuthGroup::getLastInsID();
  104. $pid = $v['id'];
  105. $this->setDepartment($pid, $idinfo);
  106. $this->model->where(array('id' => $v['id']))->update(array('status' => 1, 'role_id' => $idinfo));
  107. }
  108. return true;
  109. }
  110. }