123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 用户组
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\controller;
  8. use app\admin\logic\FieldGrantLogic;
  9. use think\Hook;
  10. use think\Request;
  11. class Groups extends ApiCommon
  12. {
  13. /**
  14. * 用于判断权限
  15. * @permission 无限制
  16. * @allow 登录用户可访问
  17. * @other 其他根据系统设置
  18. **/
  19. public function _initialize()
  20. {
  21. $action = [
  22. 'permission'=>[''],
  23. 'allow'=>['index','enables','copy','typelist','save','update','delete']
  24. ];
  25. Hook::listen('check_auth',$action);
  26. $request = Request::instance();
  27. $a = strtolower($request->action());
  28. if (!in_array($a, $action['permission'])) {
  29. parent::_initialize();
  30. }
  31. //权限判断
  32. $unAction = ['index','typelist'];
  33. if (!in_array($a, $unAction) && !checkPerByAction('admin', 'groups', 'update')) {
  34. header('Content-Type:application/json; charset=utf-8');
  35. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  36. }
  37. }
  38. /**
  39. * 角色列表
  40. * @author Michael_xu
  41. * @param
  42. * @return
  43. */
  44. public function index()
  45. {
  46. $groupModel = model('Group');
  47. $param = $this->param;
  48. $data = $groupModel->getDataList($param);
  49. return resultArray(['data' => $data]);
  50. }
  51. /**
  52. * 角色详情
  53. * @author Michael_xu
  54. * @param
  55. * @return
  56. */
  57. public function read()
  58. {
  59. $groupModel = model('Group');
  60. $param = $this->param;
  61. $data = $groupModel->getDataById($param['id']);
  62. if (!$data) {
  63. return resultArray(['error' => $groupModel->getError()]);
  64. }
  65. return resultArray(['data' => $data]);
  66. }
  67. /**
  68. * 角色添加
  69. * @author Michael_xu
  70. * @param
  71. * @return
  72. */
  73. public function save(FieldGrantLogic $fieldGrantLogic)
  74. {
  75. $userInfo=$this->userInfo;
  76. $groupModel = model('Group');
  77. $param = $this->param;
  78. $param['rules'] = arrayToString($param['rules']);
  79. $lastInsId = $groupModel->createData($param);
  80. if (!$lastInsId) {
  81. return resultArray(['error' => $groupModel->getError()]);
  82. }
  83. # 添加记录
  84. SystemActionLog($userInfo['id'], 'admin_group','role', $lastInsId, 'save', $param['title'], '', '', '添加了角色:' . $param['title']);
  85. # 新增客户管理角色的字段授权数据
  86. if (isset($param['pid']) && $param['pid'] == 2) {
  87. $fieldGrantLogic->createCrmFieldGrant($lastInsId);
  88. }
  89. return resultArray(['data' => 1]);
  90. }
  91. /**
  92. * 角色编辑
  93. * @author Michael_xu
  94. * @param
  95. * @return
  96. */
  97. public function update()
  98. {
  99. $groupModel = model('Group');
  100. $param = $this->param;
  101. $userInfo=$this->userInfo;
  102. $param['user_id']=$userInfo['id'];
  103. $dataInfo = $groupModel->getDataById($param['id']);
  104. if (!$dataInfo) {
  105. return resultArray(['error' => '参数错误']);
  106. }
  107. # 处理前端传来的type是work的错误
  108. if (!empty($param['type']) && $param['type'] == 'work') $param['type'] = 1;
  109. $param['rules'] = arrayToString($param['rules']);
  110. $data = $groupModel->updateDataById($param, $param['id']);
  111. return resultArray(['data' => '编辑成功']);
  112. }
  113. /**
  114. * 角色删除
  115. * @author Michael_xu
  116. * @param
  117. * @return
  118. */
  119. public function delete(FieldGrantLogic $fieldGrantLogic)
  120. {
  121. $groupModel = model('Group');
  122. $param = $this->param;
  123. $userInfo=$this->userInfo;
  124. $dataInfo = $groupModel->getDataById($param['id']);
  125. if (!$dataInfo) {
  126. return resultArray(['error' => '参数错误']);
  127. }
  128. if ($dataInfo['types']) {
  129. return resultArray(['error' => '系统角色,不能删除']);
  130. }
  131. $data = $groupModel->delGroupById($param['id'],$userInfo['id']);
  132. if (!$data) {
  133. return resultArray(['error' => $groupModel->getError()]);
  134. }
  135. # 删除字段授权数据
  136. $fieldGrantLogic->deleteCrmFieldGrant($param['id']);
  137. return resultArray(['data' => '删除成功']);
  138. }
  139. /**
  140. * 角色启用、禁用
  141. * @author Michael_xu
  142. * @param
  143. * @return
  144. */
  145. public function enables()
  146. {
  147. $groupModel = model('Group');
  148. $param = $this->param;
  149. $dataInfo = $groupModel->getDataById($param['id']);
  150. if (!$dataInfo) {
  151. return resultArray(['error' => '参数错误']);
  152. }
  153. if ($dataInfo['types']) {
  154. return resultArray(['error' => '系统角色,不能删除']);
  155. }
  156. $data = $groupModel->enableDatas($param['id'], $param['status'], true);
  157. if (!$data) {
  158. return resultArray(['error' => $groupModel->getError()]);
  159. }
  160. return resultArray(['data' => '操作成功']);
  161. }
  162. /**
  163. * 角色复制
  164. * @author Michael_xu
  165. * @param
  166. * @return
  167. */
  168. public function copy(FieldGrantLogic $fieldGrantLogic)
  169. {
  170. $groupModel = model('Group');
  171. $param = $this->param;
  172. $userInfo = $this->userInfo;
  173. $dataInfo = $groupModel->getDataById($param['id']);
  174. if (!$dataInfo) {
  175. return resultArray(['error' => '参数错误']);
  176. }
  177. $dataInfo = json_decode($dataInfo, true);
  178. unset($dataInfo['id']);
  179. $titleCount = db('admin_group')->where(['title' => $dataInfo['title']])->count();
  180. $dataInfo['title'] = $dataInfo['title'].'('.$titleCount.')';
  181. $data = $groupModel->createData($dataInfo);
  182. if (!$data) {
  183. return resultArray(['error' => $groupModel->getError()]);
  184. }
  185. # 添加记录
  186. SystemActionLog($userInfo['id'], 'admin_group','role', $data, 'copy', $dataInfo['title'], '', '', '添加了角色:' . $dataInfo['title']);
  187. # 复制客户管理角色的字段授权数据
  188. if (!empty($dataInfo['pid']) && $dataInfo['pid'] == 2) {
  189. $fieldGrantLogic->copyCrmFieldGrant($param['id'], $data);
  190. }
  191. return resultArray(['data' => '操作成功']);
  192. }
  193. /**
  194. * 角色分类列表
  195. * @author Michael_xu
  196. * @param
  197. * @return
  198. */
  199. public function typeList()
  200. {
  201. $groupModel = model('Group');
  202. $param = $this->param;
  203. $data = $groupModel->getTypeList($param);
  204. return resultArray(['data' => $data]);
  205. }
  206. }