Data.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php namespace Manage\Api\Page;
  2. use Dever;
  3. use Manage\Lib\Page;
  4. # 数据获取
  5. class Data extends Page
  6. {
  7. private $recycler = false;
  8. private $selection = false;
  9. private $expand = false;
  10. public function __construct($load = '')
  11. {
  12. parent::__construct('list', $load);
  13. }
  14. public function list()
  15. {
  16. if (!$this->getFunc('list', '列表', 1)) {
  17. Dever::error('无访问权限');
  18. }
  19. $data['title'] = '';
  20. $data['button'] = $this->button();
  21. $data['bodyButton'] = $this->button('data_button');
  22. $data['recycler'] = $this->recycler;
  23. $data = array_merge($data, $this->out());
  24. $data['total'] = Dever::page('total');
  25. $data['height'] = $this->config['height'] ?? '100%';
  26. $data['type'] = $this->config['type'] ?? 'table';
  27. $data['desc'] = $this->config['desc'] ?? '';
  28. $data['layout'] = $this->config['layout'] ?? array();
  29. $data['exportButton'] = $this->export();
  30. $data['show'] = array
  31. (
  32. 'selection' => $this->selection,
  33. 'expand' => $this->expand,
  34. 'index' => $this->config['index'] ?? false,
  35. );
  36. $this->column($data);
  37. return $data;
  38. }
  39. public function out()
  40. {
  41. $where = $this->config['where'] ?? array();
  42. $data['field'] = $data['head'] = array();
  43. $data['search'] = $this->search($where);
  44. if (isset($this->config['data'])) {
  45. $result = Dever::call($this->config['data'], $where);
  46. $data = array_merge($data, $result);
  47. } else {
  48. $data['field'] = $this->setting('field', $data['head'], true, 'show');
  49. $data['body'] = $this->data($where);
  50. }
  51. $method = Dever::input('method');
  52. if ($method && strstr($method, '.')) {
  53. $result = Dever::call($method, $data);
  54. unset($data);
  55. $data['field'] = $result['head'];
  56. $data['body'] = $result['body'];
  57. }
  58. $data['stat'] = array();
  59. if (isset($this->config['stat'])) {
  60. $data['stat'] = Dever::call($this->config['stat'], $where);
  61. }
  62. return $data;
  63. }
  64. private function data($where)
  65. {
  66. $set['num'] = Dever::input('pgnum', '', '', 10);
  67. $order_col = Dever::input('order_col');
  68. if ($order_col) {
  69. $order_value = Dever::input('order_value');
  70. if ($order_value) {
  71. $set['order'] = $order_col . ' ' . $order_value . ', id desc';
  72. }
  73. }
  74. if (isset($this->config['tree'])) {
  75. return $this->db->tree($where, $this->config['tree'], array($this, 'handleData'));
  76. }
  77. $data = $this->db->select($where, $set);
  78. $result = array();
  79. if ($data) {
  80. foreach ($data as $k => $v) {
  81. $result[$k] = $this->handleData($v);
  82. }
  83. }
  84. return $result;
  85. }
  86. public function handleData($v)
  87. {
  88. $result['id'] = $v['id'];
  89. $result['cdate'] = $v['cdate'];
  90. foreach ($this->field as $value) {
  91. $key = $value['key'];
  92. if (isset($v[$key])) {
  93. $result[$key] = $this->getValue($key, $v[$key], $v, $value);
  94. } elseif (strpos($key, '/')) {
  95. $other = $this->getOther($key, $value['where'], $v);
  96. if ($other) {
  97. $otherName = array();
  98. foreach ($other as $k1 => $v1) {
  99. if (isset($v1['name'])) {
  100. $otherName[] = $v1['name'];
  101. }
  102. }
  103. if ($otherName) {
  104. $result[$key] = implode('、', $otherName);
  105. } else {
  106. $result[$key] = $other;
  107. }
  108. }
  109. }
  110. }
  111. if (isset($this->config['expand']) && $this->config['expand']) {
  112. $result['expand'] = Dever::call($this->config['expand'], $result);
  113. $this->expand = true;
  114. }
  115. return $result;
  116. }
  117. private function export()
  118. {
  119. $result = false;
  120. if (isset($this->config['export'])) {
  121. $result = array();
  122. foreach ($this->config['export'] as $k => $v) {
  123. $func = $this->getFunc($k, $v, 300);
  124. if ($func) {
  125. $result[$k] = $v;
  126. }
  127. }
  128. }
  129. return $result;
  130. }
  131. private function search(&$where)
  132. {
  133. $search = Dever::input('search');
  134. $list_search = $result = array();
  135. $this->setting('search', $list_search, false, 'text');
  136. if ($list_search) {
  137. foreach ($list_search as $v) {
  138. if ($v['type'] != 'hidden') {
  139. $result[] = $v;
  140. }
  141. if ($search) {
  142. if ($value = Dever::isset($search, $v['key'])) {
  143. if ($v['type'] == 'group') {
  144. $where[$v['key']] = array('group', $value);
  145. } elseif ($v['type'] == 'selects') {
  146. $where[$v['key']] = array('group', $value);
  147. } elseif ($v['type'] == 'like') {
  148. $where[$v['key']] = array('like', $value);
  149. } elseif ($v['type'] == 'in') {
  150. $where[$v['key']] = array('in', $value);
  151. } else {
  152. $where[$v['key']] = $value;
  153. }
  154. }
  155. }
  156. }
  157. }
  158. return $result;
  159. }
  160. private function button($key = 'button')
  161. {
  162. $result = array();
  163. if (empty($this->config[$key])) {
  164. $num = 0;
  165. if (isset($this->db->config['manage']['update']['field'])) {
  166. $num = count($this->db->config['manage']['update']['field']);
  167. } elseif (isset($this->db->config['struct']) && $this->db->config['struct']) {
  168. $num = count($this->db->config['struct']);
  169. }
  170. $fast = 'fast';
  171. if ($num > 8) {
  172. $fast = '';
  173. }
  174. if ($key == 'button') {
  175. $this->config[$key] = array('新增' => $fast . 'add');
  176. } else {
  177. $this->config[$key] = array('编辑' => $fast . 'edit', '删除' => 'recycle');
  178. }
  179. }
  180. $sort = 1;
  181. foreach ($this->config[$key] as $k => $v) {
  182. $p = '';
  183. $i = '';
  184. if (is_array($v)) {
  185. if (isset($v[2])) {
  186. $i = $v[2];
  187. }
  188. if (isset($v[1])) {
  189. $p = $v[1];
  190. }
  191. $v = $v[0];
  192. }
  193. if (strstr($v, 'add')) {
  194. $icon = 'Plus';
  195. $button = 'primary';
  196. } elseif (strstr($v, 'edit')) {
  197. $icon = 'Edit';
  198. $button = 'primary';
  199. } elseif (strstr($v, 'view')) {
  200. $icon = 'View';
  201. $button = '';
  202. } elseif ($v == 'delete') {
  203. if ($key == 'button') {
  204. if (isset($this->config['layout'])) {
  205. continue;
  206. }
  207. $this->selection = true;
  208. }
  209. $icon = 'Delete';
  210. $button = 'danger';
  211. } elseif ($v == 'recycle') {
  212. if ($key == 'button') {
  213. if (isset($this->config['layout'])) {
  214. continue;
  215. }
  216. $this->selection = true;
  217. }
  218. $icon = 'Delete';
  219. $button = 'danger';
  220. } elseif ($v == 'oper') {
  221. if ($key == 'button') {
  222. if (isset($this->config['layout'])) {
  223. continue;
  224. }
  225. $this->selection = true;
  226. }
  227. $icon = 'Notification';
  228. $button = 'warning';
  229. } elseif ($v == 'api') {
  230. if ($key == 'button') {
  231. if (isset($this->config['layout'])) {
  232. continue;
  233. }
  234. $this->selection = true;
  235. }
  236. $icon = 'Notification';
  237. $button = 'warning';
  238. } elseif ($v == 'link') {
  239. $icon = 'Link';
  240. $button = 'success';
  241. } elseif ($v == 'route') {
  242. $icon = 'Link';
  243. $button = 'success';
  244. } elseif ($v == 'recover') {
  245. $icon = 'CirclePlus';
  246. $button = 'info';
  247. } else {
  248. continue;
  249. }
  250. # 权限验证
  251. $sort++;
  252. $func = $this->getFunc($v, $k, $sort, $p);
  253. if (!$func) {
  254. continue;
  255. }
  256. if ($i) {
  257. $icon = $i;
  258. }
  259. $result[] = array
  260. (
  261. 'name' => $k,
  262. 'type' => $v,
  263. 'param' => $p,
  264. 'icon' => $icon,
  265. 'button' => $button,
  266. 'func' => $func,
  267. );
  268. if (!$this->recycler && $v == 'recycle') {
  269. $this->recycler = true;
  270. }
  271. }
  272. return $result;
  273. }
  274. }