DataShare.php 9.4 KB

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