Page.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. <?php namespace Manage\Lib;
  2. use Dever;
  3. # 通用页面 项目着急上线 以后优化并封装
  4. class Page extends Auth
  5. {
  6. protected $db;
  7. protected $key;
  8. protected $id = 0;
  9. protected $input = false;
  10. protected $recycler = false;
  11. protected $menu = [];
  12. protected $config = [];
  13. protected $field = [];
  14. public $info = [];
  15. public function __construct($key = '', $load = '', $input = true, $config = [])
  16. {
  17. parent::__construct();
  18. $this->key = $key;
  19. $this->input = $input;
  20. $this->id = $this->input('id', 0);
  21. if (!$load) {
  22. $load = Dever::input('load');
  23. }
  24. list($this->db, $this->menu) = Dever::load('common', 'manage')->db($load);
  25. if ($this->menu && $this->menu['show'] == 1) {
  26. $this->checkMenu($this->menu['id'], false);
  27. }
  28. $this->config = $this->db->config['manage'][$key] ?? $this->db->config['manage'];
  29. if ($config) {
  30. $this->config = array_merge($this->config, $config);
  31. }
  32. if ($this->id && !strstr($this->id, ',')) {
  33. $this->info = $this->db->find($this->id);
  34. }
  35. }
  36. public function setting($key, &$data, $struct = true, $type = 'show', $disable = false)
  37. {
  38. if (empty($this->config[$key]) && $struct && isset($this->db->config['struct']) && $this->db->config['struct']) {
  39. $this->config[$key] = $this->db->config['struct'];
  40. }
  41. if (empty($this->config[$key])) {
  42. return;
  43. }
  44. $setting = $this->config[$key];
  45. if (is_string($setting)) {
  46. $setting = explode(',', $setting);
  47. }
  48. $field = $this->input('field', '');
  49. if ($field && is_string($field) && strstr($field, 'dever_')) {
  50. $field = '';
  51. }
  52. return $this->setData($key, $setting, $data, $field, $type, $disable);
  53. }
  54. # 获取某个数据的具体展示值
  55. public function getValue($key, $value, $data, $field = [])
  56. {
  57. if ($key == 'cdate') {
  58. $this->db->config['manage']['update']['field'][$key]['type'] = 'date';
  59. }
  60. $update = $this->db->config['manage']['update']['field'] ?? [];
  61. if ($show = Dever::issets($field, 'show')) {
  62. $value = $this->getShow($show, $data);
  63. } elseif ($value && isset($this->db->config['struct'][$key]['value']) && $this->db->config['struct'][$key]['value']) {
  64. $value = $this->db->value($key, $value);
  65. } elseif ($value && (isset($update[$key]) && isset($update[$key]['type']) && $update[$key]['type'] == 'date')) {
  66. if (isset($update[$key]['date_type']) && $update[$key]['date_type'] == 'year') {
  67. if ($update[$key]['date_type'] == 'year') {
  68. $value = date('Y', $value);
  69. } elseif ($update[$key]['date_type'] == 'month') {
  70. $value = date('Ym', $value);
  71. } else {
  72. $value = date('Ymd', $value);
  73. }
  74. } else {
  75. if (strstr($value, 'T')) {
  76. $value = date('Y-m-d H:i:s', strtotime($value));
  77. } elseif (is_numeric($value)) {
  78. $value = date('Y-m-d H:i:s', $value);
  79. } else {
  80. $value = '-';
  81. }
  82. }
  83. }
  84. return $value;
  85. }
  86. # 获取关联数据
  87. public function getOther($key, $set, $data)
  88. {
  89. $where = $config = [];
  90. if (isset($set['where'])) {
  91. foreach ($set['where'] as $k => $v) {
  92. if (!is_array($v) && isset($data[$v])) {
  93. $where[$k] = $data[$v];
  94. } else {
  95. $where[$k] = $v;
  96. }
  97. }
  98. }
  99. if (isset($set['col'])) {
  100. $config['col'] = $set['col'];
  101. }
  102. if ($where) {
  103. return Dever::db($key)->select($where, $config);
  104. }
  105. return [];
  106. }
  107. public function getShow($show, $data, $state = false)
  108. {
  109. return \Dever\Helper\Str::val($show, $data, $state);
  110. }
  111. # 获取菜单标题
  112. public function getTitle()
  113. {
  114. return $this->menu['name'];
  115. }
  116. # 获取左侧分栏
  117. protected function column(&$data, $name = '左侧分栏')
  118. {
  119. $data['column'] = false;
  120. if (isset($this->config['column'])) {
  121. if (empty($this->config['column']['hidden'])) {
  122. $data['column'] = $this->config['column'];
  123. if (isset($this->config['column']['add'])) {
  124. $data['column']['add'] = array('name' => $this->config['column']['add'], 'func' => $this->getFunc('column_add', $name . '-' . $this->config['column']['add'], 101));
  125. }
  126. if (isset($this->config['column']['edit'])) {
  127. $data['column']['edit'] = array('name' => '编辑', 'func' => $this->getFunc('column_edit', $name . '-编辑', 102));
  128. }
  129. if (isset($this->config['column']['delete'])) {
  130. $data['column']['delete'] = array('name' => '删除', 'func' => $this->getFunc('column_delete', $name . '-删除', 103));
  131. }
  132. $data['column']['data'] = $this->config['column']['data'];
  133. if (is_string($data['column']['data'])) {
  134. $data['column']['data'] = Dever::call($data['column']['data']);
  135. }
  136. $data['height'] = '100%';
  137. }
  138. if (isset($this->config['column']['active']) && $this->config['column']['where'] == 'id') {
  139. return $this->config['column']['active'];
  140. }
  141. }
  142. }
  143. # 通用的规则验证 一般为更新数据时使用
  144. protected function checkRules($set, $data)
  145. {
  146. if ($set['rules']) {
  147. if (!is_array($set['rules'])) {
  148. $set['rules'] = array
  149. (
  150. [
  151. 'required' => true,
  152. 'trigger' => 'blur',
  153. 'message' => $set['name'] . '不能为空',
  154. ],
  155. );
  156. }
  157. foreach ($set['rules'] as $k => $v) {
  158. if (isset($v['required']) && $v['required'] && !$data && $data !== '0') {
  159. Dever::error($v['message']);
  160. }
  161. if ($data || $data === '0') {
  162. if (isset($v['pattern']) && $v['pattern'] && !preg_match('/' . $v['pattern'] . '/', $data)) {
  163. Dever::error($v['message']);
  164. }
  165. if (isset($v['type']) && $v['type']) {
  166. if ($v['type'] == 'number' && !is_numeric($data)) {
  167. Dever::error($v['message']);
  168. } elseif ($v['type'] == 'array' && !is_array($data)) {
  169. Dever::error($v['message']);
  170. } elseif ($v['type'] == 'integer' && !is_int($data)) {
  171. Dever::error($v['message']);
  172. } elseif ($v['type'] == 'float' && !is_float($data)) {
  173. Dever::error($v['message']);
  174. } elseif ($v['type'] == 'string' && !is_string($data)) {
  175. Dever::error($v['message']);
  176. } elseif ($v['type'] == 'boolean' && !is_bool($data)) {
  177. Dever::error($v['message']);
  178. } elseif ($v['type'] == 'url' && !filter_var($data, FILTER_VALIDATE_URL)) {
  179. Dever::error($v['message']);
  180. } elseif ($v['type'] == 'email' && !filter_var($data, FILTER_VALIDATE_EMAIL)) {
  181. Dever::error($v['message']);
  182. } elseif ($v['type'] == 'enum' && isset($v['enum']) && !in_array($data, $v['enum'])) {
  183. Dever::error($v['message']);
  184. }
  185. }
  186. if (isset($v['len']) && $v['len'] && strlen($data) > $v['len']) {
  187. Dever::error($v['message']);
  188. }
  189. if (isset($v['min']) && $v['min'] && strlen($data) < $v['min']) {
  190. Dever::error($v['message']);
  191. }
  192. if (isset($v['max']) && $v['max'] && strlen($data) > $v['max']) {
  193. Dever::error($v['message']);
  194. }
  195. }
  196. }
  197. }
  198. }
  199. private function setData($key, $setting, &$data, $field, $type, $disable)
  200. {
  201. $result = [];
  202. foreach ($setting as $k => $v) {
  203. $this->setDefault($key, $k, $v);
  204. if (!is_array($v)) {
  205. if (is_numeric($k)) {
  206. $k = $v;
  207. $v = $type;
  208. }
  209. if ($k == 'id') {
  210. $v = ['name' => 'ID', 'type' => $v];
  211. } elseif ($k == 'cdate') {
  212. $v = ['name' => '创建时间', 'type' => $v];
  213. } elseif(isset($this->db->config['struct'][$k])) {
  214. $v = ['name' => $this->db->config['struct'][$k]['name'], 'type' => $v];
  215. } else {
  216. $v = ['name' => $v];
  217. }
  218. } else {
  219. if (isset($v['only'])) {
  220. if ($v['only'] == 'edit' && !$this->id) {
  221. continue;
  222. } elseif ($v['only'] == 'add' && $this->id) {
  223. continue;
  224. }
  225. }
  226. }
  227. if ($field) {
  228. if (is_string($field) && (strstr($field, '{') || strstr($field, '%7B'))) {
  229. $field = htmlspecialchars_decode($field);
  230. $field = Dever::json_decode($field);
  231. }
  232. if (is_array($field)) {
  233. if (isset($field['param'])) {
  234. if (isset($field['param']['set'])) {
  235. $field = array_merge($field, $field['param']['set']);
  236. }
  237. if (isset($field['param']['search'])) {
  238. $field = array_merge($field, $field['param']['search']);
  239. }
  240. } elseif (isset($field['field']) && !Dever::check($field['field'], $k)) {
  241. continue;
  242. }
  243. if (isset($field[$k]) && $field[$k] != $k) {
  244. $v['default'] = $field[$k];
  245. $v['type'] = 'hidden';
  246. }
  247. } elseif (!Dever::check($field, $k)) {
  248. continue;
  249. }
  250. }
  251. $info = $this->setField($data, $k, $v, $field, $type, $disable);
  252. if ($info) {
  253. $result[] = $info;
  254. }
  255. }
  256. return $result;
  257. }
  258. private function setField(&$data, $key, $value, $field, $type = 'show', $disable = false)
  259. {
  260. if (empty($value['align'])) {
  261. $value['align'] = 'center';
  262. }
  263. $value['key'] = $key;
  264. $this->setName($value);
  265. # 对每个字段进行权限设置
  266. if (isset($value['func']) && $value['func']) {
  267. $func = $this->getFunc('field_' . $value['key'], '字段-' . $value['name'], 200);
  268. if (!$func) {
  269. return false;
  270. }
  271. }
  272. if (strpos($key, '/') && $this->key == 'update') {
  273. $this->setType($value, 'table');
  274. $this->setShow($value);
  275. if (strpos($key, '#')) {
  276. $key = str_replace('#', '', $key);
  277. }
  278. $sku = $value['type'] == 'sku' ? true : false;
  279. $value['value'] = $this->getOther($key, $value, $this->info);
  280. if (isset($value['default'])) {
  281. $value['value'] += $value['default'];
  282. }
  283. if (isset($value['field'])) {
  284. list($app, $table) = explode('/', $key);
  285. $set = Dever::project($app);
  286. $manage = @include($set['path'] . 'manage/'.$table.'.php');
  287. if (isset($manage['update']['field'][$value['field']])) {
  288. $value = array_merge($value, $manage['update']['field'][$value['field']]);
  289. }
  290. if ($value['value']) {
  291. $data[$key . '_id'] = [
  292. 'key' => $key . '_id',
  293. 'type' => 'hidden',
  294. 'value' => $value['value'][0]['id'],
  295. ];
  296. $value['value'] = $value['value'][0][$value['field']];
  297. }
  298. $this->setRules($value);
  299. $this->setForm($value);
  300. $data[] = $value;
  301. return $value['name'] ?? 'test';
  302. }
  303. $update = new \Manage\Api\Page\Update($key, false);
  304. $value['option'] = [];
  305. $value['content'] = $update->get($value['value'], $value['option'], $sku);
  306. $data[] = $value;
  307. return $value['name'];
  308. } else {
  309. $this->setType($value, $type);
  310. $this->setDisable($value, $disable);
  311. if ($this->key == 'update') {
  312. # 一般为更新页面需要的参数
  313. $this->setShow($value);
  314. $this->setRules($value);
  315. } elseif (isset($value['remote'])) {
  316. $value['remote'] = Dever::url($value['remote']);
  317. }
  318. if ($type == 'show') {
  319. if (!isset($value['truncate'])) {
  320. $value['truncate'] = false;
  321. }
  322. $in = ['switch', 'select', 'input'];
  323. if (in_array($value['type'], $in)) {
  324. $value['func'] = $this->getFunc('list_edit_' . $value['key'], '列表更新-' . $value['name'], 104);
  325. if (!$value['func']) {
  326. $value['type'] = 'show';
  327. if (isset($value['show'])) {
  328. unset($value['show']);
  329. }
  330. }
  331. }
  332. if (isset($value['child'])) {
  333. $child = [];
  334. $this->setData($value['child'], $child, $field, $type, $disable);
  335. $value['child'] = $child;
  336. } else {
  337. $this->field[$key] = $value;
  338. }
  339. }
  340. $this->setForm($value);
  341. $data[] = $value;
  342. return $value['name'] ?? 'test';
  343. }
  344. }
  345. private function setDefault($key, &$k, &$v)
  346. {
  347. if ($v == 'cdate') {
  348. $k = $v;
  349. $v = [
  350. 'name' => '创建时间',
  351. 'type' => 'date',
  352. 'date_type' => 'datetimerange',
  353. 'value_format' => 'YYYY-MM-DD HH:mm:ss',
  354. 'start_placeholder' => '开始日期',
  355. 'end_placeholder' => '结束日期',
  356. 'range_separator' => '至',
  357. 'truncate' => true,
  358. ];
  359. } elseif ($v == 'sort') {
  360. $k = $v;
  361. $v = [
  362. 'type' => 'input',
  363. 'tip' => '双击修改,正序排序',
  364. 'width' => '75px',
  365. ];
  366. } elseif ($key != 'search' && $v == 'status') {
  367. $k = $v;
  368. $v = [
  369. 'type' => 'switch',
  370. 'show' => '{status}',
  371. 'active_value' => 1,
  372. 'inactive_value' => 2,
  373. 'width' => '75px',
  374. ];
  375. }
  376. }
  377. private function setShow(&$value)
  378. {
  379. if ($value['type'] == 'hidden') {
  380. $value['show'] = false;
  381. } elseif (!isset($value['show'])) {
  382. $value['show'] = true;
  383. }
  384. }
  385. private function setName(&$value)
  386. {
  387. if (empty($value['name']) && isset($this->db->config['struct'][$value['key']])) {
  388. $value['name'] = $this->db->config['struct'][$value['key']]['name'];
  389. }
  390. if (empty($value['placeholder']) && isset($value['name'])) {
  391. $value['placeholder'] = $value['name'];
  392. }
  393. }
  394. private function setType(&$value, $type)
  395. {
  396. if (empty($value['type'])) {
  397. $value['type'] = $type;
  398. }
  399. if (strpos($value['type'], '(')) {
  400. $value['type'] = $type;
  401. }
  402. if (empty($value['width'])) {
  403. $value['width'] = 'auto';
  404. if ($this->key == 'update') {
  405. $fast = Dever::input('fast');
  406. $value['width'] = '50%';
  407. if ($fast == 1) {
  408. $value['width'] = '100%';
  409. }
  410. }
  411. }
  412. if (isset($value['upload']) && Dever::project('upload')) {
  413. $upload = $this->getUpload($value['key']);
  414. if (is_array($value['upload'])) {
  415. $upload += $value['upload'];
  416. } else {
  417. $upload += ['id' => $value['upload']];
  418. }
  419. if (empty($upload['id'])) {
  420. Dever::error('上传配置错误');
  421. }
  422. $value['config'] = Dever::load('save', 'upload')->get($upload['id']);
  423. $value['yun'] = false;
  424. if ($value['config']['method'] == 2) {
  425. $value['yun'] = true;
  426. }
  427. $value['url'] = Dever::url('upload/save.act', $upload, true);
  428. $upload['wh'] = $value['wh'] ?? '500*500';
  429. $value['set'] = Dever::url('image/manage.set', $upload);
  430. if (isset($value['multiple']) && $value['multiple']) {
  431. $value['limit'] = 10;
  432. } else {
  433. $value['limit'] = 1;
  434. }
  435. }
  436. if (isset($value['editorMenu'])) {
  437. $this->setEditorUpload($value, ['uploadImage', 'uploadVideo']);
  438. }
  439. if (isset($value['date_type']) && $value['date_type'] == 'datetimerange' && empty($value['default_time'])) {
  440. $value['default_time'] = array(\Dever\Helper\Date::mktime(date('Y-m-d 00:00:00'))*1000, \Dever\Helper\Date::mktime(date('Y-m-d 23:59:59'))*1000);
  441. }
  442. }
  443. private function getUpload($key)
  444. {
  445. $upload['cate_id'] = 1;
  446. $upload['group_key'] = $this->db->config['table'] . '-' . $key;
  447. $upload['group_name'] = $this->db->config['name'];
  448. $upload['user_token'] = Dever::load('common', 'manage')->getToken();
  449. $upload['user_table'] = $this->user['table'];
  450. $upload['user_id'] = $this->user['id'];
  451. return $upload;
  452. }
  453. private function setEditorUpload(&$value, $key)
  454. {
  455. $upload = $this->getUpload($value['key']);
  456. foreach ($key as $k) {
  457. if ($v = Dever::issets($value['editorMenu'], $k)) {
  458. if (!is_array($v)) {
  459. $v = ['upload' => $v];
  460. }
  461. $upload['id'] = $v['upload'];
  462. $v['server'] = Dever::url('upload/save.wangEditor', $upload, true);
  463. if (empty($v['fieldName'])) {
  464. $v['fieldName'] = 'file';
  465. }
  466. $value['editorMenu'][$k] = $v;
  467. }
  468. }
  469. }
  470. private function setDisable(&$value, $disable)
  471. {
  472. if (isset($value['disable'])) {
  473. $disable = $value['disable'];
  474. }
  475. $value['disable'] = $disable;
  476. }
  477. # 设置值
  478. private function setForm(&$value)
  479. {
  480. if (empty($value['value'])) {
  481. $value['value'] = Dever::input('search')[$value['key']] ?? '';
  482. }
  483. # (float) 转换一下是为了前端select使用,必须是数字类型
  484. # 这个比较特殊
  485. if ($value['type'] == 'select_text') {
  486. if ($value['value']) {
  487. if (is_string($value['value'])) {
  488. $value['value'] = explode('|', $v['value']);
  489. }
  490. $value['value'][0] = (float) $value['value'][0];
  491. } else {
  492. $value['value'] = [1,''];
  493. }
  494. } elseif ($value['type'] == 'date') {
  495. if (empty($value['shortcuts'])) {
  496. $value['shortcuts'] = Dever::load('data', 'manage')->getShortcuts($value['date_type']);
  497. }
  498. } elseif (is_array($value['value'])) {
  499. foreach ($value['value'] as &$v) {
  500. $v = (float) $v;
  501. }
  502. }
  503. if (!$value['value']) {
  504. if (isset($value['default']) && !strstr($value['default'], '{')) {
  505. $value['value'] = $value['default'];
  506. } elseif ($this->key == 'update' && isset($this->db->config['struct'][$value['key']]['default'])) {
  507. $value['value'] = $this->db->config['struct'][$value['key']]['default'];
  508. }
  509. }
  510. if (isset($value['option']) && $value['option']) {
  511. $this->db->config['option'][$value['key']] = $value['option'];
  512. }
  513. if (isset($this->db->config['load'])) {
  514. $send = $this->info;
  515. $send['table'] = $this->db->config['load'];
  516. if ($option = $this->db->value($value['key'], false, 'id,name', $send)) {
  517. if ($value['type'] == 'checkbox') {
  518. $value['value'] = $value['value'] ? explode(',', $value['value']) : [];
  519. }
  520. $value['option'] = $option;
  521. if ($value['type'] == 'text') {
  522. $value['type'] = 'select';
  523. }
  524. if (!is_array($value['value']) && $value['value']) {
  525. $value['value'] = (float) $value['value'];
  526. }
  527. }
  528. }
  529. }
  530. private function setRules(&$value)
  531. {
  532. if (isset($value['rules']) && $value['rules']) {
  533. if (!is_array($value['rules'])) {
  534. $value['rules'] = array
  535. (
  536. [
  537. 'required' => true,
  538. 'trigger' => 'blur',
  539. 'message' => $value['name'] . '不能为空',
  540. ],
  541. );
  542. }
  543. foreach ($value['rules'] as $k => $v) {
  544. if (isset($v['only'])) {
  545. if ($v['only'] == 'edit' && !$this->id) {
  546. unset($value['rules'][$k]);
  547. break;
  548. } elseif ($v['only'] == 'add' && $this->id) {
  549. unset($value['rules'][$k]);
  550. break;
  551. }
  552. }
  553. }
  554. if (!isset($value['rules'][0])) {
  555. $value['rules'] = array_values($value['rules']);
  556. }
  557. }
  558. }
  559. protected function input($key, $value = '')
  560. {
  561. return $this->input ? Dever::input($key) : $value;
  562. }
  563. public function button($key = 'button', $data = [])
  564. {
  565. $result = [];
  566. if (!isset($this->config[$key])) {
  567. $num = 0;
  568. if (isset($this->db->config['manage']['update']['field'])) {
  569. $num = count($this->db->config['manage']['update']['field']);
  570. } elseif (isset($this->db->config['struct']) && $this->db->config['struct']) {
  571. $num = count($this->db->config['struct']);
  572. }
  573. $fast = 'fast';
  574. if ($num > 8) {
  575. $fast = '';
  576. }
  577. if ($key == 'button') {
  578. $this->config[$key] = ['新增' => $fast . 'add'];
  579. } else {
  580. $this->config[$key] = ['编辑' => $fast . 'edit', '删除' => 'recycle'];
  581. }
  582. }
  583. $sort = 1;
  584. foreach ($this->config[$key] as $k => $v) {
  585. $d = '';
  586. $p = '';
  587. $i = '';
  588. if (is_array($v)) {
  589. if (isset($v[3]) && $data) {
  590. $d = $v[3];
  591. if (strstr($d, '{')) {
  592. $state = $this->getShow($d, $data, true);
  593. } else {
  594. parse_str($d, $t);
  595. $state = false;
  596. foreach ($t as $k1 => $v1) {
  597. if (isset($data[$k1])) {
  598. $v1 = explode(',', $v1);
  599. foreach ($v1 as $v2) {
  600. if ($data[$k1] == $v2) {
  601. $state = true;
  602. }
  603. }
  604. }
  605. }
  606. }
  607. if (!$state) {
  608. continue;
  609. }
  610. }
  611. if (isset($v[2])) {
  612. $i = $v[2];
  613. }
  614. if (isset($v[1])) {
  615. $p = $v[1];
  616. }
  617. $v = $v[0];
  618. }
  619. if (strstr($v, 'add')) {
  620. $icon = 'Plus';
  621. $button = 'primary';
  622. } elseif (strstr($v, 'edit')) {
  623. $icon = 'Edit';
  624. $button = 'primary';
  625. } elseif (strstr($v, 'view')) {
  626. $icon = 'View';
  627. $button = '';
  628. } elseif (strstr($v, 'drawer')) {
  629. $icon = 'Drawer';
  630. $button = '';
  631. } elseif ($v == 'delete') {
  632. if ($key == 'button') {
  633. if (isset($this->config['layout'])) {
  634. continue;
  635. }
  636. $this->config['selection'] = true;
  637. }
  638. $icon = 'Delete';
  639. $button = 'danger';
  640. } elseif ($v == 'recycle') {
  641. if ($key == 'button') {
  642. if (isset($this->config['layout'])) {
  643. continue;
  644. }
  645. $this->config['selection'] = true;
  646. }
  647. $icon = 'Delete';
  648. $button = 'danger';
  649. } elseif ($v == 'oper') {
  650. if ($key == 'button') {
  651. if (isset($this->config['layout'])) {
  652. continue;
  653. }
  654. $this->config['selection'] = true;
  655. }
  656. $icon = 'Notification';
  657. $button = 'warning';
  658. } elseif ($v == 'api') {
  659. if ($key == 'button') {
  660. if (isset($this->config['layout'])) {
  661. continue;
  662. }
  663. $this->config['selection'] = true;
  664. }
  665. $p = Dever::url($p);
  666. $icon = 'Notification';
  667. $button = 'warning';
  668. } elseif ($v == 'link') {
  669. $icon = 'Link';
  670. $button = 'success';
  671. $p = $this->getShow($p, $data);
  672. } elseif ($v == 'route') {
  673. $icon = 'Link';
  674. $button = 'success';
  675. } elseif ($v == 'recover') {
  676. $icon = 'CirclePlus';
  677. $button = 'info';
  678. } else {
  679. continue;
  680. }
  681. # 权限验证
  682. $sort++;
  683. $func = $this->getFunc($v, $k, $sort, $p);
  684. if ($this->menu && $this->menu['show'] == 1 && !$func) {
  685. continue;
  686. }
  687. if ($i) {
  688. $icon = $i;
  689. }
  690. $result[] = [
  691. 'name' => $k,
  692. 'type' => $v,
  693. 'param' => $p,
  694. 'icon' => $icon,
  695. 'button' => $button,
  696. 'func' => $func,
  697. ];
  698. if (!$this->recycler && $v == 'recycle') {
  699. $this->recycler = true;
  700. }
  701. }
  702. return $result;
  703. }
  704. # 构造搜索
  705. protected function search(&$where)
  706. {
  707. $search = Dever::input('search');
  708. $set = Dever::input('set');
  709. $list_search = $result = [];
  710. $result['form'] = $result['field'] = $result['option'] = [];
  711. $this->setting('search', $list_search, false, 'text');
  712. if ($list_search) {
  713. foreach ($list_search as $v) {
  714. if ($v['type'] != 'hidden') {
  715. $result['form'][] = $v;
  716. if (is_numeric($v['value'])) {
  717. $v['value'] = (float) $v['value'];
  718. }
  719. $result['field'][$v['key']] = $v['value'];
  720. if ($v['type'] == 'sku') {
  721. $result['field'][$v['key'] . '_spec'] = [];
  722. }
  723. if (isset($v['option'])) {
  724. $result['option'][$v['key']] = $v['option'];
  725. }
  726. }
  727. $this->searchWhere($search, $v, $where);
  728. $this->searchWhere($set, $v, $where);
  729. }
  730. }
  731. return $result;
  732. }
  733. protected function searchWhere($value, $v, &$where)
  734. {
  735. if ($value) {
  736. if ($value = Dever::issets($value, $v['key'])) {
  737. if (isset($v['search'])) {
  738. if (is_callable($v['search'])) {
  739. $value = $v['search']($v['key'], $v['type'], $value);
  740. } elseif (isset($v['search']['table'])) {
  741. $v['search']['where'] = Dever::json_decode(str_replace('{value}', $value, Dever::json_encode($v['search']['where'])));
  742. $search = Dever::db($v['search']['table'])->select($v['search']['where'], $v['search']['set'] ?? []);
  743. $value = [];
  744. if ($search) {
  745. foreach ($search as $v1) {
  746. $value[] = $v1[$v['search']['field']];
  747. }
  748. }
  749. $value = implode(',', $value);
  750. $v['type'] = 'in';
  751. if (isset($v['search']['key'])) {
  752. $v['key'] = $v['search']['key'];
  753. }
  754. }
  755. }
  756. if ($v['type'] == 'select_text') {
  757. if ($value[1] === '') {
  758. return;
  759. }
  760. $result = current(array_filter($v['option'], function($item) use($value) {
  761. return $item['id'] == $value[0];
  762. }));
  763. if (!$result) {
  764. return;
  765. }
  766. $v['key'] = $result['value'];
  767. $v['type'] = '';
  768. if (strstr($v['key'], '.')) {
  769. $r = Dever::call($v['key'], $value[1]);
  770. if ($r) {
  771. $v['key'] = $r[0];
  772. $v['type'] = $r[1];
  773. $value = $r[2];
  774. }
  775. } else {
  776. if (strstr($v['key'], ' ')) {
  777. $temp = explode(' ', $v['key']);
  778. $v['key'] = $temp[0];
  779. $v['type'] = $temp[1];
  780. }
  781. $value = $value[1];
  782. }
  783. }
  784. if ($v['type'] == 'group') {
  785. $where[$v['key']] = ['group', $value];
  786. } elseif ($v['type'] == 'selects') {
  787. $where[$v['key']] = ['group', $value];
  788. } elseif ($v['type'] == 'cascader') {
  789. $t = $value;
  790. if (is_array($value)) {
  791. $t = implode(',', $value);
  792. }
  793. $where[$v['key']] = ['like', $t];
  794. } elseif ($v['type'] == 'like') {
  795. $where[$v['key']] = ['like', $value];
  796. } elseif ($v['type'] == 'in') {
  797. $where[$v['key']] = ['in', $value];
  798. } elseif ($v['type'] == 'date') {
  799. if (strstr($v['date_type'], 'range')) {
  800. $where[$v['key']] = array('>=', \Dever\Helper\Date::mktime($value[0]));
  801. $where[$v['key'] . '#'] = array('<=', \Dever\Helper\Date::mktime($value[1]));
  802. } else {
  803. $where[$v['key']] = $value;
  804. }
  805. } else {
  806. $where[$v['key']] = $value;
  807. }
  808. }
  809. }
  810. }
  811. }