Common.php 17 KB

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