Common.php 16 KB

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