Data.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. } elseif (isset($v['id'])) {
  100. $result['id'] = $v['id'];
  101. }
  102. $result['cdate'] = $v['cdate'];
  103. foreach ($this->field as $value) {
  104. $key = $value['key'];
  105. if (isset($v[$key])) {
  106. $result[$key] = $this->getValue($key, $v[$key], $v, $value);
  107. } elseif (strpos($key, '/')) {
  108. $other = $this->getOther($key, $value['where'], $v);
  109. if ($other) {
  110. $otherName = array();
  111. foreach ($other as $k1 => $v1) {
  112. if (isset($v1['name'])) {
  113. $otherName[] = $v1['name'];
  114. }
  115. }
  116. if ($otherName) {
  117. $result[$key] = implode('、', $otherName);
  118. } else {
  119. $result[$key] = $other;
  120. }
  121. }
  122. }
  123. if ($html == 2) {
  124. $result[$key] = strip_tags($result[$key]);
  125. }
  126. }
  127. if (isset($this->config['expand']) && $this->config['expand']) {
  128. $result['expand'] = Dever::call($this->config['expand'], array($v));
  129. $this->expand = true;
  130. }
  131. return $result;
  132. }
  133. private function export()
  134. {
  135. $result = false;
  136. if (isset($this->config['export']) && $this->config['export']) {
  137. $result = array();
  138. foreach ($this->config['export'] as $k => $v) {
  139. $func = $this->getFunc($k, $v, 300);
  140. if ($func) {
  141. $result[$k] = $v;
  142. }
  143. }
  144. }
  145. return $result;
  146. }
  147. private function search(&$where)
  148. {
  149. $search = Dever::input('search');
  150. $set = Dever::input('set');
  151. $list_search = $result = array();
  152. $result['form'] = $result['field'] = $result['option'] = array();
  153. $this->setting('search', $list_search, false, 'text');
  154. if ($list_search) {
  155. foreach ($list_search as $v) {
  156. if ($v['type'] != 'hidden') {
  157. $result['form'][] = $v;
  158. if (is_numeric($v['value'])) {
  159. $v['value'] = (float) $v['value'];
  160. }
  161. $result['field'][$v['key']] = $v['value'];
  162. if ($v['type'] == 'sku') {
  163. $result['field'][$v['key'] . '_spec'] = [];
  164. }
  165. if (isset($v['option'])) {
  166. $result['option'][$v['key']] = $v['option'];
  167. }
  168. }
  169. $this->where($search, $v, $where);
  170. $this->where($set, $v, $where);
  171. }
  172. }
  173. return $result;
  174. }
  175. private function where($value, $v, &$where)
  176. {
  177. if ($value) {
  178. if ($value = Dever::issets($value, $v['key'])) {
  179. if (isset($v['search'])) {
  180. if (is_callable($v['search'])) {
  181. $value = $v['search']($v['key'], $v['type'], $value);
  182. } elseif (isset($v['search']['table'])) {
  183. $v['search']['where'] = Dever::json_decode(str_replace('{value}', $value, Dever::json_encode($v['search']['where'])));
  184. $search = Dever::db($v['search']['table'])->select($v['search']['where'], $v['search']['set'] ?? array());
  185. $value = array();
  186. if ($search) {
  187. foreach ($search as $v1) {
  188. $value[] = $v1[$v['search']['field']];
  189. }
  190. }
  191. $value = implode(',', $value);
  192. $v['type'] = 'in';
  193. if (isset($v['search']['key'])) {
  194. $v['key'] = $v['search']['key'];
  195. }
  196. }
  197. }
  198. if ($v['type'] == 'group') {
  199. $where[$v['key']] = array('group', $value);
  200. } elseif ($v['type'] == 'selects') {
  201. $where[$v['key']] = array('group', $value);
  202. } elseif ($v['type'] == 'like') {
  203. $where[$v['key']] = array('like', $value);
  204. } elseif ($v['type'] == 'in') {
  205. $where[$v['key']] = array('in', $value);
  206. } elseif ($v['type'] == 'date') {
  207. if (strstr($v['date_type'], 'range')) {
  208. $where[$v['key']] = array('>=', \Dever\Helper\Date::mktime($value[0]));
  209. $where[$v['key'] . '#'] = array('<=', \Dever\Helper\Date::mktime($value[1]));
  210. } else {
  211. $where[$v['key']] = $value;
  212. }
  213. } else {
  214. $where[$v['key']] = $value;
  215. }
  216. }
  217. }
  218. }
  219. private function button($key = 'button', $data = array())
  220. {
  221. $result = array();
  222. if (!isset($this->config[$key])) {
  223. $num = 0;
  224. if (isset($this->db->config['manage']['update']['field'])) {
  225. $num = count($this->db->config['manage']['update']['field']);
  226. } elseif (isset($this->db->config['struct']) && $this->db->config['struct']) {
  227. $num = count($this->db->config['struct']);
  228. }
  229. $fast = 'fast';
  230. if ($num > 8) {
  231. $fast = '';
  232. }
  233. if ($key == 'button') {
  234. $this->config[$key] = array('新增' => $fast . 'add');
  235. } else {
  236. $this->config[$key] = array('编辑' => $fast . 'edit', '删除' => 'recycle');
  237. }
  238. }
  239. $sort = 1;
  240. foreach ($this->config[$key] as $k => $v) {
  241. $d = '';
  242. $p = '';
  243. $i = '';
  244. if (is_array($v)) {
  245. if (isset($v[3]) && $data) {
  246. $d = $v[3];
  247. parse_str($d, $t);
  248. $state = true;
  249. foreach ($t as $k1 => $v1) {
  250. if (!isset($data[$k1]) || (isset($data[$k1]) && $data[$k1] != $v1)) {
  251. $state = false;
  252. }
  253. }
  254. if (!$state) {
  255. continue;
  256. }
  257. }
  258. if (isset($v[2])) {
  259. $i = $v[2];
  260. }
  261. if (isset($v[1])) {
  262. $p = $v[1];
  263. }
  264. $v = $v[0];
  265. }
  266. if (strstr($v, 'add')) {
  267. $icon = 'Plus';
  268. $button = 'primary';
  269. } elseif (strstr($v, 'edit')) {
  270. $icon = 'Edit';
  271. $button = 'primary';
  272. } elseif (strstr($v, 'view')) {
  273. $icon = 'View';
  274. $button = '';
  275. } elseif ($v == 'delete') {
  276. if ($key == 'button') {
  277. if (isset($this->config['layout'])) {
  278. continue;
  279. }
  280. $this->config['selection'] = true;
  281. }
  282. $icon = 'Delete';
  283. $button = 'danger';
  284. } elseif ($v == 'recycle') {
  285. if ($key == 'button') {
  286. if (isset($this->config['layout'])) {
  287. continue;
  288. }
  289. $this->config['selection'] = true;
  290. }
  291. $icon = 'Delete';
  292. $button = 'danger';
  293. } elseif ($v == 'oper') {
  294. if ($key == 'button') {
  295. if (isset($this->config['layout'])) {
  296. continue;
  297. }
  298. $this->config['selection'] = true;
  299. }
  300. $icon = 'Notification';
  301. $button = 'warning';
  302. } elseif ($v == 'api') {
  303. if ($key == 'button') {
  304. if (isset($this->config['layout'])) {
  305. continue;
  306. }
  307. $this->config['selection'] = true;
  308. }
  309. $p = Dever::url($p);
  310. $icon = 'Notification';
  311. $button = 'warning';
  312. } elseif ($v == 'link') {
  313. $icon = 'Link';
  314. $button = 'success';
  315. } elseif ($v == 'route') {
  316. $icon = 'Link';
  317. $button = 'success';
  318. } elseif ($v == 'recover') {
  319. $icon = 'CirclePlus';
  320. $button = 'info';
  321. } else {
  322. continue;
  323. }
  324. # 权限验证
  325. $sort++;
  326. $func = $this->getFunc($v, $k, $sort, $p);
  327. if ($this->menu && $this->menu['show'] == 1 && !$func) {
  328. continue;
  329. }
  330. if ($i) {
  331. $icon = $i;
  332. }
  333. $result[] = array
  334. (
  335. 'name' => $k,
  336. 'type' => $v,
  337. 'param' => $p,
  338. 'icon' => $icon,
  339. 'button' => $button,
  340. 'func' => $func,
  341. );
  342. if (!$this->recycler && $v == 'recycle') {
  343. $this->recycler = true;
  344. }
  345. }
  346. return $result;
  347. }
  348. }