SystemPostController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\controller\system;
  8. use plugin\saiadmin\app\cache\UserInfoCache;
  9. use plugin\saiadmin\app\model\system\SystemUserPost;
  10. use plugin\saiadmin\basic\BaseController;
  11. use plugin\saiadmin\app\logic\system\SystemPostLogic;
  12. use plugin\saiadmin\app\validate\system\SystemPostValidate;
  13. use support\Request;
  14. use support\Response;
  15. /**
  16. * 岗位信息控制器
  17. */
  18. class SystemPostController extends BaseController
  19. {
  20. /**
  21. * 构造
  22. */
  23. public function __construct()
  24. {
  25. $this->logic = new SystemPostLogic();
  26. $this->validate = new SystemPostValidate;
  27. parent::__construct();
  28. }
  29. /**
  30. * 数据列表
  31. * @param Request $request
  32. * @return Response
  33. */
  34. public function index(Request $request) : Response
  35. {
  36. $where = $request->more([
  37. ['name', ''],
  38. ['code', ''],
  39. ['status', ''],
  40. ['create_time', ''],
  41. ]);
  42. $query = $this->logic->search($where);
  43. $data = $this->logic->getList($query);
  44. return $this->success($data);
  45. }
  46. /**
  47. * 修改状态
  48. * @param Request $request
  49. * @return Response
  50. */
  51. public function changeStatus(Request $request) : Response
  52. {
  53. $id = $request->input('id', '');
  54. $status = $request->input('status', 1);
  55. $model = $this->logic->findOrEmpty($id);
  56. if ($model->isEmpty()) {
  57. return $this->fail('未查找到信息');
  58. }
  59. $result = $model->save(['status' => $status]);
  60. if ($result) {
  61. $this->afterChange('changeStatus', $model);
  62. return $this->success('操作成功');
  63. } else {
  64. return $this->fail('操作失败');
  65. }
  66. }
  67. /**
  68. * 数据改变后执行
  69. * @param $type
  70. * @param $args
  71. * @return void
  72. */
  73. protected function afterChange($type, $args): void
  74. {
  75. // 批量清理用户缓存
  76. if ($type == 'update') {
  77. $post_id = request()->input('id', '');
  78. $userIds = SystemUserPost::where('post_id', $post_id)->column('user_id');
  79. $userIds = array_unique($userIds);
  80. foreach ($userIds as $userId) {
  81. $userInfoCache = new UserInfoCache($userId);
  82. $userInfoCache->clearUserInfo();
  83. }
  84. }
  85. if ($type == 'destroy') {
  86. $post_ids = request()->input('ids', '');
  87. if (is_array($post_ids)) {
  88. $userIds = SystemUserPost::whereIn('post_id', $post_ids)->column('user_id');
  89. $userIds = array_unique($userIds);
  90. foreach ($userIds as $userId) {
  91. $userInfoCache = new UserInfoCache($userId);
  92. $userInfoCache->clearUserInfo();
  93. }
  94. }
  95. }
  96. }
  97. /**
  98. * 可操作岗位
  99. * @param Request $request
  100. * @return Response
  101. */
  102. public function accessPost(Request $request) : Response
  103. {
  104. $where = [];
  105. $data = $this->logic->accessPost($where);
  106. return $this->success($data);
  107. }
  108. /**
  109. * 下载导入模板
  110. * @return Response
  111. */
  112. public function downloadTemplate() : Response
  113. {
  114. $file_name = "template.xlsx";
  115. return downloadFile($file_name);
  116. }
  117. /**
  118. * 导入数据
  119. * @param Request $request
  120. * @return Response
  121. */
  122. public function import(Request $request) : Response
  123. {
  124. $file = current($request->file());
  125. if (!$file || !$file->isValid()) {
  126. return $this->fail('未找到上传文件');
  127. }
  128. $this->logic->import($file);
  129. return $this->success('导入成功');
  130. }
  131. /**
  132. * 导出数据
  133. * @param Request $request
  134. * @return Response
  135. */
  136. public function export(Request $request) : Response
  137. {
  138. $where = $request->more([
  139. ['name', ''],
  140. ['code', ''],
  141. ['status', ''],
  142. ]);
  143. return $this->logic->export($where);
  144. }
  145. }