Index.php 11 KB

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