123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. $groupModel = model('Group');
  76. $param = $this->param;
  77. $param['rules'] = arrayToString($param['rules']);
  78. $lastInsId = $groupModel->createData($param);
  79. if (!$lastInsId) {
  80. return resultArray(['error' => $groupModel->getError()]);
  81. }
  82. # 新增客户管理角色的字段授权数据
  83. if (isset($param['pid']) && $param['pid'] == 2) {
  84. $fieldGrantLogic->createCrmFieldGrant($lastInsId);
  85. }
  86. return resultArray(['data' => 1]);
  87. }
  88. /**
  89. * 角色编辑
  90. * @author Michael_xu
  91. * @param
  92. * @return
  93. */
  94. public function update()
  95. {
  96. $groupModel = model('Group');
  97. $param = $this->param;
  98. $dataInfo = $groupModel->getDataById($param['id']);
  99. if (!$dataInfo) {
  100. return resultArray(['error' => '参数错误']);
  101. }
  102. # 处理前端传来的type是work的错误
  103. if (!empty($param['type']) && $param['type'] == 'work') $param['type'] = 1;
  104. $param['rules'] = arrayToString($param['rules']);
  105. $data = $groupModel->updateDataById($param, $param['id']);
  106. return resultArray(['data' => '编辑成功']);
  107. }
  108. /**
  109. * 角色删除
  110. * @author Michael_xu
  111. * @param
  112. * @return
  113. */
  114. public function delete(FieldGrantLogic $fieldGrantLogic)
  115. {
  116. $groupModel = model('Group');
  117. $param = $this->param;
  118. $dataInfo = $groupModel->getDataById($param['id']);
  119. if (!$dataInfo) {
  120. return resultArray(['error' => '参数错误']);
  121. }
  122. if ($dataInfo['types']) {
  123. return resultArray(['error' => '系统角色,不能删除']);
  124. }
  125. $data = $groupModel->delGroupById($param['id']);
  126. if (!$data) {
  127. return resultArray(['error' => $groupModel->getError()]);
  128. }
  129. # 删除字段授权数据
  130. $fieldGrantLogic->deleteCrmFieldGrant($param['id']);
  131. return resultArray(['data' => '删除成功']);
  132. }
  133. /**
  134. * 角色启用、禁用
  135. * @author Michael_xu
  136. * @param
  137. * @return
  138. */
  139. public function enables()
  140. {
  141. $groupModel = model('Group');
  142. $param = $this->param;
  143. $dataInfo = $groupModel->getDataById($param['id']);
  144. if (!$dataInfo) {
  145. return resultArray(['error' => '参数错误']);
  146. }
  147. if ($dataInfo['types']) {
  148. return resultArray(['error' => '系统角色,不能删除']);
  149. }
  150. $data = $groupModel->enableDatas($param['id'], $param['status'], true);
  151. if (!$data) {
  152. return resultArray(['error' => $groupModel->getError()]);
  153. }
  154. return resultArray(['data' => '操作成功']);
  155. }
  156. /**
  157. * 角色复制
  158. * @author Michael_xu
  159. * @param
  160. * @return
  161. */
  162. public function copy(FieldGrantLogic $fieldGrantLogic)
  163. {
  164. $groupModel = model('Group');
  165. $param = $this->param;
  166. $dataInfo = $groupModel->getDataById($param['id']);
  167. if (!$dataInfo) {
  168. return resultArray(['error' => '参数错误']);
  169. }
  170. $dataInfo = json_decode($dataInfo, true);
  171. unset($dataInfo['id']);
  172. $titleCount = db('admin_group')->where(['title' => $dataInfo['title']])->count();
  173. $dataInfo['title'] = $dataInfo['title'].'('.$titleCount.')';
  174. $data = $groupModel->createData($dataInfo);
  175. if (!$data) {
  176. return resultArray(['error' => $groupModel->getError()]);
  177. }
  178. # 复制客户管理角色的字段授权数据
  179. if (!empty($dataInfo['pid']) && $dataInfo['pid'] == 2) {
  180. $fieldGrantLogic->copyCrmFieldGrant($param['id'], $data);
  181. }
  182. return resultArray(['data' => '操作成功']);
  183. }
  184. /**
  185. * 角色分类列表
  186. * @author Michael_xu
  187. * @param
  188. * @return
  189. */
  190. public function typeList()
  191. {
  192. $groupModel = model('Group');
  193. $param = $this->param;
  194. $data = $groupModel->getTypeList($param);
  195. return resultArray(['data' => $data]);
  196. }
  197. }