UsersController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\Oauth;
  3. use App\Consts\SysConsts;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use App\Modules\User\Services\QappUserService;
  7. use JWTAuth;
  8. use Tymon\JWTAuth\Exceptions\JWTException;
  9. class UsersController extends Controller
  10. {
  11. /**
  12. * @apiDefine Login 登录
  13. */
  14. /**
  15. * @apiVersion 1.0.0
  16. * @apiDescription 登录
  17. * @api {post} login 登录
  18. * @apiParam {String} device_no 设备号
  19. * @apiParam {String} device_info 设备信息json字符串格式
  20. * @apiParam {Int} send_order_id send_order_id
  21. * @apiParam {Int} timestamp 时间戳10分钟过期
  22. * @apiParam {String} sign 签名(见微信支付签名https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=4_3)
  23. * @apiGroup Login
  24. * @apiName index
  25. * @apiSuccess {int} code 状态码
  26. * @apiSuccess {String} msg 信息
  27. * @apiSuccess {object} data 结果集
  28. * @apiSuccess {String} data.uid 用户uid
  29. * @apiSuccess {String} data.token token
  30. * @apiSuccess {Int} data.time 过期时间
  31. * @apiSuccessExample {json} Success-Response:
  32. * HTTP/1.1 200 OK
  33. * {
  34. * code: 0,
  35. * msg: "",
  36. * data: {
  37. * token:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
  38. * time:123455,
  39. * uid:1
  40. * }
  41. * }
  42. */
  43. public function index(Request $request)
  44. {
  45. $headers = $request->header();
  46. myLog('login')->info('header', compact('headers'));
  47. $codeVersion = $request->header('x-codeversion', '');
  48. $package = $request->input('package', '');
  49. $send_order_id = $request->input('send_order_id', 0);
  50. $device_no = $request->input('device_no', '');
  51. $androidid = $request->input('androidid', '');
  52. $imei = $request->input('imei', '');
  53. $mac = $request->input('mac', '');
  54. $device_info = $request->input('device_info', '');
  55. if ($device_no) {
  56. $data = (new QappUserService)->login(compact('package', 'send_order_id', 'device_no', 'androidid', 'mac', 'device_info', 'imei', 'codeVersion'));
  57. return response()->success($data);
  58. } else {
  59. return response()->error('PARAM_ERROR');
  60. }
  61. }
  62. /**
  63. * @apiVersion 1.0.0
  64. * @apiDescription 刷新token
  65. * @api {get} refreshToken 刷新token
  66. * @apiParam {String} [token] token
  67. * @apiHeader {String} [Authorization] token 两个token任选其一
  68. * @apiGroup Login
  69. * @apiName RefreshToken
  70. * @apiSuccess {int} code 状态码
  71. * @apiSuccess {String} msg 信息
  72. * @apiSuccess {object} data 结果集
  73. * @apiSuccess {String} data.token token
  74. * @apiSuccess {Int} data.time 过期时间
  75. * @apiSuccessExample {json} Success-Response:
  76. * HTTP/1.1 200 OK
  77. * {
  78. * code: 0,
  79. * msg: "",
  80. * data: {
  81. * token:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
  82. * time:123455
  83. * }
  84. * }
  85. */
  86. public function refreshToken()
  87. {
  88. try {
  89. $old_token = JWTAuth::getToken();
  90. $token = JWTAuth::refresh($old_token);
  91. $time = strtotime("+1 month");
  92. return response()->success(compact('token', 'time'));
  93. } catch (JWTException $e) {
  94. return response()->error('QAPP_NOT_LOGIN');
  95. }
  96. }
  97. }