Data.php 12 KB

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