ManagerController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <?php
  2. /**
  3. * 管理员控制器.
  4. *
  5. * @author nj
  6. * @date 2021-01-23 00:08:32
  7. */
  8. namespace Modules\ManageApi\Controllers;
  9. use App\Constants\CommonConstant;
  10. use App\Repositories\Criterias\CommonWithCriteria;
  11. use App\Repositories\Criterias\ManagerPagesCriteria;
  12. use App\Repositories\Criterias\OrderLimitCriteria;
  13. use App\Models\Mysql\ManagerModel;
  14. use App\Repositories\ManagerRepository;
  15. use App\Repositories\PermissionRoleRepository;
  16. use Modules\ManageApi\Responses\CommonSuccessUidResponse;
  17. use Modules\ManageApi\Responses\ManagerDetailResponse;
  18. use Modules\ManageApi\Responses\ManagerPagesResponse;
  19. use Otp\GoogleAuthenticator;
  20. use Illuminate\Support\Facades\Validator;
  21. class ManagerController extends BaseController
  22. {
  23. /**
  24. * 分页列表.
  25. * @return mixed
  26. */
  27. public function lists()
  28. {
  29. /**
  30. * @var Validator $validatorFactory
  31. */
  32. $validatorFactory = app('validator');
  33. /**
  34. * @var ManagerRepository $managerRepo
  35. */
  36. $managerRepo = app(ManagerRepository::class);
  37. $inputData = $this->getJsonData();
  38. // 针对layui table自动分页请求参数,优先GET值.
  39. $queryPage = request()->has('page') ? request()->input('page', 1) : null;
  40. $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
  41. if (!is_null($queryPage)) {
  42. $inputData['page'] = $queryPage;
  43. }
  44. if (!is_null($queryPageSize)) {
  45. $inputData['page_size'] = $queryPageSize;
  46. }
  47. $rules = [
  48. 'page' => 'nullable|integer',
  49. 'page_size' => 'nullable|integer',
  50. 'manager_uid' => 'nullable',
  51. 'username' => 'nullable',
  52. 'nick' => 'nullable',
  53. 'is_locked' => 'nullable|integer',
  54. 'role_id' => 'nullable|integer',
  55. 'email' => 'nullable|email',
  56. ];
  57. $validator = $validatorFactory->make($inputData, $rules);
  58. if ($validator->fails()) {
  59. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  60. return json_fail($validator->errors()->first());
  61. }
  62. $orders = [
  63. ['created_at', 'DESC'],
  64. ];
  65. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  66. if ($pageSize > 500) {
  67. $pageSize = CommonConstant::PAGE_SIZE;
  68. }
  69. $page = format_page($inputData['page'] ?? 0);
  70. // 注意,单例分页一定要克隆一个(保证前面没用使用到paginate);
  71. $managers = $managerRepo->pushCriteria(new ManagerPagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
  72. return new ManagerPagesResponse($managers);
  73. }
  74. public function checkUsername()
  75. {
  76. $inputData = $this->getJsonData();
  77. $rules = [
  78. 'username' => 'unique:manager,username|required|max:16',
  79. ];
  80. $message = [
  81. 'username.unique' => '账号已存在!',
  82. 'username.required' => '用户名不能为空!',
  83. 'username.max' => '用户名不能大于16字节!'
  84. ];
  85. $validator = Validator::make($inputData,$rules,$message);
  86. if ($validator->fails()) {
  87. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  88. return json_fail($validator->errors()->first());
  89. }
  90. return json_fail('用户名可用!',200,[]);
  91. }
  92. /**
  93. * 详情
  94. * @return mixed
  95. */
  96. public function detail()
  97. {
  98. /**
  99. * @var Validator $validatorFactory
  100. */
  101. $validatorFactory = app('validator');
  102. /**
  103. * @var ManagerRepository $managerRepo
  104. */
  105. $managerRepo = app(ManagerRepository::class);
  106. $inputData = $this->getJsonData();
  107. $rules = [
  108. 'manager_uid' => 'required|string',
  109. ];
  110. $validator = $validatorFactory->make($inputData, $rules);
  111. if ($validator->fails()) {
  112. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  113. return json_fail($validator->errors()->first());
  114. }
  115. $manager = $managerRepo->pushCriteria(new CommonWithCriteria(['role']))->findBy('manager_uid', $inputData['manager_uid']);
  116. return new ManagerDetailResponse($manager);
  117. }
  118. /**
  119. * 添加.
  120. * @return mixed|CommonSuccessUidResponse
  121. */
  122. public function add()
  123. {
  124. /**
  125. * @var Validator $validatorFactory
  126. */
  127. $validatorFactory = app('validator');
  128. /**
  129. * @var ManagerRepository $managerRepo
  130. */
  131. $managerRepo = app(ManagerRepository::class);
  132. /**
  133. * @var PermissionRoleRepository $permissionRoleRepository
  134. */
  135. $permissionRoleRepository = make(PermissionRoleRepository::class);
  136. $inputData = $this->getJsonData();
  137. $rules = [
  138. 'username' => 'required|min:2|max:60',
  139. 'nick' => 'required|max:60',
  140. 'role_id' => 'required|integer',
  141. 'password' => 'required|min:6|max:64',
  142. 'email' => 'nullable|max:120',
  143. 'otp_secret' => 'nullable|max:32',
  144. ];
  145. $validator = $validatorFactory->make($inputData, $rules);
  146. if ($validator->fails()) {
  147. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  148. return json_fail($validator->errors()->first());
  149. }
  150. $username = strtolower(trim($inputData['username']));
  151. if ($managerRepo->isExistsUsername($username)) {
  152. return json_fail('用户名已经存在');
  153. }
  154. $roleId = intval($inputData['role_id']);
  155. $roleName = '';
  156. $roleObj = $permissionRoleRepository->find($roleId);
  157. if (!empty($roleObj)) {
  158. $roleName = $roleObj->name;
  159. }
  160. $setarr = [
  161. 'username' => $username,
  162. 'nick' => trim($inputData['nick']),
  163. 'role_id' => intval($roleId),
  164. 'role_name' => $roleName,
  165. 'otp_secret' => isset($inputData['otp_secret']) ? trim($inputData['otp_secret']) : GoogleAuthenticator::generateRandom(),
  166. ];
  167. if (isset($inputData['email'])) {
  168. $email = trim($inputData['email']);
  169. } else {
  170. $email = '';
  171. }
  172. $salt = random(6);
  173. $password = $managerRepo->generatePassword($inputData['password'], $salt);
  174. $setarr['salt'] = $salt;
  175. $setarr['password'] = $password;
  176. $setarr['email'] = $email;
  177. $result = $managerRepo->create($setarr);
  178. if (!$result) {
  179. return json_fail('创建失败');
  180. } else {
  181. $logSubject = '添加一个管理员,账号:'. $username;
  182. $logSvalue = $result->manager_uid;
  183. $logDbData = [
  184. 'after_items' => [
  185. $result->toArray(),
  186. ],
  187. 'before_items' => [],
  188. ];
  189. $this->log($logSubject, $logSvalue, $logDbData);
  190. return new CommonSuccessUidResponse($result->manager_uid);
  191. }
  192. }
  193. /**
  194. * 修改管理员.
  195. * @return mixed
  196. */
  197. public function modify()
  198. {
  199. /**
  200. * @var Validator $validatorFactory
  201. */
  202. $validatorFactory = app('validator');
  203. /**
  204. * @var ManagerRepository $managerRepo
  205. */
  206. $managerRepo = app(ManagerRepository::class);
  207. /**
  208. * @var PermissionRoleRepository $permissionRoleRepository
  209. */
  210. $permissionRoleRepository = make(PermissionRoleRepository::class);
  211. $inputData = $this->getJsonData();
  212. $rules = [
  213. 'manager_uid' => 'required|string',
  214. 'nick' => 'required|max:60',
  215. 'role_id' => 'required|integer',
  216. 'password' => 'nullable|min:6|max:64',
  217. 'email' => 'nullable|max:120',
  218. ];
  219. $validator = $validatorFactory->make($inputData, $rules);
  220. if ($validator->fails()) {
  221. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  222. return json_fail($validator->errors()->first());
  223. }
  224. $manager = $managerRepo->findBy('manager_uid', $inputData['manager_uid']);
  225. if (empty($manager)) {
  226. return json_fail(trans('common.err_resource_not_found'));
  227. }
  228. $oldManager = clone $manager;
  229. $manager->nick = $inputData['nick'];
  230. $manager->role_id = intval($inputData['role_id']);
  231. $roleName = '';
  232. $roleObj = $permissionRoleRepository->find($manager->role_id);
  233. if (!empty($roleObj)) {
  234. $roleName = $roleObj->name;
  235. }
  236. $manager->role_name = $roleName;
  237. if (isset($inputData['email'])) {
  238. $email = trim($inputData['email']);
  239. $manager->email = $email;
  240. } else {
  241. $manager->email = '';
  242. }
  243. if (!empty($inputData['password'])) {
  244. $salt = $manager->salt;
  245. if (empty($salt)) {
  246. $salt = random(6);
  247. }
  248. $password = $managerRepo->generatePassword($inputData['password'], $manager->salt);
  249. $manager->salt = $salt;
  250. $manager->password = $password;
  251. }
  252. $result = $manager->save();
  253. if (!$result) {
  254. return json_fail('修改失败');
  255. } else {
  256. $logSubject = '修改一个管理员,账号:'. $manager->username;
  257. $logSvalue = $manager->manager_uid;
  258. $logDbData = [
  259. 'after_items' => [
  260. $manager->toArray(),
  261. ],
  262. 'before_items' => [
  263. $oldManager->toArray(),
  264. ],
  265. ];
  266. $this->log($logSubject, $logSvalue, $logDbData);
  267. return new CommonSuccessUidResponse($manager->manager_uid);
  268. }
  269. }
  270. /**
  271. * 删除单个.
  272. * @return mixed
  273. */
  274. public function delete()
  275. {
  276. /**
  277. * @var Validator $validatorFactory
  278. */
  279. $validatorFactory = app('validator');
  280. /**
  281. * @var ManagerRepository $managerRepo
  282. */
  283. $managerRepo = app(ManagerRepository::class);
  284. $inputData = $this->getJsonData();
  285. $rules = [
  286. 'manager_uid' => 'required|string',
  287. ];
  288. $validator = $validatorFactory->make($inputData, $rules);
  289. if ($validator->fails()) {
  290. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  291. return json_fail($validator->errors()->first());
  292. }
  293. $manager = $managerRepo->findBy('manager_uid', $inputData['manager_uid']);
  294. if ($manager->id === 1) {
  295. return json_fail('此用户不允许删除!');
  296. }
  297. if (empty($manager)) {
  298. return json_fail(trans('common.err_resource_not_found'));
  299. }
  300. $oldManager = clone $manager;
  301. $result = $manager->delete();
  302. if (!$result) {
  303. return json_fail('删除失败');
  304. } else {
  305. $logSubject = '删除一个管理员,账号:'. $oldManager->username;
  306. $logSvalue = $oldManager->manager_uid;
  307. $logDbData = [
  308. 'after_items' => [
  309. ],
  310. 'before_items' => [
  311. $oldManager->toArray(),
  312. ],
  313. ];
  314. $this->log($logSubject, $logSvalue, $logDbData);
  315. return new CommonSuccessUidResponse($inputData['manager_uid']);
  316. }
  317. }
  318. /**
  319. * 批量操作.
  320. * @return mixed
  321. */
  322. public function batch()
  323. {
  324. /**
  325. * @var Validator $validatorFactory
  326. */
  327. $validatorFactory = app('validator');
  328. /**
  329. * @var ManagerRepository $managerRepo
  330. */
  331. $managerRepo = app(ManagerRepository::class);
  332. $inputData = $this->getJsonData();
  333. $operateName = $inputData['operate_name'] ?? '';
  334. switch ($operateName) {
  335. case 'delete':
  336. $itemPks = $inputData['item_pks'] ?? [];
  337. if (!empty($itemPks)) {
  338. $model = new ManagerModel();
  339. $resultItems = $model->whereIn('manager_uid', $itemPks)->delete();
  340. if ($resultItems) {
  341. $strPks = implode(',', $itemPks);
  342. $logSubject = '批量删除多个管理员';
  343. $logSvalue = $strPks;
  344. $logDbData = [
  345. 'after_items' => [
  346. ],
  347. 'before_items' => [],
  348. ];
  349. $this->log($logSubject, $logSvalue, $logDbData);
  350. }
  351. }
  352. $data = [
  353. 'result' => 'success',
  354. 'pks' => $itemPks,
  355. ];
  356. return json_success($data);
  357. break;
  358. default:
  359. break;
  360. }
  361. return json_fail('未知操作');
  362. }
  363. /**
  364. * 我的详情.
  365. * @return mixed|ManagerDetailResponse
  366. */
  367. public function detailMe()
  368. {
  369. $manager = $this->getCurrentManger();
  370. if (empty($manager)) {
  371. return json_fail(trans('common.err_resource_not_found'));
  372. }
  373. return new ManagerDetailResponse($manager);
  374. }
  375. /**
  376. * 修改自己信息.
  377. * @return mixed|CommonSuccessUidResponse
  378. */
  379. public function modifyMe()
  380. {
  381. /**
  382. * @var Validator $validatorFactory
  383. */
  384. $validatorFactory = app('validator');
  385. /**
  386. * @var ManagerRepository $managerRepo
  387. */
  388. $managerRepo = app(ManagerRepository::class);
  389. $inputData = $this->getJsonData();
  390. $rules = [
  391. 'nick' => 'required|max:60',
  392. 'password' => 'nullable|min:6|max:64',
  393. 'email' => 'nullable|max:120',
  394. ];
  395. $validator = $validatorFactory->make($inputData, $rules);
  396. if ($validator->fails()) {
  397. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  398. return json_fail($validator->errors()->first());
  399. }
  400. $manager = $this->getCurrentManger();
  401. if (empty($manager)) {
  402. return json_fail(trans('common.err_resource_not_found'));
  403. }
  404. $oldManager = clone $manager;
  405. $manager->nick = $inputData['nick'];
  406. if (isset($inputData['email'])) {
  407. $email = trim($inputData['email']);
  408. $manager->email = $email;
  409. } else {
  410. $manager->email = '';
  411. }
  412. if (!empty($inputData['password'])) {
  413. $salt = $manager->salt;
  414. if (empty($salt)) {
  415. $salt = random(6);
  416. }
  417. $password = $managerRepo->generatePassword($inputData['password'], $manager->salt);
  418. $manager->salt = $salt;
  419. $manager->password = $password;
  420. }
  421. $result = $manager->save();
  422. if (!$result) {
  423. return json_fail('修改失败');
  424. } else {
  425. $logSubject = '修改自己管理员信息,账号:'. $manager->username;
  426. $logSvalue = $manager->manager_uid;
  427. $logDbData = [
  428. 'after_items' => [
  429. $manager->toArray(),
  430. ],
  431. 'before_items' => [
  432. $oldManager->toArray(),
  433. ],
  434. ];
  435. $this->log($logSubject, $logSvalue, $logDbData);
  436. return new CommonSuccessUidResponse($manager->manager_uid);
  437. }
  438. }
  439. }