StaffController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 app\common\helpers\QrCodeHelper;
  12. use app\common\models\Member;
  13. use business\common\models\Department;
  14. use business\common\models\Staff;
  15. use business\common\services\BusinessService;
  16. use business\common\services\DepartmentService;
  17. use business\common\services\SettingService;
  18. use business\common\services\StaffService;
  19. use business\common\controllers\components\BaseController;
  20. class StaffController extends BaseController
  21. {
  22. /*
  23. * 根据手机号查询会员
  24. */
  25. public function businessGetMemberByMobile()
  26. {
  27. $type = request()->search_type;
  28. $search = request()->search;
  29. $mobile = request()->mobile;
  30. if (!$mobile && !in_array($type, ['uid', 'mobile', 'strict_mobile'])) {
  31. return $this->errorJson('请输入搜索条件');
  32. }
  33. $query = Member::uniacid()->select('uid', 'avatar', 'nickname', 'realname', 'mobile');
  34. switch ($type) {
  35. case 'uid' :
  36. $query->where('uid', $search);
  37. break;
  38. case 'strict_mobile':
  39. $query->where('mobile', $search);
  40. break;
  41. }
  42. if (request()->mobile || $type == 'mobile') {
  43. $query->where('mobile', 'like', '%' . (request()->mobile ?: $search) . '%');
  44. }
  45. $member = $query->get();
  46. return $this->successJson('成功', $member);
  47. }
  48. /*
  49. * 查找企业员工
  50. */
  51. public function searchStaff()
  52. {
  53. $request = \request();
  54. $kwd = $request->kwd;
  55. $where = [
  56. [function ($query) use ($kwd) {
  57. $query->where('name', 'like', "%{$kwd}%")->orWhere('mobile', 'like', "%{$kwd}%");
  58. }]
  59. ];
  60. if ($request->is_qy_wx) { //是否只查询企业微信员工
  61. $where[] = ['status', '<>', 0];
  62. }
  63. if ($request->disabled) { //是否屏蔽禁用员工
  64. $where[] = ['disabled', 0];
  65. }
  66. $staff = Staff::business()->where($where)->get();
  67. if ($staff->isNotEmpty()) {
  68. $staff = $staff->map(function ($v) {
  69. $v->gender_desc = Staff::GENDER_DESC[$v->gender] ?: '未知';
  70. $v->status_desc = Staff::STATUS_DESC[$v->status] ?: '未知';
  71. return $v;
  72. });
  73. }
  74. return $this->successJson('成功', $staff);
  75. }
  76. /*
  77. * 创建员工
  78. */
  79. public function createStaff()
  80. {
  81. if (\request()->id) {
  82. return $this->errorJson('参数异常');
  83. }
  84. $check_res = StaffService::checkManageRightByKey('createStaff', \request()->id, \request()->department_id);
  85. if (!$check_res['result']) {
  86. return $this->errorJson($check_res['msg']);
  87. }
  88. if (request()->department_id) {
  89. $true_request_department_id = [];
  90. $request_department_id = request()->department_id;
  91. foreach ($request_department_id as $v) {
  92. $true_request_department_id[] = end($v);
  93. }
  94. request()->offsetSet('department_id', $true_request_department_id);
  95. }
  96. $res = StaffService::changeStaff(\request()); //推送
  97. if (!$res['result']) {
  98. return $this->errorJson($res['msg']);
  99. }
  100. if (SettingService::EnabledQyWx() && $staff = $res['data']['staff']) {
  101. $res = StaffService::refreshStaff(0, $staff->id); //同步
  102. if (!$res['result']) {
  103. return $this->errorJson($res['msg']);
  104. }
  105. }
  106. return $this->successJson('创建成功');
  107. }
  108. /*
  109. * 编辑员工
  110. */
  111. public function updateStaff()
  112. {
  113. $check_res = StaffService::checkManageRightByKey('updateStaff', \request()->id, \request()->department_id);
  114. if (!$check_res['result']) {
  115. return $this->errorJson($check_res['msg']);
  116. }
  117. if (!\request()->id) {
  118. return $this->errorJson('请选择要编辑的员工');
  119. }
  120. if (!\request()->is_edit) {
  121. $staff = Staff::business()->select('id', 'uid', 'name', 'mobile', 'telephone', 'email', 'avatar', 'alias', 'address', 'position')
  122. ->with(['hasOneMember' => function ($query) {
  123. $query->select('uid', 'avatar', 'nickname', 'realname', 'mobile');
  124. }])->with(['hasManyDepartmentStaff' => function ($query) {
  125. $query->with('hasOneDepartment');
  126. }])->find(\request()->id);
  127. if (!$staff) {
  128. return $this->errorJson('员工不存在');
  129. }
  130. $department_list = [];
  131. $department_id = [];
  132. $department_name = [];
  133. $staff->hasManyDepartmentStaff->each(function ($v) use (&$department_list) {
  134. if ($v->hasOneDepartment->id) {
  135. if (!$department_list[$v->hasOneDepartment->level]) $department_list[$v->hasOneDepartment->level] = collect([]);
  136. $department_list[$v->hasOneDepartment->level]->push(['id' => $v->hasOneDepartment->id, 'name' => $v->hasOneDepartment->name, 'order' => $v->hasOneDepartment->order]);
  137. }
  138. });
  139. ksort($department_list);
  140. foreach ($department_list as $v) {
  141. $v = $v->sortByDesc('order');
  142. $department_id = array_merge($department_id, $v->pluck('id')->all());
  143. $department_name = array_merge($department_name, $v->pluck('name')->all());
  144. }
  145. $all_department = Department::business()->get();
  146. $true_department_id = [];
  147. foreach ($department_id as $v) {
  148. $this_parent_department = Department::getAllDepartmentParentId($v, $all_department);
  149. $this_parent_department = array_reverse($this_parent_department);
  150. $this_parent_department[] = $v;
  151. $true_department_id[] = $this_parent_department;
  152. }
  153. $staff->setAttribute('department_id', $true_department_id);
  154. $staff->setAttribute('department_name', $department_name);
  155. $staff->unsetRelation('hasManyDepartmentStaff');
  156. return $this->successJson('获取成功', ['staff' => $staff]);
  157. }
  158. if (request()->department_id) {
  159. $true_request_department_id = [];
  160. $request_department_id = request()->department_id;
  161. foreach ($request_department_id as $v) {
  162. $true_request_department_id[] = end($v);
  163. }
  164. request()->offsetSet('department_id', $true_request_department_id);
  165. }
  166. $res = StaffService::changeStaff(\request());
  167. if (!$res['result']) {
  168. return $this->errorJson($res['msg']);
  169. }
  170. if (SettingService::EnabledQyWx() && $staff = $res['data']['staff']) {
  171. $res = StaffService::refreshStaff(0, $staff->id); //同步
  172. if (!$res['result']) {
  173. return $this->errorJson($res['msg']);
  174. }
  175. }
  176. return $this->successJson('修改成功');
  177. }
  178. /*
  179. * 删除员工
  180. */
  181. public function deleteStaff()
  182. {
  183. $check_res = StaffService::checkManageRightByKey('deleteStaff', \request()->id, []);
  184. if (!$check_res['result']) {
  185. return $this->errorJson($check_res['msg']);
  186. }
  187. $disabled = request()->disabled ? 0 : 1;
  188. $res = StaffService::deleteStaff(\request()->id, $disabled);
  189. if (!$res['result']) {
  190. return $this->errorJson($res['msg']);
  191. }
  192. return $this->successJson($disabled ? '禁用成功' : '解禁成功');
  193. }
  194. /*
  195. * 推送员工信息到企业微信
  196. */
  197. // public function pushStaff()
  198. // {
  199. // $res = StaffService::pushStaff(\request()->id);
  200. // if (!$res['result']) {
  201. // return $this->errorJson($res['msg']);
  202. // }
  203. // return $this->successJson('推送成功');
  204. // }
  205. /*
  206. * 设置部门领导
  207. */
  208. public function setDepartmentLeader()
  209. {
  210. $right = BusinessService::checkBusinessRight();
  211. if (!$right['identity'] || $right['identity'] < 2) {
  212. return $this->errorJson('无权设置部门主管');
  213. }
  214. $res = StaffService::setStaffLeader(\request()->department_id, \request()->staff_id);
  215. if (!$res['result']) {
  216. return $this->errorJson($res['msg']);
  217. }
  218. return $this->successJson('设置成功');
  219. }
  220. /*
  221. * 从企业微信同步员工列表
  222. */
  223. public function refreshStaffList()
  224. {
  225. if (!SettingService::EnabledQyWx()) {
  226. return $this->errorJson('未开启企业微信', ['is_set' => 1]);
  227. }
  228. if ($department_id = \request()->department_id) {
  229. $department = Department::business()->where('wechat_department_id', '<>', 0)->find($department_id);
  230. } else {
  231. $department = Department::business()->where('level', 1)->where('wechat_department_id', '<>', 0)->first();
  232. }
  233. if (!$department) {
  234. return $this->errorJson('部门不存在或未关联企业微信');
  235. }
  236. $res = StaffService::refreshStaff($department->id);
  237. if (!$res['result']) {
  238. return $this->errorJson($res['msg']);
  239. }
  240. return $this->successJson('同步成功');
  241. }
  242. /*
  243. * 根据部门id获取员工列表
  244. */
  245. public function getStaffList()
  246. {
  247. $request = request();
  248. // if (!DepartmentService::checkDepartmentIdByMethod('getStaffList', $request->department_id)) {
  249. // return $this->errorJson('暂无权限查看此部门员工');
  250. // }
  251. //
  252. // if (!$department = Department::business()->find($request->department_id)) {
  253. // return $this->errorJson('部门不存在');
  254. // }
  255. // 需求改成显示当前部门下(包括下级)所有可显示的员工
  256. $department_ids = DepartmentService::getDepartmentIds($request->department_id ?: 0, true);
  257. if ($request->department_id and DepartmentService::checkDepartmentIdByMethod('getStaffList', $request->department_id)) {
  258. $department_ids[] = (int)$request->department_id;
  259. }
  260. if (!$request->sub_page) {
  261. $res = StaffService::getStaffList($department_ids, [
  262. 'kwd' => trim(\request()->kwd),
  263. 'id' => intval(\request()->id) ?: 0,
  264. 'status' => \request()->status,
  265. 'disabled' => \request()->disabled,
  266. ])->paginate()->toArray();
  267. } else {
  268. $list = StaffService::getStaffList($department_ids, [
  269. 'kwd' => trim(\request()->kwd),
  270. 'id' => intval(\request()->id) ?: 0,
  271. 'status' => \request()->status,
  272. 'disabled' => \request()->disabled,
  273. ])->get()->toArray();
  274. $res['data'] = $list;
  275. }
  276. StaffService::addStaffPremission($res['data']);
  277. $return_list = [];
  278. $leader_list = [];
  279. foreach ($res['data'] as $v) {
  280. $return_list[] = $this_data = [
  281. 'id' => $v['staff_id'],
  282. 'uid' => $v['has_one_staff']['uid'] ?: 0,
  283. 'name' => $v['has_one_staff']['name'] ?: '',
  284. 'mobile' => $v['has_one_staff']['mobile'] ?: '',
  285. 'position' => $v['has_one_staff']['position'] ?: '',
  286. 'gender_desc' => Staff::GENDER_DESC[$v['has_one_staff']['gender']] ?: '未知',
  287. 'status' => Staff::STATUS_DESC[$v['has_one_staff']['status'] ?: 0],
  288. // 'status' => $v['has_one_staff']['status'] ?: 0,
  289. 'disabled' => $v['has_one_staff']['disabled'] ? 1 : 0,
  290. 'premission' => $v['premission'],
  291. 'has_one_member' => $v['has_one_staff']['has_one_member'] ?: null
  292. ];
  293. if ($request->get_leader && $v['is_leader']) {
  294. $leader_list[] = $this_data;
  295. }
  296. }
  297. $res['data'] = $return_list;
  298. if ($request->get_leader) {
  299. $res['leader_list'] = $leader_list ? array_column($leader_list, null, 'id') : [];
  300. }
  301. $res['premission']['createStaff'] = BusinessService::getPremissionByRoute('createStaff');
  302. $res = array_merge($res, SettingService::getStaffTicketCodeUrl());
  303. return $this->successJson('成功', $res);
  304. }
  305. }