UsersController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. class UsersController extends Controller
  9. {
  10. /**
  11. * @apiDefine Login 登录
  12. */
  13. /**
  14. * @apiVersion 1.0.0
  15. * @apiDescription 登录
  16. * @api {post} login 登录
  17. * @apiParam {String} js_code js_code
  18. * @apiParam {Int} distribution_channel_id distribution_channel_id
  19. * @apiParam {String} sign 签名
  20. * @apiGroup Login
  21. * @apiName index
  22. * @apiSuccess {int} code 状态码
  23. * @apiSuccess {String} msg 信息
  24. * @apiSuccess {object} data 结果集
  25. * @apiSuccess {String} data.uid 用户uid
  26. * @apiSuccess {String} data.token token
  27. * @apiSuccess {Int} data.time 过期时间
  28. * @apiSuccessExample {json} Success-Response:
  29. * HTTP/1.1 200 OK
  30. * {
  31. * code: 0,
  32. * msg: "",
  33. * data: {
  34. * token:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
  35. * time:123455,
  36. * uid:1
  37. * }
  38. * }
  39. */
  40. public function index(Request $request)
  41. {
  42. $send_order_id = $request->input('send_order_id', 0);
  43. $device_no = $request->input('device_no', '');
  44. $device_info = $request->input('device_info', '');
  45. if ($device_no) {
  46. $data = QappUserService::loginStatic(compact('send_order_id', 'device_no', 'device_info'));
  47. return response()->success($data);
  48. } else {
  49. return response()->error('PARAM_ERROR');
  50. }
  51. }
  52. /**
  53. * @apiVersion 1.0.0
  54. * @apiDescription 刷新token
  55. * @api {get} RefreshToken 刷新token
  56. * @apiParam {String} [token] token
  57. * @apiHeader {String} [Authorization] token 两个token任选其一
  58. * @apiGroup Login
  59. * @apiName RefreshToken
  60. * @apiSuccess {int} code 状态码
  61. * @apiSuccess {String} msg 信息
  62. * @apiSuccess {object} data 结果集
  63. * @apiSuccess {String} data.token token
  64. * @apiSuccess {Int} data.time 过期时间
  65. * @apiSuccessExample {json} Success-Response:
  66. * HTTP/1.1 200 OK
  67. * {
  68. * code: 0,
  69. * msg: "",
  70. * data: {
  71. * token:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
  72. * time:123455
  73. * }
  74. * }
  75. */
  76. public function RefreshToken()
  77. {
  78. try {
  79. $old_token = JWTAuth::getToken();
  80. $token = JWTAuth::refresh($old_token);
  81. $time = time() + SysConsts::ONE_HOUR_SECONDS * 2;
  82. return response()->success(compact('token', 'time'));
  83. } catch (Exception $e) { }
  84. return response()->error('XCX_NOT_LOGIN');
  85. }
  86. /**
  87. * 检验数据的真实性,并且获取解密后的明文.
  88. * @param $encryptedData string 加密的用户数据
  89. * @param $iv string 与用户数据一同返回的初始向量
  90. * @param $data string 解密后的原文
  91. *
  92. * @return int 成功0,失败返回对应的错误码
  93. */
  94. private function decryptData($encryptedData, $iv, &$data)
  95. {
  96. if (strlen($iv) != 24) {
  97. return ErrorCode::$IllegalIv;
  98. }
  99. $aesIV = base64_decode($iv);
  100. $aesCipher = base64_decode($encryptedData);
  101. $aesKey = "";
  102. $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
  103. $dataObj = json_decode($result);
  104. if ($dataObj == NULL) {
  105. return ErrorCode::$IllegalBuffer;
  106. }
  107. $data = $result;
  108. return ErrorCode::$OK;
  109. }
  110. }
  111. class ErrorCode
  112. {
  113. public static $OK = 0;
  114. public static $IllegalAesKey = -41001;
  115. public static $IllegalIv = -41002;
  116. public static $IllegalBuffer = -41003;
  117. public static $DecodeBase64Error = -41004;
  118. }