DataShare.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\BaseController;
  4. use think\facade\Db;
  5. use think\facade\Validate;
  6. use app\admin\model\DataShare as DataShareModel;
  7. use app\admin\model\DataGroup as DataGroupModel;
  8. //数据共享规则
  9. class DataShare extends BaseController
  10. {
  11. //列表
  12. public function getDataShareList()
  13. {
  14. $param = $this->request->filter('trim')->only(['page' => 1, 'size' => 10, 'source_nickname' => '', 'source_group_title' => '', 'nickname' => '', 'group_title' => '', 'type' => '', 'token'], 'post');
  15. $db = new DataShareModel();
  16. $rs = $db
  17. ->alias('a')
  18. ->leftJoin('data_group c', 'c.id=a.source_data_group_id AND c.is_del=' . DataGroupModel::$delete_正常)
  19. ->leftJoin('data_group e', 'e.id=a.data_group_id AND e.is_del=' . DataGroupModel::$delete_正常)
  20. ->where('a.is_del', $db::$delete_正常);
  21. $val = Validate::rule([
  22. 'token' => 'require',
  23. 'page|页码' => 'require|number|gt:0',
  24. 'size|每页数量' => 'require|number|gt:0|elt:100',
  25. 'status|状态' => 'number|in:' . $db::$status_启用 . ',' . $db::$status_禁用,
  26. 'source_nickname|数据源用户' => 'max:100',
  27. 'source_group_title|数据源用户组' => 'max:100',
  28. 'nickname|共享用户' => 'max:100',
  29. 'type|共享权限' => 'number|in:' . $db::$type_只读 . ',' . $db::$type_读写,
  30. ]);
  31. if (!$val->check($param)) return error_show(1005, $val->getError());
  32. if ($param['source_nickname'] != '') {
  33. $source_uid = array_column(GetAccountall($param['token'], ['name' => $param['source_nickname']])['data'], 'id');
  34. $rs->whereIn('a.source_uid', $source_uid);
  35. }
  36. if ($param['source_group_title'] != '') $rs->whereLike('c.title', '%' . $param['source_group_title'] . '%');
  37. if ($param['nickname'] != '') {
  38. $source_uid = array_column(GetAccountall($param['token'], ['name' => $param['nickname']])['data'], 'id');
  39. $rs->whereIn('a.uid', $source_uid);
  40. }
  41. if ($param['group_title'] != '') $rs->whereLike('e.title', '%' . $param['group_title'] . '%');
  42. if ($param['type'] != '') $rs->whereLike('a.type', $param['type']);
  43. $count = $rs->count('a.id');
  44. $list = $rs
  45. ->field('a.*,"" source_nickname,c.title source_group_title,"" nickname,e.title group_title')
  46. ->append(['menu_list'])
  47. ->withAttr('menu_list', function ($val, $data) {
  48. return Db::name('admin_menu')
  49. ->field('id,menu_name')
  50. ->whereIn('id', $data['menu_ids'])
  51. ->where('is_del', 0)
  52. ->select()
  53. ->toArray();
  54. })
  55. ->page($param['page'], $param['size'])
  56. ->order('a.id', 'desc')
  57. ->select()
  58. ->toArray();
  59. $all_uid = array_merge(array_column($list, 'source_uid'), array_column($list, 'uid'));
  60. $all_uid = array_unique($all_uid);
  61. $all_uname = GetUserlist($param['token'], ['id' => $all_uid]);
  62. if ($all_uname['code'] == 0) {
  63. $all_uname = array_column($all_uname['data'], 'nickname', 'id');
  64. foreach ($list as &$val) {
  65. $val['source_nickname'] = $all_uname[$val['source_uid']] ?? '';
  66. $val['nickname'] = $all_uname[$val['uid']] ?? '';
  67. }
  68. }
  69. return app_show(0, '请求成功', ['count' => $count, 'list' => $list]);
  70. }
  71. //新建
  72. public function addDataShare()
  73. {
  74. $param = $this->request->filter('trim')->only(['title' => '', 'menu_ids', 'source_uid', 'source_data_group_id', 'uid', 'data_group_id', 'type', 'remark' => ''], 'post');
  75. $val = Validate::rule([
  76. 'title|共享规则名称' => 'max:100',
  77. 'menu_ids|菜单数据' => 'require|array|max:100',
  78. 'source_uid|数据源用户' => 'requireWithout:source_data_group_id|number|gt:0',
  79. 'source_data_group_id|数据源用户组' => 'requireWithout:source_uid|number|gt:0',
  80. 'uid|数据共享用户' => 'requireWithout:data_group_id|number|gt:0',
  81. 'data_group_id|数据共享用户组' => 'requireWithout:uid|number|gt:0',
  82. 'type|数据权限' => 'require|number|in:' . DataShareModel::$type_只读 . ',' . DataShareModel::$type_读写,
  83. 'remark|备注' => 'max:100',
  84. ]);
  85. if (!$val->check($param)) return error_show(1005, $val->getError());
  86. $rs = DataShareModel::create([
  87. 'title' => $param['title'],
  88. 'menu_ids' => implode(',', $param['menu_ids']),
  89. 'source_uid' => $param['source_uid'],
  90. 'source_data_group_id' => $param['source_data_group_id'],
  91. 'uid' => $param['uid'],
  92. 'data_group_id' => $param['data_group_id'],
  93. 'type' => $param['type'],
  94. 'remark' => $param['remark'],
  95. 'is_del' => DataShareModel::$delete_正常,
  96. 'status' => DataShareModel::$status_启用,
  97. 'createrid' => $this->uid,
  98. 'creater' => $this->uname,
  99. ])->save();
  100. return $rs ? app_show(0, '新建共享规则成功') : error_show(1004, '新建共享规则失败');
  101. }
  102. //获取详情
  103. public function getDataShareDetail()
  104. {
  105. $id = $this->request->filter('trim')->post('id/d', 0);
  106. $info = DataShareModel::field('a.*,b.nickname source_nickname,c.title source_group_title,d.nickname,e.title group_title')
  107. ->alias('a')
  108. ->leftJoin('user_role b', 'b.uid=a.source_uid AND b.is_del=0')
  109. ->leftJoin('data_group c', 'c.id=a.source_data_group_id AND c.is_del=' . DataGroupModel::$delete_正常)
  110. ->leftJoin('user_role d', 'd.uid=a.uid AND d.is_del=0')
  111. ->leftJoin('data_group e', 'e.id=a.data_group_id AND e.is_del=' . DataGroupModel::$delete_正常)
  112. ->where(['a.id' => $id, 'a.is_del' => DataShareModel::$delete_正常])
  113. ->append(['menu_list'])
  114. ->withAttr('menu_list', function ($val, $data) {
  115. return Db::name('admin_menu')
  116. ->field('id,menu_name')
  117. ->whereIn('id', $data['menu_ids'])
  118. ->where('is_del', 0)
  119. ->select()
  120. ->toArray();
  121. })
  122. ->findOrEmpty()
  123. ->toArray();
  124. return app_show(0, '获取详情成功', $info);
  125. }
  126. //修改
  127. public function editDataShare()
  128. {
  129. $param = $this->request->filter('trim')->only(['id', 'menu_ids', 'source_uid', 'source_data_group_id', 'uid', 'data_group_id', 'type', 'remark'], 'post');
  130. $val = Validate::rule([
  131. 'id' => 'require|number|gt:0',
  132. 'title|共享规则名称' => 'max:100',
  133. 'menu_ids|菜单数据' => 'require|array|max:100',
  134. 'source_uid|数据源用户' => 'requireWithout:source_data_group_id|number|gt:0',
  135. 'source_data_group_id|数据源用户组' => 'requireWithout:source_uid|number|gt:0',
  136. 'uid|数据共享用户' => 'requireWithout:data_group_id|number|gt:0',
  137. 'data_group_id|数据共享用户组' => 'requireWithout:uid|number|gt:0',
  138. 'type|数据权限' => 'require|number|in:' . DataShareModel::$type_只读 . ',' . DataShareModel::$type_读写,
  139. 'remark|备注' => 'max:100',
  140. ]);
  141. if (!$val->check($param)) return error_show(1005, $val->getError());
  142. $res = DataShareModel::field('id')
  143. ->where(['id' => $param['id'], 'is_del' => DataShareModel::$delete_正常])
  144. ->findOrEmpty()
  145. ->isEmpty();
  146. if ($res) return error_show(1005, '该记录不存在');
  147. if (isset($param['menu_ids']) && is_array($param['menu_ids'])) $param['menu_ids'] = implode(',', $param['menu_ids']);
  148. $rs = DataShareModel::where(['id' => $param['id'], 'is_del' => DataShareModel::$delete_正常])->strict(false)->save($param);
  149. return $rs ? app_show(0, '修改共享规则成功') : error_show(1005, '修改共享规则失败');
  150. }
  151. //删除
  152. public function deleteDataShare()
  153. {
  154. $param = $this->request->filter('trim')->only(['id'], 'post');
  155. $val = Validate::rule(['id' => 'require|array|max:100']);
  156. if (!$val->check($param)) return error_show(1005, $val->getError());
  157. $rs = DataShareModel::whereIn('id', $param['id'])
  158. ->where(['is_del' => DataShareModel::$delete_正常])
  159. ->update(['is_del' => DataShareModel::$delete_删除]);
  160. return $rs ? app_show(0, '删除共享规则成功') : error_show(1005, '删除共享规则失败');
  161. }
  162. //启/禁用
  163. public function statusDataShare()
  164. {
  165. $param = $this->request->filter('trim')->only(['id', 'status'], 'post');
  166. $val = Validate::rule([
  167. 'id' => 'require|array|max:100',
  168. 'status|状态' => 'require|number|in:' . DataShareModel::$status_启用 . ',' . DataShareModel::$status_禁用,
  169. ]);
  170. if (!$val->check($param)) return error_show(1005, $val->getError());
  171. $status = [0 => '禁用', 1 => '启用'];
  172. $rs = DataShareModel::where([
  173. ['id', 'in', $param['id']],
  174. ['is_del', '=', DataShareModel::$delete_正常],
  175. ['status', '<>', $param['status']],
  176. ])->update(['status' => $param['status']]);
  177. return $rs ? app_show(0, $status[$param['status']] . '共享规则成功') : error_show(1005, $status[$param['status']] . '共享规则失败');
  178. }
  179. }