Util.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 Util
  8. {
  9. # 获取后台传入的数据
  10. public function request($col)
  11. {
  12. return Dever::input(['set', 'field'])[$col] ?? 0;
  13. }
  14. # 快速生成tip
  15. public function createTip($call)
  16. {
  17. $name = '-';
  18. $content = [];
  19. $call($name, $content);
  20. $result['name'] = $name;
  21. if ($content) {
  22. $result['content'] = ['type' => 'line', 'content' => $content];
  23. }
  24. return $result;
  25. }
  26. # 加入cron
  27. public function cron($name, $project, $api, $time = 3600)
  28. {
  29. $data = ['project' => $project, 'interface' => $api];
  30. $info = Dever::db('cron', 'manage')->find($data);
  31. if (!$info) {
  32. $data['name'] = $name;
  33. $data['ldate'] = time();
  34. $data['time'] = 3600;
  35. Dever::db('cron', 'manage')->insert($data);
  36. }
  37. }
  38. # 快速使用tip里的content
  39. public function getTip($data, $key)
  40. {
  41. return $data['content']['content'][$key]['content'] ?? '';
  42. }
  43. public function info()
  44. {
  45. $auth = $this->auth();
  46. $system = Dever::db('system', 'manage')->find($auth['extend']['system_id']);
  47. return Dever::db($system['user_table'])->find($auth['uid']);
  48. }
  49. public function auth()
  50. {
  51. $auth = Dever::input('authorization');
  52. if ($auth) {
  53. $auth = Str::decode($auth);
  54. }
  55. if (!$auth) {
  56. $auth = Env::header('authorization');
  57. }
  58. if ($auth) {
  59. $auth = str_replace('Bearer ', '', $auth);
  60. Dever::session('auth', $auth);
  61. $info = Secure::checkLogin($auth);
  62. return $info;
  63. }
  64. return false;
  65. }
  66. # 获取当前的扩展数据
  67. public function extend()
  68. {
  69. # 先从query的set中获取,这个不影响用户登录
  70. $auth = $this->request('authorization');
  71. if ($auth) {
  72. $auth = Str::decode($auth);
  73. $info = Secure::checkLogin($auth);
  74. if ($info) {
  75. return $info['extend'];
  76. }
  77. }
  78. # 从登录里获取
  79. $info = $this->auth();
  80. if (!$info) {
  81. $auth = Dever::session('auth');
  82. if (!$auth) {
  83. return false;
  84. }
  85. $info = Secure::checkLogin($auth);
  86. }
  87. if ($info && isset($info['extend'])) {
  88. return $info['extend'];
  89. }
  90. return false;
  91. }
  92. # 获取页面类
  93. public function page($load, $config = [], $key = 'list', $input = true)
  94. {
  95. $page = new Page($key, $load, $input, $config);
  96. return $page;
  97. }
  98. # 获取当前使用的系统 一般为数据库隔离使用
  99. public function system($info = false, $module = true, $field = false)
  100. {
  101. if (!$info) {
  102. # 单独的数据库隔离,不影响当前登录状态
  103. $info = $this->extend();
  104. }
  105. if ($info && isset($info['info_id']) && isset($info['partition'])) {
  106. # 这里后续增加从数据库中获取
  107. $value = $info['system_id'] . '_' . $info['info_id'];
  108. $result = [];
  109. if (strpos($info['partition'], '.')) {
  110. $temp = explode('.', $info['partition']);
  111. $result = $this->partition($result, $temp[0], $info['system_key'], $value);
  112. if ($module && isset($info['data_id']) && $info['data_id']) {
  113. if ($temp[0] == $temp[1]) {
  114. $value .= '/' . $info['module_id'] . '_' . $info['data_id'];
  115. $result = $this->partition($result, $temp[0], $info['system_key'], $value);
  116. } else {
  117. $result = $this->partition($result, $temp[1], $info['system_key'], $info['module_id'] . '_' . $info['data_id']);
  118. }
  119. }
  120. } else {
  121. $result = $this->partition($result, $info['partition'], $info['system_key'], $value);
  122. }
  123. if ($field) {
  124. $result['field'] = Dever::call($field);
  125. }
  126. return $result;
  127. }
  128. return false;
  129. }
  130. # 设置数据隔离
  131. private function partition(&$result, $type, $key, $value)
  132. {
  133. if ($type == 'field') {
  134. $result[$type] = [
  135. 'type' => 'key',
  136. 'field' => $key,
  137. 'value' => $value,
  138. ];
  139. } elseif ($type == 'where') {
  140. $result[$type] = [
  141. $key => $value
  142. ];
  143. } else {
  144. $result[$type] = $value;
  145. }
  146. return $result;
  147. }
  148. # 获取token需要用到的key
  149. public function getToken()
  150. {
  151. $extend = $this->extend();
  152. if ($extend) {
  153. return implode('-', array_values($extend));
  154. }
  155. return '';
  156. }
  157. # 将token设置到route权限中,方便后续读取
  158. # 系统、模块、模块账户、数据id
  159. public function setAuth($system, $module_id, $info_id, $data_id = '')
  160. {
  161. if (is_string($system)) {
  162. $system = Dever::db('system', 'manage')->find(['key' => $system]);
  163. }
  164. if (is_string($module_id)) {
  165. $module_id = Dever::db('system_module', 'manage')->column(['key' => $module_id], 'id');
  166. }
  167. $token = $this->token(-1, '', $system['partition'], $system['key'], $system['id'], $info_id, $module_id, $data_id);
  168. return \Dever\Route::$data['authorization'] = Secure::encode($token['token']);
  169. }
  170. # 生成token
  171. public function token($uid, $mobile, $partition, $system_key, $system_id, $info_id, $module_id, $data_id)
  172. {
  173. $extend['partition'] = $partition;
  174. $extend['system_key'] = $system_key;
  175. $extend['system_id'] = $system_id;
  176. $extend['info_id'] = $info_id;
  177. $extend['module_id'] = $module_id;
  178. $extend['data_id'] = $data_id;
  179. if ($uid && $uid > 0) {
  180. $select['uid'] = $uid;
  181. $select['system_id'] = $system_id;
  182. $select['info_id'] = $info_id;
  183. $info = Dever::db('system_user', 'manage')->find($select);
  184. $select += $extend;
  185. if (!$info) {
  186. Dever::db('system_user', 'manage')->insert($select);
  187. } else {
  188. Dever::db('system_user', 'manage')->update($info['id'], $select);
  189. }
  190. }
  191. return array('token' => Secure::login($uid, $extend));
  192. }
  193. # 生成密码
  194. public function createPwd($password)
  195. {
  196. $data['salt'] = Str::salt(8);
  197. $data['password'] = $this->hash($password, $data['salt']);
  198. return $data;
  199. }
  200. # 生成时间
  201. public function crateDate($date)
  202. {
  203. return Date::mktime($date);
  204. }
  205. # hash加密
  206. public function hash($password, $salt)
  207. {
  208. return hash('sha256', $password . $salt);
  209. }
  210. # 自动更新key
  211. public function updateKey($db, $data)
  212. {
  213. if ($data['name'] && !$data['key']) {
  214. if (Dever::project('pinyin')) {
  215. $where = [];
  216. if (isset($data['id']) && $data['id']) {
  217. $where['id'] = ['!=', $data['id']];
  218. }
  219. $data['key'] = Dever::load('convert', 'pinyin')->getPinyin($data['name']);
  220. # 检查是否存在
  221. $where['key'] = $data['key'];
  222. $info = $db->find($where);
  223. if ($info) {
  224. $data['key'] .= '-' . date('YmdHis');
  225. }
  226. }
  227. }
  228. return $data;
  229. }
  230. # 设置联动
  231. public function cascader($total, $func)
  232. {
  233. $total = Dever::input('total', 'is_numeric', '联动总数', $total);
  234. $level = Dever::input('level', 'is_numeric', '联动级别', 1);
  235. $parent = Dever::input('parent', 'isset', '联动ID', 0);
  236. if ($parent < 0) {
  237. Dever::error('error');
  238. }
  239. $data = $func($level, $parent);
  240. if ($level >= $total) {
  241. foreach ($data as &$v) {
  242. $v['leaf'] = true;
  243. }
  244. }
  245. $result['total'] = $total;
  246. $result['list'] = $data;
  247. return $result;
  248. }
  249. # 根据load获取db
  250. public function db($load)
  251. {
  252. $menu = [];
  253. $load = explode('/', ltrim($load, '/'));
  254. if (isset($load[2])) {
  255. $app = $load[1];
  256. $table = $load[2];
  257. } else {
  258. $app = $load[0];
  259. $table = $load[1];
  260. }
  261. $parent = Dever::db('menu', 'manage')->find(['key' => $app]);
  262. if ($parent) {
  263. $menu = Dever::db('menu', 'manage')->find(['parent_id' => $parent['id'], 'key' => $table]);
  264. if ($menu) {
  265. $app = $menu['app'];
  266. }
  267. }
  268. $set = Dever::project($app);
  269. $manage = $set['path'] . 'manage/'.$table.'.php';
  270. if (is_file($manage)) {
  271. $manage = include $manage;
  272. if ($source = Dever::issets($manage, 'source')) {
  273. if (strpos($source, '/')) {
  274. $source = explode('/', $source);
  275. $app = $source[0];
  276. $table = $source[1];
  277. } else {
  278. $table = $source;
  279. }
  280. }
  281. }
  282. $db = Dever::db($table, $app);
  283. $db->config['manage'] = $manage;
  284. return [$db, $menu];
  285. }
  286. # 获取项目
  287. public function project()
  288. {
  289. $result = [];
  290. $app = \Dever\Project::read();
  291. foreach ($app as $k => $v) {
  292. $result[] = [
  293. 'id' => $k,
  294. 'name' => $v['lang'] ?? $k,
  295. ];
  296. }
  297. return $result;
  298. }
  299. }