Common.php 8.3 KB

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