Page.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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']) && !strstr($value['remote'], 'Dever')) {
  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. if (empty($value['width'])) {
  342. $value['width'] = 'auto';
  343. if ($this->key == 'update') {
  344. $fast = Dever::input('fast');
  345. $value['width'] = '50%';
  346. if (!$this->input || (empty($value['option']) && $fast == 1) || $value['type'] == 'editor') {
  347. $value['width'] = '100%';
  348. }
  349. }
  350. }
  351. $data[] = $value;
  352. return $value['name'] ?? 'test';
  353. }
  354. }
  355. private function setDefault($key, &$k, &$v)
  356. {
  357. if ($v == 'cdate') {
  358. $k = $v;
  359. $v = [
  360. 'name' => '创建时间',
  361. 'type' => 'date',
  362. 'date_type' => 'datetimerange',
  363. 'value_format' => 'YYYY-MM-DD HH:mm:ss',
  364. 'start_placeholder' => '开始日期',
  365. 'end_placeholder' => '结束日期',
  366. 'range_separator' => '至',
  367. 'truncate' => true,
  368. ];
  369. } elseif ($v == 'sort') {
  370. $k = $v;
  371. $v = [
  372. 'type' => 'input',
  373. 'tip' => '双击修改,正序排序',
  374. 'width' => '90px',
  375. ];
  376. } elseif ($key != 'search' && $v == 'status') {
  377. if (isset($this->db->config['struct']['status']['value']) && count($this->db->config['struct']['status']['value']) == 2) {
  378. $k = $v;
  379. $v = [
  380. 'type' => 'switch',
  381. 'show' => '{status}',
  382. 'active_value' => 1,
  383. 'inactive_value' => 2,
  384. 'width' => '75px',
  385. ];
  386. }
  387. }
  388. }
  389. private function setShow(&$value)
  390. {
  391. if ($value['type'] == 'hidden') {
  392. $value['show'] = false;
  393. } elseif (!isset($value['show'])) {
  394. $value['show'] = true;
  395. }
  396. }
  397. private function setName(&$value)
  398. {
  399. if (empty($value['name']) && isset($this->db->config['struct'][$value['key']])) {
  400. $value['name'] = $this->db->config['struct'][$value['key']]['name'];
  401. }
  402. if (empty($value['placeholder']) && isset($value['name'])) {
  403. $value['placeholder'] = $value['name'];
  404. }
  405. }
  406. private function setType(&$value, $type)
  407. {
  408. if (empty($value['type'])) {
  409. $value['type'] = $type;
  410. }
  411. if (strpos($value['type'], '(')) {
  412. $value['type'] = $type;
  413. }
  414. if (isset($value['upload']) && Dever::project('upload')) {
  415. $upload = $this->getUpload($value['key']);
  416. if (is_array($value['upload'])) {
  417. $upload += $value['upload'];
  418. } else {
  419. $upload += ['id' => $value['upload']];
  420. }
  421. if (empty($upload['id'])) {
  422. Dever::error('上传配置错误');
  423. }
  424. $value['config'] = Dever::load('save', 'upload')->get($upload['id']);
  425. $value['yun'] = false;
  426. if ($value['config']['method'] == 2) {
  427. $value['yun'] = true;
  428. }
  429. $value['url'] = Dever::url('upload/save.act', $upload, true);
  430. $upload['wh'] = $value['wh'] ?? '500*500';
  431. $value['set'] = Dever::url('image/manage.set', $upload, true);
  432. if (isset($value['multiple']) && $value['multiple']) {
  433. $value['limit'] = 10;
  434. } else {
  435. $value['limit'] = 1;
  436. }
  437. }
  438. if (isset($value['editorMenu'])) {
  439. $this->setEditorUpload($value, ['uploadImage', 'uploadVideo']);
  440. }
  441. if (isset($value['date_type']) && $value['date_type'] == 'datetimerange' && empty($value['default_time'])) {
  442. $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);
  443. }
  444. }
  445. protected function getUpload($key)
  446. {
  447. $upload['cate_id'] = 1;
  448. $upload['group_key'] = $this->db->config['table'] . '-' . $key;
  449. $upload['group_name'] = $this->db->config['name'];
  450. $upload['user_token'] = Dever::load('common', 'manage')->getToken();
  451. $upload['user_table'] = $this->user['table'];
  452. $upload['user_id'] = $this->user['id'];
  453. return $upload;
  454. }
  455. protected function setEditorUpload(&$value, $key)
  456. {
  457. $upload = $this->getUpload($value['key']);
  458. foreach ($key as $k) {
  459. if ($v = Dever::issets($value['editorMenu'], $k)) {
  460. if (!is_array($v)) {
  461. $v = ['upload' => $v];
  462. }
  463. $upload['id'] = $v['upload'];
  464. $v['server'] = Dever::url('upload/save.wangEditor', $upload, true);
  465. if (empty($v['fieldName'])) {
  466. $v['fieldName'] = 'file';
  467. }
  468. $value['editorMenu'][$k] = $v;
  469. }
  470. }
  471. }
  472. private function setDisable(&$value, $disable)
  473. {
  474. if (isset($value['disable'])) {
  475. $disable = $value['disable'];
  476. }
  477. $value['disable'] = $disable;
  478. }
  479. # 设置值
  480. private function setForm(&$value)
  481. {
  482. if (empty($value['value'])) {
  483. $value['value'] = Dever::input('search')[$value['key']] ?? '';
  484. }
  485. # (float) 转换一下是为了前端select使用,必须是数字类型
  486. # 这个比较特殊
  487. if ($value['type'] == 'select_text') {
  488. if ($value['value']) {
  489. if (is_string($value['value'])) {
  490. $value['value'] = explode('|', $v['value']);
  491. }
  492. $value['value'][0] = (float) $value['value'][0];
  493. } else {
  494. $value['value'] = [1,''];
  495. }
  496. } elseif ($value['type'] == 'date') {
  497. if (empty($value['shortcuts']) && isset($value['date_type'])) {
  498. $value['shortcuts'] = Dever::load('data', 'manage')->getShortcuts($value['date_type']);
  499. }
  500. } elseif (is_array($value['value'])) {
  501. foreach ($value['value'] as &$v) {
  502. $v = (float) $v;
  503. }
  504. }
  505. if (!$value['value']) {
  506. if (isset($value['default']) && !strstr($value['default'], '{')) {
  507. $value['value'] = $value['default'];
  508. } elseif ($this->key == 'update' && isset($this->db->config['struct'][$value['key']]['default'])) {
  509. $value['value'] = $this->db->config['struct'][$value['key']]['default'];
  510. }
  511. }
  512. if (isset($value['option']) && $value['option']) {
  513. $this->db->config['option'][$value['key']] = $value['option'];
  514. }
  515. if (isset($this->db->config['load'])) {
  516. $send = $this->info;
  517. $send['table'] = $this->db->config['load'];
  518. if ($option = $this->db->value($value['key'], false, 'id,name', $send)) {
  519. if ($value['type'] == 'checkbox') {
  520. $value['value'] = $value['value'] ? explode(',', $value['value']) : [];
  521. }
  522. $value['option'] = $option;
  523. if ($value['type'] == 'text') {
  524. $value['type'] = 'select';
  525. }
  526. if (isset($this->db->config['struct'][$value['key']]['type']) && !is_array($value['value']) && $value['value'] && !strstr($this->db->config['struct'][$value['key']]['type'], 'char')) {
  527. $value['value'] = (float) $value['value'];
  528. }
  529. }
  530. }
  531. }
  532. private function setRules(&$value)
  533. {
  534. if (isset($value['rules']) && $value['rules']) {
  535. if (!is_array($value['rules'])) {
  536. $value['rules'] = array
  537. (
  538. [
  539. 'required' => true,
  540. 'trigger' => 'blur',
  541. 'message' => $value['name'] . '不能为空',
  542. ],
  543. );
  544. }
  545. foreach ($value['rules'] as $k => $v) {
  546. if (isset($v['only'])) {
  547. if ($v['only'] == 'edit' && !$this->id) {
  548. unset($value['rules'][$k]);
  549. break;
  550. } elseif ($v['only'] == 'add' && $this->id) {
  551. unset($value['rules'][$k]);
  552. break;
  553. }
  554. }
  555. }
  556. if (!isset($value['rules'][0])) {
  557. $value['rules'] = array_values($value['rules']);
  558. }
  559. }
  560. }
  561. protected function input($key, $value = '')
  562. {
  563. return $this->input ? Dever::input($key) : $value;
  564. }
  565. public function button($key = 'button', $data = [], $default = true)
  566. {
  567. if (empty($this->config[$key])) {
  568. return [];
  569. }
  570. $result = [];
  571. /*
  572. if (!isset($this->config[$key])) {
  573. $num = 0;
  574. if (isset($this->db->config['manage']['update']['field'])) {
  575. $num = count($this->db->config['manage']['update']['field']);
  576. } elseif (isset($this->db->config['struct']) && $this->db->config['struct']) {
  577. $num = count($this->db->config['struct']);
  578. }
  579. $fast = 'fast';
  580. if ($num > 8) {
  581. $fast = '';
  582. }
  583. if ($key == 'button') {
  584. $this->config[$key] = ['新增' => $fast . 'add'];
  585. } else {
  586. $this->config[$key] = ['编辑' => $fast . 'edit', '删除' => 'recycle'];
  587. }
  588. }*/
  589. $sort = 1;
  590. foreach ($this->config[$key] as $k => $v) {
  591. $d = '';
  592. $p = '';
  593. $i = '';
  594. if (is_array($v)) {
  595. if (isset($v[3]) && $data) {
  596. $d = $v[3];
  597. if (strstr($d, '{')) {
  598. $state = $this->getShow($d, $data, true);
  599. } else {
  600. parse_str($d, $t);
  601. $state = false;
  602. foreach ($t as $k1 => $v1) {
  603. if (isset($data[$k1])) {
  604. $v1 = explode(',', $v1);
  605. foreach ($v1 as $v2) {
  606. if ($data[$k1] == $v2) {
  607. $state = true;
  608. }
  609. }
  610. }
  611. }
  612. }
  613. if (!$state) {
  614. continue;
  615. }
  616. }
  617. if (isset($v[2])) {
  618. $i = $v[2];
  619. }
  620. if (isset($v[1])) {
  621. $p = $v[1];
  622. # 针对数据隔离做单独的处理
  623. if (is_array($p) && isset($p['param']['set']['authorization'])) {
  624. $value = explode(',', $p['param']['set']['authorization']);
  625. if (empty($value[3])) {
  626. $value[3] = 0;
  627. }
  628. $p['param']['set']['authorization'] = Dever::load('common', 'manage')->setAuth($value[0], $value[1], $data[$value[2]] ?? $temvaluep[2], $data[$value[3]] ?? $value[3]);
  629. }
  630. }
  631. $v = $v[0];
  632. }
  633. if (strstr($v, 'add')) {
  634. $icon = 'Plus';
  635. $button = 'primary';
  636. } elseif (strstr($v, 'edit')) {
  637. $icon = 'Edit';
  638. $button = 'primary';
  639. } elseif (strstr($v, 'view')) {
  640. $icon = 'View';
  641. $button = '';
  642. } elseif (strstr($v, 'drawer')) {
  643. $icon = 'Drawer';
  644. $button = '';
  645. } elseif ($v == 'delete') {
  646. if ($key == 'button') {
  647. if (isset($this->config['layout'])) {
  648. continue;
  649. }
  650. $this->config['selection'] = true;
  651. }
  652. $icon = 'Delete';
  653. $button = 'danger';
  654. } elseif ($v == 'recycle') {
  655. if ($key == 'button') {
  656. if (isset($this->config['layout'])) {
  657. continue;
  658. }
  659. $this->config['selection'] = true;
  660. }
  661. $icon = 'Delete';
  662. $button = 'danger';
  663. } elseif ($v == 'oper') {
  664. if ($key == 'button') {
  665. if (isset($this->config['layout'])) {
  666. continue;
  667. }
  668. $this->config['selection'] = true;
  669. }
  670. $icon = 'Notification';
  671. $button = 'warning';
  672. } elseif ($v == 'api') {
  673. if ($key == 'button') {
  674. if (isset($this->config['layout'])) {
  675. continue;
  676. }
  677. $this->config['selection'] = true;
  678. }
  679. $p = Dever::url($p);
  680. $icon = 'Notification';
  681. $button = 'warning';
  682. } elseif ($v == 'link') {
  683. $icon = 'Link';
  684. $button = 'success';
  685. $p = $this->getShow($p, $data);
  686. } elseif ($v == 'route') {
  687. $icon = 'Link';
  688. $button = 'success';
  689. } elseif ($v == 'recover') {
  690. $icon = 'CirclePlus';
  691. $button = 'info';
  692. } else {
  693. continue;
  694. }
  695. # 权限验证
  696. $sort++;
  697. $func = $this->getFunc($v, $k, $sort, $p);
  698. if ($this->menu && $this->menu['show'] == 1 && !$func) {
  699. continue;
  700. }
  701. if ($i) {
  702. $icon = $i;
  703. }
  704. $result[] = [
  705. 'name' => $k,
  706. 'type' => $v,
  707. 'param' => $p,
  708. 'icon' => $icon,
  709. 'button' => $button,
  710. 'func' => $func,
  711. ];
  712. if (!$this->recycler && $v == 'recycle') {
  713. $this->recycler = true;
  714. }
  715. }
  716. return $result;
  717. }
  718. # 构造搜索
  719. protected function search(&$where)
  720. {
  721. $search = Dever::input('search');
  722. $set = Dever::input('set');
  723. $list_search = $result = [];
  724. $result['form'] = $result['field'] = $result['option'] = [];
  725. $this->setting('search', $list_search, false, 'text');
  726. if ($list_search) {
  727. foreach ($list_search as $v) {
  728. if ($v['type'] != 'hidden') {
  729. $result['form'][] = $v;
  730. if (is_numeric($v['value'])) {
  731. $v['value'] = (float) $v['value'];
  732. }
  733. $result['field'][$v['key']] = $v['value'];
  734. if ($v['type'] == 'sku') {
  735. $result['field'][$v['key'] . '_spec'] = [];
  736. }
  737. if (isset($v['option'])) {
  738. $result['option'][$v['key']] = $v['option'];
  739. }
  740. }
  741. $this->searchWhere($search, $v, $where);
  742. $this->searchWhere($set, $v, $where);
  743. }
  744. }
  745. return $result;
  746. }
  747. protected function searchWhere($value, $v, &$where)
  748. {
  749. if ($value) {
  750. if ($value = Dever::issets($value, $v['key'])) {
  751. if (isset($v['search'])) {
  752. if (is_callable($v['search'])) {
  753. $value = $v['search']($v['key'], $v['type'], $value);
  754. } elseif (isset($v['search']['table'])) {
  755. $v['search']['where'] = Dever::json_decode(str_replace('{value}', $value, Dever::json_encode($v['search']['where'])));
  756. $search = Dever::db($v['search']['table'])->select($v['search']['where'], $v['search']['set'] ?? []);
  757. $value = [];
  758. if ($search) {
  759. foreach ($search as $v1) {
  760. $value[] = $v1[$v['search']['field']];
  761. }
  762. }
  763. $value = implode(',', $value);
  764. $v['type'] = 'in';
  765. if (isset($v['search']['key'])) {
  766. $v['key'] = $v['search']['key'];
  767. }
  768. }
  769. }
  770. if ($v['type'] == 'select_text') {
  771. if ($value[1] === '') {
  772. return;
  773. }
  774. $result = current(array_filter($v['option'], function($item) use($value) {
  775. return $item['id'] == $value[0];
  776. }));
  777. if (!$result) {
  778. return;
  779. }
  780. $v['key'] = $result['value'];
  781. $v['type'] = '';
  782. if (strstr($v['key'], '.')) {
  783. $r = Dever::call($v['key'], $value[1]);
  784. if ($r) {
  785. $v['key'] = $r[0];
  786. $v['type'] = $r[1];
  787. $value = $r[2];
  788. }
  789. } else {
  790. if (strstr($v['key'], ' ')) {
  791. $temp = explode(' ', $v['key']);
  792. $v['key'] = $temp[0];
  793. $v['type'] = $temp[1];
  794. }
  795. $value = $value[1];
  796. }
  797. }
  798. if ($v['type'] == 'group') {
  799. $where[$v['key']] = ['group', $value];
  800. } elseif ($v['type'] == 'selects') {
  801. $where[$v['key']] = ['group', $value];
  802. } elseif ($v['type'] == 'cascader') {
  803. $t = $value;
  804. if (is_array($value)) {
  805. $t = implode(',', $value);
  806. }
  807. //$where[$v['key']] = ['group', $t];
  808. $where[$v['key']] = $t;
  809. } elseif ($v['type'] == 'like') {
  810. $where[$v['key']] = ['like', $value];
  811. } elseif ($v['type'] == 'in') {
  812. $where[$v['key']] = ['in', $value];
  813. } elseif ($v['type'] == 'date') {
  814. if (strstr($v['date_type'], 'range')) {
  815. $where[$v['key']] = array('>=', \Dever\Helper\Date::mktime($value[0]));
  816. $where[$v['key'] . '#'] = array('<=', \Dever\Helper\Date::mktime($value[1]));
  817. } else {
  818. $where[$v['key']] = $value;
  819. }
  820. } else {
  821. $where[$v['key']] = $value;
  822. }
  823. }
  824. }
  825. }
  826. }