Packet.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\controller;
  10. use app\common\builder\ZBuilder;
  11. use app\admin\model\Packet as PacketModel;
  12. /**
  13. * 数据包控制器
  14. * @package app\admin\controller
  15. */
  16. class Packet extends Admin
  17. {
  18. /**
  19. * 首页
  20. * @param string $group 分组
  21. * @author 蔡伟明 <314013107@qq.com>
  22. * @return mixed|string
  23. * @throws \think\Exception
  24. */
  25. public function index($group = 'local')
  26. {
  27. // 配置分组信息
  28. $list_group = ['local' => '本地数据包'];
  29. $tab_list = [];
  30. foreach ($list_group as $key => $value) {
  31. $tab_list[$key]['title'] = $value;
  32. $tab_list[$key]['url'] = url('index', ['group' => $key]);
  33. }
  34. $PacketModel = new PacketModel;
  35. $data_list = $PacketModel->getAll();
  36. foreach ($data_list as &$value) {
  37. if (isset($value['author_url']) && !empty($value['author_url'])) {
  38. $value['author'] = '<a href="'. $value['author_url']. '" target="_blank">'. $value['author'] .'</a>';
  39. }
  40. }
  41. if ($data_list === false) {
  42. $this->error($PacketModel->getError());
  43. }
  44. // 自定义按钮
  45. $btn_install = [
  46. 'title' => '安装',
  47. 'icon' => 'fa fa-fw fa-sign-in',
  48. 'class' => 'btn btn-xs btn-default ajax-get confirm',
  49. 'href' => url('install', ['name' => '__id__'])
  50. ];
  51. $btn_uninstall = [
  52. 'title' => '卸载',
  53. 'icon' => 'fa fa-fw fa-sign-out',
  54. 'class' => 'btn btn-xs btn-default ajax-get confirm',
  55. 'href' => url('uninstall', ['name' => '__id__'])
  56. ];
  57. $btn_install_all = [
  58. 'title' => '安装',
  59. 'icon' => 'fa fa-fw fa-sign-in',
  60. 'class' => 'btn btn-primary ajax-post confirm',
  61. 'href' => url('install')
  62. ];
  63. $btn_uninstall_all = [
  64. 'title' => '卸载',
  65. 'icon' => 'fa fa-fw fa-sign-out',
  66. 'class' => 'btn btn-danger ajax-post confirm',
  67. 'href' => url('uninstall')
  68. ];
  69. switch ($group) {
  70. case 'local':
  71. // 使用ZBuilder快速创建数据表格
  72. return ZBuilder::make('table')
  73. ->setPageTitle('数据包管理') // 设置页面标题
  74. ->setPrimaryKey('name')
  75. ->setTabNav($tab_list, $group) // 设置tab分页
  76. ->addColumns([ // 批量添加数据列
  77. ['name', '名称'],
  78. ['title', '标题'],
  79. ['author', '作者'],
  80. ['version', '版本号'],
  81. ['status', '是否安装', 'yesno'],
  82. ['right_button', '操作', 'btn']
  83. ])
  84. ->addTopButton('custom', $btn_install_all)
  85. ->addTopButton('custom', $btn_uninstall_all)
  86. ->addRightButton('custom', $btn_install) // 添加右侧按钮
  87. ->addRightButton('custom', $btn_uninstall) // 添加右侧按钮
  88. ->setRowList($data_list) // 设置表格数据
  89. ->fetch(); // 渲染模板
  90. break;
  91. case 'online':
  92. return '<h2>正在制作中...</h2>';
  93. break;
  94. }
  95. }
  96. /**
  97. * 安装
  98. * @param string $name 数据包名
  99. * @author 蔡伟明 <314013107@qq.com>
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\ModelNotFoundException
  102. * @throws \think\exception\DbException
  103. */
  104. public function install($name = '')
  105. {
  106. $names = $name != '' ? (array)$name : $this->request->param('ids/a');
  107. foreach ($names as $name) {
  108. $result = PacketModel::install($name);
  109. if ($result === true) {
  110. if (!PacketModel::where('name', $name)->find()) {
  111. $data = PacketModel::getInfoFromFile($name);
  112. $data['status'] = 1;
  113. $data['tables'] = json_encode($data['tables']);
  114. PacketModel::create($data);
  115. }
  116. } else {
  117. $this->error('安装失败:'. $result);
  118. }
  119. }
  120. // 记录行为
  121. $packet_titles = PacketModel::where('name', 'in', $names)->column('title');
  122. action_log('packet_install', 'admin_packet', 0, UID, implode('、', $packet_titles));
  123. $this->success('安装成功');
  124. }
  125. /**
  126. * 卸载
  127. * @param string $name 数据包名
  128. * @author 蔡伟明 <314013107@qq.com>
  129. */
  130. public function uninstall($name = '')
  131. {
  132. $names = $name != '' ? (array)$name : $this->request->param('ids/a');
  133. // 记录行为
  134. $packet_titles = PacketModel::where('name', 'in', $names)->column('title');
  135. action_log('packet_uninstall', 'admin_packet', 0, UID, implode('、', $packet_titles));
  136. foreach ($names as $name) {
  137. PacketModel::uninstall($name);
  138. }
  139. $this->success('卸载成功');
  140. }
  141. }