Page.php 35 KB

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