Api.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\common\controller;
  3. use think\App;
  4. use app\enterprise\model\{User,Group};
  5. use app\index\controller\Extension;
  6. use think\facade\Session;
  7. use think\facade\Cache;
  8. use think\facade\Db;
  9. use GatewayClient\Gateway;
  10. use app\manage\model\Config;
  11. use thans\jwt\facade\JWTAuth;
  12. /**
  13. * API接口类
  14. */
  15. class Api
  16. {
  17. /**
  18. * Request实例
  19. * @var \think\Request
  20. */
  21. protected $request;
  22. /**
  23. * 应用实例
  24. * @var \think\App
  25. */
  26. protected $app;
  27. protected $middleware=['apiAuth'];
  28. /**
  29. * 构造方法
  30. * @access public
  31. * @param App $app 应用对象
  32. */
  33. public function __construct(App $app)
  34. {
  35. $this->app = $app;
  36. $this->request = $this->app->request;
  37. }
  38. // 创建用户
  39. public function createUser()
  40. {
  41. $data = $this->request->param();
  42. if(!isset($data['account']) || !isset($data['realname'])){
  43. return warning('缺少参数');
  44. }
  45. $user=new User();
  46. $verify=$user->checkAccount($data);
  47. if(!$verify){
  48. return success('账号已存在');
  49. }
  50. $salt=\utils\Str::random(4);
  51. $data['password'] = password_hash_tp(rand(100000,999999),$salt);
  52. $data['salt'] =$salt;
  53. $data['register_ip'] =$this->request->ip();
  54. $data['name_py'] = pinyin_sentence($data['realname']);
  55. $user->save($data);
  56. $data['user_id']=$user->user_id;
  57. $data['open_id']=encryptIds($user->user_id);
  58. // 监听用户注册后的操作
  59. event('UserRegister',$data);
  60. return success('注册成功', $data);
  61. }
  62. // 用户登录
  63. public function login()
  64. {
  65. $param=$this->request->param();
  66. $isMobile=$param['is_mobile'] ?? false;
  67. if(!isset($param['account']) || !isset($param['open_id'])){
  68. return warning('缺少参数');
  69. }
  70. $userInfo=User::where(['account'=> $param['account']])->withoutField('register_ip,login_count,update_time,create_time')->find();
  71. if(!$userInfo){
  72. return warning('当前用户不存在!');
  73. }
  74. try{
  75. $hash_id=decryptIds($param['open_id']);
  76. if($hash_id!=$userInfo['user_id']){
  77. return warning('当前用户不存在!');
  78. }
  79. }catch (\Exception $e){
  80. return error($e->getMessage());
  81. }
  82. $md5=md5(json_encode($userInfo));
  83. // 将用户信息缓存5分钟
  84. Cache::set($md5,$userInfo,300);
  85. // 生成Url
  86. if($isMobile){
  87. $url=rtrim(request()->domain(),'/').'/h5/#/pages/login/index?token='.$md5;
  88. }else{
  89. $url=rtrim(request()->domain(),'/').'/#/login?token='.$md5;
  90. }
  91. return success('登录成功',$url);
  92. }
  93. }