Data.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 $expand = false;
  9. public function __construct($load = '')
  10. {
  11. parent::__construct('list', $load);
  12. }
  13. public function list()
  14. {
  15. if ($this->menu && $this->menu['show'] == 1 && !$this->getFunc('list', '列表', 1)) {
  16. Dever::error('无访问权限');
  17. }
  18. $data['title'] = '';
  19. $data['button'] = $this->button();
  20. $data['recycler'] = $this->recycler;
  21. $data = array_merge($data, $this->out());
  22. $data['total'] = Dever::page('total');
  23. $data['height'] = $this->config['height'] ?? '100%';
  24. $data['type'] = $this->config['type'] ?? 'table';
  25. $data['desc'] = $this->config['desc'] ?? '';
  26. $data['layout'] = $this->config['layout'] ?? array();
  27. $data['exportButton'] = $this->export();
  28. $data['show'] = array
  29. (
  30. 'selection' => $this->config['selection'] ?? false,
  31. 'expand' => $this->expand,
  32. 'index' => $this->config['index'] ?? false,
  33. );
  34. $this->column($data);
  35. return $data;
  36. }
  37. public function out()
  38. {
  39. $where = $this->config['where'] ?? array();
  40. $set = $this->config['set'] ?? array();
  41. $data['field'] = $data['head'] = array();
  42. $data['search'] = $this->search($where);
  43. $ids = Dever::input('ids');
  44. if ($ids) {
  45. $where['id'] = array('in', $ids);
  46. }
  47. if (isset($this->config['data'])) {
  48. $result = Dever::call($this->config['data'], array($where, $set));
  49. $data = array_merge($data, $result);
  50. } else {
  51. $data['field'] = $this->setting('field', $data['head'], true, 'show');
  52. $data['body'] = $this->data($where, $set);
  53. }
  54. $method = Dever::input('method');
  55. if ($method && strstr($method, '.')) {
  56. $result = Dever::call($method, array($data));
  57. unset($data);
  58. $data['field'] = $result['head'];
  59. $data['body'] = $result['body'];
  60. }
  61. $data['stat'] = array();
  62. if (isset($this->config['stat'])) {
  63. $data['stat'] = Dever::call($this->config['stat'], array($where));
  64. }
  65. return $data;
  66. }
  67. private function data($where, $set = array())
  68. {
  69. $set['num'] = Dever::input('pgnum', '', '', 10);
  70. $order_col = Dever::input('order_col');
  71. if ($order_col) {
  72. $order_value = Dever::input('order_value');
  73. if ($order_value) {
  74. $set['order'] = $order_col . ' ' . $order_value . ', id desc';
  75. }
  76. }
  77. if (isset($this->config['tree'])) {
  78. return $this->db->tree($where, $this->config['tree'], array($this, 'handleData'));
  79. }
  80. $data = $this->db->select($where, $set);
  81. $result = array();
  82. if ($data) {
  83. foreach ($data as $k => $v) {
  84. $result[$k] = $this->handleData($k, $v);
  85. }
  86. }
  87. return $result;
  88. }
  89. public function handleData($k, $v)
  90. {
  91. $button = $this->button('data_button', $v);
  92. if ($button) {
  93. $result['button'] = $button;
  94. }
  95. # 是否保留html代码,1是保留,2是不保留
  96. $html = Dever::input('html', '', '', 1);
  97. if (isset($v['id'])) {
  98. $result['id'] = $v['id'];
  99. }
  100. $result['cdate'] = $v['cdate'];
  101. foreach ($this->field as $value) {
  102. $key = $value['key'];
  103. if (isset($v[$key])) {
  104. $result[$key] = $this->getValue($key, $v[$key], $v, $value);
  105. } elseif (strpos($key, '/')) {
  106. $other = $this->getOther($key, $value['where'], $v);
  107. if ($other) {
  108. $otherName = array();
  109. foreach ($other as $k1 => $v1) {
  110. if (isset($v1['name'])) {
  111. $otherName[] = $v1['name'];
  112. }
  113. }
  114. if ($otherName) {
  115. $result[$key] = implode('、', $otherName);
  116. } else {
  117. $result[$key] = $other;
  118. }
  119. }
  120. }
  121. if ($html == 2) {
  122. $result[$key] = strip_tags($result[$key]);
  123. }
  124. }
  125. if (isset($this->config['expand']) && $this->config['expand']) {
  126. $result['expand'] = Dever::call($this->config['expand'], array($v));
  127. $this->expand = true;
  128. }
  129. return $result;
  130. }
  131. private function export()
  132. {
  133. $result = false;
  134. if (isset($this->config['export']) && $this->config['export']) {
  135. $result = array();
  136. foreach ($this->config['export'] as $k => $v) {
  137. $func = $this->getFunc($k, $v, 300);
  138. if ($func) {
  139. $result[$k] = $v;
  140. }
  141. }
  142. }
  143. return $result;
  144. }
  145. private function search(&$where)
  146. {
  147. $search = Dever::input('search');
  148. $set = Dever::input('set');
  149. $list_search = $result = array();
  150. $result['form'] = $result['field'] = $result['option'] = array();
  151. $this->setting('search', $list_search, false, 'text');
  152. if ($list_search) {
  153. foreach ($list_search as $v) {
  154. if ($v['type'] != 'hidden') {
  155. $result['form'][] = $v;
  156. $result['field'][$v['key']] = $v['value'];
  157. if ($v['type'] == 'sku') {
  158. $result['field'][$v['key'] . '_spec'] = [];
  159. }
  160. if (isset($v['option'])) {
  161. $result['option'][$v['key']] = $v['option'];
  162. }
  163. }
  164. $this->where($search, $v, $where);
  165. $this->where($set, $v, $where);
  166. }
  167. }
  168. return $result;
  169. }
  170. private function where($value, $v, &$where)
  171. {
  172. if ($value) {
  173. if ($value = Dever::isset($value, $v['key'])) {
  174. if (isset($v['search'])) {
  175. if (is_callable($v['search'])) {
  176. $value = $v['search']($v['key'], $v['type'], $value);
  177. } elseif (isset($v['search']['table'])) {
  178. $v['search']['where'] = Dever::json_decode(str_replace('{value}', $value, Dever::json_encode($v['search']['where'])));
  179. $search = Dever::db($v['search']['table'])->select($v['search']['where'], $v['search']['set'] ?? array());
  180. $value = array();
  181. if ($search) {
  182. foreach ($search as $v1) {
  183. $value[] = $v1[$v['search']['field']];
  184. }
  185. }
  186. $value = implode(',', $value);
  187. $v['type'] = 'in';
  188. }
  189. }
  190. if ($v['type'] == 'group') {
  191. $where[$v['key']] = array('group', $value);
  192. } elseif ($v['type'] == 'selects') {
  193. $where[$v['key']] = array('group', $value);
  194. } elseif ($v['type'] == 'like') {
  195. $where[$v['key']] = array('like', $value);
  196. } elseif ($v['type'] == 'in') {
  197. $where[$v['key']] = array('in', $value);
  198. } elseif ($v['type'] == 'date') {
  199. if (strstr($v['date_type'], 'range')) {
  200. $where[$v['key']] = array('>=', \Dever\Helper\Date::mktime($value[0]));
  201. $where[$v['key'] . '#'] = array('<=', \Dever\Helper\Date::mktime($value[1]));
  202. } else {
  203. $where[$v['key']] = $value;
  204. }
  205. } else {
  206. $where[$v['key']] = $value;
  207. }
  208. }
  209. }
  210. }
  211. private function button($key = 'button', $data = array())
  212. {
  213. $result = array();
  214. if (!isset($this->config[$key])) {
  215. $num = 0;
  216. if (isset($this->db->config['manage']['update']['field'])) {
  217. $num = count($this->db->config['manage']['update']['field']);
  218. } elseif (isset($this->db->config['struct']) && $this->db->config['struct']) {
  219. $num = count($this->db->config['struct']);
  220. }
  221. $fast = 'fast';
  222. if ($num > 8) {
  223. $fast = '';
  224. }
  225. if ($key == 'button') {
  226. $this->config[$key] = array('新增' => $fast . 'add');
  227. } else {
  228. $this->config[$key] = array('编辑' => $fast . 'edit', '删除' => 'recycle');
  229. }
  230. }
  231. $sort = 1;
  232. foreach ($this->config[$key] as $k => $v) {
  233. $d = '';
  234. $p = '';
  235. $i = '';
  236. if (is_array($v)) {
  237. if (isset($v[3]) && $data) {
  238. $d = $v[3];
  239. parse_str($d, $t);
  240. $state = true;
  241. foreach ($t as $k1 => $v1) {
  242. if (!isset($data[$k1]) || (isset($data[$k1]) && $data[$k1] != $v1)) {
  243. $state = false;
  244. }
  245. }
  246. if (!$state) {
  247. continue;
  248. }
  249. }
  250. if (isset($v[2])) {
  251. $i = $v[2];
  252. }
  253. if (isset($v[1])) {
  254. $p = $v[1];
  255. }
  256. $v = $v[0];
  257. }
  258. if (strstr($v, 'add')) {
  259. $icon = 'Plus';
  260. $button = 'primary';
  261. } elseif (strstr($v, 'edit')) {
  262. $icon = 'Edit';
  263. $button = 'primary';
  264. } elseif (strstr($v, 'view')) {
  265. $icon = 'View';
  266. $button = '';
  267. } elseif ($v == 'delete') {
  268. if ($key == 'button') {
  269. if (isset($this->config['layout'])) {
  270. continue;
  271. }
  272. $this->config['selection'] = true;
  273. }
  274. $icon = 'Delete';
  275. $button = 'danger';
  276. } elseif ($v == 'recycle') {
  277. if ($key == 'button') {
  278. if (isset($this->config['layout'])) {
  279. continue;
  280. }
  281. $this->config['selection'] = true;
  282. }
  283. $icon = 'Delete';
  284. $button = 'danger';
  285. } elseif ($v == 'oper') {
  286. if ($key == 'button') {
  287. if (isset($this->config['layout'])) {
  288. continue;
  289. }
  290. $this->config['selection'] = true;
  291. }
  292. $icon = 'Notification';
  293. $button = 'warning';
  294. } elseif ($v == 'api') {
  295. if ($key == 'button') {
  296. if (isset($this->config['layout'])) {
  297. continue;
  298. }
  299. $this->config['selection'] = true;
  300. }
  301. $p = Dever::url($p);
  302. $icon = 'Notification';
  303. $button = 'warning';
  304. } elseif ($v == 'link') {
  305. $icon = 'Link';
  306. $button = 'success';
  307. } elseif ($v == 'route') {
  308. $icon = 'Link';
  309. $button = 'success';
  310. } elseif ($v == 'recover') {
  311. $icon = 'CirclePlus';
  312. $button = 'info';
  313. } else {
  314. continue;
  315. }
  316. # 权限验证
  317. $sort++;
  318. $func = $this->getFunc($v, $k, $sort, $p);
  319. if ($this->menu && $this->menu['show'] == 1 && !$func) {
  320. continue;
  321. }
  322. if ($i) {
  323. $icon = $i;
  324. }
  325. $result[] = array
  326. (
  327. 'name' => $k,
  328. 'type' => $v,
  329. 'param' => $p,
  330. 'icon' => $icon,
  331. 'button' => $button,
  332. 'func' => $func,
  333. );
  334. if (!$this->recycler && $v == 'recycle') {
  335. $this->recycler = true;
  336. }
  337. }
  338. return $result;
  339. }
  340. }