Data.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php namespace Manage\Api\Page;
  2. use Dever;
  3. use Manage\Lib\Page;
  4. # 数据获取
  5. class Data extends Page
  6. {
  7. private $expand = false;
  8. public function __construct($load = '')
  9. {
  10. parent::__construct('list', $load);
  11. }
  12. public function list()
  13. {
  14. if ($this->menu && $this->menu['show'] == 1 && !$this->getFunc('list', '列表', 1)) {
  15. Dever::error('无访问权限');
  16. }
  17. $data['title'] = '';
  18. $data['button'] = $this->button();
  19. $data['recycler'] = $this->recycler;
  20. $data = array_merge($data, $this->out());
  21. $data['total'] = Dever::page('total');
  22. $data['height'] = $this->config['height'] ?? '100%';
  23. $data['type'] = $this->config['type'] ?? 'table';
  24. $data['desc'] = $this->config['desc'] ?? '';
  25. $data['layout'] = $this->config['layout'] ?? array();
  26. $data['exportButton'] = $this->export();
  27. $data['show'] = array
  28. (
  29. 'selection' => $this->config['selection'] ?? false,
  30. 'expand' => $this->expand,
  31. 'index' => $this->config['index'] ?? false,
  32. );
  33. $this->column($data);
  34. return $data;
  35. }
  36. public function out()
  37. {
  38. $where = $this->config['where'] ?? array();
  39. $set = $this->config['set'] ?? array();
  40. $data['field'] = $data['head'] = array();
  41. $data['search'] = $this->search($where);
  42. $ids = Dever::input('ids');
  43. if ($ids) {
  44. $where['id'] = array('in', $ids);
  45. }
  46. $set['num'] = Dever::input('pgnum', '', '', 10);
  47. $order_col = Dever::input('order_col');
  48. if ($order_col) {
  49. $order_value = Dever::input('order_value');
  50. if ($order_value) {
  51. $set['order'] = $order_col . ' ' . $order_value . ', id desc';
  52. }
  53. }
  54. if (isset($this->config['data'])) {
  55. $result = Dever::call($this->config['data'], array($where, $set));
  56. $data = array_merge($data, $result);
  57. } else {
  58. $data['field'] = $this->setting('field', $data['head'], true, 'show');
  59. $data['body'] = $this->data($where, $set);
  60. }
  61. $method = Dever::input('method');
  62. if ($method && strstr($method, '.')) {
  63. $result = Dever::call($method, array($data));
  64. unset($data);
  65. $data['field'] = $result['head'];
  66. $data['body'] = $result['body'];
  67. }
  68. $data['stat'] = array();
  69. if (isset($this->config['stat'])) {
  70. $data['stat'] = Dever::call($this->config['stat'], array($where));
  71. }
  72. return $data;
  73. }
  74. private function data($where, $set = array())
  75. {
  76. if (isset($this->config['tree'])) {
  77. return $this->db->tree($where, $this->config['tree'], array($this, 'handleData'));
  78. }
  79. $data = $this->db->select($where, $set);
  80. $result = array();
  81. if ($data) {
  82. foreach ($data as $k => $v) {
  83. $result[$k] = $this->handleData($k, $v);
  84. }
  85. }
  86. return $result;
  87. }
  88. public function handleData($k, $v)
  89. {
  90. $result['index'] = $k+1*Dever::input('pg', '', '', 1);
  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, $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. } elseif (isset($value['show'])) {
  123. $result[$key] = $this->getShow($value['show'], $v);
  124. }
  125. if ($html == 2) {
  126. $result[$key] = strip_tags($result[$key]);
  127. }
  128. }
  129. if (isset($this->config['expand']) && $this->config['expand']) {
  130. $result['expand'] = Dever::call($this->config['expand'], array($v));
  131. $this->expand = true;
  132. }
  133. return $result;
  134. }
  135. private function export()
  136. {
  137. $result = false;
  138. if (isset($this->config['export']) && $this->config['export']) {
  139. $result = array();
  140. foreach ($this->config['export'] as $k => $v) {
  141. $func = $this->getFunc($k, $v, 300);
  142. if ($func) {
  143. $result[$k] = $v;
  144. }
  145. }
  146. }
  147. return $result;
  148. }
  149. private function search(&$where)
  150. {
  151. $search = Dever::input('search');
  152. $set = Dever::input('set');
  153. $list_search = $result = array();
  154. $result['form'] = $result['field'] = $result['option'] = array();
  155. $this->setting('search', $list_search, false, 'text');
  156. if ($list_search) {
  157. foreach ($list_search as $v) {
  158. if ($v['type'] != 'hidden1') {
  159. $result['form'][] = $v;
  160. if (is_numeric($v['value'])) {
  161. $v['value'] = (float) $v['value'];
  162. }
  163. $result['field'][$v['key']] = $v['value'];
  164. if ($v['type'] == 'sku') {
  165. $result['field'][$v['key'] . '_spec'] = [];
  166. }
  167. if (isset($v['option'])) {
  168. $result['option'][$v['key']] = $v['option'];
  169. }
  170. }
  171. $this->where($search, $v, $where);
  172. $this->where($set, $v, $where);
  173. }
  174. }
  175. return $result;
  176. }
  177. private function where($value, $v, &$where)
  178. {
  179. if ($value) {
  180. if ($value = Dever::issets($value, $v['key'])) {
  181. if (isset($v['search'])) {
  182. if (is_callable($v['search'])) {
  183. $value = $v['search']($v['key'], $v['type'], $value);
  184. } elseif (isset($v['search']['table'])) {
  185. $v['search']['where'] = Dever::json_decode(str_replace('{value}', $value, Dever::json_encode($v['search']['where'])));
  186. $search = Dever::db($v['search']['table'])->select($v['search']['where'], $v['search']['set'] ?? array());
  187. $value = array();
  188. if ($search) {
  189. foreach ($search as $v1) {
  190. $value[] = $v1[$v['search']['field']];
  191. }
  192. }
  193. $value = implode(',', $value);
  194. $v['type'] = 'in';
  195. if (isset($v['search']['key'])) {
  196. $v['key'] = $v['search']['key'];
  197. }
  198. }
  199. }
  200. if ($v['type'] == 'group') {
  201. $where[$v['key']] = array('group', $value);
  202. } elseif ($v['type'] == 'selects') {
  203. $where[$v['key']] = array('group', $value);
  204. } elseif ($v['type'] == 'cascader') {
  205. $t = $value;
  206. if (is_array($value)) {
  207. $t = implode(',', $value);
  208. }
  209. $where[$v['key']] = array('like', $t);
  210. } elseif ($v['type'] == 'like') {
  211. $where[$v['key']] = array('like', $value);
  212. } elseif ($v['type'] == 'in') {
  213. $where[$v['key']] = array('in', $value);
  214. } elseif ($v['type'] == 'date') {
  215. if (strstr($v['date_type'], 'range')) {
  216. $where[$v['key']] = array('>=', \Dever\Helper\Date::mktime($value[0]));
  217. $where[$v['key'] . '#'] = array('<=', \Dever\Helper\Date::mktime($value[1]));
  218. } else {
  219. $where[$v['key']] = $value;
  220. }
  221. } else {
  222. $where[$v['key']] = $value;
  223. }
  224. }
  225. }
  226. }
  227. }