Common.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. # 根据load获取db
  121. public function db($load)
  122. {
  123. $menu = array();
  124. $load = explode('/', ltrim($load, '/'));
  125. if (isset($load[2])) {
  126. $app = $load[1];
  127. $table = $load[2];
  128. } else {
  129. $app = $load[0];
  130. $table = $load[1];
  131. }
  132. $parent = Dever::db('menu', 'manage')->find(array('key' => $app));
  133. if ($parent) {
  134. $menu = Dever::db('menu', 'manage')->find(array('parent_id' => $parent['id'], 'key' => $table));
  135. if ($menu) {
  136. $app = $menu['app'];
  137. }
  138. }
  139. $set = Dever::project($app);
  140. $manage = $set['path'] . 'manage/'.$table.'.php';
  141. if (is_file($manage)) {
  142. $manage = include $manage;
  143. if ($source = Dever::issets($manage, 'source')) {
  144. if (strpos($source, '/')) {
  145. $source = explode('/', $source);
  146. $app = $source[0];
  147. $table = $source[1];
  148. } else {
  149. $table = $source;
  150. }
  151. }
  152. }
  153. $db = Dever::db($table, $app);
  154. $db->config['manage'] = $manage;
  155. return array($db, $menu);
  156. }
  157. # 获取项目
  158. public function project()
  159. {
  160. $result = array();
  161. $app = \Dever\Project::read();
  162. foreach ($app as $k => $v) {
  163. $result[] = array
  164. (
  165. 'id' => $k,
  166. 'name' => $v['lang'] ?? $k,
  167. );
  168. }
  169. return $result;
  170. }
  171. # 仅为测试用
  172. public function out($data)
  173. {
  174. $result = array();
  175. $result['head'] = array('id', '姓名', '时间');
  176. $result['body'] = array();
  177. foreach ($data['body'] as $k => $v) {
  178. $result['body'][$k] = array($v['id'], $v['name'], $v['cdate']);
  179. }
  180. return $result;
  181. }
  182. # 仅为测试用,展示表格更多内容
  183. public function show($data)
  184. {
  185. # 返回类型:string字符串,table表格,card卡片,desc描述,list列表
  186. $result['type'] = 'string';
  187. $result['content'] = 'ddddd';
  188. $result['type'] = 'table';
  189. $result['head'] = array('id' => 'id', 'name' => '姓名');
  190. $result['body'] = array
  191. (
  192. array('id' => '1', 'name' => 'test'),
  193. array('id' => '2', 'name' => 'test2'),
  194. );
  195. $result['type'] = 'card';
  196. $result['content'][] = array
  197. (
  198. 'title' => '订单信息',
  199. 'shadow' => 'never',//always | never | hover
  200. 'content' => array
  201. (
  202. '1111',
  203. '2222',
  204. ),
  205. );
  206. $result['type'] = 'list';
  207. $result['content'] = array
  208. (
  209. array('日志类型', 'test'),
  210. array('姓名', 'test1'),
  211. );
  212. return $result;
  213. }
  214. # 仅为测试用,展示详情
  215. public function view($page)
  216. {
  217. # 这里获取基本信息
  218. //print_r($page->info);die;
  219. $info[] = array
  220. (
  221. # 类型,desc描述 table表格,表格有head和body即可
  222. 'type' => 'desc',
  223. 'name' => '基本信息',
  224. # 每行展示数量
  225. 'column' => 4,
  226. # 是否有边框
  227. 'border' => true,
  228. # 排列方向:horizontal横向 vertical纵向
  229. 'direction' => 'vertical',
  230. # 右侧按钮
  231. 'button' => array
  232. (
  233. array
  234. (
  235. 'name' => '编辑',
  236. # fastedit、fastadd、oper、api、link、route,参数与list里的data_button一致,多了一个load,可以单独设置路由
  237. 'type' => 'fastedit',
  238. 'path' => 'platform/role',
  239. # 增加权限,第三个参数是排序,建议大一些
  240. //'func' => $page->getFunc('view_fastedit', '详情页-编辑角色', 1000),
  241. # 这里是按钮用到的参数数据
  242. 'row' => array
  243. (
  244. 'id' => 1,
  245. ),
  246. ),
  247. ),
  248. # 具体内容
  249. 'content' => array
  250. (
  251. array
  252. (
  253. 'name' => '标题',
  254. # 类型,text普通文本,tag标签,link链接,image图片 progress进度条 stat统计 timeline时间线 table表格
  255. 'type' => 'text',
  256. 'content' => '内容',
  257. # 样式primary success warning danger info exception
  258. 'style' => 'primary',
  259. ),
  260. array
  261. (
  262. 'name' => '标题',
  263. 'type' => 'tag',
  264. 'content' => '内容',
  265. 'style' => 'warning',
  266. ),
  267. array
  268. (
  269. 'name' => '标题',
  270. 'type' => 'link',
  271. 'content' => '内容',
  272. ),
  273. array
  274. (
  275. 'name' => '图片',
  276. 'type' => 'image',
  277. 'content' => 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
  278. # 'fill', 'contain', 'cover', 'none', 'scale-down'
  279. 'fit' => 'fill',
  280. ),
  281. array
  282. (
  283. 'name' => '进度条',
  284. 'type' => 'progress',
  285. 'content' => '10',
  286. 'style' => 'exception',
  287. 'width' => '20',
  288. 'inside' => true,
  289. # line dashboard 仪表盘 circle 圆形
  290. 'show' => 'line',
  291. # 开启条纹
  292. 'striped' => true,
  293. # 开启动画
  294. 'indeterminate' => true,
  295. ),
  296. array
  297. (
  298. 'name' => '统计',
  299. 'type' => 'stat',
  300. 'content' => array
  301. (
  302. array
  303. (
  304. # 一共24
  305. 'span' => '6',
  306. 'name' => '测试',
  307. 'value' => '1000',
  308. ),
  309. array
  310. (
  311. 'span' => '6',
  312. 'name' => '测试1',
  313. 'value' => '1000',
  314. ),
  315. array
  316. (
  317. 'span' => '6',
  318. 'name' => '测试2',
  319. 'value' => '1000',
  320. ),
  321. array
  322. (
  323. 'span' => '6',
  324. 'name' => '测试2',
  325. 'value' => '1000',
  326. ),
  327. ),
  328. ),
  329. array
  330. (
  331. 'name' => '时间线',
  332. 'type' => 'timeline',
  333. 'content' => array
  334. (
  335. array
  336. (
  337. 'time' => '2020-10-11',
  338. 'name' => '测试',
  339. 'color' => '#0bbd87',
  340. 'size' => 'large',
  341. 'type' => 'primary',
  342. 'hollow' => true,
  343. ),
  344. array
  345. (
  346. 'time' => '2020-10-11',
  347. 'name' => '测试',
  348. ),
  349. array
  350. (
  351. 'time' => '2020-10-11',
  352. 'name' => '测试',
  353. ),
  354. array
  355. (
  356. 'time' => '2020-10-11',
  357. 'name' => '测试',
  358. ),
  359. ),
  360. ),
  361. array
  362. (
  363. 'name' => '表格',
  364. 'type' => 'table',
  365. 'border' => true,
  366. 'height' => '200',
  367. 'head' => array
  368. (
  369. array
  370. (
  371. 'key' => 'name',
  372. 'name' => '姓名',
  373. 'fixed' => 'fixed',
  374. ),
  375. array
  376. (
  377. 'key' => 'desc',
  378. 'name' => '描述',
  379. 'fixed' => 'fixed',
  380. ),
  381. ),
  382. 'button' => array
  383. (
  384. array
  385. (
  386. 'name' => '编辑',
  387. 'type' => 'fastedit',
  388. 'load' => 'platform/role',
  389. ),
  390. ),
  391. 'body' => array
  392. (
  393. array
  394. (
  395. 'id' => 1,
  396. 'name' => 'test',
  397. 'desc' => 'dfdf',
  398. ),
  399. ),
  400. ),
  401. ),
  402. );
  403. $info[] = array
  404. (
  405. 'type' => 'table',
  406. 'name' => '表格信息',
  407. 'border' => true,
  408. 'height' => '200',
  409. 'head' => array
  410. (
  411. array
  412. (
  413. 'key' => 'name',
  414. 'name' => '姓名',
  415. 'fixed' => 'fixed',
  416. ),
  417. array
  418. (
  419. 'key' => 'desc',
  420. 'name' => '描述',
  421. 'fixed' => 'fixed',
  422. ),
  423. ),
  424. 'button' => array
  425. (
  426. array
  427. (
  428. 'name' => '编辑',
  429. 'type' => 'fastedit',
  430. 'load' => 'platform/role',
  431. # 增加权限,第三个参数是排序,建议大一些
  432. 'func' => $page->getFunc('view_fastedit', '详情页-编辑角色', 1000),
  433. ),
  434. ),
  435. 'body' => array
  436. (
  437. array
  438. (
  439. 'id' => 1,
  440. 'name' => 'test',
  441. 'desc' => 'dfdf',
  442. ),
  443. ),
  444. );
  445. $tab = array
  446. (
  447. 'active' => 'table1',
  448. 'content' => array
  449. (
  450. 'table1' => array
  451. (
  452. # 这里跟desc一样
  453. 'name' => '标题',
  454. 'type' => 'text',
  455. 'content' => '内容',
  456. 'style' => 'primary',
  457. ),
  458. 'tab2' => array
  459. (
  460. 'name' => '表格',
  461. 'type' => 'table',
  462. 'border' => true,
  463. 'height' => '200',
  464. 'head' => array
  465. (
  466. array
  467. (
  468. 'key' => 'name',
  469. 'name' => '姓名',
  470. 'fixed' => 'fixed',
  471. ),
  472. array
  473. (
  474. 'key' => 'desc',
  475. 'name' => '描述',
  476. 'fixed' => 'fixed',
  477. ),
  478. ),
  479. 'button' => array
  480. (
  481. array
  482. (
  483. 'name' => '编辑',
  484. 'type' => 'fastedit',
  485. 'load' => 'platform/role',
  486. ),
  487. ),
  488. 'body' => array
  489. (
  490. array
  491. (
  492. 'id' => 1,
  493. 'name' => 'test',
  494. 'desc' => 'dfdf',
  495. ),
  496. ),
  497. ),
  498. )
  499. );
  500. return array('info' => $info, 'tab' => $tab);
  501. }
  502. public function stat($where)
  503. {
  504. return array
  505. (
  506. array
  507. (
  508. # 一共24
  509. 'span' => '8',
  510. 'name' => '测试',
  511. 'value' => '1000',
  512. ),
  513. array
  514. (
  515. 'span' => '8',
  516. 'name' => '测试1',
  517. 'value' => '1000',
  518. ),
  519. array
  520. (
  521. 'span' => '8',
  522. 'name' => '测试2',
  523. 'value' => '1000',
  524. ),
  525. );
  526. }
  527. }