UseradminController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. class UseradminController extends Controller
  3. {
  4. public function filters()
  5. {
  6. return array(
  7. 'accessControl', // perform access control for CRUD operations
  8. );
  9. }
  10. public function actionInfo()
  11. {
  12. $model = Useradmin::model()->findByPk($this->getUserId());
  13. if (!$model) {
  14. Helper::error('信息未找到');
  15. }
  16. $authIds = DB::getScalerWithCriteria('role', DbCriteria::simpleCompare(['id' => $model->role_id])->setSelect('auth_ids'));
  17. $authIds = $authIds ? explode(',', $authIds) : [];
  18. $authIds = array_map(function ($item) {
  19. return (int)$item;
  20. }, $authIds);
  21. Helper::ok([
  22. 'id' => $model->id,
  23. 'username' => $model->username,
  24. 'auth_ids' => $authIds,
  25. 'buttons' => [],
  26. 'avatar' => Helper::getImageUrl($model->avatar),
  27. 'email' => $model->email,
  28. 'phone' => $model->phone,
  29. 'descr' => $model->descr,
  30. ]);
  31. }
  32. public function actionRoleList()
  33. {
  34. $name = Helper::getPostString('name');
  35. $name = $name ? '%' . $name : null;
  36. $cri = DbCriteria::simpleCompareWithPage(['name' => $name])
  37. ->setSelect('id, name, auth_ids, descr, create_date, show_ids, date_auth_type')
  38. ->setOrder('id desc');
  39. $data = DB::getListWithCriteria('role', $cri);
  40. if (!empty($data['records'])) {
  41. $data['records'] = array_map(function ($item) {
  42. $item['auth_ids'] = $item['auth_ids'] ? explode(',', $item['auth_ids']) : [];
  43. $item['auth_ids'] = array_map(function ($aid) {
  44. return (int)$aid;
  45. }, $item['auth_ids']);
  46. $item['show_ids'] = $item['show_ids'] ? explode(',', $item['show_ids']) : [];
  47. $item['show_ids'] = array_map(function ($aid) {
  48. return (int)$aid;
  49. }, $item['show_ids']);
  50. return $item;
  51. }, $data['records']);
  52. }
  53. Helper::ok($data);
  54. }
  55. public function actionUserList()
  56. {
  57. $name = Helper::getPostString('name');
  58. $name = $name ? '%' . $name : null;
  59. $filters = [
  60. 'username' => $name,
  61. 'u.id' => '!=1',
  62. 'u.status' => '1',
  63. 'role_id' => Helper::getPostInt('role_id')?:null,
  64. 'phone' => Helper::getPostString('phone')?:null,
  65. ];
  66. $cri = DbCriteria::simpleCompareWithPage($filters)
  67. ->setAlias('u')
  68. ->setDebugUntil('234', '-1')
  69. ->setSelect('u.id, u.username, r.date_auth_type, u.cities, r.name as role_name, u.status, u.role_id, u.sex, u.phone, u.create_date, u.avatar, u.update_date, u.company_ids, u.school_ids')
  70. ->setJoin('left join wx_role r on u.role_id = r.id')
  71. ->setOrder('id desc');
  72. $data = DB::getListWithCriteria('useradmin', $cri);
  73. if (empty($data['records'])) {
  74. Helper::ok([]);
  75. }
  76. $schools = Helper::arrayColumn(
  77. DB::getListWithCriteria('school', DbCriteria::simpleCompare([])->setSelect('id, name')),
  78. 'name',
  79. 'id'
  80. );
  81. $companys = Helper::arrayColumn(
  82. DB::getListWithCriteria('company', DbCriteria::simpleCompare([])->setSelect('id, name')),
  83. 'name',
  84. 'id'
  85. );
  86. foreach ($data['records'] as &$item) {
  87. $item['avatar'] = Helper::getImageUrl($item['avatar']);
  88. if ($item['date_auth_type'] == AUTH_TYPE_CITY) {
  89. $item['company_ids'] = [];
  90. $item['school_ids'] = [];
  91. $item['company_names'] = [];
  92. $item['school_names'] = [];
  93. $item['cities'] = array_filter(explode(',', $item['cities']));
  94. } elseif ($item['date_auth_type'] == AUTH_TYPE_ALL) {
  95. $item['company_ids'] = [];
  96. $item['school_ids'] = [];
  97. $item['cities'] = ['全部'];
  98. $item['company_names'] = ['全部'];
  99. $item['school_names'] = ['全部'];
  100. } else {
  101. $item['company_ids'] = $item['company_ids'] ? array_map(function ($item) {return (int)$item;}, explode(',', $item['company_ids'])) : [];
  102. $item['school_ids'] = $item['school_ids'] ? array_map(function ($item) {return (int)$item;}, explode(',', $item['school_ids'])) : [];
  103. $item['cities'] = [];
  104. $item['company_names'] = array_map(function ($item) use ($companys) {
  105. if (isset($companys[$item])) return $companys[$item];
  106. }, $item['company_ids']);
  107. $item['school_names'] = array_map(function ($item) use ($schools) {
  108. if (isset($schools[$item])) return $schools[$item];
  109. }, $item['school_ids']);
  110. }
  111. }
  112. Helper::ok($data);
  113. }
  114. public function actionSaveRoleAuth()
  115. {
  116. $id = Helper::getPostInt('id');
  117. $leaf_ids = Helper::getArrParam($_POST, 'leaf_ids', 'array_int', []);
  118. $half_Leaf_ids = Helper::getArrParam($_POST, 'half_Leaf_ids', 'array_int', []);
  119. if ($id < 0 || !$leaf_ids) {
  120. Helper::error('参数错误');
  121. }
  122. $info = [
  123. 'auth_ids' => implode(',', Helper::concatArray($half_Leaf_ids, $leaf_ids)),
  124. 'show_ids' => $leaf_ids ? implode(',', $leaf_ids) : '',
  125. ];
  126. DB::updateById('role', $info, $id);
  127. $users = DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare(['role_id' => $id])->setSelect('id'))?:[];
  128. Logger::errorMult('auth_change', $users);
  129. foreach ($users['records'] as $user) {
  130. $this->clearAuth($user['id']);
  131. }
  132. Helper::ok();
  133. }
  134. public function actionSaveDataAuth()
  135. {
  136. $id = Helper::getPostInt('id');
  137. $authType = Helper::getPostInt('auth_type');
  138. if ($id < 0 || !in_array($authType, [0, 1, 2])) {
  139. Helper::error('参数错误');
  140. }
  141. $info = [
  142. 'date_auth_type' => $authType,
  143. ];
  144. DB::updateById('role', $info, $id);
  145. $users = DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare(['role_id' => $id])->setSelect('id'))?:[];
  146. Logger::errorMult('auth_change', $users);
  147. foreach ($users['records'] as $user) {
  148. $this->clearAuth($user['id']);
  149. }
  150. Helper::ok();
  151. }
  152. public function actionEditUser()
  153. {
  154. $id = Helper::getPostInt('id');
  155. $username = Helper::getPostString('username');
  156. $password = Helper::getPostString('password');
  157. $phone = Helper::getPostString('phone');
  158. $sex = Helper::getPostInt('sex');
  159. $role_id = Helper::getPostInt('role_id');
  160. $company_ids = Helper::getArrParam($_POST, 'company_ids', 'array_int', []);
  161. $school_ids = Helper::getArrParam($_POST, 'school_ids', 'array_int', []);
  162. $cities = Helper::getArrParam($_POST, 'cities', 'array_string', []);
  163. // username不能为空和重复
  164. if (!$username) {
  165. Helper::error('用户名不能为空');
  166. }
  167. $cri = DbCriteria::simpleCompare(['username' => $username])->setSelect('id');
  168. if ($id > 0) {
  169. $cri->addCondition('id!=' . $id);
  170. }
  171. if ($fid = DB::getScalerWithCriteria('useradmin', $cri)) {
  172. Helper::error('用户名已存在 ' . $fid);
  173. }
  174. $this->dobuleCheck(1);
  175. $info = [
  176. 'username' => $username,
  177. 'phone' => $phone,
  178. 'sex' => $sex,
  179. 'role_id' => $role_id,
  180. 'company_ids' => $company_ids ? implode(',', $company_ids) : '',
  181. 'school_ids' => $school_ids ? implode(',', $school_ids) : '',
  182. 'cities' => $cities ? implode(',', $cities) : '',
  183. ];
  184. if (!$id) {
  185. // 新增用户
  186. if (!$password) {
  187. Helper::error('密码不能为空');
  188. }
  189. if (!$role_id) {
  190. Helper::error('请选择角色');
  191. }
  192. $info['password'] = md5($password);
  193. DB::addData('useradmin', $info);
  194. } else {
  195. $this->clearAuth($id);
  196. DB::updateById('useradmin', $info, $id);
  197. }
  198. Helper::ok();
  199. }
  200. public function actionDeleteUser()
  201. {
  202. $id = Helper::getPostInt('id');
  203. if ($id < 1) {
  204. Helper::error('参数错误');
  205. }
  206. DB::updateById('useradmin', ['status' => 0], $id);
  207. Helper::ok();
  208. }
  209. public function actionDeleteRole()
  210. {
  211. $id = Helper::getPostInt('id');
  212. if ($id < 1) {
  213. Helper::error('参数错误');
  214. }
  215. if (DB::getScalerWithCriteria('useradmin', DbCriteria::simpleCompare(['role_id' => $id, 'status' => 1])->setselect('id'))) {
  216. Helper::error('该角色下有用户,请先删除用户');
  217. }
  218. DB::deleteById('role', $id);
  219. Helper::ok();
  220. }
  221. public function actionEditRole()
  222. {
  223. $id = Helper::getPostInt('id');
  224. $name = Helper::getPostString('name');
  225. $descr = Helper::getPostString('descr');
  226. if (!$name) {
  227. Helper::error('角色名称不能为空');
  228. }
  229. $this->dobuleCheck();
  230. if ($id) {
  231. DB::updateById('role', ['name' => $name, 'descr' => $descr], $id);
  232. } else {
  233. DB::addData('role', ['name' => $name, 'descr' => $descr]);
  234. }
  235. Helper::ok();
  236. }
  237. }