Index.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\api\controller;
  9. use app\api\BaseController;
  10. use app\api\middleware\Auth;
  11. use Firebase\JWT\JWT;
  12. use Firebase\JWT\Key;
  13. use think\facade\Db;
  14. use think\facade\Request;
  15. class Index extends BaseController
  16. {
  17. /**
  18. * 控制器中间件 [登录、注册 不需要鉴权]
  19. * @var array
  20. */
  21. protected $middleware = [
  22. Auth::class => ['except' => ['index','reg','login'] ]
  23. ];
  24. private $loginAttempts = [];
  25. private $maxAttempts = 5;
  26. private $lockDuration = 600; // 10 minutes
  27. /**
  28. * @param $user_id
  29. * @return string
  30. */
  31. public function getToken($user_id){
  32. $time = time(); //当前时间
  33. $conf = $this->jwt_conf;
  34. $token = [
  35. 'iss' => $conf['iss'], //签发者 可选
  36. 'aud' => $conf['aud'], //接收该JWT的一方,可选
  37. 'iat' => $time, //签发时间
  38. 'nbf' => $time-1 , //(Not Before):某个时间点后才能访问,比如设置time+30,表示当前时间30秒后才能使用
  39. 'exp' => $time+$conf['exptime'], //过期时间,这里设置2个小时
  40. 'data' => [
  41. //自定义信息,不要定义敏感信息
  42. 'userid' =>$user_id,
  43. ]
  44. ];
  45. return JWT::encode($token, $conf['secrect'], 'HS256'); //输出Token 默认'HS256'
  46. }
  47. /**
  48. * @param $token
  49. */
  50. public static function checkToken($token){
  51. try {
  52. JWT::$leeway = 60;//当前时间减去60,把时间留点余地
  53. $decoded = JWT::decode($token, self::$config['secrect'], ['HS256']); //HS256方式,这里要和签发的时候对应
  54. return (array)$decoded;
  55. } catch(\Firebase\JWT\SignatureInvalidException $e) { //签名不正确
  56. return json(['code'=>403,'msg'=>'签名错误']);
  57. }catch(\Firebase\JWT\BeforeValidException $e) { // 签名在某个时间点之后才能用
  58. return json(['code'=>401,'msg'=>'token失效']);
  59. }catch(\Firebase\JWT\ExpiredException $e) { // token过期
  60. return json(['code'=>401,'msg'=>'token已过期']);
  61. }catch(Exception $e) { //其他错误
  62. return json(['code'=>404,'msg'=>'非法请求']);
  63. }catch(\UnexpectedValueException $e) { //其他错误
  64. return json(['code'=>404,'msg'=>'非法请求']);
  65. } catch(\DomainException $e) { //其他错误
  66. return json(['code'=>404,'msg'=>'非法请求']);
  67. }
  68. }
  69. /**
  70. * @api {post} /index/index API页面
  71. * @apiDescription 返回首页信息
  72. */
  73. public function index()
  74. {
  75. $list = Db::name('Article')->select();
  76. $seo = get_system_config('web');
  77. add_user_log('api', '首页');
  78. $this->apiSuccess('请求成功',['list' => $list,'seo' => $seo]);
  79. }
  80. /**
  81. * @api {post} /index/login 会员登录
  82. * @apiDescription 系统登录接口,返回 token 用于操作需验证身份的接口
  83. * @apiParam (请求参数:) {string} username 登录用户名
  84. * @apiParam (请求参数:) {string} password 登录密码
  85. * @apiParam (响应字段:) {string} token Token
  86. * @apiSuccessExample {json} 成功示例
  87. * {"code":0,"msg":"登录成功","time":1627374739,"data":{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhcGkuZ291Z3VjbXMuY29tIiwiYXVkIjoiZ291Z3VjbXMiLCJpYXQiOjE2MjczNzQ3MzksImV4cCI6MTYyNzM3ODMzOSwidWlkIjoxfQ.gjYMtCIwKKY7AalFTlwB2ZVWULxiQpsGvrz5I5t2qTs"}}
  88. * @apiErrorExample {json} 失败示例
  89. * {"code":1,"msg":"帐号或密码错误","time":1627374820,"data":[]}
  90. */
  91. public function login()
  92. {
  93. $param = get_params();
  94. if(empty($param['username']) || empty($param['password'])){
  95. $this->apiError('参数错误');
  96. }
  97. $ip = request()->ip();
  98. if (isset($this->loginAttempts[$ip])) {
  99. if ($this->loginAttempts[$ip]['count'] >= $this->maxAttempts &&
  100. time() - $this->loginAttempts[$ip]['last_attempt'] < $this->lockDuration) {
  101. $this->apiError('登录尝试次数过多,请稍后再试');
  102. } elseif (time() - $this->loginAttempts[$ip]['last_attempt'] >= $this->lockDuration) {
  103. $this->loginAttempts[$ip]['count'] = 0;
  104. }
  105. }
  106. // 校验用户名密码
  107. $user = Db::name('User')->where(['username' => $param['username']])->find();
  108. if (empty($user)) {
  109. $this->recordFailedAttempt($ip);
  110. $this->apiError('帐号或密码错误');
  111. }
  112. $param['pwd'] = set_password($param['password'], $user['salt']);
  113. if ($param['pwd'] !== $user['password']) {
  114. $this->recordFailedAttempt($ip);
  115. $this->apiError('帐号或密码错误');
  116. }
  117. if ($user['status'] == -1) {
  118. $this->apiError('该用户禁止登录,请于平台联系');
  119. }
  120. // Reset failed attempts on successful login
  121. $this->loginAttempts[$ip]['count'] = 0;
  122. $data = [
  123. 'last_login_time' => time(),
  124. 'last_login_ip' => request()->ip(),
  125. 'login_num' => $user['login_num'] + 1,
  126. ];
  127. $res = Db::name('user')->where(['id' => $user['id']])->update($data);
  128. if ($res) {
  129. $token = self::getToken($user['id']);
  130. add_user_log('api', '登录');
  131. $this->apiSuccess('登录成功', ['token' => $token]);
  132. }
  133. }
  134. private function recordFailedAttempt($ip) {
  135. if (!isset($this->loginAttempts[$ip])) {
  136. $this->loginAttempts[$ip] = ['count' => 0, 'last_attempt' => 0];
  137. }
  138. $this->loginAttempts[$ip]['count']++;
  139. $this->loginAttempts[$ip]['last_attempt'] = time();
  140. }
  141. /**
  142. * @api {post} /index/reg 会员注册
  143. * @apiDescription 系统注册接口,返回是否成功的提示,需再次登录
  144. * @apiParam (请求参数:) {string} username 用户名
  145. * @apiParam (请求参数:) {string} password 密码
  146. * @apiSuccessExample {json} 成功示例
  147. * {"code":0,"msg":"注册成功","time":1627375117,"data":[]}
  148. * @apiErrorExample {json} 失败示例
  149. * {"code":1,"msg":"该账户已经存在","time":1627374899,"data":[]}
  150. */
  151. public function reg()
  152. {
  153. $param = get_params();
  154. if(empty($param['username']) || empty($param['pwd'])){
  155. $this->apiError('参数错误');
  156. }
  157. $user = Db::name('user')->where(['username' => $param['username']])->find();
  158. if (!empty($user)) {
  159. $this->apiError('该账户已经存在');
  160. }
  161. // Check password strength
  162. if (!preg_match('/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/', $param['pwd'])) {
  163. $this->apiError('密码必须至少包含8个字符,含有大小写字母、数字和特殊字符');
  164. }
  165. $param['salt'] = set_salt(20);
  166. $param['password'] = set_password($param['pwd'], $param['salt']);
  167. $param['register_time'] = time();
  168. $param['headimgurl'] = '';
  169. $param['register_ip'] = request()->ip();
  170. $char = mb_substr($param['username'], 0, 1, 'utf-8');
  171. $uid = Db::name('User')->strict(false)->field(true)->insertGetId($param);
  172. if($uid){
  173. add_user_log('api', '注册');
  174. $this->apiSuccess('注册成功,请登录');
  175. }else{
  176. $this->apiError('注册失败');
  177. }
  178. }
  179. /**
  180. * @api {post} /index/demo 测试页面
  181. * @apiDescription 返回文章列表信息
  182. * @apiParam (请求参数:) {string} token Token
  183. * @apiSuccessExample {json} 响应数据样例
  184. * {"code":1,"msg":"","time":1563517637,"data":{"id":13,"email":"test110@qq.com","password":"e10adc3949ba59abbe56e057f20f883e","sex":1,"last_login_time":1563517503,"last_login_ip":"127.0.0.1","qq":"123455","mobile":"","mobile_validated":0,"email_validated":0,"type_id":1,"status":1,"create_ip":"127.0.0.1","update_time":1563507130,"create_time":1563503991,"type_name":"注册会员"}}
  185. */
  186. public function demo()
  187. {
  188. $uid = JWT_UID;
  189. $userInfo = Db::name('User')->where(['id' => $uid])->find();
  190. $this->apiSuccess('请求成功', ['user' => $userInfo]);
  191. }
  192. //获取部门
  193. public function get_department()
  194. {
  195. $department = get_department();
  196. return to_assign(0, '', $department);
  197. }
  198. //获取部门树形节点列表
  199. public function get_department_tree()
  200. {
  201. $department = get_department();
  202. $list = get_tree($department, 0, 2);
  203. $data['trees'] = $list;
  204. return json($data);
  205. }
  206. //获取部门树形节点列表2
  207. public function get_department_select()
  208. {
  209. $keyword = get_params('keyword');
  210. $selected = [];
  211. if(!empty($keyword)){
  212. $selected = explode(",",$keyword);
  213. }
  214. $department = get_department();
  215. $list = get_select_tree($department, 0,0,$selected);
  216. return to_assign(0, '',$list);
  217. }
  218. //获取子部门所有员工
  219. public function get_employee($did = 0)
  220. {
  221. $did = get_params('did');
  222. if($did == 1){
  223. $department = $did;
  224. }
  225. else{
  226. $department = get_department_son($did);
  227. }
  228. $employee = Db::name('admin')
  229. ->field('a.id,a.did,a.position_id,a.mobile,a.name,a.nickname,a.sex,a.status,a.thumb,a.username,d.title as department')
  230. ->alias('a')
  231. ->join('Department d', 'a.did = d.id')
  232. ->where(['a.status' => 1])
  233. ->where('a.id', ">", 1)
  234. ->where('a.did', "in", $department)
  235. ->select();
  236. return to_assign(0, '', $employee);
  237. }
  238. //获取所有员工
  239. public function get_personnel()
  240. {
  241. $param = get_params();
  242. $where[] = ['a.status', '=', 1];
  243. $where[] = ['a.id', '>', 1];
  244. if (!empty($param['keywords'])) {
  245. $where[] = ['a.name', 'like', '%' . $param['keywords'] . '%'];
  246. }
  247. if(!empty($param['ids'])){
  248. $where[] = ['a.id', 'notin', $param['ids']];
  249. }
  250. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  251. $list = Db::name('admin')
  252. ->field('a.id,a.did,a.position_id,a.mobile,a.name,a.nickname,a.sex,a.status,a.thumb,a.username,d.title as department')
  253. ->alias('a')
  254. ->join('Department d', 'a.did = d.id')
  255. ->where($where)
  256. ->order('a.id desc')
  257. ->paginate($rows, false, ['query' => $param]);
  258. return table_assign(0, '', $list);
  259. }
  260. //获取部门所有员工
  261. public function get_employee_select()
  262. {
  263. $keyword = get_params('keyword');
  264. $selected = [];
  265. if(!empty($keyword)){
  266. $selected = explode(",",$keyword);
  267. }
  268. $employee = Db::name('admin')
  269. ->field('id as value,name')
  270. ->where(['status' => 1])
  271. ->select()->toArray();
  272. foreach($employee as $k => &$v){
  273. $v['selected'] = '';
  274. if(in_array($v['value'],$selected)){
  275. $v['selected'] = 'selected';
  276. }
  277. }
  278. return to_assign(0, '', $employee);
  279. }
  280. //获取角色列表
  281. public function get_position()
  282. {
  283. $position = Db::name('Position')->field('id,title as name')->where([['status', '=', 1], ['id', '>', 1]])->select();
  284. return to_assign(0, '', $position);
  285. }
  286. //获取审核类型
  287. public function get_flow_cate($type=0)
  288. {
  289. $flows = Db::name('FlowType')->where(['type'=>$type,'status'=>1])->select()->toArray();
  290. return to_assign(0, '', $flows);
  291. }
  292. //获取审核步骤人员
  293. public function get_flow_users($id=0)
  294. {
  295. $flow = Db::name('Flow')->where(['id' => $id])->find();
  296. $flowData = unserialize($flow['flow_list']);
  297. if(!empty($flowData)){
  298. foreach ($flowData as $key => &$val) {
  299. $val['user_id_info'] = Db::name('Admin')->field('id,name,thumb')->where('id','in',$val['flow_uids'])->select()->toArray();
  300. }
  301. }
  302. $data['copy_uids'] = $flow['copy_uids'];
  303. $data['copy_unames'] ='';
  304. if($flow['copy_uids']!=''){
  305. $copy_unames = Db::name('Admin')->where('id', 'in', $flow['copy_uids'])->column('name');
  306. $data['copy_unames'] = implode(',', $copy_unames);
  307. }
  308. $data['flow_data'] = $flowData;
  309. return to_assign(0, '', $data);
  310. }
  311. //获取url参数
  312. function get_params($key = "")
  313. {
  314. return Request::instance()->param($key);
  315. }
  316. }