Page.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 $menu = array();
  11. protected $config = array();
  12. protected $field = array();
  13. public $info = array();
  14. public function __construct($key = '', $load = '', $input = true)
  15. {
  16. parent::__construct();
  17. $this->key = $key;
  18. $this->input = $input;
  19. $this->id = $this->input('id', 0);
  20. if (!$load) {
  21. $load = Dever::input('load');
  22. }
  23. list($this->db, $this->menu) = Dever::load('common', 'manage')->db($load);
  24. if ($this->menu && $this->menu['show'] == 1) {
  25. $this->checkMenu($this->menu['id'], false);
  26. }
  27. $this->config = $this->db->config['manage'][$key] ?? array();
  28. if ($this->id && !strstr($this->id, ',')) {
  29. $this->info = $this->db->find($this->id);
  30. }
  31. }
  32. public function setting($key, &$data, $struct = true, $type = 'show', $disable = false)
  33. {
  34. if (empty($this->config[$key]) && $struct && isset($this->db->config['struct']) && $this->db->config['struct']) {
  35. $this->config[$key] = $this->db->config['struct'];
  36. }
  37. if (empty($this->config[$key])) {
  38. return;
  39. }
  40. $setting = $this->config[$key];
  41. if (is_string($setting)) {
  42. $setting = explode(',', $setting);
  43. }
  44. $field = $this->input('field', '');
  45. return $this->setData($setting, $data, $field, $type, $disable);
  46. }
  47. # 获取某个数据的具体展示值
  48. public function getValue($key, $value, $data, $field = array())
  49. {
  50. if ($show = Dever::isset($field, 'show')) {
  51. $value = $this->getShow($show, $data);
  52. } elseif ($value && isset($this->db->config['struct'][$key]['value']) && $this->db->config['struct'][$key]['value']) {
  53. $value = $this->db->value($key, $value);
  54. } elseif ($key == 'cdate') {
  55. if (strstr($value, 'T')) {
  56. $value = date('Y-m-d H:i:s', strtotime($value));
  57. } elseif ($value) {
  58. $value = date('Y-m-d H:i:s', $value);
  59. } else {
  60. $value = '-';
  61. }
  62. }
  63. return $value;
  64. }
  65. # 获取关联数据
  66. public function getOther($key, $set, $data)
  67. {
  68. $where = array();
  69. foreach ($set as $k => $v) {
  70. if (!is_array($v) && isset($data[$v])) {
  71. $where[$k] = $data[$v];
  72. } else {
  73. $where[$k] = $v;
  74. }
  75. }
  76. if ($where) {
  77. return Dever::db($key)->select($where);
  78. }
  79. return array();
  80. }
  81. public function getShow($show, $data)
  82. {
  83. if (strpos($show, '{') !== false && strpos($show, '{"') === false) {
  84. $func = function ($r) use ($data) {
  85. if (isset($data[$r[1]])) {
  86. return $data[$r[1]];
  87. }
  88. return false;
  89. };
  90. $show = preg_replace_callback('/{(.*?)}/', $func, $show);
  91. }
  92. $eval = '$show = ' . $show . ';';
  93. @eval($eval);
  94. return $show;
  95. }
  96. # 获取菜单标题
  97. public function getTitle()
  98. {
  99. return $this->menu['name'];
  100. }
  101. # 获取左侧分栏
  102. protected function column(&$data, $name = '左侧分栏')
  103. {
  104. $data['column'] = false;
  105. if (isset($this->config['column'])) {
  106. $data['column'] = $this->config['column'];
  107. if (isset($this->config['column']['add'])) {
  108. $data['column']['add'] = array('name' => $this->config['column']['add'], 'func' => $this->getFunc('column_add', $name . '-' . $this->config['column']['add'], 101));
  109. }
  110. if (isset($this->config['column']['edit'])) {
  111. $data['column']['edit'] = array('name' => '编辑', 'func' => $this->getFunc('column_edit', $name . '-编辑', 102));
  112. }
  113. if (isset($this->config['column']['delete'])) {
  114. $data['column']['delete'] = array('name' => '删除', 'func' => $this->getFunc('column_delete', $name . '-删除', 103));
  115. }
  116. $data['column']['data'] = Dever::call($this->config['column']['data']);
  117. $data['height'] = '100%';
  118. }
  119. }
  120. # 通用的规则验证 一般为更新数据时使用
  121. protected function checkRules($set, $data)
  122. {
  123. if ($set['rules']) {
  124. if (!is_array($set['rules'])) {
  125. $set['rules'] = array
  126. (
  127. array
  128. (
  129. 'required' => true,
  130. 'trigger' => 'blur',
  131. 'message' => $set['name'] . '不能为空',
  132. ),
  133. );
  134. }
  135. foreach ($set['rules'] as $k => $v) {
  136. if (isset($v['required']) && $v['required'] && !$data && $data !== 0) {
  137. Dever::error($v['message']);
  138. }
  139. if ($data || $data === 0) {
  140. if (isset($v['pattern']) && $v['pattern'] && !preg_match('/' . $v['pattern'] . '/', $data)) {
  141. Dever::error($v['message']);
  142. }
  143. if (isset($v['type']) && $v['type']) {
  144. if ($v['type'] == 'number' && !is_numeric($data)) {
  145. Dever::error($v['message']);
  146. } elseif ($v['type'] == 'array' && !is_array($data)) {
  147. Dever::error($v['message']);
  148. } elseif ($v['type'] == 'integer' && !is_int($data)) {
  149. Dever::error($v['message']);
  150. } elseif ($v['type'] == 'float' && !is_float($data)) {
  151. Dever::error($v['message']);
  152. } elseif ($v['type'] == 'string' && !is_string($data)) {
  153. Dever::error($v['message']);
  154. } elseif ($v['type'] == 'boolean' && !is_bool($data)) {
  155. Dever::error($v['message']);
  156. } elseif ($v['type'] == 'url' && !filter_var($data, FILTER_VALIDATE_URL)) {
  157. Dever::error($v['message']);
  158. } elseif ($v['type'] == 'email' && !filter_var($data, FILTER_VALIDATE_EMAIL)) {
  159. Dever::error($v['message']);
  160. } elseif ($v['type'] == 'enum' && isset($v['enum']) && !in_array($data, $v['enum'])) {
  161. Dever::error($v['message']);
  162. }
  163. }
  164. if (isset($v['len']) && $v['len'] && strlen($data) > $v['len']) {
  165. Dever::error($v['message']);
  166. }
  167. if (isset($v['min']) && $v['min'] && strlen($data) < $v['min']) {
  168. Dever::error($v['message']);
  169. }
  170. if (isset($v['max']) && $v['max'] && strlen($data) > $v['max']) {
  171. Dever::error($v['message']);
  172. }
  173. }
  174. }
  175. }
  176. }
  177. private function setData($setting, &$data, $field, $type, $disable)
  178. {
  179. $result = array();
  180. foreach ($setting as $k => $v) {
  181. if (!is_array($v)) {
  182. if (is_numeric($k)) {
  183. $k = $v;
  184. $v = $type;
  185. }
  186. if ($k == 'id') {
  187. $v = array('name' => 'ID', 'type' => $v);
  188. } elseif ($k == 'cdate') {
  189. $v = array('name' => '创建时间', 'type' => $v);
  190. } elseif(isset($this->db->config['struct'][$k])) {
  191. $v = array('name' => $this->db->config['struct'][$k]['name'], 'type' => $v);
  192. } else {
  193. $v = array('name' => $v);
  194. }
  195. } else {
  196. if (isset($v['only'])) {
  197. if ($v['only'] == 'edit' && !$this->id) {
  198. continue;
  199. } elseif ($v['only'] == 'add' && $this->id) {
  200. continue;
  201. }
  202. }
  203. }
  204. if ($field) {
  205. if (is_array($field)) {
  206. if (isset($field['param'])) {
  207. if (isset($field['param']['set'])) {
  208. $field = array_merge($field, $field['param']['set']);
  209. }
  210. if (isset($field['param']['search'])) {
  211. $field = array_merge($field, $field['param']['search']);
  212. }
  213. } elseif (isset($field['field']) && !Dever::check($field['field'], $k)) {
  214. continue;
  215. }
  216. if (isset($field[$k])) {
  217. $v['default'] = $field[$k];
  218. $v['type'] = 'hidden';
  219. }
  220. } elseif (!Dever::check($field, $k)) {
  221. continue;
  222. }
  223. }
  224. $info = $this->setField($data, $k, $v, $field, $type, $disable);
  225. if ($info) {
  226. $result[] = $info;
  227. }
  228. }
  229. return $result;
  230. }
  231. private function setField(&$data, $key, $value = array(), $field, $type = 'show', $disable = false)
  232. {
  233. $value['key'] = $key;
  234. $this->setName($value);
  235. # 对每个字段进行权限设置
  236. if (isset($value['func']) && $value['func']) {
  237. $func = $this->getFunc('field_' . $value['key'], '字段-' . $value['name'], 200);
  238. if (!$func) {
  239. return false;
  240. }
  241. }
  242. if (strpos($key, '/') && $this->key == 'update') {
  243. $this->setType($value, 'array');
  244. $this->setShow($value);
  245. if (strpos($key, '#')) {
  246. $key = str_replace('#', '', $key);
  247. }
  248. $sku = $value['type'] == 'sku' ? true : false;
  249. $value['value'] = $this->getOther($key, $value['where'], $this->info);
  250. $update = new \Manage\Api\Page\Update($key, false);
  251. $value['option'] = array();
  252. $value['content'] = $update->get($value['value'], $value['option'], $sku);
  253. $data[] = $value;
  254. return $value['name'];
  255. } else {
  256. $this->setType($value, $type);
  257. $this->setDisable($value, $disable);
  258. if ($this->key == 'update') {
  259. # 一般为更新页面需要的参数
  260. $this->setShow($value);
  261. $this->setRules($value);
  262. } elseif (isset($value['remote'])) {
  263. $value['remote'] = Dever::url($value['remote']);
  264. }
  265. if ($type == 'show') {
  266. $in = array('switch', 'select', 'input');
  267. if (in_array($value['type'], $in)) {
  268. $value['func'] = $this->getFunc('list_edit_' . $value['key'], '列表更新-' . $value['name'], 104);
  269. if (!$value['func']) {
  270. $value['type'] = 'show';
  271. if (isset($value['show'])) {
  272. unset($value['show']);
  273. }
  274. }
  275. }
  276. if (isset($value['child'])) {
  277. $child = array();
  278. $this->setData($value['child'], $child, $field, $type, $disable);
  279. $value['child'] = $child;
  280. } else {
  281. $this->field[$key] = $value;
  282. }
  283. }
  284. $this->setForm($value);
  285. $data[] = $value;
  286. return $value['name'];
  287. }
  288. }
  289. private function setShow(&$value)
  290. {
  291. if ($value['type'] == 'hidden') {
  292. $value['show'] = false;
  293. } elseif (!isset($value['show'])) {
  294. $value['show'] = true;
  295. }
  296. }
  297. private function setName(&$value)
  298. {
  299. if (empty($value['name']) && isset($this->db->config['struct'][$value['key']])) {
  300. $value['name'] = $this->db->config['struct'][$value['key']]['name'];
  301. }
  302. if (empty($value['placeholder']) && isset($value['name'])) {
  303. $value['placeholder'] = $value['name'];
  304. }
  305. }
  306. private function setType(&$value, $type)
  307. {
  308. if (empty($value['type'])) {
  309. $value['type'] = $type;
  310. }
  311. if (strpos($value['type'], '(')) {
  312. $value['type'] = $type;
  313. }
  314. if (empty($value['width'])) {
  315. $value['width'] = 'auto';
  316. }
  317. if (isset($value['upload']) && Dever::project('upload')) {
  318. $value['url'] = Dever::url('upload/save.act', array('id' => $value['upload']));
  319. $value['config'] = Dever::load('save', 'upload')->get($value['upload']);
  320. if (isset($value['multiple']) && $value['multiple']) {
  321. $value['limit'] = 10;
  322. } else {
  323. $value['limit'] = 1;
  324. }
  325. }
  326. if (isset($value['editorMenu'])) {
  327. if (isset($value['editorMenu']['uploadImage'])) {
  328. if (!is_array($value['editorMenu']['uploadImage'])) {
  329. $value['editorMenu']['uploadImage'] = array('upload' => $value['editorMenu']['uploadImage']);
  330. }
  331. $value['editorMenu']['uploadImage']['server'] = Dever::url('upload/save.wangEditor', array('id' => $value['editorMenu']['uploadImage']['upload']));
  332. if (empty($value['editorMenu']['uploadImage']['fieldName'])) {
  333. $value['editorMenu']['uploadImage']['fieldName'] = 'file';
  334. }
  335. }
  336. if (isset($value['editorMenu']['uploadVideo'])) {
  337. if (!is_array($value['editorMenu']['uploadVideo'])) {
  338. $value['editorMenu']['uploadVideo'] = array('upload' => $value['editorMenu']['uploadVideo']);
  339. }
  340. $value['editorMenu']['uploadVideo']['server'] = Dever::url('upload/save.wangEditor', array('id' => $value['editorMenu']['uploadVideo']['upload']));
  341. if (empty($value['editorMenu']['uploadImage']['fieldName'])) {
  342. $value['editorMenu']['uploadImage']['fieldName'] = 'file';
  343. }
  344. }
  345. }
  346. if (isset($value['date_type']) && $value['date_type'] == 'datetimerange' && empty($value['default_time'])) {
  347. $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);
  348. }
  349. }
  350. private function setDisable(&$value, $disable)
  351. {
  352. if (isset($value['disable'])) {
  353. $disable = $value['disable'];
  354. }
  355. $value['disable'] = $disable;
  356. }
  357. private function setForm(&$value)
  358. {
  359. $value['value'] = Dever::input('search')[$value['key']] ?? '';
  360. if (!$value['value']) {
  361. if (isset($value['default']) && !strstr($value['default'], '{')) {
  362. $value['value'] = $value['default'];
  363. } elseif ($this->key == 'update' && isset($this->db->config['struct'][$value['key']]['default'])) {
  364. $value['value'] = $this->db->config['struct'][$value['key']]['default'];
  365. }
  366. }
  367. if (isset($value['option']) && $value['option']) {
  368. $this->db->config['struct'][$value['key']]['value'] = $value['option'];
  369. }
  370. if ($option = $this->db->value($value['key'])) {
  371. if ($value['type'] == 'checkbox') {
  372. $value['value'] = $value['value'] ? explode(',', $value['value']) : array();
  373. }
  374. $value['option'] = $option;
  375. if ($value['type'] == 'text') {
  376. $value['type'] = 'select';
  377. }
  378. }
  379. }
  380. private function setRules(&$value)
  381. {
  382. if (isset($value['rules']) && $value['rules']) {
  383. if (!is_array($value['rules'])) {
  384. $value['rules'] = array
  385. (
  386. array
  387. (
  388. 'required' => true,
  389. 'trigger' => 'blur',
  390. 'message' => $value['name'] . '不能为空',
  391. ),
  392. );
  393. }
  394. foreach ($value['rules'] as $k => $v) {
  395. if (isset($v['only'])) {
  396. if ($v['only'] == 'edit' && !$this->id) {
  397. unset($value['rules'][$k]);
  398. break;
  399. } elseif ($v['only'] == 'add' && $this->id) {
  400. unset($value['rules'][$k]);
  401. break;
  402. }
  403. }
  404. }
  405. if (!isset($value['rules'][0])) {
  406. $value['rules'] = array_values($value['rules']);
  407. }
  408. }
  409. }
  410. protected function input($key, $value = '')
  411. {
  412. return $this->input ? Dever::input($key) : $value;
  413. }
  414. }