Data.php 12 KB

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