Page.php 35 KB

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