Util.php 10 KB

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