| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- /**
- * Created by PhpStorm.
- * Name: 芸众商城系统
- * Author: 广州市芸众信息科技有限公司
- * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
- * Date: 2021/9/22
- * Time: 13:37
- */
- namespace business\admin\controllers;
- use business\common\models\Department;
- use business\common\models\DepartmentStaff;
- use business\common\services\DepartmentService;
- use business\common\services\SettingService;
- use business\common\controllers\components\BaseController;
- use Exception;
- class DepartmentController extends BaseController
- {
- /*
- * 从企业微信同步部门列表
- */
- public function refreshDepartmentList()
- {
- if (!SettingService::EnabledQyWx()) {
- return $this->errorJson('未开启企业微信', ['is_set' => 1]);
- }
- $res = DepartmentService::refreshDepartment();
- if (!$res['result']) {
- return $this->errorJson($res['msg']);
- }
- return $this->successJson('成功');
- }
- /*
- * 获取部门列表
- */
- public function getDepatmemtList()
- {
- $res = DepartmentService::getDepartmentList();
- if (!$res['result']) {
- return $this->errorJson($res['msg']);
- }
- $res['data'] = DepartmentService::addDepartmentPremission($res['data']);
- return $this->successJson('成功', ['list' => $res['data']]);
- }
- /*
- * 添加部门
- */
- public function createDepartment()
- {
- if (!DepartmentService::checkDepartmentIdByMethod('createDepartment', \request()->parent_id)) {
- return $this->errorJson('无权操作');
- }
- $order = intval(\request()->order) ?: null;
- $res = DepartmentService::changeDepartment(\request()->name, \request()->parent_id??0, 0, $order);
- if (!$res['result']) {
- return $this->errorJson($res['msg']);
- }
- return $this->successJson('创建成功');
- }
- /*
- * 编辑部门
- */
- public function updateDepartment()
- {
- if (!DepartmentService::checkDepartmentIdByMethod('updateDepartment', \request()->id)) {
- return $this->errorJson('无权操作');
- }
- if (!$department = Department::business()->find(\request()->id)) {
- return $this->errorJson('请选择要编辑的部门');
- }
- $order = intval(\request()->order) ?: null;
- $res = DepartmentService::changeDepartment(\request()->name, $department->parent_id, $department->id, $order);
- if (!$res['result']) {
- return $this->errorJson($res['msg']);
- }
- return $this->successJson('编辑成功');
- }
- /*
- * 删除部门
- */
- public function deleteDepartment()
- {
- if (!DepartmentService::checkDepartmentIdByMethod('deleteDepartment', \request()->id)) {
- return $this->errorJson('无权操作');
- }
- $res = DepartmentService::deleteDepartment(\request()->id);
- if (!$res['result']) {
- return $this->errorJson($res['msg']);
- }
- return $this->successJson('删除成功');
- }
- /*
- * 推送部门列表到企业微信
- */
- public function pushDepartment()
- {
- try {
- if (!SettingService::EnabledQyWx()) {
- throw new Exception('未开启企业微信');
- }
- $department = Department::business()->with('hasOneParentDepartment')->orderBy('level', 'ASC')->get();
- $department->each(function ($v) {
- if ($v->level == 1 && $v->wechat_department_id == 0) {
- throw new Exception('一级部门未关联企业微信');
- }
- $res = DepartmentService::pushDepartment($v);
- if (!$res['result']) {
- throw new Exception($res['msg']);
- }
- });
- } catch (Exception $e) {
- return $this->errorJson($e->getMessage());
- }
- return $this->successJson('推送成功');
- }
- }
|