DepartmentController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/9/22
  8. * Time: 13:37
  9. */
  10. namespace business\admin\controllers;
  11. use business\common\models\Department;
  12. use business\common\models\DepartmentStaff;
  13. use business\common\services\DepartmentService;
  14. use business\common\services\SettingService;
  15. use business\common\controllers\components\BaseController;
  16. use Exception;
  17. class DepartmentController extends BaseController
  18. {
  19. /*
  20. * 从企业微信同步部门列表
  21. */
  22. public function refreshDepartmentList()
  23. {
  24. if (!SettingService::EnabledQyWx()) {
  25. return $this->errorJson('未开启企业微信', ['is_set' => 1]);
  26. }
  27. $res = DepartmentService::refreshDepartment();
  28. if (!$res['result']) {
  29. return $this->errorJson($res['msg']);
  30. }
  31. return $this->successJson('成功');
  32. }
  33. /*
  34. * 获取部门列表
  35. */
  36. public function getDepatmemtList()
  37. {
  38. $res = DepartmentService::getDepartmentList();
  39. if (!$res['result']) {
  40. return $this->errorJson($res['msg']);
  41. }
  42. $res['data'] = DepartmentService::addDepartmentPremission($res['data']);
  43. return $this->successJson('成功', ['list' => $res['data']]);
  44. }
  45. /*
  46. * 添加部门
  47. */
  48. public function createDepartment()
  49. {
  50. if (!DepartmentService::checkDepartmentIdByMethod('createDepartment', \request()->parent_id)) {
  51. return $this->errorJson('无权操作');
  52. }
  53. $order = intval(\request()->order) ?: null;
  54. $res = DepartmentService::changeDepartment(\request()->name, \request()->parent_id??0, 0, $order);
  55. if (!$res['result']) {
  56. return $this->errorJson($res['msg']);
  57. }
  58. return $this->successJson('创建成功');
  59. }
  60. /*
  61. * 编辑部门
  62. */
  63. public function updateDepartment()
  64. {
  65. if (!DepartmentService::checkDepartmentIdByMethod('updateDepartment', \request()->id)) {
  66. return $this->errorJson('无权操作');
  67. }
  68. if (!$department = Department::business()->find(\request()->id)) {
  69. return $this->errorJson('请选择要编辑的部门');
  70. }
  71. $order = intval(\request()->order) ?: null;
  72. $res = DepartmentService::changeDepartment(\request()->name, $department->parent_id, $department->id, $order);
  73. if (!$res['result']) {
  74. return $this->errorJson($res['msg']);
  75. }
  76. return $this->successJson('编辑成功');
  77. }
  78. /*
  79. * 删除部门
  80. */
  81. public function deleteDepartment()
  82. {
  83. if (!DepartmentService::checkDepartmentIdByMethod('deleteDepartment', \request()->id)) {
  84. return $this->errorJson('无权操作');
  85. }
  86. $res = DepartmentService::deleteDepartment(\request()->id);
  87. if (!$res['result']) {
  88. return $this->errorJson($res['msg']);
  89. }
  90. return $this->successJson('删除成功');
  91. }
  92. /*
  93. * 推送部门列表到企业微信
  94. */
  95. public function pushDepartment()
  96. {
  97. try {
  98. if (!SettingService::EnabledQyWx()) {
  99. throw new Exception('未开启企业微信');
  100. }
  101. $department = Department::business()->with('hasOneParentDepartment')->orderBy('level', 'ASC')->get();
  102. $department->each(function ($v) {
  103. if ($v->level == 1 && $v->wechat_department_id == 0) {
  104. throw new Exception('一级部门未关联企业微信');
  105. }
  106. $res = DepartmentService::pushDepartment($v);
  107. if (!$res['result']) {
  108. throw new Exception($res['msg']);
  109. }
  110. });
  111. } catch (Exception $e) {
  112. return $this->errorJson($e->getMessage());
  113. }
  114. return $this->successJson('推送成功');
  115. }
  116. }