Module.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. declare (strict_types = 1);
  14. namespace app\home\controller;
  15. use app\base\BaseController;
  16. use app\home\validate\ModuleCheck;
  17. use think\exception\ValidateException;
  18. use think\facade\Db;
  19. use think\facade\View;
  20. class Module extends BaseController
  21. {
  22. public function index()
  23. {
  24. if (request()->isAjax()) {
  25. $module = Db::name('AdminModule')->select();
  26. return to_assign(0, '', $module);
  27. } else {
  28. $sys_module = Db::name('AdminModule')->select()->toArray();
  29. View::assign('sys_module', $sys_module);
  30. return view();
  31. }
  32. }
  33. //新增/编辑模块
  34. public function add()
  35. {
  36. $param = get_params();
  37. if (request()->isAjax()) {
  38. if($this->uid!=1){
  39. return to_assign(1,'只有系统超级管理员才有权限新增或编辑模块!');
  40. }
  41. if (!empty($param['id']) && $param['id'] > 0) {
  42. try {
  43. validate(ModuleCheck::class)->scene('edit')->check($param);
  44. } catch (ValidateException $e) {
  45. // 验证失败 输出错误信息
  46. return to_assign(1, $e->getError());
  47. }
  48. Db::name('AdminModule')->where(['id' => $param['id']])->strict(false)->field(true)->update($param);
  49. add_log('edit', $param['id'], $param);
  50. } else {
  51. try {
  52. validate(ModuleCheck::class)->scene('add')->check($param);
  53. } catch (ValidateException $e) {
  54. // 验证失败 输出错误信息
  55. return to_assign(1, $e->getError());
  56. }
  57. $mid = Db::name('AdminModule')->strict(false)->field(true)->insertGetId($param);
  58. add_log('add', $mid, $param);
  59. }
  60. return to_assign();
  61. } else {
  62. $id = isset($param['id']) ? $param['id'] : 0;
  63. $module=[];
  64. if ($id > 0) {
  65. $module = Db::name('AdminModule')->where(['id' => $id])->find();
  66. }
  67. View::assign('id', $id);
  68. View::assign('module', $module);
  69. return view();
  70. }
  71. }
  72. //安装模块
  73. public function install()
  74. {
  75. if($this->uid!=1){
  76. return to_assign(1,'只有系统超级管理员才有权限安装模块!');
  77. }
  78. $param = get_params();
  79. $name = $param['name'];
  80. $data = curl_post('https://www.gougucms.com/home/get_module/module',['name'=>$name]);
  81. $json_data = json_decode($data, true);
  82. if($json_data['code'] == 1){
  83. return to_assign(1,$json_data['msg']);
  84. }
  85. $detail = $json_data['data'];
  86. $rule = unserialize($detail['rule']);
  87. if(empty($rule)){
  88. return to_assign(1,'找不到该模块的信息');
  89. }
  90. $prefix = get_config('database.connections.mysql.prefix');
  91. $insert = [];
  92. $insert['title'] = $detail['title'];
  93. $insert['name'] = $detail['name'];
  94. $insert['type'] = $detail['type'];
  95. $insert['sourse'] = $detail['sourse'];
  96. $insert['create_time'] = time();
  97. try {
  98. validate(ModuleCheck::class)->scene('add')->check($insert);
  99. } catch (ValidateException $e) {
  100. // 验证失败 输出错误信息
  101. return to_assign(1, $e->getError());
  102. }
  103. //sql语句
  104. $sql_file = CMS_ROOT . '/app/'.$name.'/config/install.sql';
  105. $sql_array = [];
  106. if(file_exists($sql_file)){
  107. $sql = file_get_contents($sql_file);
  108. $sql_array = preg_split("/;[\r\n]+/", str_replace("oa_", $prefix, $sql));
  109. }
  110. //var_dump($sql_array);exit;
  111. Db::startTrans();
  112. try {
  113. // 导入sql数据并创建表
  114. if(!empty($sql_array)){
  115. foreach ($sql_array as $k => $v) {
  116. if (!empty($v)) {
  117. Db::execute($v);
  118. }
  119. }
  120. }
  121. //如果安装过该模块,删除原来的菜单信息
  122. Db::name('AdminRule')->where('module',$name)->delete();
  123. $sort = Db::name('AdminRule')->where('pid',0)->max('sort');
  124. $this->add_rule($rule,0,$sort+1);
  125. $mid = Db::name('AdminModule')->strict(false)->field(true)->insertGetId($insert);
  126. Db::commit();
  127. }
  128. catch (\Exception $e) {
  129. //回滚事务
  130. Db::rollback();
  131. return to_assign(1,'捕获到异常'.$e->getMessage());
  132. }
  133. //更新超级管理员的权限节点
  134. $rules = Db::name('AdminRule')->column('id');
  135. $admin_rules = implode(',',$rules);
  136. $res = Db::name('AdminGroup')->strict(false)->where('id',1)->update(['rules'=>$admin_rules,'update_time'=>time()]);
  137. if($res!==false){
  138. // 删除后台节点缓存
  139. clear_cache('adminRules');
  140. add_log('install', $mid, $insert);
  141. return to_assign();
  142. }
  143. else{
  144. return to_assign(1,'操作失败');
  145. }
  146. }
  147. //递归插入菜单数据
  148. protected function add_rule($data, $pid=0,$sort=0)
  149. {
  150. foreach($data as $k => $v)
  151. {
  152. $rule=[
  153. 'title' => $v['title'],
  154. 'name' => $v['name'],
  155. 'src' => $v['src'],
  156. 'module' => $v['module'],
  157. 'menu' => $v['menu'],
  158. 'icon' => $v['icon'],
  159. 'pid' => $pid,
  160. 'sort' => $sort,
  161. 'create_time' => time()
  162. ];
  163. $new_id = Db::name('AdminRule')->strict(false)->field(true)->insertGetId($rule);
  164. if(!empty($v['son'] && $new_id)){
  165. $this->add_rule($v['son'],$new_id);
  166. }
  167. }
  168. }
  169. //删除
  170. public function del()
  171. {
  172. if($this->uid!=1){
  173. return to_assign(1,'只有系统超级管理员才有权限删除模块!');
  174. }
  175. $param = get_params();
  176. $module = Db::name('AdminModule')->where('id',$param['id'])->find();
  177. if($module['type'] == 1){
  178. return to_assign(1,'系统模块不能删除');
  179. }
  180. $param['update_time']= time();
  181. $res = Db::name('AdminModule')->where('id',$param['id'])->delete();
  182. if($res!==false){
  183. add_log('delete', $module['id'], $param);
  184. return to_assign();
  185. }
  186. else{
  187. return to_assign(1,'操作失败');
  188. }
  189. }
  190. //卸载
  191. public function uninstall()
  192. {
  193. if($this->uid!=1){
  194. return to_assign(1,'只有系统超级管理员才有权限卸载模块!');
  195. }
  196. $param = get_params();
  197. $module = Db::name('AdminModule')->where('name',$param['name'])->find();
  198. if($module['type'] == 1){
  199. return to_assign(1,'系统模块不能卸载');
  200. }
  201. $param['update_time']= time();
  202. $res = Db::name('AdminModule')->where('name',$param['name'])->update(['status'=>0]);
  203. if($res!==false){
  204. Db::name('AdminRule')->strict(false)->where('module',$module['name'])->update(['status'=>0]);
  205. // 删除后台节点缓存
  206. clear_cache('adminRules');
  207. add_log('uninstall', $module['id'], $param);
  208. return to_assign();
  209. }
  210. else{
  211. return to_assign(1,'操作失败');
  212. }
  213. }
  214. //恢复
  215. public function recovery()
  216. {
  217. if($this->uid!=1){
  218. return to_assign(1,'只有系统超级管理员才有权限恢复模块!');
  219. }
  220. $param = get_params();
  221. $module = Db::name('AdminModule')->where('name',$param['name'])->find();
  222. $param['update_time']= time();
  223. $res = Db::name('AdminModule')->where('name',$param['name'])->update(['status'=>1]);
  224. if($res!==false){
  225. Db::name('AdminRule')->strict(false)->where('module',$module['name'])->update(['status'=>1]);
  226. // 删除后台节点缓存
  227. clear_cache('adminRules');
  228. add_log('recovery', $module['id'], $param);
  229. return to_assign();
  230. }
  231. else{
  232. return to_assign(1,'操作失败');
  233. }
  234. }
  235. //数据权限列表
  236. public function data_auth()
  237. {
  238. if (request()->isAjax()) {
  239. $auth = Db::name('DataAuth')->select();
  240. return to_assign(0, '', $auth);
  241. } else {
  242. return view();
  243. }
  244. }
  245. //数据权限详情
  246. public function data_auth_detail()
  247. {
  248. $param = get_params();
  249. if (request()->isPost()) {
  250. $param['update_time'] = time();
  251. $res = Db::name('DataAuth')->strict(false)->field(true)->update($param);
  252. return to_assign();
  253. } else {
  254. $detail = Db::name('DataAuth')->where('name',$param['name'])->find();
  255. View::assign('detail', $detail);
  256. return view();
  257. }
  258. }
  259. }