Data.php 13 KB

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