Department.php 3.5 KB

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