Data.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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'] ?? [];
  26. $data['exportButton'] = $this->export();
  27. $data['show'] = [
  28. 'selection' => $this->config['selection'] ?? false,
  29. 'expand' => $this->expand,
  30. 'index' => $this->config['index'] ?? false,
  31. ];
  32. $this->column($data);
  33. return $data;
  34. }
  35. public function out()
  36. {
  37. $where = $this->config['where'] ?? [];
  38. $set = $this->config['set'] ?? [];
  39. $data['field'] = $data['head'] = [];
  40. $data['search'] = $this->search($where);
  41. $ids = Dever::input('ids');
  42. if ($ids) {
  43. $where['id'] = ['in', $ids];
  44. }
  45. $set['num'] = Dever::input('pgnum', '', '', 10);
  46. $order_col = Dever::input('order_col');
  47. if ($order_col) {
  48. $order_value = Dever::input('order_value');
  49. if ($order_value) {
  50. $set['order'] = $order_col . ' ' . $order_value . ', id desc';
  51. }
  52. }
  53. if (isset($this->config['data'])) {
  54. $result = Dever::call($this->config['data'], [$where, $set]);
  55. $data = array_merge($data, $result);
  56. } else {
  57. $data['field'] = $this->setting('field', $data['head'], true, 'show');
  58. $data['body'] = $this->data($where, $set);
  59. }
  60. $method = Dever::input('method');
  61. if ($method && strstr($method, '.')) {
  62. $result = Dever::call($method, [$data]);
  63. unset($data);
  64. $data['field'] = $result['head'];
  65. $data['body'] = $result['body'];
  66. }
  67. $data['stat'] = [];
  68. if (isset($this->config['stat'])) {
  69. $data['stat'] = Dever::call($this->config['stat'], [$where]);
  70. }
  71. return $data;
  72. }
  73. private function data($where, $set = [])
  74. {
  75. if (isset($this->config['tree'])) {
  76. return $this->db->tree($where, $this->config['tree'], [$this, 'handleData']);
  77. }
  78. $data = $this->db->select($where, $set);
  79. $result = [];
  80. if ($data) {
  81. foreach ($data as $k => $v) {
  82. $result[$k] = $this->handleData($k, $v);
  83. }
  84. }
  85. return $result;
  86. }
  87. public function handleData($k, $v)
  88. {
  89. $result['index'] = $k+1*Dever::input('pg', '', '', 1);
  90. $button = $this->button('data_button', $v);
  91. if ($button) {
  92. $result['button'] = $button;
  93. }
  94. # 是否保留html代码,1是保留,2是不保留
  95. $html = Dever::input('html', '', '', 1);
  96. if (isset($v['_id'])) {
  97. $result['id'] = $v['_id'];
  98. } elseif (isset($v['id'])) {
  99. $result['id'] = $v['id'];
  100. }
  101. $result['cdate'] = $v['cdate'];
  102. foreach ($this->field as $value) {
  103. $key = $value['key'];
  104. if (isset($v[$key])) {
  105. $result[$key] = $this->getValue($key, $v[$key], $v, $value);
  106. } elseif (strpos($key, '/')) {
  107. $other = $this->getOther($key, $value, $v);
  108. if ($other) {
  109. $otherName = [];
  110. foreach ($other as $k1 => $v1) {
  111. if (isset($v1['name'])) {
  112. $otherName[] = $v1['name'];
  113. }
  114. }
  115. if ($otherName) {
  116. $result[$key] = implode('、', $otherName);
  117. } else {
  118. $result[$key] = $other;
  119. }
  120. }
  121. } elseif (isset($value['show'])) {
  122. $result[$key] = $this->getShow($value['show'], $v);
  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'], [$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 = [];
  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 = [];
  153. $result['form'] = $result['field'] = $result['option'] = [];
  154. $this->setting('search', $list_search, false, 'text');
  155. if ($list_search) {
  156. foreach ($list_search as $v) {
  157. if ($v['type'] != 'hidden1') {
  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::issets($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'] ?? []);
  186. $value = [];
  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']] = ['group', $value];
  201. } elseif ($v['type'] == 'selects') {
  202. $where[$v['key']] = ['group', $value];
  203. } elseif ($v['type'] == 'cascader') {
  204. $t = $value;
  205. if (is_array($value)) {
  206. $t = implode(',', $value);
  207. }
  208. $where[$v['key']] = ['like', $t];
  209. } elseif ($v['type'] == 'like') {
  210. $where[$v['key']] = ['like', $value];
  211. } elseif ($v['type'] == 'in') {
  212. $where[$v['key']] = ['in', $value];
  213. } elseif ($v['type'] == 'date') {
  214. if (strstr($v['date_type'], 'range')) {
  215. $where[$v['key']] = array('>=', \Dever\Helper\Date::mktime($value[0]));
  216. $where[$v['key'] . '#'] = array('<=', \Dever\Helper\Date::mktime($value[1]));
  217. } else {
  218. $where[$v['key']] = $value;
  219. }
  220. } else {
  221. $where[$v['key']] = $value;
  222. }
  223. }
  224. }
  225. }
  226. }