Common.php 16 KB

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