Common.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <?php namespace Manage\Lib;
  2. use Dever;
  3. use Dever\Helper\Str;
  4. use Dever\Helper\Env;
  5. use Dever\Helper\Secure;
  6. use Dever\Helper\Date;
  7. class Common
  8. {
  9. public function info()
  10. {
  11. $auth = $this->auth();
  12. $system = Dever::db('system', 'manage')->find($auth['extend']['system_id']);
  13. return Dever::db($system['user_table'])->find($auth['uid']);
  14. }
  15. public function auth()
  16. {
  17. $auth = Dever::input('authorization');
  18. if ($auth) {
  19. $auth = \Dever\Helper\Str::decode($auth);
  20. }
  21. if (!$auth) {
  22. $auth = Env::header('authorization');
  23. }
  24. if ($auth) {
  25. $auth = str_replace('Bearer ', '', $auth);
  26. Dever::session('auth', $auth);
  27. $info = Secure::checkLogin($auth);
  28. return $info;
  29. }
  30. return false;
  31. }
  32. # 获取当前的扩展数据
  33. public function extend()
  34. {
  35. $info = $this->auth();
  36. if (!$info) {
  37. $auth = Dever::session('auth');
  38. if (!$auth) {
  39. return false;
  40. }
  41. $info = Secure::checkLogin($auth);
  42. }
  43. if ($info && isset($info['extend'])) {
  44. return $info['extend'];
  45. }
  46. return false;
  47. }
  48. # 获取页面类
  49. public function page($load, $config = array(), $key = 'list', $input = true)
  50. {
  51. $page = new Page($key, $load, $input, $config);
  52. return $page;
  53. }
  54. # 获取当前使用的系统 一般为数据库隔离使用
  55. public function system($info = false, $module = true)
  56. {
  57. if (!$info) {
  58. $info = $this->extend();
  59. }
  60. if ($info && isset($info['info_id']) && isset($info['partition'])) {
  61. # 这里后续增加从数据库中获取
  62. $value = $info['system_id'] . '_' . $info['info_id'];
  63. $result = array();
  64. if (strpos($info['partition'], '.')) {
  65. $temp = explode('.', $info['partition']);
  66. $result = array($temp[0] => $value);
  67. if ($module && isset($info['data_id']) && $info['data_id']) {
  68. if ($temp[0] == $temp[1]) {
  69. $result[$temp[0]] .= '/' . $info['module_id'] . '_' . $info['data_id'];
  70. } else {
  71. $result[$temp[1]] = $info['module_id'] . '_' . $info['data_id'];
  72. }
  73. }
  74. } else {
  75. $result = array($info['partition'] => $value);
  76. }
  77. return $result;
  78. }
  79. return false;
  80. }
  81. # 生成token
  82. public function token($uid, $mobile, $partition, $system_id, $info_id, $module_id, $data_id)
  83. {
  84. $extand['partition'] = $partition;
  85. $extand['system_id'] = $system_id;
  86. $extand['info_id'] = $info_id;
  87. $extand['module_id'] = $module_id;
  88. $extand['data_id'] = $data_id;
  89. if ($uid) {
  90. $select['uid'] = $uid;
  91. $select['system_id'] = $system_id;
  92. $select['info_id'] = $info_id;
  93. $info = Dever::db('system_user', 'manage')->find($select);
  94. $select += $extand;
  95. if (!$info) {
  96. Dever::db('system_user', 'manage')->insert($select);
  97. } else {
  98. Dever::db('system_user', 'manage')->update($info['id'], $select);
  99. }
  100. }
  101. return array('token' => Secure::login($uid, $extand));
  102. }
  103. # 生成密码
  104. public function createPwd($password)
  105. {
  106. $data['salt'] = Str::salt(8);
  107. $data['password'] = $this->hash($password, $data['salt']);
  108. return $data;
  109. }
  110. # 生成时间
  111. public function crateDate($date)
  112. {
  113. return Date::mktime($date);
  114. }
  115. # hash加密
  116. public function hash($password, $salt)
  117. {
  118. return hash('sha256', $password . $salt);
  119. }
  120. # 自动更新key
  121. public function updateKey($db, $data)
  122. {
  123. if ($data['name'] && !$data['key']) {
  124. if (Dever::project('pinyin')) {
  125. $where = array();
  126. if (isset($data['id']) && $data['id']) {
  127. $where['id'] = array('!=', $data['id']);
  128. }
  129. $data['key'] = Dever::load('convert', 'pinyin')->getPinyin($data['name']);
  130. # 检查是否存在
  131. $where['key'] = $data['key'];
  132. $info = $db->find($where);
  133. if ($info) {
  134. $data['key'] .= '-' . date('YmdHis');
  135. }
  136. }
  137. }
  138. return $data;
  139. }
  140. # 设置联动
  141. public function cascader($total, $func)
  142. {
  143. $total = Dever::input('total', 'is_numeric', '联动总数', $total);
  144. $level = Dever::input('level', 'is_numeric', '联动级别', 1);
  145. $parent = Dever::input('parent', 'is_numeric', '联动ID', 0);
  146. if ($parent < 0) {
  147. Dever::error('error');
  148. }
  149. $data = $func($level, $parent);
  150. if ($level >= $total) {
  151. foreach ($data as &$v) {
  152. $v['leaf'] = true;
  153. }
  154. }
  155. $result['total'] = $total;
  156. $result['list'] = $data;
  157. return $result;
  158. }
  159. # 根据load获取db
  160. public function db($load)
  161. {
  162. $menu = array();
  163. $load = explode('/', ltrim($load, '/'));
  164. if (isset($load[2])) {
  165. $app = $load[1];
  166. $table = $load[2];
  167. } else {
  168. $app = $load[0];
  169. $table = $load[1];
  170. }
  171. $parent = Dever::db('menu', 'manage')->find(array('key' => $app));
  172. if ($parent) {
  173. $menu = Dever::db('menu', 'manage')->find(array('parent_id' => $parent['id'], 'key' => $table));
  174. if ($menu) {
  175. $app = $menu['app'];
  176. }
  177. }
  178. $set = Dever::project($app);
  179. $manage = $set['path'] . 'manage/'.$table.'.php';
  180. if (is_file($manage)) {
  181. $manage = include $manage;
  182. if ($source = Dever::issets($manage, 'source')) {
  183. if (strpos($source, '/')) {
  184. $source = explode('/', $source);
  185. $app = $source[0];
  186. $table = $source[1];
  187. } else {
  188. $table = $source;
  189. }
  190. }
  191. }
  192. $db = Dever::db($table, $app);
  193. $db->config['manage'] = $manage;
  194. return array($db, $menu);
  195. }
  196. # 获取项目
  197. public function project()
  198. {
  199. $result = array();
  200. $app = \Dever\Project::read();
  201. foreach ($app as $k => $v) {
  202. $result[] = array
  203. (
  204. 'id' => $k,
  205. 'name' => $v['lang'] ?? $k,
  206. );
  207. }
  208. return $result;
  209. }
  210. # 仅为测试用
  211. public function out($data)
  212. {
  213. $result = array();
  214. $result['head'] = array('id', '姓名', '时间');
  215. $result['body'] = array();
  216. foreach ($data['body'] as $k => $v) {
  217. $result['body'][$k] = array($v['id'], $v['name'], $v['cdate']);
  218. }
  219. return $result;
  220. }
  221. # 仅为测试用,展示表格更多内容
  222. public function show($data)
  223. {
  224. # 返回类型:string字符串,table表格,card卡片,desc描述,list列表
  225. $result['type'] = 'string';
  226. $result['content'] = 'ddddd';
  227. $result['type'] = 'table';
  228. $result['head'] = array('id' => 'id', 'name' => '姓名');
  229. $result['body'] = array
  230. (
  231. array('id' => '1', 'name' => 'test'),
  232. array('id' => '2', 'name' => 'test2'),
  233. );
  234. $result['type'] = 'card';
  235. $result['content'][] = array
  236. (
  237. 'title' => '订单信息',
  238. 'shadow' => 'never',//always | never | hover
  239. 'content' => array
  240. (
  241. '1111',
  242. '2222',
  243. ),
  244. );
  245. $result['type'] = 'list';
  246. $result['content'] = array
  247. (
  248. array('日志类型', 'test'),
  249. array('姓名', 'test1'),
  250. );
  251. return $result;
  252. }
  253. # 仅为测试用,展示详情
  254. public function view($page)
  255. {
  256. # 这里获取基本信息
  257. //print_r($page->info);die;
  258. $info[] = array
  259. (
  260. # 类型,desc描述 table表格,表格有head和body即可
  261. 'type' => 'desc',
  262. 'name' => '基本信息',
  263. # 每行展示数量
  264. 'column' => 4,
  265. # 是否有边框
  266. 'border' => true,
  267. # 排列方向:horizontal横向 vertical纵向
  268. 'direction' => 'vertical',
  269. # 右侧按钮
  270. 'button' => array
  271. (
  272. array
  273. (
  274. 'name' => '编辑',
  275. # fastedit、fastadd、oper、api、link、route,参数与list里的data_button一致,多了一个load,可以单独设置路由
  276. 'type' => 'fastedit',
  277. 'path' => 'platform/role',
  278. # 增加权限,第三个参数是排序,建议大一些
  279. //'func' => $page->getFunc('view_fastedit', '详情页-编辑角色', 1000),
  280. # 这里是按钮用到的参数数据
  281. 'row' => array
  282. (
  283. 'id' => 1,
  284. ),
  285. ),
  286. ),
  287. # 具体内容
  288. 'content' => array
  289. (
  290. array
  291. (
  292. 'name' => '标题',
  293. # 类型,text普通文本,tag标签,link链接,image图片 progress进度条 stat统计 timeline时间线 table表格
  294. 'type' => 'text',
  295. 'content' => '内容',
  296. # 样式primary success warning danger info exception
  297. 'style' => 'primary',
  298. ),
  299. array
  300. (
  301. 'name' => '标题',
  302. 'type' => 'tag',
  303. 'content' => '内容',
  304. 'style' => 'warning',
  305. ),
  306. array
  307. (
  308. 'name' => '标题',
  309. 'type' => 'link',
  310. 'content' => '内容',
  311. ),
  312. array
  313. (
  314. 'name' => '图片',
  315. 'type' => 'image',
  316. 'content' => 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
  317. # 'fill', 'contain', 'cover', 'none', 'scale-down'
  318. 'fit' => 'fill',
  319. ),
  320. array
  321. (
  322. 'name' => '进度条',
  323. 'type' => 'progress',
  324. 'content' => '10',
  325. 'style' => 'exception',
  326. 'width' => '20',
  327. 'inside' => true,
  328. # line dashboard 仪表盘 circle 圆形
  329. 'show' => 'line',
  330. # 开启条纹
  331. 'striped' => true,
  332. # 开启动画
  333. 'indeterminate' => true,
  334. ),
  335. array
  336. (
  337. 'name' => '统计',
  338. 'type' => 'stat',
  339. 'content' => array
  340. (
  341. array
  342. (
  343. # 一共24
  344. 'span' => '6',
  345. 'name' => '测试',
  346. 'value' => '1000',
  347. ),
  348. array
  349. (
  350. 'span' => '6',
  351. 'name' => '测试1',
  352. 'value' => '1000',
  353. ),
  354. array
  355. (
  356. 'span' => '6',
  357. 'name' => '测试2',
  358. 'value' => '1000',
  359. ),
  360. array
  361. (
  362. 'span' => '6',
  363. 'name' => '测试2',
  364. 'value' => '1000',
  365. ),
  366. ),
  367. ),
  368. array
  369. (
  370. 'name' => '时间线',
  371. 'type' => 'timeline',
  372. 'content' => array
  373. (
  374. array
  375. (
  376. 'time' => '2020-10-11',
  377. 'name' => '测试',
  378. 'color' => '#0bbd87',
  379. 'size' => 'large',
  380. 'type' => 'primary',
  381. 'hollow' => true,
  382. ),
  383. array
  384. (
  385. 'time' => '2020-10-11',
  386. 'name' => '测试',
  387. ),
  388. array
  389. (
  390. 'time' => '2020-10-11',
  391. 'name' => '测试',
  392. ),
  393. array
  394. (
  395. 'time' => '2020-10-11',
  396. 'name' => '测试',
  397. ),
  398. ),
  399. ),
  400. array
  401. (
  402. 'name' => '表格',
  403. 'type' => 'table',
  404. 'border' => true,
  405. 'height' => '200',
  406. 'head' => array
  407. (
  408. array
  409. (
  410. 'key' => 'name',
  411. 'name' => '姓名',
  412. 'fixed' => 'fixed',
  413. ),
  414. array
  415. (
  416. 'key' => 'desc',
  417. 'name' => '描述',
  418. 'fixed' => 'fixed',
  419. ),
  420. ),
  421. 'button' => array
  422. (
  423. array
  424. (
  425. 'name' => '编辑',
  426. 'type' => 'fastedit',
  427. 'load' => 'platform/role',
  428. ),
  429. ),
  430. 'body' => array
  431. (
  432. array
  433. (
  434. 'id' => 1,
  435. 'name' => 'test',
  436. 'desc' => 'dfdf',
  437. ),
  438. ),
  439. ),
  440. ),
  441. );
  442. $info[] = array
  443. (
  444. 'type' => 'table',
  445. 'name' => '表格信息',
  446. 'border' => true,
  447. 'height' => '200',
  448. 'head' => array
  449. (
  450. array
  451. (
  452. 'key' => 'name',
  453. 'name' => '姓名',
  454. 'fixed' => 'fixed',
  455. ),
  456. array
  457. (
  458. 'key' => 'desc',
  459. 'name' => '描述',
  460. 'fixed' => 'fixed',
  461. ),
  462. ),
  463. 'button' => array
  464. (
  465. array
  466. (
  467. 'name' => '编辑',
  468. 'type' => 'fastedit',
  469. 'load' => 'platform/role',
  470. # 增加权限,第三个参数是排序,建议大一些
  471. 'func' => $page->getFunc('view_fastedit', '详情页-编辑角色', 1000),
  472. ),
  473. ),
  474. 'body' => array
  475. (
  476. array
  477. (
  478. 'id' => 1,
  479. 'name' => 'test',
  480. 'desc' => 'dfdf',
  481. ),
  482. ),
  483. );
  484. $tab = array
  485. (
  486. 'active' => 'table1',
  487. 'content' => array
  488. (
  489. 'table1' => array
  490. (
  491. # 这里跟desc一样
  492. 'name' => '标题',
  493. 'type' => 'text',
  494. 'content' => '内容',
  495. 'style' => 'primary',
  496. ),
  497. 'tab2' => array
  498. (
  499. 'name' => '表格',
  500. 'type' => 'table',
  501. 'border' => true,
  502. 'height' => '200',
  503. 'head' => array
  504. (
  505. array
  506. (
  507. 'key' => 'name',
  508. 'name' => '姓名',
  509. 'fixed' => 'fixed',
  510. ),
  511. array
  512. (
  513. 'key' => 'desc',
  514. 'name' => '描述',
  515. 'fixed' => 'fixed',
  516. ),
  517. ),
  518. 'button' => array
  519. (
  520. array
  521. (
  522. 'name' => '编辑',
  523. 'type' => 'fastedit',
  524. 'load' => 'platform/role',
  525. ),
  526. ),
  527. 'body' => array
  528. (
  529. array
  530. (
  531. 'id' => 1,
  532. 'name' => 'test',
  533. 'desc' => 'dfdf',
  534. ),
  535. ),
  536. ),
  537. )
  538. );
  539. return array('info' => $info, 'tab' => $tab);
  540. }
  541. public function stat($where)
  542. {
  543. return array
  544. (
  545. array
  546. (
  547. # 一共24
  548. 'span' => '8',
  549. 'name' => '测试',
  550. 'value' => '1000',
  551. ),
  552. array
  553. (
  554. 'span' => '8',
  555. 'name' => '测试1',
  556. 'value' => '1000',
  557. ),
  558. array
  559. (
  560. 'span' => '8',
  561. 'name' => '测试2',
  562. 'value' => '1000',
  563. ),
  564. );
  565. }
  566. }